-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoExpirePlugin.php
239 lines (203 loc) · 6.63 KB
/
AutoExpirePlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
namespace Craft;
class AutoExpirePlugin extends BasePlugin
{
public function getName()
{
return 'Auto Expire';
}
public function getVersion()
{
return '1.2.0';
}
public function getSchemaVersion()
{
return '1.2.0';
}
public function getDeveloper()
{
return 'carlcs';
}
public function getDeveloperUrl()
{
return 'https://github.com/carlcs';
}
public function getDocumentationUrl()
{
return 'https://github.com/carlcs/craft-autoexpire';
}
public function getReleaseFeedUrl()
{
return 'https://github.com/carlcs/craft-autoexpire/raw/master/releases.json';
}
// Properties
// =========================================================================
/**
* Used to keep track of entries and rules we already re-saved.
*
* @var array
*/
private $_handledRules = [];
/**
* Used to keep track of entries which are handled by entries.onSaveEntry.
*
* @var array
*/
private $_handledOnEntrySave = [];
/**
* A list of of EntryModel attributes of type DateTime.
*
* @var array
*/
private static $_dateAttributes = ['postDate', 'expiryDate'];
// Public Methods
// =========================================================================
/**
* Initializes the plugin
*/
public function init()
{
craft()->on('entries.onBeforeSaveEntry', [$this, 'handleBeforeEntrySave']);
craft()->on('entries.onSaveEntry', [$this, 'handleEntrySave']);
craft()->on('elements.onSaveElement', [$this, 'handleElementSave']);
}
/**
* Make sure requirements are met before installation.
*
* @return bool
* @throws Exception
*/
public function onBeforeInstall()
{
if (!defined('PHP_VERSION') || version_compare(PHP_VERSION, '5.4', '<')) {
throw new Exception($this->getName().' plugin requires PHP 5.4 or later.');
}
}
/**
* Returns the URL to the plugin's settings page in the CP.
*
* @return string
*/
public function getSettingsUrl()
{
return 'settings/plugins/autoexpire/index';
}
/**
* Returns an array of CP routes that should be registered for the plugin.
*
* @return array
*/
public function registerCpRoutes()
{
return [
'settings/plugins/autoexpire/index' => 'autoexpire/settings/index',
'settings/plugins/autoexpire/new' => 'autoexpire/settings/_edit',
'settings/plugins/autoexpire/(?P<ruleId>\d+)' => 'autoexpire/settings/_edit',
];
}
/**
* Keeps track of entries which are handled by entries.onSaveEntry.
*
* @param Event $event
*/
public function handleBeforeEntrySave(Event $event){
$entry = $event->params['entry'];
$this->_handledOnEntrySave[] = $entry->id;
}
/**
* Applies the rules in case EntriesService::saveEntry() was used.
*
* @param Event $event
*/
public function handleEntrySave(Event $event){
$entry = $event->params['entry'];
$this->applyRules($entry);
}
/**
* Applies the rules in case ElementsService::saveElement() was used directly.
*
* @param Event $event
*/
public function handleElementSave(Event $event){
$element = $event->params['element'];
$isNewElement = $event->params['isNewElement'];
if ($element->getElementType() !== ElementType::Entry) {
return null;
}
// Will this re-save be taken care of by the entries.onSaveEntry event handler?
if ($isNewElement || in_array($element->id, $this->_handledOnEntrySave)) {
return null;
}
$this->applyRules($element);
}
/**
* Applies the rules which relate to a given entry.
*
* @param EntryModel $entry
*/
public function applyRules($entry)
{
foreach (craft()->autoExpire->getRules() as $rule) {
if (($entry->sectionId == $rule->sectionId) && ($entry->getType()->id == $rule->entryTypeId)) {
$ruleName = $entry->id.'::'.$rule->id;
// Did we already re-save the entry for this rule? Necessary because of the recursive
// call of EntriesService::saveEntry().
if (!in_array($ruleName, $this->_handledRules)) {
$this->_handledRules[] = $ruleName;
$this->applyRule($entry, $rule);
}
}
}
}
/**
* Re-saves an entry with the date from a rule.
*
* @param EntryModel $entry
* @param AutoExpire_RuleModel $rule
*/
public function applyRule($entry, $rule)
{
$fieldHandle = $rule['fieldHandle'];
$fieldIsEmpty = $this->fieldIsEmpty($entry, $fieldHandle);
if ($fieldIsEmpty || !$rule->allowOverwrite) {
try {
$newDateString = craft()->templates->renderObjectTemplate($rule->dateTemplate, $entry);
} catch (\Exception $e) {
AutoExpirePlugin::log('Couldn’t render template for entry with id “'.$entry->id .
'” and Auto Expire rule “'.$rule->name.'” ('.$e->getMessage().').', LogLevel::Error);
return null;
}
$newDate = DateTime::createFromString($newDateString);
if (!$newDate instanceof \DateTime) {
AutoExpirePlugin::log('Couldn’t create date from string “'.$newDateString.'” for entry with id “' .
$entry->id.'” and Auto Expire rule “'.$rule->name.'”.', LogLevel::Error);
return null;
}
if (in_array($fieldHandle, static::$_dateAttributes)) {
$entry->{$fieldHandle} = $newDate;
} else {
$entry->setContentFromPost(array($fieldHandle => $newDate));
}
$success = craft()->entries->saveEntry($entry);
if (!$success) {
AutoExpirePlugin::log('Couldn’t save entry with id “'.$entry->id.'” and Auto Expire rule “' .
$rule->name.'”.', LogLevel::Error);
}
}
}
/**
* Returns whether an entry's date field is empty.
*
* @param EntryModel $entry
* @param string $fieldHandle
*
* @return bool
*/
protected function fieldIsEmpty($entry, $fieldHandle)
{
if ($fieldHandle == 'postDate') {
return $entry->{$fieldHandle} == DateTimeHelper::currentUTCDateTime();
}
return $entry->{$fieldHandle} === null;
}
}