Source of file BannerComponent.php
Size: 12,915 Bytes - Last Modified: 2021-12-23T10:36:39+00:00
/var/www/docs.ssmods.com/process/src/src/Components/BannerComponent.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 | <?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\Banner\Components * @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-banner */ namespace SilverWare\Banner\Components; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\NumericField; use SilverStripe\Forms\TextField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataList; use SilverStripe\ORM\DB; use SilverStripe\ORM\SS_List; use SilverWare\Components\BaseComponent; use SilverWare\Extensions\Model\ImageResizeExtension; use SilverWare\Forms\FieldSection; use SilverWare\Forms\ToggleGroup; use SilverWare\Model\Slide; /** * An extension of the base component for a banner component. * * @package SilverWare\Banner\Components * @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-banner */ class BannerComponent extends BaseComponent { /** * Define sort constants. */ const SORT_ORDER = 'order'; const SORT_RANDOM = 'random'; /** * Define animation constants. */ const ANIM_SCROLL_LEFT = 'scroll-left'; const ANIM_SCROLL_RIGHT = 'scroll-right'; /** * Human-readable singular name. * * @var string * @config */ private static $singular_name = 'Banner Component'; /** * Human-readable plural name. * * @var string * @config */ private static $plural_name = 'Banner Components'; /** * Description of this object. * * @var string * @config */ private static $description = 'A component which shows a banner of images'; /** * Icon file for this object. * * @var string * @config */ private static $icon = 'silverware/banner: admin/client/dist/images/icons/BannerComponent.png'; /** * Defines the table name to use for this object. * * @var string * @config */ private static $table_name = 'SilverWare_BannerComponent'; /** * Defines an ancestor class to hide from the admin interface. * * @var string * @config */ private static $hide_ancestor = BaseComponent::class; /** * Defines the default child class for this object. * * @var string * @config */ private static $default_child = Slide::class; /** * Maps field names to field types for this object. * * @var array * @config */ private static $db = [ 'SortBy' => 'Varchar(16)', 'Animate' => 'Boolean', 'AnimationType' => 'Varchar(16)', 'AnimationDuration' => 'Int', 'NumberOfSlides' => 'AbsoluteInt' ]; /** * Defines the default values for the fields of this object. * * @var array * @config */ private static $defaults = [ 'SortBy' => self::SORT_ORDER, 'Animate' => 0, 'AnimationDuration' => 20, 'HideTitle' => 1 ]; /** * Defines the allowed children for this object. * * @var array|string * @config */ private static $allowed_children = [ Slide::class ]; /** * Maps field and method names to the class names of casting objects. * * @var array * @config */ private static $casting = [ 'WrapperAttributesHTML' => 'HTMLFragment' ]; /** * Defines the extension classes to apply to this object. * * @var array * @config */ private static $extensions = [ ImageResizeExtension::class ]; /** * Defines the asset folder for uploading images. * * @var string * @config */ private static $asset_folder = 'Slides/Banner'; /** * Holds a list of slides which override the child slides. * * @var SS_List */ protected $slides; /** * 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__ . '.DROPDOWNSELECT', 'Select'); // Create Options Fields: $fields->addFieldToTab( 'Root.Options', FieldSection::create( 'BannerOptions', $this->fieldLabel('BannerOptions'), [ TextField::create( 'NumberOfSlides', $this->fieldLabel('NumberOfSlides') ), DropdownField::create( 'SortBy', $this->fieldLabel('SortBy'), $this->getSortByOptions() ), ToggleGroup::create( 'Animate', $this->fieldLabel('Animate'), [ DropdownField::create( 'AnimationType', $this->fieldLabel('AnimationType'), $this->getAnimationTypeOptions() )->setEmptyString(' ')->setAttribute('data-placeholder', $placeholder), NumericField::create( 'AnimationDuration', $this->fieldLabel('AnimationDuration') )->setRightTitle( _t( __CLASS__ . '.ANIMATIONDURATIONINSECONDS', 'Duration of the animation cycle in seconds.' ) ) ] ) ] ) ); // 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['SortBy'] = _t(__CLASS__ . '.SORTBY', 'Sort by'); $labels['Animate'] = _t(__CLASS__ . '.ANIMATE', 'Animate'); $labels['BannerOptions'] = _t(__CLASS__ . '.BANNER', 'Banner'); $labels['NumberOfSlides'] = _t(__CLASS__ . '.NUMBEROFSLIDES', 'Number of slides'); $labels['AnimationType'] = _t(__CLASS__ . '.ANIMATIONTYPE', 'Animation type'); $labels['AnimationDuration'] = _t(__CLASS__ . '.ANIMATIONDURATION', 'Animation duration'); // Define Relation Labels: if ($includerelations) { } // Answer Field Labels: return $labels; } /** * Answers the asset folder used by the receiver. * * @return string */ public function getAssetFolder() { return $this->config()->asset_folder; } /** * Answers an array of class names for the HTML template. * * @return array */ public function getClassNames() { $classes = parent::getClassNames(); if ($this->Animate) { $classes[] = 'animated'; } return $classes; } /** * Answers an array of HTML tag attributes for the wrapper. * * @return array */ public function getWrapperAttributes() { $attributes = [ 'id' => $this->WrapperID, 'class' => $this->WrapperClass ]; if ($this->Animate && $this->AnimationDuration) { $attributes['style'] = "animation-duration: {$this->AnimationDuration}s"; } $this->extend('updateWrapperAttributes', $attributes); return $attributes; } /** * Answers the HTML tag attributes for the wrapper as a string. * * @return string */ public function getWrapperAttributesHTML() { return $this->getAttributesHTML($this->getWrapperAttributes()); } /** * Answers an array of wrapper class names for the HTML template. * * @return array */ public function getWrapperClassNames() { $classes = ['wrapper']; if ($this->Animate) { $classes[] = 'animated'; if ($this->AnimationType) { $classes[] = $this->getAnimationTypeClass(); } } $this->extend('updateWrapperClassNames', $classes); return $classes; } /** * Answers a unique ID for the wrapper element. * * @return string */ public function getWrapperID() { return sprintf('%s_Wrapper', $this->getHTMLID()); } /** * Answers a unique CSS ID for the wrapper element. * * @return string */ public function getWrapperCSSID() { return $this->getCSSID($this->getWrapperID()); } /** * Defines the slides property for the receiver. * * @param SS_List $slides * * @return $this */ public function setSlides(SS_List $slides) { $list = ArrayList::create(); foreach ($slides as $slide) { $list->push($slide->setParentInstance($this)); } $this->slides = $list; } /** * Answers a list of all slides within the receiver. * * @return DataList */ public function getSlides() { return $this->slides ?: $this->getAllChildrenByClass(Slide::class); } /** * Answers true if the receiver has at least one slide. * * @return boolean */ public function hasSlides() { return (boolean) $this->getSlides()->exists(); } /** * Answers a list of the enabled slides within the receiver. * * @return ArrayList */ public function getEnabledSlides() { $slides = ArrayList::create(); foreach ($this->getSlides() as $slide) { $slides->merge($slide->getEnabledSlides()); } $slides = $this->sort($slides); if ($this->NumberOfSlides) { return $slides->limit($this->NumberOfSlides); } return $slides; } /** * Answers the CSS class for the selected animation type. * * @return string */ public function getAnimationTypeClass() { switch ($this->AnimationType) { case self::ANIM_SCROLL_LEFT: return 'scroll-left'; case self::ANIM_SCROLL_RIGHT: return 'scroll-right'; } } /** * Answers an array of options for the animation type field. * * @return array */ public function getAnimationTypeOptions() { return [ self::ANIM_SCROLL_LEFT => _t(__CLASS__ . '.SCROLLLEFT', 'Scroll Left'), self::ANIM_SCROLL_RIGHT => _t(__CLASS__ . '.SCROLLRIGHT', 'Scroll Right') ]; } /** * Answers an array of options for the sort by field. * * @return array */ public function getSortByOptions() { return [ self::SORT_ORDER => _t(__CLASS__ . '.ORDER', 'Order'), self::SORT_RANDOM => _t(__CLASS__ . '.RANDOM', 'Random') ]; } /** * Sorts the given list of slides. * * @param SS_List $list * * @return SS_List */ protected function sort(SS_List $list) { switch ($this->SortBy) { // Random Sort Order: case self::SORT_RANDOM: $slides = $list->toArray(); shuffle($slides); return ArrayList::create($slides); // Default Sort Order: default: return $list; } } } |