-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailNotificationPlugin.php
410 lines (370 loc) · 14.4 KB
/
EmailNotificationPlugin.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/**
* @version 1.4
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @copyright Daniele Binaghi, 2018-2021
* @package EmailNotification
*/
class EmailNotificationPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'uninstall',
'initialize',
'config_form',
'config',
'before_save_item',
'before_save_collection',
'before_save_exhibit',
'after_save_item',
'after_save_collection',
'after_save_exhibit'
);
/**
* Install the plugin.
*/
public function hookInstall()
{
set_option('email_notification_new_item', '0');
set_option('email_notification_new_item_email_subject', __('New Item added'));
set_option('email_notification_new_item_email_message', __('A new Item has been added to the Omeka repository.'));
set_option('email_notification_new_collection', '0');
set_option('email_notification_new_collection_email_subject', __('New Collection added'));
set_option('email_notification_new_collection_email_message', __('A new Collection has been added to the Omeka repository.'));
set_option('email_notification_new_exhibit', '0');
set_option('email_notification_new_exhibit_email_subject', __('New Exhibit added'));
set_option('email_notification_new_exhibit_email_message', __('A new Exhibit has been added to the Omeka repository.'));
set_option('email_notification_recipient_address', get_option('administrator_email'));
set_option('email_notification_notify_editors', '0');
set_option('email_notification_notify_owner', '0');
set_option('email_notification_notification_alert', '0');
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
delete_option('email_notification_new_item');
delete_option('email_notification_new_item_email_subject');
delete_option('email_notification_new_item_email_message');
delete_option('email_notification_new_collection');
delete_option('email_notification_new_collection_email_subject');
delete_option('email_notification_new_collection_email_message');
delete_option('email_notification_new_exhibit');
delete_option('email_notification_new_exhibit_email_subject');
delete_option('email_notification_new_exhibit_email_message');
delete_option('email_notification_recipient_address');
delete_option('email_notification_notify_editors');
delete_option('email_notification_notify_owner');
delete_option('email_notification_notification_alert');
}
/**
* Initializes the plugin.
*/
public function hookInitialize()
{
add_translation_source(dirname(__FILE__) . '/languages');
}
/**
* Shows plugin configuration page.
*
* @return void
*/
public function hookConfigForm()
{
include 'config_form.php';
}
/**
* Processes the configuration form.
*
* @return void
*/
public function hookConfig($args)
{
$post = $args['post'];
set_option('email_notification_new_item', $post['email_notification_new_item']);
set_option('email_notification_new_item_email_subject', $post['email_notification_new_item_email_subject']);
set_option('email_notification_new_item_email_message', $post['email_notification_new_item_email_message']);
set_option('email_notification_new_collection', $post['email_notification_new_collection']);
set_option('email_notification_new_collection_email_subject', $post['email_notification_new_collection_email_subject']);
set_option('email_notification_new_collection_email_message', $post['email_notification_new_collection_email_message']);
set_option('email_notification_new_exhibit', $post['email_notification_new_exhibit']);
set_option('email_notification_new_exhibit_email_subject', $post['email_notification_new_exhibit_email_subject']);
set_option('email_notification_new_exhibit_email_message', $post['email_notification_new_exhibit_email_message']);
set_option('email_notification_recipient_address', $post['email_notification_recipient_address']);
set_option('email_notification_notify_editors', $post['email_notification_notify_editors']);
set_option('email_notification_notify_owner', $post['email_notification_notify_owner']);
set_option('email_notification_notification_alert', $post['email_notification_notification_alert']);
}
/**
* Before save item hook.
*
* @param array $args
*/
public function hookBeforeSaveItem($args)
{
if (!$args['post'] || $args['insert'] == 1) {
// saving new item, so no action is required
return;
}
$record = $args['record'];
$item = get_record_by_id('Item', $record->id);
if ($item->public == 1) {
// item is already public, so no action is required
return;
}
if ($args['post']['public'] == 0) {
// item hasn't been made public, so no action is required
return;
}
if ($item->owner_id == current_user()->id) {
// item has been edited by creator, so no action is required
return;
}
// alert creator
$this->notifyCreator('item', $item);
}
/**
* Before save collection hook.
*
* @param array $args
*/
public function hookBeforeSaveCollection($args)
{
if (!$args['post'] || $args['insert'] == 1) {
// saving new collection, so no action is required
return;
}
$record = $args['record'];
$collection = get_record_by_id('Collection', $record->id);
if ($collection->public == 1) {
// collection is already public, so no action is required
return;
}
if ($args['post']['public'] == 0) {
// collection hasn't been made public, so no action is required
return;
}
if ($collection->owner_id == current_user()->id) {
// collection has been edited by creator, so no action is required
return;
}
// alert creator
$this->notifyCreator('collection', $collection);
}
/**
* Before save exhibit hook.
*
* @param array $args
*/
public function hookBeforeSaveExhibit($args)
{
if (!$args['post'] || $args['insert'] == 1) {
// saving new exhibit, so no action is required
return;
}
$record = $args['record'];
$exhibit = get_record_by_id('Exhibit', $record->id);
if ($exhibit->public == 1) {
// exhibit is already public, so no action is required
return;
}
if ($args['post']['public'] == 0) {
// exhibit hasn't been made public, so no action is required
return;
}
if ($exhibit->owner_id == current_user()->id) {
// exhibit has been edited by creator, so no action is required
return;
}
// alert creator
$this->notifyCreator('exhibit', $exhibit);
}
/**
* After save item hook.
*
* @param array $args
*/
public function hookAfterSaveItem($args)
{
if (!$args['post'] || $args['insert'] != 1) {
return;
}
$this->notifyUsers('item', $args['record']);
}
/**
* After save collection hook.
*
* @param array $args
*/
public function hookAfterSaveCollection($args)
{
if (!$args['post'] || $args['insert'] != 1) {
return;
}
$this->notifyUsers('collection', $args['record']);
}
/**
* After save exhibit hook.
*
* @param array $args
*/
public function hookAfterSaveExhibit($args)
{
if (!$args['post'] || $args['insert'] != 1) {
return;
}
$this->notifyUsers('exhibit', $args['record']);
}
/**
* Creates notification for users.
*
* @param string $type
* @param array $record
*/
private function notifyUsers($type, $record) {
$recipientAddress = get_option('email_notification_recipient_address');
$notifyEditors = get_option('email_notification_notify_editors');
$bodyHtml = '';
$subject = '';
$bMessageSent = false;
if ($recipientAddress != '' || $notifyEditors) {
// creates e-mail elements
if ($type == 'exhibit') {
$title = metadata($record, 'title');
} else {
$title = metadata($record, array('Dublin Core', 'Title'));
}
$date = metadata($record, 'added');
$creator = current_user()->name;
$public = ($record->public == 1 ? __('public') : __('private'));
$featured = ($record->featured == 1 ? __('is featured') : __('is not featured'));
if ($type == 'item' && get_option('email_notification_new_item')) {
$subject = get_option('email_notification_new_item_email_subject');
$bodyHtml = get_option('email_notification_new_item_email_message');
$url_admin = $_SERVER['HTTP_HOST'] . '/admin/items/show/id/' . $record['id'];
$url_public = $_SERVER['HTTP_HOST'] . '/items/show/id' . $record['id'];
$collection_title = metadata($record, 'Collection Name');
if ($title == '') {
$title = __('not provided');
$message = __('No title has been provided for the new Item');
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage($message, 'error');
}
$fields = array('{item_title}', '{item_creator}', '{item_creation_date}', '{item_collection_title}', '{item_public_status}', '{item_featured_status}', '{item_admin_url}', '{item_public_url}');
$values = array($title, $creator, $date, $collection_title, $public, $featured, $url_admin, $url_public);
$bodyHtml = str_replace($fields, $values, $bodyHtml);
} elseif ($type == 'collection' && get_option('email_notification_new_collection')) {
$subject = get_option('email_notification_new_collection_email_subject');
$bodyHtml = get_option('email_notification_new_collection_email_message');
$url_admin = $_SERVER['HTTP_HOST'] . '/admin/collections/show/id/' . $record['id'];
$url_public = $_SERVER['HTTP_HOST'] . '/collections/show/id' . $record['id'];
if ($title == '') {
$title = __('not provided');
$message = __('No title has been provided for the new Collection.');
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage($message, 'error');
}
$fields = array('{collection_title}', '{collection_creator}', '{collection_creation_date}', '{collection_public_status}', '{collection_featured_status}', '{collection_admin_url}', '{collection_public_url}');
$values = array($title, $creator, $date, $public, $featured, $url_admin, $url_public);
$bodyHtml = str_replace($fields, $values, $bodyHtml);
} elseif ($type == 'exhibit' && get_option('email_notification_new_exhibit') && plugin_is_active('ExhibitBuilder')) {
$subject = get_option('email_notification_new_exhibit_email_subject');
$bodyHtml = get_option('email_notification_new_exhibit_email_message');
$url_admin = $_SERVER['HTTP_HOST'] . '/admin/exhibits/show/id/' . $record['id'];
$url_public = $_SERVER['HTTP_HOST'] . '/exhibits/show/id' . $record['id'];
if ($title == '') {
$title = __('not provided');
$message = __('No title has been provided for the new Exhibit.');
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage($message, 'error');
}
$fields = array('{exhibit_title}', '{exhibit_creator}', '{exhibit_creation_date}', '{exhibit_public_status}', '{exhibit_featured_status}', '{exhibit_admin_url}', '{exhibit_public_url}');
$values = array($title, $creator, $date, $public, $featured, $url_admin, $url_public);
$bodyHtml = str_replace($fields, $values, $bodyHtml);
}
if ($bodyHtml != '' && $subject != '') {
$bodyHtml .= '<hr>' . __('This is an automatically generated message - please do not reply directly to this e-mail');
if ($recipientAddress != '') {
// sends e-mail to recipient address(es)
$bMessageSent = $this->sendEmail(explode(',', $recipientAddress), $subject, $bodyHtml);
}
if ($notifyEditors) {
// sends e-mail to all users with general 'edit' privilege
$acl = get_acl();
$users = get_db()->getTable('User')->findAll();
foreach ($users as $key => $user) {
if ($acl->isAllowed($user, new Item, 'edit')) {
if ($this->sendEmail($user->email, $subject, $bodyHtml)) $bMessageSent = true;
}
}
}
if (get_option('email_notification_message_sent') && $bMessageSent) {
// shows alert for new item/collection/exhibit creator
$message = __('Admin/editors have been notified about the new addition.');
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage($message, 'success');
}
}
}
}
/**
* Creates notification for creator.
*
* @param string $type
* @param array $record
*/
private function notifyCreator($type, $record) {
$bodyHtml = '';
$subject = '';
$recipientAddress = $record->getOwner()->email;
if ($recipientAddress != '') {
// creates e-mail elements
$title = metadata($record, array('Dublin Core', 'Title'));
$editor = current_user()->name;
$type_localized = __($type);
if (is_array($type_localized)) $type_localized = $type_localized[0];
$subject = __('%s made public', ucfirst($type_localized));
$url_public = $_SERVER['HTTP_HOST'] . '/' . $type . 's/show/id' . $record['id'];
$bodyHtml = '<p>' . __('Hello!') . '</p>';
$bodyHtml .= '<p>' . __('Your %s "<b>%s</b>" has just been made public by <b>%s</b>.', $type_localized, $title, $editor) . '</p>';
$bodyHtml .= '<p>' . __('The %s is now available at the page %s.', $type_localized, $url_public) . '</p>';
if ($bodyHtml != '' && $subject != '') {
$bodyHtml .= '<hr>' . __('This is an automatically generated message - please do not reply directly to this e-mail');
if ($recipientAddress != '') {
// sends e-mail to recipient address
$bMessageSent = $this->sendEmail($recipientAddress, $subject, $bodyHtml);
}
if (get_option('email_notification_notification_alert') && $bMessageSent) {
// shows alert for publisher
$message = __('The owner of the %s has been notified of the publishing.', $type_localized);
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage($message, 'success');
}
}
}
}
/**
* Sends e-mail.
*
* @param string $recipient
* @param string $subject
* @param string $body
*
* returns boolean
*/
private function sendEmail($recipient, $subject, $body) {
if ($recipient == '' || $subject == '' || $body == '') return false;
$email = new Zend_Mail('utf-8');
$email->setFrom(get_option('administrator_email'))
->addTo($recipient)
->setSubject($subject)
->setBodyHtml($body)
->addHeader('X-Mailer', 'PHP/' . phpversion())
->send();
return true;
}
}