Version: current

     1 <?php
     2
     3 /**
     4  * Логирование добавления, удаления, изменения объектов
     5  *
     6  * @author inxo
     7  */
     8 class DataObjectLoggerExtension extends Extension {
     9
    10     private $class_name;
    11     private $item_id;
    12
    13     function onAfterWrite() {
    14         //file_put_contents(TEMP_FOLDER.'/debug', time()." {$this->class_name} != {$this->owner->ClassName}  && {$this->item_id} != {$this->owner->ID}\n", FILE_APPEND );
    15
    16         if ($this->owner->ClassName && $this->owner->ClassName != 'LogItem'
    17             && $this->class_name != $this->owner->ClassName
    18             && $this->item_id != $this->owner->ID
    19         ) {
    20             LogItem::add(array(
    21                 'Class' => $this->owner->ClassName,
    22                 'ItemID' => $this->owner->ID,
    23                 'Action' => isset($this->owner->LastEdited) ? 'Modify' : 'Create',
    24                 'ItemTitle' => $this->owner->Title,
    25             ));
    26             // для избежания повторного логирования т.к. onAfterWrite может вызываться несколько раз
    27             // TODO а остальные действия?
    28             $this->class_name = $this->owner->ClassName;
    29             $this->item_id = $this->owner->ID;
    30         }
    31     }
    32
    33     function onAfterPublish($orig=null) {
    34         if ($this->owner->ClassName && $this->owner->ClassName != 'LogItem') {
    35             LogItem::add(array(
    36                 'Class' => $this->owner->ClassName,
    37                 'ItemID' => $this->owner->ID,
    38                 'Action' => 'Publish',
    39                 'ItemTitle' => $this->owner->Title,
    40             ));
    41         }
    42     }
    43
    44     function onAfterUnpublish() {
    45         if ($this->owner->ClassName && $this->owner->ClassName != 'LogItem') {
    46             LogItem::add(array(
    47                 'Class' => $this->owner->ClassName,
    48                 'ItemID' => $this->owner->ID,
    49                 'Action' => 'Unpublish',
    50                 'ItemTitle' => $this->owner->Title,
    51             ));
    52         }
    53     }
    54
    55     function onAfterDelete() {
    56         if ($this->owner->ClassName && $this->owner->ClassName != 'LogItem') {
    57             LogItem::add(array(
    58                 'Class' => $this->owner->ClassName,
    59                 'ItemID' => $this->owner->ID,
    60                 'Action' => 'Delete',
    61                 'ItemTitle' => $this->owner->Title,
    62             ));
    63         }
    64     }
    65
    66     function memberBeforeLoggedOut() {
    67         LogItem::add(array(
    68             'Class' => 'Member',
    69             'ItemID' => $this->owner->ID,
    70             'Action' => 'Logout',
    71             'ItemTitle' => $this->owner->Title,
    72         ));
    73         $this->class_name = $this->owner->ClassName;
    74         $this->item_id = $this->owner->ID;
    75     }
    76
    77     function memberLoggedIn() {
    78         LogItem::add(array(
    79             'Class' => 'Member',
    80             'ItemID' => $this->owner->ID,
    81             'Action' => 'Login',
    82             'ItemTitle' => $this->owner->Title,
    83         ));
    84     }
    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. -