1 <?php
2 3 4 5 6 7
8 class PhotoGalleryPage extends Page {
9
10 static $icon = "photo_gallery/images/gallery";
11 static $allowed_children = array( 'PhotoAlbumPage' );
12
13 static $db = array (
14 'ImagesPerPage' => 'Int',
15 'CropImages' => 'Boolean',
16 );
17
18 static $has_many = array (
19 'Albums' => 'PhotoAlbumPage',
20 'Items' => 'PhotoAlbumItem',
21 );
22
23 static $has_one = array (
24 'Folder' => 'Folder',
25 );
26
27 static $defaults = array (
28 'ImagesPerPage' => '20',
29 'CropImages' => '0',
30 'AutoChild' => '0',
31 );
32
33
34 static $autoAlbum = true;
35
36 static function setAutoAlbum($data=true) {
37 self::$autoAlbum = $data;
38 }
39
40 public function getCMSFields() {
41 $fields = parent::getCMSFields();
42
43 $fields->addFieldToTab( 'Root.Content.Main', new NumericField( 'ImagesPerPage', $this->fieldLabel('ImagesPerPage')), 'Content');
44 $fields->addFieldToTab( 'Root.Content.Main', new CheckboxField('CropImages', $this->fieldLabel('CropImages')), 'Content');
45 $fields->removeFieldFromTab('Root.Behaviour','AutoChild');
46
47 return $fields;
48 }
49
50 51 52 53 54
55 function getFolderPath() {
56 if ($this->FolderID && $this->Folder()->ID) {
57 return $this->Folder()->Filename;
58 }
59 elseif ($this->ID && $folder = $this->createFolder()) {
60 return $folder->Filename;
61 }
62 return false;
63 }
64
65 66 67
68 function createFolder() {
69 if (!$this->ID) return false;
70 $rootFolder = Folder::findOrMake('photo-gallery');
71 if ($rootFolder->Title != 'Photo Gallery') {
72 $rootFolder->Title = 'Photo Gallery';
73 $rootFolder->write();
74 }
75 $folder = Folder::findOrMake($rootFolder->Filename . $this->ID);
76 $folder->Title = $this->Title;
77 $folder->write();
78 $this->FolderID = $folder->ID;
79
80 $this->writeToStage('Stage');
81 if ($this->isPublished())
82 $this->publish('Stage', 'Live');
83 return $folder;
84 }
85
86 function onBeforeWrite() {
87 if (!$this->FolderID) {
88 $this->createFolder();
89 }
90 if ($this->isChanged('Title') && $this->FolderID && $folder = $this->Folder()) {
91 $folder->Title = $this->Title;
92 $folder->write();
93 }
94 parent::onBeforeWrite();
95 }
96
97 function onAfterDelete() {
98 parent::onAfterDelete();
99
100 if ($this->IsDeletedFromStage && !$this->ExistsOnLive) {
101 $this->Folder()->delete();
102 }
103 }
104 }
105
106 107 108 109 110 111
112 class PhotoGalleryPage_Controller extends Page_Controller {
113
114 function index() {
115 if (PhotoGalleryPage::$autoAlbum && $this->AllChildren()->Count() == 1) {
116 if ($firstChild = $this->AllChildren()->First()) {
117 return Director::redirect($firstChild->Link());
118 }
119 }
120 return parent::defaultAction('index');
121
122 }
123
124
125 126 127 128 129
130 function ReverseChildren() {
131 $children = $this->Children();
132 $children->sort('Sort', 'DESC');
133 return $children;
134 }
135 }
136
[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.
-