Source of file PublicationArchive.php
Size: 10,348 Bytes - Last Modified: 2021-12-23T10:37:03+00:00
/var/www/docs.ssmods.com/process/src/src/Pages/PublicationArchive.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 | <?php /** * This file is part of SilverWare. * * PHP version >=5.6.0 * * For full copyright and license information, please view the * LICENSE.md file that was distributed with this source code. * * @package SilverWare\Publications\Pages * @author Colin Tucker <colin@praxis.net.au> * @copyright 2018 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-publications */ namespace SilverWare\Publications\Pages; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\DropdownField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\SS_List; use SilverStripe\View\ArrayData; use SilverWare\Extensions\Lists\ListViewExtension; use SilverWare\Extensions\Model\ImageDefaultsExtension; use SilverWare\Forms\FieldSection; use SilverWare\Lists\ListSource; use Page; /** * An extension of the page class for a publication archive. * * @package SilverWare\Publications\Pages * @author Colin Tucker <colin@praxis.net.au> * @copyright 2018 Praxis Interactive * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause * @link https://github.com/praxisnetau/silverware-publications */ class PublicationArchive extends Page implements ListSource { /** * Define constants. */ const SORT_ORDER = 'order'; const SORT_TITLE = 'title'; const SORT_DATE_ASC = 'date-asc'; const SORT_DATE_DESC = 'date-desc'; /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = 'Publication Archive'; /** * Human-readable plural name. * * @var string * @config */ private static $plural_name = 'Publication Archives'; /** * Description of this object. * * @var string * @config */ private static $description = 'Holds a series of publications organised into categories'; /** * Icon file for this object. * * @var string * @config */ private static $icon = 'silverware/publications: admin/client/dist/images/icons/PublicationArchive.png'; /** * Defines the table name to use for this object. * * @var string * @config */ private static $table_name = 'SilverWare_PublicationArchive'; /** * Defines the default child class for this object. * * @var string * @config */ private static $default_child = PublicationCategory::class; /** * Maps field names to field types for this object. * * @var array * @config */ private static $db = [ 'SortOrder' => 'Varchar(16)', 'ShowFileInfo' => 'Boolean' ]; /** * Defines the default values for the fields of this object. * * @var array * @config */ private static $defaults = [ 'ShowFileInfo' => 1, 'ImageDefaultAlignment' => 'right', 'ImageDefaultResizeWidth' => 300, 'ImageDefaultResizeHeight' => 400, 'ImageDefaultResizeMethod' => 'scale-width' ]; /** * Defines the allowed children for this object. * * @var array|string * @config */ private static $allowed_children = [ PublicationCategory::class, Publication::class ]; /** * Defines the extension classes to apply to this object. * * @var array * @config */ private static $extensions = [ ListViewExtension::class, ImageDefaultsExtension::class ]; /** * Defines the default values for the list view component. * * @var array * @config */ private static $list_view_defaults = [ 'ImageAlign' => 'right', 'ImageResizeWidth' => 150, 'ImageResizeHeight' => 200, 'ImageResizeMethod' => 'scale-width', 'HideNoDataMessage' => 1 ]; /** * Answers a list of field objects for the CMS interface. * * @return FieldList */ public function getCMSFields() { // Obtain Field Objects (from parent): $fields = parent::getCMSFields(); // Define Placeholder: $placeholder = _t(__CLASS__ . '.DROPDOWNDEFAULT', '(default)'); // Create Options Fields: $fields->addFieldsToTab( 'Root.Options', [ FieldSection::create( 'PublicationOptions', $this->fieldLabel('PublicationOptions'), [ DropdownField::create( 'SortOrder', $this->fieldLabel('SortOrder'), $this->getSortOrderOptions() )->setEmptyString(' ')->setAttribute('data-placeholder', $placeholder), CheckboxField::create( 'ShowFileInfo', $this->fieldLabel('ShowFileInfo') ) ] ) ] ); // Answer Field Objects: return $fields; } /** * Answers the labels for the fields of the receiver. * * @param boolean $includerelations Include labels for relations. * * @return array */ public function fieldLabels($includerelations = true) { // Obtain Field Labels (from parent): $labels = parent::fieldLabels($includerelations); // Define Field Labels: $labels['SortOrder'] = _t(__CLASS__ . '.SORTORDER', 'Sort order'); $labels['ShowFileInfo'] = _t(__CLASS__ . '.SHOWFILEINFORMATION', 'Show file information'); $labels['PublicationOptions'] = _t(__CLASS__ . '.PUBLICATIONS', 'Publications'); // Answer Field Labels: return $labels; } /** * Answers a list of publications within the publication archive. * * @return DataList */ public function getPublications() { $publications = ArrayList::create(); $publications->merge(Publication::get()->filter('ParentID', $this->AllChildren()->column('ID') ?: null)); $publications->merge($this->getChildMembers()); return $this->getSortedPublications($publications); } /** * Answers a list of the immediate child publications of the archive. * * @return DataList */ public function getChildPublications() { return $this->getSortedPublications($this->AllChildren()->filter('ClassName', Publication::class)); } /** * Answers a list of publications within the receiver. * * @return DataList */ public function getListItems() { return $this->getPublications(); } /** * Answers all categories within the receiver. * * @return DataList */ public function getAllCategories() { return PublicationCategory::get()->filter('ParentID', $this->ID); } /** * Answers all non-empty categories within the receiver. * * @return ArrayList */ public function getNonEmptyCategories() { return $this->getAllCategories()->filterByCallback(function ($category) { return $category->hasPublications(); }); } /** * Answers all visible categories within the receiver. * * @return ArrayList */ public function getVisibleCategories() { $data = ArrayList::create(); foreach ($this->getNonEmptyCategories()->filter('ShowOnSeparatePage', 0) as $category) { $data->push( ArrayData::create([ 'Title' => $category->Title, 'Category' => $category, 'Publications' => $this->getPublicationList($category) ]) ); } return $data; } /** * Answers the child publication list component for the template. * * @return BaseListComponent */ public function getChildPublicationList() { $list = clone $this->getListComponent(); $list->setSource($this->getChildPublications()); $list->setStyleIDFrom($this); return $list; } /** * Answers the publication list component for the template. * * @param PublicationCategory $category * * @return BaseListComponent */ public function getPublicationList(PublicationCategory $category) { $list = clone $this->getListComponent(); $list->setSource($category->getPublications()); $list->setStyleIDFrom($this, $category->Title); return $list; } /** * Answers a message string to be shown when no data is available. * * @return string */ public function getNoDataMessage() { return _t(__CLASS__ . '.NODATAAVAILABLE', 'No data available.'); } /** * Answers an array of options for the sort order field. * * @return array */ public function getSortOrderOptions() { return [ self::SORT_ORDER => _t(__CLASS__ . '.ORDER', 'Order'), self::SORT_TITLE => _t(__CLASS__ . '.TITLE', 'Title'), self::SORT_DATE_ASC => _t(__CLASS__ . '.DATEASCENDING', 'Date Ascending'), self::SORT_DATE_DESC => _t(__CLASS__ . '.DATEDESCENDING', 'Date Descending') ]; } /** * Sorts the given list of publications. * * @param SS_List $list * * @return SS_List */ public function getSortedPublications(SS_List $list) { switch ($this->SortOrder) { case self::SORT_DATE_ASC: return $list->sort('Date', 'ASC'); case self::SORT_DATE_DESC: return $list->sort('Date', 'DESC'); case self::SORT_ORDER: return $list->sort('Sort'); case self::SORT_TITLE: return $list->sort('Title', 'ASC'); } return $list; } } |