Version: current

     1 <?php
     2
     3 /**
     4  * Расширение для прикрепленных файлов
     5  *
     6  * @author inxo
     7  */
     8 class AttachedFiles extends DataObjectDecorator {
     9
    10     static function add_extension($class, $extension_class='AttachedFiles') {
    11         SS_Object::add_extension($class, 'AssociatedFolderDecorator');
    12         SS_Object::add_extension($class . '_Controller', 'AttachedFilesExtension');
    13         SS_Object::add_extension($class, $extension_class);
    14     }
    15
    16     function extraStatics() {
    17         return array(
    18             'has_many' => array(
    19                 "Photos" => "MediawebPage_Photo.MediawebPage",
    20                 "Files" => "MediawebPage_File.MediawebPage",
    21             )
    22         );
    23     }
    24
    25     function updateCMSFields(FieldSet &$fields) {
    26         if (!$this->owner->ID || (!$this->owner->ExistsOnLive && $this->owner->IsDeletedFromStage)) // если страница не сохранена, то не нужно создавать AssociatedFolder и показывать вкладки с прикрепленными фотками и файлами
    27             return;
    28
    29         if ($this->owner->AssociatedFolderID && $this->owner->AssociatedFolder()->ID)
    30             $folder = $this->owner->AssociatedFolder();
    31         else
    32             $folder = $this->owner->createAssociatedFolder();
    33
    34         $folder = substr_replace(str_replace(ASSETS_DIR.'/', '', $folder->Filename), "", -1);
    35
    36         $fields->addFieldToTab('Root.Content', new Tab("PagePhotos", _t("MediawebPage.PagePhotos", "Photos")), 'Metadata');
    37         $photoManager = new MediawebPagePhoto_Manager($this->owner, "Photos", "MediawebPage_Photo", "Photo", array("Caption" => _t("MediawebPage.Caption", "Caption")), "getCMSFields_forPopup");
    38         $photoManager->setUploadFolder($folder);
    39         $fields->addFieldToTab('Root.Content.PagePhotos', $photoManager);
    40
    41         $fields->addFieldToTab('Root.Content', new Tab("PageFiles", _t("MediawebPage.PageFiles", "Files")), 'Metadata');
    42         $fileManager = new MediawebPageFiles_Manager($this->owner, "Files", "MediawebPage_File", "Attach", array("Caption" => _t("MediawebPage.Caption", "Caption")), "getCMSFields_forPopup");
    43         $fileManager->setUploadFolder($folder);
    44         $fields->addFieldToTab('Root.Content.PageFiles', $fileManager);
    45     }
    46
    47     public function ImageCount() {
    48         $images = DataObject::get("MediawebPage_Photo", "MediawebPageID = {$this->owner->ID}");
    49         return $images ? $images->Count() : 0;
    50     }
    51
    52     public function FileCount() {
    53         $files = DataObject::get("MediawebPage_File", "MediawebPageID = {$this->owner->ID}");
    54         return $files ? $files->Count() : 0;
    55     }
    56
    57     function onAfterDelete() {
    58         if ($this->owner->IsDeletedFromStage && !$this->owner->ExistsOnLive) {
    59             if ($this->owner->Files()) {
    60                 foreach ($this->owner->Files() as $obj) {
    61                     $obj->delete();
    62                 }
    63             }
    64             if ($this->owner->Photos()) {
    65                 foreach ($this->owner->Photos() as $obj) {
    66                     $obj->delete();
    67                 }
    68             }
    69         }
    70         parent::onAfterDelete();
    71     }
    72
    73 }
    74
    75 class AttachedFilesExtension extends Extension {
    76
    77     public function rotateimage() {
    78         if ($image = DataObject::get_by_id("Image", $this->urlParams['ID'])) {
    79             $rotatedImage = $this->urlParams['OtherID'] == 'cw' ? $image->RotateClockwise() : $image->RotateCounterClockwise();
    80             if (copy(Director::baseFolder() . '/' . $rotatedImage->Filename, Director::baseFolder() . '/' . $image->Filename)) {
    81                 $image->flushCache();
    82                 $image->deleteFormattedImages();
    83             }
    84             echo $image->SetHeight(200)->URL . "?t=" . time();
    85         }
    86     }
    87
    88 }
    89
    90
    91

[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. -