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