Source of file MagnificGalleryPage.php
Size: 10,810 Bytes - Last Modified: 2021-12-23T10:01:19+00:00
/var/www/docs.ssmods.com/process/src/code/MagnificGalleryPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 | <?php class MagnificGalleryPage extends Page { private static $description = 'A magnific image gallery'; private static $icon = 'magnific-gallery/images/image-gallery-icon.png'; private static $db = array( 'AlbumEffect' => 'Varchar', 'MediaPerPage' => 'Int', ); private static $has_one = array( 'RootFolder' => 'Folder' ); private static $defaults = array( 'AlbumEffect' => 'lily', 'MediaPerPage' => '30', ); private static $has_many = array( 'Albums' => 'MagnificGalleryAlbum', 'GalleryItems' => 'MagnifcGalleryItem' ); private static $translate = array( 'Title', 'Content', 'URLSegment', 'MenuTitle', 'MetaDescription' ); /** * List all available effects * @link http://tympanus.net/Development/HoverEffectIdeas/ * @return array */ public static function listEffects() { return array('lily', 'sadie', 'honey', 'layla', 'zoe', 'oscar', 'marley', 'ruby', 'roxy', 'bubba', 'romeo', 'dexter', 'sarah', 'chico', 'milo', 'julia', 'goliath', 'hera', 'winston', 'selena', 'terry', 'phoebe', 'apollo', 'kira', 'steve', 'moses', 'jazz', 'ming', 'lexi', 'duke'); } function onBeforeWrite() { parent::onBeforeWrite(); $this->checkFolder(); } function onBeforeDelete() { // check if Page still exists in live mode $className = $this->ClassName; $livePage = Versioned::get_one_by_stage($className, "Live", "\"{$className}_Live\".\"ID\" = {$this->ID}"); // check if Page still exists in stage mode $stagePage = Versioned::get_one_by_stage($className, "Stage", "\"{$className}\".\"ID\" = {$this->ID}"); // if Page only exists in Live OR Stage mode -> Page will be deleted completely if (!($livePage && $stagePage)) { // delete existing Albums $this->Albums()->removeAll(); // remove folder` if ($this->RootFolderID && $this->RootFolder()->ID) { $this->RootFolder()->delete(); } } parent::onBeforeDelete(); } function checkFolder() { if (!$this->exists()) { return; } if (!$this->URLSegment) { return; } $baseFolder = ''; if (class_exists('Subsite') && self::config()->use_subsite_integration) { if ($this->SubsiteID) { $subsite = $this->Subsite(); if ($subsite->hasField('BaseFolder')) { $baseFolder = $subsite->BaseFolder; } else { $filter = new FileNameFilter(); $baseFolder = $filter->filter($subsite->getTitle()); $baseFolder = str_replace(' ', '', ucwords(str_replace('-', ' ', $baseFolder))); } $baseFolder .= '/'; } } $folderPath = $baseFolder."galleries/{$this->URLSegment}"; $folder = Folder::find_or_make($folderPath); if ($this->RootFolderID && $folder->ID != $this->RootFolderID) { if ($this->RootFolder()->exists()) { // We need to rename current folder $this->RootFolder()->setFilename($folder->Filename); $this->RootFolder()->write(); $folder->deleteDatabaseOnly(); //Otherwise we keep a stupid clone that will be used as the parent } else { $this->RootFolderID = $folder->ID; } } else { $this->RootFolderID = $folder->ID; } } public function getCMSFields() { $fields = parent::getCMSFields(); // Build albums tab $fields->addFieldToTab('Root', $albumTab = new Tab('Albums'), 'Main'); $albumTab->setTitle(_t('MagnificGalleryPage.ALBUMS', 'Albums')); if ($rootFolder = $this->RootFolder()) { $albumConfig = GridFieldConfig_RecordEditor::create(); // Enable bulk image loading if necessary module is installed // @see composer.json/suggests if (class_exists('GridFieldBulkManager')) { $albumConfig->addComponent(new GridFieldBulkManager()); } // Enable album sorting if necessary module is installed // @see composer.json/suggests if (class_exists('GridFieldSortableRows')) { $albumConfig->addComponent(new GridFieldSortableRows('SortOrder')); } else if (class_exists('GridFieldOrderableRows')) { $albumConfig->addComponent(new GridFieldOrderableRows('SortOrder')); } $albumField = new GridField('Albums', 'Albums', $this->Albums(), $albumConfig); $fields->addFieldToTab("Root.Albums", $albumField); } else { $fields->addFieldToTab( "Root.Albums", new HeaderField( _t("MagnificGalleryPage.ALBUMSNOTSAVED", "You may add albums to your gallery once you have saved the page for the first time."), $headingLevel = "3" ) ); } // Build configuration fields $fields->addFieldToTab('Root', $configTab = new Tab('Configuration')); $configTab->setTitle(_t('MagnificGalleryPage.CONFIGURATION', 'Configuration')); $fields->addFieldsToTab("Root.Configuration", array( $albumEffects = new DropdownField('AlbumEffect', _t('MagnificGalleryPage.ALBUMEFFECT', 'Album Effect'), array_combine(self::listEffects(), self::listEffects())), new NumericField('MediaPerPage', _t('MagnificGalleryPage.IMAGESPERPAGE', 'Number of images per page')), new LiteralField('FolderUsed', '<div class="message">'._t('MagnificGalleryPage.FOLDERUSED', 'Images will be saved in : %s', array($this->RootFolder()->Filename)).'</div>') )); $albumEffects->setDescription(_t('MagnificGalleryPage.PREVIEWPAGE', 'Preview effects <a target="_blank" href="http://tympanus.net/Development/HoverEffectIdeas/">here</a>')); return $fields; } } class MagnificGalleryPage_Controller extends Page_Controller { private static $allowed_actions = array( 'album', 'rss' ); /** * @var MagnificGalleryAlbum */ protected $currentAlbum = null; public function init() { parent::init(); Requirements::themedCSS('MagnificGallery', 'magnific-gallery'); Requirements::javascript("magnific-gallery/javascript/imagegallery_init.js"); } public function index() { if ($this->SingleAlbumView()) { return $this->album(); } else { return $this->renderWith(array($this->data(), 'MagnificGalleryPage', 'Page')); } } protected function adjacentAlbum($dir) { $currentAlbum = $this->CurrentAlbum(); if (empty($currentAlbum)) return null; $direction = ($dir == "next") ? ">" : "<"; $sort = ($dir == "next") ? "ASC" : "DESC"; $parentID = Convert::raw2sql($this->ID); $adjacentID = Convert::raw2sql($currentAlbum->ID); $adjacentSort = Convert::raw2sql($currentAlbum->SortOrder); // Get next/previous album by sort (or ID if sort values haven't been set) $filter = "\"MagnificGalleryAlbum\".\"GalleryPageID\" = '$parentID' AND \"MagnificGalleryAlbum\".\"SortOrder\" {$direction} '$adjacentSort' OR ( \"MagnificGalleryAlbum\".\"SortOrder\" = '$adjacentSort' AND \"MagnificGalleryAlbum\".\"ID\" {$direction} '$adjacentID' )"; return MagnificGalleryAlbum::get()->where($filter)->sort("\"SortOrder\" $sort, \"ID\" $sort")->first(); } public function NextAlbum() { return $this->adjacentAlbum("next"); } public function PrevAlbum() { return $this->adjacentAlbum("prev"); } public function album() { if (!MagnificGalleryItem::config()->image_crop) { Requirements::javascript("magnific-gallery/javascript/freewall.js"); } Requirements::javascript("magnific-gallery/javascript/magnific-popup.js"); Requirements::css("magnific-gallery/javascript/magnific-popup.css"); if (!$this->CurrentAlbum()) { return $this->httpError(404); } return $this->renderWith(array($this->data().'_album', 'MagnificGalleryPage_album', 'Page')); } public function PaginatedItems() { if (!$this->CurrentAlbum()) { return; } if (!$this->data()->MediaPerPage) { return $this->CurrentAlbum()->GalleryItems(); } $list = new PaginatedList($this->CurrentAlbum()->GalleryItems(), $this->request); $list->setPageLength($this->data()->MediaPerPage); return $list; } /** * @return MagnificGalleryAlbum */ public function CurrentAlbum() { if ($this->currentAlbum) return $this->currentAlbum; $params = Controller::curr()->getURLParams(); if (!empty($params['ID'])) { return MagnificGalleryAlbum::get()->filter(array( "URLSegment" => Convert::raw2sql($params['ID']), "GalleryPageID" => $this->ID ))->first(); } return false; } public function SingleAlbumView() { if ($this->Albums()->Count() == 1) { $this->currentAlbum = $this->Albums()->First(); return true; } return false; } public function RssLink() { return $this->Link('rss'); } public function rss() { $rss = new RSSFeed( $list = MagnificGalleryItem::get()->sort('Created DESC')->limit(10)->toArray(), // an SS_List containing your feed items $link = Director::absoluteURL($this->RssLink()), // a HTTP link to this feed $title = _t('MagnificGalleryPage.RSS_TITLE', 'Recent images from %s', array(Siteconfig::current_site_config()->Title)), // title for this feed, displayed in RSS readers $description = _t('MagnificGalleryPage.RSS_DESCRIPTION', 'Get recents images from our website') // description ); // Outputs the RSS feed to the user. return $rss->outputToBrowser(); } } |