diff --git a/modules/cms/controllers/index/config_page_list.yaml b/modules/cms/controllers/index/config_page_list.yaml index c2205e19b..b28e06ec0 100644 --- a/modules/cms/controllers/index/config_page_list.yaml +++ b/modules/cms/controllers/index/config_page_list.yaml @@ -9,3 +9,7 @@ descriptionProperties: noRecordsMessage: 'cms::lang.page.no_list_records' deleteConfirmation: 'cms::lang.page.delete_confirm_multiple' itemType: page +sortingProperties: + url: 'cms::lang.page.url' + title: 'cms::lang.page.title' + fileName: 'cms::lang.page.file_name' \ No newline at end of file diff --git a/modules/cms/lang/en/lang.php b/modules/cms/lang/en/lang.php index 5d9aee367..3bbcf122b 100644 --- a/modules/cms/lang/en/lang.php +++ b/modules/cms/lang/en/lang.php @@ -115,7 +115,10 @@ return [ 'delete_confirm_multiple' => 'Delete selected pages?', 'delete_confirm_single' => 'Delete this page?', 'no_layout' => '-- no layout --', - 'cms_page' => 'CMS page' + 'cms_page' => 'CMS page', + 'title' => 'Page title', + 'url' => 'Page URL', + 'file_name' => 'Page file name' ], 'layout' => [ 'not_found_name' => "The layout ':name' is not found", @@ -241,7 +244,8 @@ return [ 'not_found' => 'Template not found.', 'saved' => 'Template saved.', 'no_list_records' => 'No records found', - 'delete_confirm' => 'Delete selected templates?' + 'delete_confirm' => 'Delete selected templates?', + 'order_by' =>'Order by' ], 'permissions' => [ 'name' => 'CMS', diff --git a/modules/cms/widgets/TemplateList.php b/modules/cms/widgets/TemplateList.php index 3c230a121..593db8146 100644 --- a/modules/cms/widgets/TemplateList.php +++ b/modules/cms/widgets/TemplateList.php @@ -17,6 +17,8 @@ use Backend\Classes\WidgetBase; */ class TemplateList extends WidgetBase { + const SORTING_FILENAME = 'fileName'; + use \Backend\Traits\SelectableWidget; protected $searchTerm = false; @@ -69,6 +71,12 @@ class TemplateList extends WidgetBase */ public $ignoreDirectories = []; + /** + * @var boolean Defines sorting properties. + * The sorting feature is disabled if there are no sorting properties defined. + */ + public $sortingProperties = []; + /* * Public methods */ @@ -137,6 +145,16 @@ class TemplateList extends WidgetBase return $this->updateList(); } + public function onApplySorting() + { + $this->setSortingProperty(Input::get('sortProperty')); + + $result = $this->updateList(); + $result['#'.$this->getId('sorting-options')] = $this->makePartial('sorting-options'); + + return $result; + } + // // Methods for the internal use // @@ -156,9 +174,7 @@ class TemplateList extends WidgetBase $items = array_map([$this, 'normalizeItem'], $items); - usort($items, function ($a, $b) { - return strcmp($a->fileName, $b->fileName); - }); + $this->sortItems($items); /* * Apply the search @@ -217,6 +233,10 @@ class TemplateList extends WidgetBase } } + // Sort folders by name regardless of the + // selected sorting options. + ksort($foundGroups); + foreach ($foundGroups as $group) { $result[] = $group; } @@ -224,6 +244,15 @@ class TemplateList extends WidgetBase return $result; } + protected function sortItems(&$items) + { + $sortingProperty = $this->getSortingProperty(); + + usort($items, function ($a, $b) use ($sortingProperty) { + return strcmp($a->$sortingProperty, $b->$sortingProperty); + }); + } + protected function removeIgnoredDirectories($items) { if (!$this->ignoreDirectories) { @@ -274,6 +303,10 @@ class TemplateList extends WidgetBase 'descriptions' => $descriptions ]; + foreach ($this->sortingProperties as $property=>$name) { + $result[$property] = $item->$property; + } + return (object) $result; } @@ -385,4 +418,20 @@ class TemplateList extends WidgetBase $this->groupStatusCache = $statuses; $this->putSession($this->getThemeSessionKey('groups'), $statuses); } + + protected function getSortingProperty() + { + $property = $this->getSession($this->getThemeSessionKey('sorting_property'), self::SORTING_FILENAME); + + if (!array_key_exists($property, $this->sortingProperties)) { + return self::SORTING_FILENAME; + } + + return $property; + } + + protected function setSortingProperty($property) + { + $this->putSession($this->getThemeSessionKey('sorting_property'), $property); + } } diff --git a/modules/cms/widgets/templatelist/partials/_sorting-options.htm b/modules/cms/widgets/templatelist/partials/_sorting-options.htm new file mode 100644 index 000000000..da02a5b92 --- /dev/null +++ b/modules/cms/widgets/templatelist/partials/_sorting-options.htm @@ -0,0 +1,7 @@ +sortingProperties as $propertyName=>$propertyTitle): ?> +