Version: current

      1 <?php
      2 /**
      3  * @package photo_gallery
      4  * @class PhotoAlbumPage - страница альбома из фотогалереи
      5  *
      6  * @author menedem, dvp
      7  */
      8 class PhotoAlbumPage extends Page {
      9
     10     static $icon = "photo_gallery/images/gallery";
     11     static $allowed_children = 'none';
     12     static $default_parent = "PhotoGalleryPage";
     13     static $can_be_root = false;
     14
     15     static $db = array (
     16         'RandomCover' => 'Boolean'
     17     );
     18
     19
     20     static $has_one = array (
     21         'Folder' => 'Folder',
     22         'CoverImage' => 'Image'
     23     );
     24
     25     static $has_many = array (
     26         'Items' => 'PhotoAlbumItem'
     27     );
     28
     29     static $defaults = array (
     30         'RandomCover' => '0',
     31     );
     32
     33     /**
     34      * возвращает список фото для виджетов
     35      *
     36      * @param int $count - лимот возвращаемых фото [1]
     37      * @param int $pageID - ID фотогалереи или альбома, если не указан - из всех
     38      * @param boolean $random  - флаг случайная сортировка или по умолчанию
     39      *
     40      * @return DataObjectSet
     41      */
     42     static function get_photos($count=1, $pageID=false, $random = false) {
     43         $filter = '';
     44         $sort = ($random) ? 'RAND()' : 'ID DESC';
     45
     46         if ($pageID) {
     47             $obj = DataObject::get_by_id('SiteTree', $pageID);
     48             if ($obj) {
     49                 if ($obj->ClassName == 'PhotoAlbumPage') {
     50                     $filter = "AlbumID = $pageID";
     51                 }
     52                 if ($obj->ClassName == 'PhotoGalleryPage') {
     53                     $filter = "GalleryID = $pageID";
     54                 }
     55             }
     56         }
     57         return DataObject::get('PhotoAlbumItem', $filter, $sort, '', $count);
     58     }
     59
     60     /**
     61      * Возвращает фотогалерю альбома
     62      *
     63      * @return PhotoGalleryPage
     64      */
     65     function Gallery() {
     66         return $this->Parent();
     67     }
     68
     69     /**
     70      * Возвращает путь к папке альбома
     71      *
     72      * @return string
     73      */
     74     function getFolderPath() {
     75         if ($this->FolderID && $this->Folder()->ID) {
     76             return $this->Folder()->Filename;
     77         }
     78         elseif ($this->ID && $folder = $this->createFolder()) {
     79             return $folder->Filename;
     80         }
     81         return false;
     82     }
     83
     84     /**
     85      * Создает папку альбома
     86      */
     87     function createFolder() {
     88         if (!$this->ID) return false;
     89         if (!$this->ParentID || !$this->Parent()->ID) return false;
     90
     91         if ($rootPath = $this->Parent()->FolderPath) {
     92             $folder = Folder::findOrMake($rootPath . $this->ID);
     93             $folder->Title = $this->Title;
     94             $folder->write();
     95             $this->FolderID = $folder->ID;
     96             //$this->writeWithoutVersion();
     97             $this->writeToStage('Stage');
     98             if ($this->isPublished())
     99                 $this->publish('Stage', 'Live');
    100             return $folder;
    101         }
    102         return false;
    103     }
    104
    105     public function getCMSFields() {
    106         $fields = parent::getCMSFields();
    107 //      $fields->removeFieldFromTab(  'Root.Content.Main', 'Content');
    108
    109         $fields->insertAfter(new Tab("Photos", _t('PhotoAlbumPage.Photos','Photos')), 'Main');
    110         if ($this->FolderPath) {
    111             $manager = new PhotoAlbumManager(
    112                         $this,
    113                         'Items',
    114                         'PhotoAlbumItem',
    115                         'Image',
    116                         array('Caption' => singleton('PhotoAlbumItem')->fieldLabel('Caption')),
    117                         'getCMSFields_forPopup'
    118             );
    119             $manager->setUploadFolder($this->FolderPath);
    120             $manager->setBrowseButtonText(_t('PhotoAlbumPage.Upload','Upload'));
    121             $fields->addFieldToTab("Root.Content.Photos", $manager);
    122         } else {
    123             $fields->addFieldToTab("Root.Content.Photos", new LiteralField('SavePlease', _t('PhotoAlbumPage.SavePlease','Please save Gallery and Album before adding photos')));
    124         }
    125
    126         $fields->addFieldToTab("Root.Content.Main", new ImageField('CoverImage', $this->fieldLabel('CoverImage')), 'Content');
    127         $fields->addFieldToTab("Root.Content.Main", new CheckboxField('RandomCover', $this->fieldLabel('RandomCover')), 'Content');
    128         return $fields;
    129     }
    130
    131     function AlbumCover() {
    132         if ($this->RandomCover) {
    133             $cover = DataObject::get_one("PhotoAlbumItem", "AlbumID = {$this->ID}", true, "RAND()");
    134             if ($cover) return $cover->Image();
    135         } else {
    136             return $this->CoverImage();
    137         }
    138         return false;
    139     }
    140
    141     function onBeforeWrite() {
    142         if (!$this->FolderID) {
    143             $this->createFolder();
    144         }
    145         if ($this->isChanged('Title') && $this->FolderID && $folder = $this->Folder()) {
    146             $folder->Title = $this->Title;
    147             $folder->write();
    148         }
    149         parent::onBeforeWrite();
    150     }
    151
    152     function onAfterWrite() {
    153         parent::onAfterWrite();
    154
    155     }
    156
    157     function onAfterDelete() {
    158         parent::onAfterDelete();
    159         // Total delete from site
    160         if ($this->IsDeletedFromStage && !$this->ExistsOnLive) {
    161             if ($items = $this->Items()) {
    162                 foreach ($items as $item)
    163                     $item->delete();
    164             }
    165             //"дурацкая" проверка на совпадение папки альбома и галереи (чтоб не удалялась, если все хранится в одной папке - следствие бага при создании папок)
    166             if ($this->FolderID && $this->Folder()->ID && ($this->Folder()->ID != $this->Gallery()->Folder()->ID))
    167                 $this->Folder()->delete();
    168         }
    169     }
    170 }
    171
    172 /**
    173  * @package photo_gallery
    174  * @class PhotoAlbumPage_Controller - контроллер страницы альбома из фотогалереи
    175  */
    176 class PhotoAlbumPage_Controller extends Page_Controller {
    177     function AlbumPhotos() {
    178         $offset = 0;
    179         if (isset($_GET['start'])) {
    180             $offset = (int) $_GET['start'];
    181         }
    182         $pageSize = $this->Gallery()->ImagesPerPage;
    183         if (!$pageSize) {
    184             $pageSize = PhotoGalleryPage::$defaults['ImagesPerPage'];
    185         }
    186         $rs = DataObject::get('PhotoAlbumItem', "AlbumID = {$this->ID}", 'SortOrder', '', "$offset,$pageSize");
    187         if ($this->hasMethod('setSEOVars')) {
    188             $this->setSEOVars($rs); //Выставляем SEO-переменные (ф-я setSEOVars находится в Webylon Page_Controller)
    189         }
    190         return $rs;
    191     }
    192 }
    193

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -