Version: current

     1 <?php
     2 /**
     3  * Добавляем трекинг файлов для всех DataObject
     4  *
     5  *
     6  * @package sapphire
     7  * @author menedem
     8  *
     9  */
    10
    11 class DataObjectFileTrackingDecorator extends Extension {
    12     function extraStatics() {}
    13
    14     // классы, которые не нужно трекать
    15     // выкидываем LogItem, т.к. нет там фото и много их
    16     static $skip_classes = array('DataObjectFileTracking', 'File', 'LogItem', 'VirtualPage');
    17
    18     // имена объектов, которые не нужно трекать
    19     static $skip_object_names = array('ImageTracking');
    20
    21     function onAfterWrite() {
    22         $this->owner->saveTracking();
    23     }
    24
    25     function onAfterDelete() {
    26         if ($this->owner->is_a('SiteTree')) {
    27             if ($this->owner->IsDeletedFromStage && !$this->owner->ExistsOnLive) {
    28                 DataObjectFileTracking::delete_track($this->owner->ID, $this->owner->ClassName);
    29             }
    30         } else {
    31             DataObjectFileTracking::delete_track($this->owner->ID, $this->owner->ClassName);
    32         }
    33     }
    34
    35     function saveTracking() {
    36         // пропускаем ненужные для трека классы
    37         if (count(self::$skip_classes)) {
    38             foreach(self::$skip_classes as $skip_class) {
    39                 if ($this->owner->is_a($skip_class)) {
    40                     return;
    41                 }
    42             }
    43         }
    44
    45         if ($this->owner->ID) {
    46             if (($has_one = $this->owner->has_one()) && count($has_one)) {
    47                 foreach($has_one as $name=>$type) {
    48                     if (in_array($name, self::$skip_object_names)) {
    49                         continue;
    50                     }
    51                     $IDs = array();
    52                     if (($id = $this->owner->{"{$name}ID"}) && ($obj = DataObject::get_by_id($type, $this->owner->{"{$name}ID"})) && $obj->ID) {
    53                         if ($obj->is_a('File') && !$obj->is_a('Folder')) {
    54                             $IDs[] = $id;
    55                         }
    56                     }
    57                     DataObjectFileTracking::update_track($this->owner->ID, $this->owner->ClassName, "{$name}ID", $IDs);
    58                 }
    59             }
    60
    61             // по идее, связи has_many Files быть не может
    62
    63
    64             if (($many_many = $this->owner->many_many()) && count($many_many)) {
    65                 foreach($many_many as $name=>$type) {
    66                     if (in_array($name, self::$skip_object_names)) {
    67                         continue;
    68                     }
    69                     $IDs = array();
    70                     if (($manyManyObjects = $this->owner->$name()) && $manyManyObjects->Count()) {
    71                         foreach($manyManyObjects as $manyManyObject) {
    72                             if ($manyManyObject->is_a('File') && !$manyManyObject->is_a('Folder')) {
    73                                 $IDs[] = $manyManyObject->ID;
    74                             }
    75                         }
    76                     }
    77                     DataObjectFileTracking::update_track($this->owner->ID, $this->owner->ClassName, $name, $IDs);
    78                 }
    79             }
    80         }
    81     }
    82
    83     function updateFieldTracking($fieldName, $linkedFiles) {
    84         DataObjectFileTracking::update_track($this->owner->ID, $this->owner->ClassName, $fieldName, $linkedFiles);
    85     }
    86 }
    87

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