Merge pull request #994 from tractorcow/pulls/3.1-asset-datefilter

API Filter by date created for files
This commit is contained in:
Ingo Schommer 2014-04-28 23:40:17 +12:00
commit 9bff36c078
2 changed files with 50 additions and 22 deletions

View File

@ -106,17 +106,19 @@ JS
// Don't filter list when a detail view is requested, // Don't filter list when a detail view is requested,
// to avoid edge cases where the filtered list wouldn't contain the 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). // 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; return $list;
} }
// Re-add previously removed "Name" filter as combined filter // Re-add previously removed "Name" filter as combined filter
// TODO Replace with composite SearchFilter once that API exists // TODO Replace with composite SearchFilter once that API exists
if(isset($params['Name'])) { if(!empty($params['Name'])) {
$list = $list->where(sprintf( $list = $list->filterAny(array(
'"Name" LIKE \'%%%s%%\' OR "Title" LIKE \'%%%s%%\'', 'Name:PartialMatch' => $params['Name'],
Convert::raw2sql($params['Name']), 'Title:PartialMatch' => $params['Name']
Convert::raw2sql($params['Name'])
)); ));
} }
@ -125,23 +127,26 @@ JS
// If a search is conducted, check for the "current folder" limitation. // If a search is conducted, check for the "current folder" limitation.
// Otherwise limit by the current folder as denoted by the URL. // 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); $list = $list->filter('ParentID', $folder->ID);
} }
// Category filter // Category filter
if(isset($params['AppCategory'])) { if(!empty($params['AppCategory'])
if(isset(File::config()->app_categories[$params['AppCategory']])) { && !empty(File::config()->app_categories[$params['AppCategory']])
) {
$exts = File::config()->app_categories[$params['AppCategory']]; $exts = File::config()->app_categories[$params['AppCategory']];
} else { $list = $list->filter('Name:PartialMatch', $exts);
$exts = array();
} }
$categorySQLs = array();
foreach($exts as $ext) $categorySQLs[] = '"File"."Name" LIKE \'%.' . $ext . '\''; // Date filter
// TODO Use DataList->filterAny() once OR connectives are implemented properly if(!empty($params['CreatedFrom'])) {
if (count($categorySQLs) > 0) { $fromDate = new DateField(null, null, $params['CreatedFrom']);
$list = $list->where('(' . implode(' OR ', $categorySQLs) . ')'); $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; return $list;
@ -350,6 +355,21 @@ JS
foreach($context->getFilters() as $filter) $filter->setFullName(sprintf('q[%s]', $filter->getFullName())); foreach($context->getFilters() as $filter) $filter->setFullName(sprintf('q[%s]', $filter->getFullName()));
// Customize fields // 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( $appCategories = array(
'image' => _t('AssetAdmin.AppCategoryImage', 'Image'), 'image' => _t('AssetAdmin.AppCategoryImage', 'Image'),
'audio' => _t('AssetAdmin.AppCategoryAudio', 'Audio'), 'audio' => _t('AssetAdmin.AppCategoryAudio', 'Audio'),

View File

@ -5,8 +5,8 @@ Feature: Manage files
So that I can insert them into my content efficiently So that I can insert them into my content efficiently
Background: Background:
Given a "image" "assets/folder1/file1.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" And a "image" "assets/folder1/folder1.1/file2.jpg" was created "2010-01-01 12:00:00"
And a "folder" "assets/folder2" And a "folder" "assets/folder2"
And I am logged in with "ADMIN" permissions And I am logged in with "ADMIN" permissions
And I go to "/admin/assets" And I go to "/admin/assets"
@ -75,3 +75,11 @@ Feature: Manage files
And I press the "Apply Filter" button And I press the "Apply Filter" button
Then the "Files" table should contain "file1" Then the "Files" table should contain "file1"
And the "Files" table should not contain "document" 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"