Source of file PhotoAlbum.php
Size: 6,392 Bytes - Last Modified: 2022-02-21T10:01:39+00:00
/var/www/docs.ssmods.com/process/src/src/Models/PhotoAlbum.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 | <?php namespace AndrewHoule\PhotoGallery\Models; use SilverStripe\Assets\Image; use SilverStripe\Forms\TabSet; use SilverStripe\Assets\Folder; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\ORM\PaginatedList; use SilverStripe\Forms\TextareaField; use SilverStripe\Versioned\Versioned; use SilverStripe\Forms\GridField\GridField; use AndrewHoule\PhotoGallery\Models\PhotoItem; use SilverStripe\AssetAdmin\Forms\UploadField; use AndrewHoule\PhotoGallery\Pages\PhotoGallery; use AndrewHoule\PhotoGallery\Extensions\Sortable; use Bummzack\SortableFile\Forms\SortableUploadField; use AndrewHoule\PhotoGallery\Traits\CMSPermissionProvider; class PhotoAlbum extends DataObject { use CMSPermissionProvider; private static $db = [ 'Name' => 'Text', 'Description' => 'HTMLText', ]; private static $has_one = [ 'PhotoGallery' => PhotoGallery::class, 'AlbumCover' => Image::class, ]; private static $many_many = [ 'PhotoItems' => [ 'through' => PhotoItem::class, 'from' => 'PhotoAlbum', 'to' => 'Photo', ] ]; private static $owns = [ 'AlbumCover', 'PhotoItems', ]; private static $cascade_deletes = [ 'AlbumCover', 'PhotoItems', ]; private static $summary_fields = [ 'Thumbnail' => 'Cover Photo', 'Name' => 'Name', 'DescriptionExcerpt' => 'Description', ]; private static $extensions = [ Versioned::class, Sortable::class, ]; private static $table_name = 'PhotoAlbum'; public function AlbumFolderName() { $directoryName = 'photogallery'; $defaultName = 'album'; $photoGallery = $this->PhotoGallery(); if ($photoGallery) { if ($name = $this->Name) { $string = strtolower($name); $string = preg_replace("/[^a-z0-9_\s-]/", "", $string); $string = preg_replace("/[\s-]+/", " ", $string); $string = preg_replace("/[\s_]/", "-", $string); return $directoryName . '/' . $string; } else { return $directoryName . '/' . $defaultName; } } else { return $directoryName . '/' . $defaultName; } } public function getCMSFields() { $fields = parent::getCMSFields(); $fields = FieldList::create(TabSet::create('Root')); // Content $fields->addFieldsToTab('Root.Main.Album', [ TextField::create('Name') ->setTitle('Title'), TextareaField::create('Description'), UploadField::create('AlbumCover') ->setTitle('Album Cover Photo') ->setDescription('jpg, gif and png filetypes allowed.') ->setFolderName($this->AlbumFolderName()) ->setAllowedExtensions([ 'jpg', 'jpeg', 'png', 'gif' ]), SortableUploadField::create('PhotoItems') ->setTitle('Photos') ->setDescription('jpg, gif and png filetypes allowed.') ->setFolderName($this->AlbumFolderName()) ->setAllowedExtensions([ 'jpg', 'jpeg', 'png', 'gif' ]) ->setSortColumn('SortID') ]); return $fields; } public function Thumbnail() { $image = $this->AlbumCover(); return ($image) ? $image->CMSThumbnail() : false; } public function DescriptionExcerpt($length = 75) { $text = strip_tags($this->Description); $length = abs((int)$length); if (strlen($text) > $length) { $text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text); } return $text; } public function Link() { if ($PhotoGallery = $this->PhotoGallery()) { $Action = 'album/' . $this->ID; return $PhotoGallery->Link($Action); } } public function Photos() { $photoset = new ArrayList(); $this->extend('GetItems', $photoset); if (!$photoset->count()) { $photos = PhotoItem::get() ->filter('PhotoAlbumID', $this->ID) ->sort('SortID'); if ($photos) { foreach ($photos as $photo) { if ($photo->getComponent('Photo')->exists()) { $photoset->push($photo); } } } } return $photoset; } public function getTitle() { return $this->Name; } public function PaginatedPhotos() { $items = PaginatedList::create($this->Photos(), $this->request) ->setPageLength($this->PhotosPerPage); return $items ? $items : false; } public function getAlbumThumb() { $albumCover = $this->AlbumCover(); $defaultAlbumCover = $this->PhotoGallery()->DefaultAlbumCover(); if ($albumCover->exists()) { return $albumCover; } elseif ($defaultAlbumCover->exists()) { return $defaultAlbumCover; } else { return false; } } public function OnBeforeDelete() { // Delete the album cover $albumcover = $this->AlbumCover(); $albumcoverfile = Image::get()->byID($albumcover->ID); if ($albumcoverfile) { $albumcoverfile->delete(); } // Delete the photo items in that album $photoitems = $this->getManyManyComponents("PhotoItems"); foreach ($photoitems as $photoitem) { $photoitemfile = Image::get()->byID($photoitem->ID); if ($photoitemfile) { $photoitemfile->delete(); } } // Delete the album folder $albumfolder = $this->AlbumFolderName(); $folder = Folder::get()->filter('Name', $albumfolder)->first(); if ($folder) { $folder->delete(); } return parent::OnBeforeDelete(); } } |