Guest User

Untitled

a guest
Jun 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <?php
  2.  
  3. class ImageGalleryItem extends DataObject
  4. {
  5. protected $ui;
  6.  
  7. public static $delete_permission = "CMS_ACCESS_CMSMain";
  8.  
  9. static $db = array (
  10. 'Caption' => 'Text'
  11. );
  12.  
  13. static $has_one = array (
  14. 'ImageGalleryPage' => 'ImageGalleryPage',
  15. 'Album' => 'ImageGalleryAlbum',
  16. 'Image' => 'ImageGalleryImage'
  17. );
  18.  
  19. public function getCMSFields_forPopup()
  20. {
  21. $fields = new FieldSet();
  22. $fields->push(new TextareaField('Caption', _t('ImageGalleryItem.CAPTION','Caption')));
  23. if($this->ImageGalleryPageID)
  24. $fields->push(new DropdownField('AlbumID', _t('ImageGalleryItem.ALBUM','Album'), $this->ImageGalleryPage()->Albums()->toDropdownMap('ID','AlbumName')));
  25. $class = class_exists('UploadifyField') ? 'ImageUploadField' : 'ImageField';
  26. $fields->push(new $class('Image'));
  27.  
  28. return $fields;
  29. }
  30.  
  31. public function onBeforeDelete()
  32. {
  33. $this->Image()->delete();
  34. parent::onBeforeDelete();
  35. }
  36.  
  37. public function onBeforeWrite()
  38. {
  39. parent::onBeforeWrite();
  40.  
  41. $image = $this->Image();
  42. if($image->ID) {
  43. if(isset($_POST['AlbumID']) && $album = DataObject::get_by_id("ImageGalleryAlbum", $_POST['AlbumID'])) {
  44. $image->setField("ParentID",$album->FolderID);
  45. $image->write();
  46. }
  47. } else if(!$image->ID && $this->ImageID) {
  48. // Image is not a valid ImageGalleryImage, but we have an ImageID, if that
  49. // ImageID references a real image we should change it into an ImageGalleryImage.
  50. if ($image = DataObject::get_by_id('Image', $this->ImageID)) {
  51. $image = $image->newClassInstance('ImageGalleryImage');
  52. $image->write();
  53. }
  54. }
  55. }
  56.  
  57. public function Thumbnail()
  58. {
  59. $i = $this->ImageGalleryPage();
  60. if($i->Square)
  61. return $this->Image()->CroppedImage($i->ThumbnailSize, $i->ThumbnailSize);
  62. return $this->Image()->SetHeight($i->ThumbnailSize);
  63. }
  64.  
  65. public function Medium()
  66. {
  67. if($this->Image()->Landscape())
  68. return $this->Image()->SetWidth($this->ImageGalleryPage()->MediumSize);
  69. else
  70. return $this->Image()->SetHeight($this->ImageGalleryPage()->MediumSize);
  71. }
  72.  
  73. public function Large()
  74. {
  75. if($this->Image()->Landscape())
  76. return $this->Image()->SetWidth($this->ImageGalleryPage()->NormalSize);
  77. else {
  78. $height = $this->ImageGalleryPage()->NormalHeight > 0 ? $this->ImageGalleryPage()->NormalHeight : $this->ImageGalleryPage()->NormalSize;
  79. return $this->Image()->SetHeight($height);
  80. }
  81. }
  82.  
  83. public function setUI(ImageGalleryUI $ui)
  84. {
  85. $this->UI = $ui;
  86. }
  87.  
  88. public function GalleryItem()
  89. {
  90. if($this->UI)
  91. return $this->renderWith(array($this->UI->item_template));
  92. return false;
  93. }
  94.  
  95. public function canDelete()
  96. {
  97. return Permission::check(self::$delete_permission);
  98. }
  99. }
Add Comment
Please, Sign In to add comment