Skip to content
Browse files

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

API Filter by date created for files
  • Loading branch information...
2 parents 66919ed + 3765030 commit 9bff36c0788f1f701d77b94f9a1a38904c234b41 @chillu chillu committed Apr 28, 2014
Showing with 50 additions and 22 deletions.
  1. +40 −20 code/controllers/AssetAdmin.php
  2. +10 −2 tests/behat/features/manage-files.feature
View
60 code/controllers/AssetAdmin.php
@@ -106,17 +106,19 @@ public function getList() {
// 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']
));
}
@@ -125,23 +127,26 @@ public function getList() {
// 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']])) {
- $exts = File::config()->app_categories[$params['AppCategory']];
- } else {
- $exts = array();
- }
- $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) . ')');
- }
+ if(!empty($params['AppCategory'])
+ && !empty(File::config()->app_categories[$params['AppCategory']])
+ ) {
+ $exts = File::config()->app_categories[$params['AppCategory']];
+ $list = $list->filter('Name:PartialMatch', $exts);
+ }
+
+ // 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;
@@ -350,6 +355,21 @@ public function getSearchContext() {
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'),
@@ -390,7 +410,7 @@ public function SearchForm() {
$fields = $context->getSearchFields();
$actions = new FieldList(
FormAction::create('doSearch', _t('CMSMain_left_ss.APPLY_FILTER', 'Apply Filter'))
- ->addExtraClass('ss-ui-action-constructive'),
+ ->addExtraClass('ss-ui-action-constructive'),
Object::create('ResetFormAction', 'clear', _t('CMSMain_left_ss.RESET', 'Reset'))
);
View
12 tests/behat/features/manage-files.feature
@@ -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"

0 comments on commit 9bff36c

Please sign in to comment.
Something went wrong with that request. Please try again.