-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailNotificationPlugin.php
More file actions
384 lines (342 loc) · 12.4 KB
/
EmailNotificationPlugin.php
File metadata and controls
384 lines (342 loc) · 12.4 KB
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
<?php
/**
* @version 1.6
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @copyright Daniele Binaghi, 2018-2026
* @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)
{
$this->handleBeforeSave($args, 'item', 'Item');
}
/**
* Before save collection hook.
*
* @param array $args
*/
public function hookBeforeSaveCollection($args)
{
$this->handleBeforeSave($args, 'collection', 'Collection');
}
/**
* Before save exhibit hook.
*
* @param array $args
*/
public function hookBeforeSaveExhibit($args)
{
$this->handleBeforeSave($args, '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']);
}
/**
* Shared logic for before_save hooks.
* Checks conditions and optionally notifies the record creator.
*
* @param array $args
* @param string $type lowercase type: 'item', 'collection', 'exhibit'
* @param string $recordClass Omeka record class name: 'Item', 'Collection', 'Exhibit'
*/
private function handleBeforeSave($args, $type, $recordClass)
{
if (!$args['post'] || $args['insert'] == 1) {
// new record being inserted, no action required
return;
}
$record = get_record_by_id($recordClass, $args['record']->id);
if ($record->public == 1) {
// record is already public, no action required
return;
}
if ($args['post']['public'] == 0) {
// record is not being made public, no action required
return;
}
if ($record->owner_id == current_user()->id) {
// record is being published by its own creator, no action required
return;
}
if (get_option('email_notification_notify_owner')) {
$this->notifyCreator($type, $record);
}
}
/**
* Creates notification for admin/editor users when a new record is added.
*
* @param string $type 'item', 'collection', or 'exhibit'
* @param object $record Omeka record object
*/
private function notifyUsers($type, $record)
{
$recipientAddress = get_option('email_notification_recipient_address');
$notifyEditors = get_option('email_notification_notify_editors');
$bMessageSent = false;
// Early exits: nobody to notify, or notification not enabled for this type
if (!$recipientAddress && !$notifyEditors) {
return;
}
if (!get_option('email_notification_new_' . $type)) {
return;
}
if ($type == 'exhibit' && !plugin_is_active('ExhibitBuilder')) {
return;
}
// Build common field values
$title = ($type == 'exhibit')
? metadata($record, '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 ($title == '') {
$title = __('not provided');
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage(__('No title has been provided for the new %s.', __(ucfirst($type))), 'error');
}
$subject = get_option('email_notification_new_' . $type . '_email_subject');
$bodyHtml = get_option('email_notification_new_' . $type . '_email_message');
$url_admin = absolute_url($type . 's/show/' . $record['id'], 'admin');
$url_public = absolute_url($type . 's/show/' . $record['id']);
// Build placeholder arrays — common to all types
$fields = array(
'{' . $type . '_title}',
'{' . $type . '_creator}',
'{' . $type . '_creation_date}',
'{' . $type . '_public_status}',
'{' . $type . '_featured_status}',
'{' . $type . '_admin_url}',
'{' . $type . '_public_url}',
);
$values = array($title, $creator, $date, $public, $featured, $url_admin, $url_public);
// Extra placeholder only for items: collection title
if ($type == 'item') {
$collection_title = metadata($record, 'Collection Name');
array_splice($fields, 3, 0, array('{item_collection_title}'));
array_splice($values, 3, 0, array($collection_title));
}
$bodyHtml = str_replace($fields, $values, $bodyHtml);
$bodyHtml .= '<hr>' . __('This is an automatically generated message - please do not reply directly to this e-mail');
// Send to explicit recipient address(es)
if ($recipientAddress) {
$bMessageSent = $this->sendEmail(explode(',', $recipientAddress), $subject, $bodyHtml);
}
// Send to all users with edit privilege
if ($notifyEditors) {
$acl = get_acl();
$users = get_db()->getTable('User')->findAll();
foreach ($users as $user) {
if ($acl->isAllowed($user, new Item, 'edit')) {
if ($this->sendEmail($user->email, $subject, $bodyHtml)) {
$bMessageSent = true;
}
}
}
}
if (get_option('email_notification_notification_alert') && $bMessageSent) {
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage(__('Admin/editors have been notified about the new addition.'), 'success');
}
}
/**
* Creates notification for the original record creator when it is made public.
*
* @param string $type 'item', 'collection', or 'exhibit'
* @param object $record Omeka record object
*/
private function notifyCreator($type, $record)
{
$recipientAddress = $record->getOwner()->email;
if ($recipientAddress == '') {
return;
}
$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 = absolute_url($type . 's/show/' . $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>';
$bodyHtml .= '<hr>' . __('This is an automatically generated message - please do not reply directly to this e-mail');
$bMessageSent = $this->sendEmail($recipientAddress, $subject, $bodyHtml);
if (get_option('email_notification_notification_alert') && $bMessageSent) {
$type_localized = __($type);
if (is_array($type_localized)) {
$type_localized = $type_localized[0];
}
$flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');
$flash->addMessage(__('The owner of the %s has been notified of the publishing.', $type_localized), 'success');
}
}
/**
* Sends an HTML e-mail via Zend_Mail.
*
* @param string|array $recipient Single address or array of addresses
* @param string $subject
* @param string $body HTML body
*
* @return bool true on success, false on empty input or send failure
*/
private function sendEmail($recipient, $subject, $body)
{
if ($recipient == '' || $subject == '' || $body == '') {
return false;
}
try {
$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;
} catch (Exception $e) {
// FIX 4: log the error instead of letting the exception bubble up
_log(
'EmailNotification: failed to send email to '
. (is_array($recipient) ? implode(', ', $recipient) : $recipient)
. ' — ' . $e->getMessage(),
Zend_Log::ERR
);
return false;
}
}
}