API Filter by date created for files

Added test cases
Do not merge before https://github.com/silverstripe-labs/silverstripe-behat-extension/pull/32

ref: CWPBUG-144
This commit is contained in:
Damian Mooyman 2014-04-11 17:49:17 +12:00
parent baff7f12bd
commit 3765030faa
2 changed files with 50 additions and 22 deletions

View File

@ -98,17 +98,19 @@ JS
// Don't filter list when a detail view is requested,
// to avoid edge cases where the filtered list wouldn't contain the requested
// record due to faulty session state (current folder not always encoded in URL, see #7408).
if(!$folder->ID && $this->request->requestVar('ID') === null && ($this->request->param('ID') == 'field')) {
if(!$folder->ID
&& $this->request->requestVar('ID') === null
&& ($this->request->param('ID') == 'field')
) {
return $list;
}
// Re-add previously removed "Name" filter as combined filter
// TODO Replace with composite SearchFilter once that API exists
if(isset($params['Name'])) {
$list = $list->where(sprintf(
'"Name" LIKE \'%%%s%%\' OR "Title" LIKE \'%%%s%%\'',
Convert::raw2sql($params['Name']),
Convert::raw2sql($params['Name'])
if(!empty($params['Name'])) {
$list = $list->filterAny(array(
'Name:PartialMatch' => $params['Name'],
'Title:PartialMatch' => $params['Name']
));
}
@ -117,23 +119,26 @@ JS
// If a search is conducted, check for the "current folder" limitation.
// Otherwise limit by the current folder as denoted by the URL.
if(!$params || @$params['CurrentFolderOnly']) {
if(empty($params) || !empty($params['CurrentFolderOnly'])) {
$list = $list->filter('ParentID', $folder->ID);
}
// Category filter
if(isset($params['AppCategory'])) {
if(isset(File::config()->app_categories[$params['AppCategory']])) {
if(!empty($params['AppCategory'])
&& !empty(File::config()->app_categories[$params['AppCategory']])
) {
$exts = File::config()->app_categories[$params['AppCategory']];
} else {
$exts = array();
$list = $list->filter('Name:PartialMatch', $exts);
}
$categorySQLs = array();
foreach($exts as $ext) $categorySQLs[] = '"File"."Name" LIKE \'%.' . $ext . '\'';
// TODO Use DataList->filterAny() once OR connectives are implemented properly
if (count($categorySQLs) > 0) {
$list = $list->where('(' . implode(' OR ', $categorySQLs) . ')');
// Date filter
if(!empty($params['CreatedFrom'])) {
$fromDate = new DateField(null, null, $params['CreatedFrom']);
$list = $list->filter("Created:GreaterThanOrEqual", $fromDate->dataValue());
}
if(!empty($params['CreatedTo'])) {
$toDate = new DateField(null, null, $params['CreatedTo']);
$list = $list->filter("Created:LessThanOrEqual", $toDate->dataValue());
}
return $list;
@ -342,6 +347,21 @@ JS
foreach($context->getFilters() as $filter) $filter->setFullName(sprintf('q[%s]', $filter->getFullName()));
// Customize fields
$context->addField(
new HeaderField('q[Date]', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4)
);
$context->addField(
DateField::create(
'q[CreatedFrom]',
_t('CMSSearch.FILTERDATEFROM', 'From')
)->setConfig('showcalendar', true)
);
$context->addField(
DateField::create(
'q[CreatedTo]',
_t('CMSSearch.FILTERDATETO', 'To')
)->setConfig('showcalendar', true)
);
$appCategories = array(
'image' => _t('AssetAdmin.AppCategoryImage', 'Image'),
'audio' => _t('AssetAdmin.AppCategoryAudio', 'Audio'),

View File

@ -5,8 +5,8 @@ Feature: Manage files
So that I can insert them into my content efficiently
Background:
Given a "image" "assets/folder1/file1.jpg"
And a "image" "assets/folder1/folder1.1/file2.jpg"
Given a "image" "assets/folder1/file1.jpg" was created "2012-01-01 12:00:00"
And a "image" "assets/folder1/folder1.1/file2.jpg" was created "2010-01-01 12:00:00"
And a "folder" "assets/folder2"
And I am logged in with "ADMIN" permissions
And I go to "/admin/assets"
@ -75,3 +75,11 @@ Feature: Manage files
And I press the "Apply Filter" button
Then the "Files" table should contain "file1"
And the "Files" table should not contain "document"
Scenario: I can filter out files that don't match the date range
Given I expand the "Filter" CMS Panel
And I fill in "From" with "2003-01-01"
And I fill in "To" with "2011-01-01"
And I press the "Apply Filter" button
And the "Files" table should contain "file2"
And the "Files" table should not contain "file1"