1 <?php
2 class MediawebPage extends Page {
3 static $hide_ancestor = 'Page';
4 static $icon = "webylon/img/page";
5
6 static $db = array(
7 'RID' => 'Text'
8 );
9 static $defaults = array(
10 'RID' => ''
11 );
12 static $has_one = array(
13 'CoverImage' => 'MediawebPage_Photo',
14 );
15 static $has_many = array(
16 "Photos" => "MediawebPage_Photo",
17 "Files" => "MediawebPage_File",
18 );
19
20 function getCMSFields() {
21 $fields = parent::getCMSFields();
22
23 if (Director::isDev()) {
24 $fields->addFieldToTab('Root.Content', new Tab("Configuration", _t("MediawebPage.Configuration", "Configuration")), 'Metadata');
25 $fields->addFieldToTab("Root.Content.Configuration", new TextField('RID',_t('MediawebPage.RID','RID')));
26 }
27
28
29 if (!$this->ID || (!$this->ExistsOnLive && $this->IsDeletedFromStage))
30 return $fields;
31
32 $folder = substr_replace(str_replace(ASSETS_DIR.'/', '', $this->getAssociatedFolder()->Filename), "", -1);
33
34 $fields->addFieldToTab('Root.Content', new Tab("PagePhotos", _t("MediawebPage.PagePhotos", "Photos")), 'Metadata');
35 $photoManager = new MediawebPagePhoto_Manager($this, "Photos", "MediawebPage_Photo", "Photo", array("Caption"=>_t("MediawebPage.Caption","Caption")), "getCMSFields_forPopup");
36 $photoManager->setUploadFolder($folder);
37 $fields->addFieldToTab('Root.Content.PagePhotos', $photoManager);
38
39
40 $fields->addFieldToTab('Root.Content', new Tab("PageFiles", _t("MediawebPage.PageFiles", "Files")), 'Metadata');
41 $fileManager = new MediawebPageFiles_Manager($this, "Files", "MediawebPage_File", "Attach", array("Caption"=>_t("MediawebPage.Caption","Caption")), "getCMSFields_forPopup");
42 $fileManager->setUploadFolder($folder);
43 $fields->addFieldToTab('Root.Content.PageFiles', $fileManager);
44
45 $photos = DataObject::get("MediawebPage_Photo", "MediawebPageID = ".$this->ID);
46 if ( $photos && $photos->Count() > 0 ) {
47 $coverPhotoField = new DropdownField('CoverImageID',_t('MediawebPage.CoverPhoto', 'Cover Photo'), $photos->toDropdownMap('ID','Caption'));
48 $coverPhotoField->setRightTitle(_t('MediawebPage.CoverPhotoMessage','Choose a photo that will be used in event lists.'));
49 $coverPhotoField->setEmptyString(' ');
50 $fields->addFieldToTab('Root.Content.Main', $coverPhotoField, 'Content');
51 }
52
53 $fields->push(new HiddenField('folder_id','',$this->AssociatedFolderID));
54 $fields->push(new HiddenField('folder_path','', ASSETS_DIR .'/' . $folder . '/'));
55
56 $this->extend('hideCMSFields', $fields);
57 return $fields;
58 }
59
60
61 public function duplicate($doWrite = true) {
62 $page = parent::duplicate(false);
63
64 $page->write();
65 if ($this->Photos() && $this->Photos()->Count()) {
66 foreach($this->Photos() as $photo) {
67 $newPhoto = $photo->duplicate();
68 $newPhoto->MediawebPageID = $page->ID;
69 $newPhoto->write();
70 }
71 }
72 if ($this->Files() && $this->Files()->Count()) {
73 foreach($this->Files() as $file) {
74 $newFile = $file->duplicate();
75 $newFile->MediawebPageID = $page->ID;
76 $newFile->write();
77 }
78 }
79 return $page;
80 }
81
82
83 public function Identify() {
84 $RID ='';
85 $p = $this;
86 while ($p->RID == '') {
87 if ($p->Parent()->ClassName != 'SiteTree') {
88 $p = $p->Parent();
89 }
90 else {
91 break;
92 }
93 }
94 return $p->RID;
95 }
96
97 public function ImageCount() {
98 $images = DataObject::get("MediawebPage_Photo","MediawebPageID = {$this->ID}");
99 return $images ? $images->Count() : 0;
100 }
101 public function FileCount() {
102 $files = DataObject::get("MediawebPage_File","MediawebPageID = {$this->ID}");
103 return $files ? $files->Count() : 0;
104 }
105
106 function onAfterDelete() {
107 if ($this->IsDeletedFromStage && !$this->ExistsOnLive) {
108 if ($this->Files()) {
109 foreach ($this->Files() as $obj) {
110 $obj->delete();
111 }
112 }
113 if ($this->Photos()) {
114 foreach ($this->Photos() as $obj) {
115 $obj->delete();
116 }
117 }
118 }
119 parent::onAfterDelete();
120 }
121 }
122
123 SS_Object::add_extension('MediawebPage', 'AssociatedFolderDecorator');
124
125 class MediawebPage_Controller extends Page_Controller {
126
127 public function rotateimage() {
128 if ($image = DataObject::get_by_id("Image", $this->urlParams['ID'])) {
129 $rotatedImage = $this->urlParams['OtherID'] == 'cw' ? $image->RotateClockwise() : $image->RotateCounterClockwise();
130 if (copy(Director::baseFolder().'/'.$rotatedImage->Filename, Director::baseFolder().'/'.$image->Filename)) {
131 $image->flushCache();
132 $image->deleteFormattedImages();
133 }
134 echo $image->SetHeight(200)->URL . "?t=".time();
135 }
136 }
137 }
138
139
140 class MediawebPage_Photo extends DataObject {
141 static $db = array (
142 'Caption' => 'Text',
143 );
144
145 static $has_one = array (
146 'MediawebPage' => 'MediawebPage',
147 'Photo' => 'Image'
148 );
149
150 public function () {
151 $fields = new FieldSet();
152 $fields->push(new TextareaField('Caption',_t('MediawebPage.Caption', 'Caption')));
153
154
155 return $fields;
156 }
157
158 public function onBeforeDelete() {
159 if ($this->MediawebPageID && ($page = DataObject::get_by_id('SiteTree', $this->MediawebPageID))) {
160 if ($this->PhotoID && $this->Photo()->ID && $page->AssociatedFolderID == $this->Photo()->ParentID)
161 $this->Photo()->delete();
162 }
163 parent::onBeforeDelete();
164 }
165
166 public function onAfterWrite() {
167 parent::onAfterWrite();
168
169 }
170
171 public function Thumbnail() {
172 $thumbnailSize = $this->SiteConfig()->MediawebpageThumbnailSize;
173 return $this->Photo()->CroppedImage($thumbnailSize,$thumbnailSize);
174 }
175
176 public function Large() {
177 $photo = $this->Photo();
178 $normalSize = $this->SiteConfig()->MediawebpageNormalSize;
179 if ($photo->getOrientation() == Image::ORIENTATION_LANDSCAPE) {
180 return $photo->SetWidth($normalSize);
181 } else {
182 return $photo->SetHeight($normalSize);
183 }
184 }
185
186 function ParentObject() {
187 return $this->MediawebPage();
188 }
189
190 }
191
192
193 class MediawebPage_File extends DataObject {
194 static $db = array (
195 'Caption' => 'Text',
196 );
197
198 static $has_one = array (
199 'MediawebPage' => 'MediawebPage',
200 'Attach' => 'File'
201 );
202
203 function FileLabel(){
204 return $this->Caption;
205 }
206
207 public function () {
208 $fields = new FieldSet();
209 $fields->push(new TextareaField('Caption',_t('MediawebPage.Caption', 'Caption')));
210 $fields->push(new FileField('Attach',_t('MediawebPage.Attach', 'Attachment')));
211 return $fields;
212 }
213
214 public function onAfterWrite() {
215 parent::onAfterWrite();
216
217 }
218
219 public function onBeforeDelete() {
220 if ($this->MediawebPageID && ($page = DataObject::get_by_id('SiteTree', $this->MediawebPageID))) {
221 if ($this->AttachID && $this->Attach()->ID && $page->AssociatedFolderID == $this->Attach()->ParentID)
222 $this->Attach()->delete();
223 }
224 parent::onBeforeDelete();
225 }
226
227 function ParentObject() {
228 return $this->MediawebPage();
229 }
230 }
231
232 class MediawebPage_Image extends Image {
233 public function generateRotateClockwise(GD $gd) {
234 return $gd->rotate(90);
235 }
236
237 public function generateRotateCounterClockwise(GD $gd) {
238 return $gd->rotate(270);
239 }
240
241 public function Landscape() {
242 return $this->getWidth() > $this->getHeight();
243 }
244
245 public function Portrait() {
246 return $this->getWidth() < $this->getHeight();
247 }
248 }
249
250
251 class extends FileDataObjectManager_Popup {
252 function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) {
253 parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
254 Requirements::customScript("
255 (function($) {
256 $(function() {
257 $('.rotate-controls a').click(function() {
258 link = $(this).attr('href');
259 $.ajax({
260 url: link,
261 success: function(html){
262 $('#preview-image img.preview').attr('src', html);
263 }
264
265 });
266 return false;
267 });
268 });
269
270 $().ajaxSend(function(r,s){
271 $('.ajax-loader').slideDown();
272 });
273
274 $().ajaxStop(function(r,s){
275 $('.ajax-loader').slideUp();
276 });
277 })(jQuery);
278 ");
279 }
280 }
281
282
283 class MediawebPageFiles_Manager extends FileDataObjectManager {
284 public = "MediawebPage_Popup";
285
286 public function __construct($controller, $name, $sourceClass, $fileFieldName, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "SortOrder", $sourceJoin = "",$parentClass='MediawebPage') {
287 parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
288 if (!class_exists("FileDataObjectManager")) {
289 die("<strong>Error</strong>: requires the DataObjectManager module.");
290 }
291 $this->setGridLabelField('Caption');
292 $this->setAddTitle(_t('MediawebPage.Files', 'Files'));
293 $this->setBrowseButtonText(_t('MediawebPage.Upload','Upload'));
294 $this->setParentClass($parentClass);
295
296
297 if (!singleton('File')->canCreate()) {
298 $this->removePermission('upload');
299 }
300 }
301
302 public function EditUploadedForm() {
303 $form = parent::EditUploadedForm();
304 if ($current = $form->Fields()->fieldByName('current')) {
305 $current = $current->dataValue();
306 $dataObject = DataObject::get_by_id($this->sourceClass(), $current);
307 $fileObject = $dataObject->obj($this->fileFieldName);
308
309
310
311 }
312 return $form;
313 }
314 }
315
316
317 class MediawebPagePhoto_Manager extends ImageDataObjectManager {
318 public = "MediawebPage_Popup";
319
320 public function __construct($controller, $name, $sourceClass, $fileFieldName, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "SortOrder", $sourceJoin = "",$parentClass='MediawebPage') {
321 parent::__construct($controller, $name, $sourceClass, $fileFieldName, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
322 if (!class_exists("ImageDataObjectManager")) {
323 die("<strong>Error</strong>: requires the DataObjectManager module.");
324 }
325
326 $this->setGridLabelField('Caption');
327 $this->setAddTitle(_t('MediawebPage.Photos', 'Photos'));
328 $this->setBrowseButtonText(_t('MediawebPage.Upload','Upload'));
329
330
331 $this->setParentClass($parentClass);
332
333
334 if (!singleton('Image')->canCreate()) {
335 $this->removePermission('upload');
336 }
337 }
338
339 public function EditUploadedForm() {
340 $form = parent::EditUploadedForm();
341 if ($current = $form->Fields()->fieldByName('current')) {
342 $current = $current->dataValue();
343 $dataObject = DataObject::get_by_id($this->sourceClass(), $current);
344 $fileObject = $dataObject->obj($this->fileFieldName);
345
346
347
348 }
349 return $form;
350 }
351
352 public function getPreviewFieldFor($fileObject, $size = 150) {
353 if ($fileObject instanceof Image) {
354 $URL = $fileObject->SetHeight($size)->URL;
355 return new LiteralField("icon",
356 "<div class='current-image'>
357 <div id='preview-image'>
358 <img src='$URL' alt='' class='preview' />
359 <div class='ajax-loader'><img src='dataobject_manager/images/ajax-loader.gif' /> Rotating...</div>
360 </div>
361 <div class='rotate-controls'>
362 <a href='".$this->CounterClockwiseLink($fileObject)."' title='Rotate clockwise'><img src='webylon/img/clockwise.gif' /></a> |
363 <a href='".$this->ClockwiseLink($fileObject)."' title='Rotate counter-clockwise'><img src='webylon/img/counterclockwise.gif' /></a>
364 </div>
365 <h3>$fileObject->Filename</h3>
366 </div>"
367 );
368 }
369 }
370
371 public function RotateLink($imgObj, $dir) {
372 return "MediawebPage_Controller/rotateimage/{$imgObj->ID}/{$dir}?flush=1";
373 }
374
375 private function CounterClockwiseLink($fileObject) {
376 return $this->RotateLink($fileObject, "ccw");
377 }
378
379 private function ClockwiseLink($fileObject) {
380 return $this->RotateLink($fileObject, "cw");
381 }
382 }
383
[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.
-