Source of file GalleryExtension.php
Size: 3,767 Bytes - Last Modified: 2021-12-23T10:08:47+00:00
/var/www/docs.ssmods.com/process/src/src/extensions/GalleryExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | <?php namespace NZTA\Gallery\Extensions; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataExtension; use Symbiote\GridFieldExtensions\GridFieldOrderableRows; use NZTA\Gallery\Model\GalleryItem; class GalleryExtension extends DataExtension { /** * Defines the character limit that the thumbnail captions are set to. * * @var integer */ private static $thumbnail_caption_length = 35; /** * @var array */ private static $db = [ 'GalleryHeading' => 'Varchar(255)', 'IsActive' => 'Boolean' ]; /** * @var array */ private static $has_many = [ 'GalleryItems' => GalleryItem::class ]; /** * Defines the PageTypes that can have a Gallery, note these can be defined * in the configurations/yml file * * @var array */ private static $pages_allowed_gallery = []; /** * @param FieldList $fields */ public function updateCMSFields(FieldList $fields) { $allowedPages = Config::inst()->get(GalleryExtension::class, 'pages_allowed_gallery'); if (count($allowedPages) == 0 || in_array($this->owner->ClassName, $allowedPages)) { $fields->addFieldsToTab( 'Root.Gallery', [ CheckboxField::create('IsActive', 'Include Gallery') ->setDescription('Ensure to tick this box if you wish to include a Gallery'), TextField::create('GalleryHeading', 'Gallery Heading') ->setDescription('This is the heading displayed above the list of Gallery Items'), GridField::create( 'GalleryItems', 'Gallery Items', $this->owner->GalleryItems()->sort('SortOrder'), GridFieldConfig_RecordEditor::create() ->addComponent(new GridFieldOrderableRows('SortOrder')) ) ->setDescription('This is the list of Gallery Items to be displayed on the page') ] ); } } /** * Helper to get all the gallery data as JSON so a front end JS application * can generate the gallery with a lightbox feature. * * @return string */ public function getGalleryData() { $data = []; if (!$this->owner->IsActive) { return json_encode($data); } $items = $this->owner->GalleryItems(); if ($items->count() == 0) { return json_encode($data); } foreach ($items as $item) { $hasImage = ($item->ImageID != 0 && $item->Image()->exists()); if ($hasImage) { $captionLength = Config::inst()->get(GalleryExtension::class, 'thumbnail_caption_length'); $data[] = [ 'id' => $item->ID, 'caption' => $item->Caption ? $item->Caption : $item->Image()->Title, 'thumbnail' => $item->Image()->Fill(480, 375)->AbsoluteLink(), 'thumbnailCaption' => $item->Caption ? $item->dbObject('Caption')->LimitCharacters($captionLength) : $item->Image()->dbObject('Title')->LimitCharacters($captionLength), 'url' => $item->Image()->AbsoluteLink() ]; } } return json_encode($data); } } |