|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class UpdateNoteExtension extends DataExtension |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
8
|
|
|
'UpdateNotes' => 'UpdateNote' |
|
9
|
|
|
); |
|
10
|
|
|
|
|
11
|
|
|
private static $seconds_grace = 600; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
private static $latest_limit = 600; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
private static $_run_once_only = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Event handler called after writing to the database. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function onAfterWrite() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::onAfterWrite(); |
|
23
|
|
|
$id = $this->owner->ID; |
|
24
|
|
|
$class = $this->owner->ClassName; |
|
25
|
|
|
$userID = Member::currentUserID(); |
|
26
|
|
|
$filter = array( |
|
27
|
|
|
'UpdateNoteRecordID' => $id, |
|
28
|
|
|
'UpdateNoteRecordClass' => $class, |
|
29
|
|
|
'UpdatedByID' => $userID |
|
30
|
|
|
); |
|
31
|
|
|
$key = implode('_', $filter); |
|
32
|
|
|
if (! isset(self::$_run_once_only[$key])) { |
|
33
|
|
|
self::$_run_once_only[$key] = true; |
|
34
|
|
|
$now = SS_Datetime::now()->Rfc2822(); |
|
35
|
|
|
$gracePointInTime = strtotime($now) - Config::inst()->get('UpdateNoteExtension', 'seconds_grace'); |
|
36
|
|
|
$log = DataObject::get_one( |
|
37
|
|
|
'UpdateNote', |
|
38
|
|
|
$filter, |
|
39
|
|
|
$cacheDataObjectGetOne = false, |
|
40
|
|
|
array('Created' => 'DESC') |
|
|
|
|
|
|
41
|
|
|
); |
|
42
|
|
|
if ($log) { |
|
43
|
|
|
$editPointInTime = strtotime($log->Created); |
|
44
|
|
|
debug::log(Date('l jS \of F Y h:i:s A', $editPointInTime).'...'.Date('l jS \of F Y h:i:s A', $gracePointInTime)); |
|
45
|
|
|
if ($editPointInTime < $gracePointInTime) { |
|
46
|
|
|
$log = null; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
if ($log && $log->exists()) { |
|
|
|
|
|
|
50
|
|
|
//already exists ... |
|
51
|
|
|
} else { |
|
52
|
|
|
$log = UpdateNote::create($filter); |
|
53
|
|
|
} |
|
54
|
|
|
$log->write(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Update Fields |
|
60
|
|
|
* @return FieldList |
|
61
|
|
|
*/ |
|
62
|
|
|
public function updateCMSFields(FieldList $fields) |
|
63
|
|
|
{ |
|
64
|
|
|
$owner = $this->owner; |
|
|
|
|
|
|
65
|
|
|
$name = Injector::inst()->get('UpdateNote')->i18n_plural_name(); |
|
66
|
|
|
$fields->addFieldToTab( |
|
67
|
|
|
'Root.'.$name, |
|
68
|
|
|
GridField::create( |
|
69
|
|
|
'UpdateNotes', |
|
70
|
|
|
$name, |
|
71
|
|
|
$this->owner->UpdateNotes(), |
|
72
|
|
|
GridFieldConfig_RecordEditor::create() |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
return $fields; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Event handler called before deleting from the database. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function onBeforeDelete() |
|
83
|
|
|
{ |
|
84
|
|
|
foreach ($this->owner->UpdateNotes() as $note) { |
|
85
|
|
|
$note->delete(); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
public function LatestUpdateNotes() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
return $this->owner->UpdateNotes() |
|
93
|
|
|
->limit(Config::inst()->get('xx', 'latest_limit')); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.