-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplemail.php
executable file
·533 lines (463 loc) · 14 KB
/
simplemail.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/*
* General todos:
*
* TODO (robin): Automate adding a new option value for Group Type for Mailing Category on installation
* TODO (robin): Automate adding a new option group and values for header categories
*/
require_once 'simplemail.civix.php';
simplemail_civicrm_init();
///////////
// Hooks //
///////////
/**
* Implementation of hook_civicrm_config
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function simplemail_civicrm_config(&$config) {
_simplemail_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function simplemail_civicrm_xmlMenu(&$files) {
_simplemail_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function simplemail_civicrm_install() {
return _simplemail_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function simplemail_civicrm_uninstall() {
return _simplemail_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function simplemail_civicrm_enable() {
return _simplemail_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function simplemail_civicrm_disable() {
return _simplemail_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function simplemail_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _simplemail_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function simplemail_civicrm_managed(&$entities) {
return _simplemail_civix_civicrm_managed($entities);
}
/**
* Implementation of hook_civicrm_caseTypes
*
* Generate a list of case-types
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function simplemail_civicrm_caseTypes(&$caseTypes) {
_simplemail_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implementation of hook_civicrm_alterSettingsFolders
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function simplemail_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_simplemail_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implementation of hook_civicrm_entityTypes
*
* This has been used here for registering entities
*
* @param $entityTypes
*/
function simplemail_civicrm_entityTypes(&$entityTypes) {
$entityTypes[] = array(
'name' => 'SimpleMailHeader',
'class' => 'CRM_Simplemail_DAO_SimpleMailHeader',
'table' => 'civicrm_simplemailheader'
);
$entityTypes[] = array(
'name' => 'SimpleMailMessage',
'class' => 'CRM_Simplemail_DAO_SimpleMailMessage',
'table' => 'civicrm_simplemailmessage'
);
$entityTypes[] = array(
'name' => 'SimpleMail',
'class' => 'CRM_Simplemail_DAO_SimpleMail',
'table' => 'civicrm_simplemail'
);
$entityTypes[] = array(
'name' => 'SimpleMailHeaderFilter',
'class' => 'CRM_Simplemail_DAO_SimpleMailHeaderFilter',
'table' => 'civicrm_simplemailheaderfilter'
);
$entityTypes[] = array(
'name' => 'SimpleMailInlineAttachment',
'class' => 'CRM_Simplemail_DAO_SimpleMailInlineAttachment',
'table' => 'civicrm_simplemailinlineattachment'
);
}
/**
* Implementation of hook_civicrm_searchTasks
*
* This has been used here for adding custom search actions on the contact search results page
*
* @param $objectType
* @param $tasks
*/
function simplemail_civicrm_searchTasks($objectType, &$tasks) {
$tasks[] = array(
'title' => 'Send a MailJet email',
'class' => array(
// because this class is now deprecated
// 'CRM_Simplemail_Form_SimpleMailRecipientsFromSearch'
'CRM_Simplemail_Form_Task_SimpleMail'
)
);
}
/**
* Implementation of hook_civicrm_navigationMenu
*
* This has been used here for adding menu links for various Simple Mail pages
*
* @param $params
*/
function simplemail_civicrm_navigationMenu(&$params) {
/* Added by Scott 10-12-2014
* Only provides this extension's menu if a customised one does not already exist */
foreach ($params as $parameter) {
if ($parameter['attributes']['name'] == 'MailJet') {
return;
}
}
// Get the maximum key of $params
$maxKey = _getMenuKeyMax($params);
$parentId = $maxKey + 1;
$children = array();
$parent = array(
'attributes' => array(
'label' => SM_EXT_NAME,
'name' => SM_EXT_NAME,
'url' => NULL,
'permission' => 'access CiviSimpleMail',
'operator' => NULL,
'separator' => NULL,
'parentID' => NULL,
'navID' => $parentId,
'active' => 1
)
);
$currentChildId = $parentId + 1;
$children[$currentChildId] = array(
'attributes' => array(
'label' => 'New Mailing',
'name' => 'New Mailings',
'url' => 'civicrm/simple-mail#/mailings/new',
'permission' => NULL,
'operator' => NULL,
'parentID' => $parentId,
'navID' => $currentChildId,
'active' => 1
)
);
$currentChildId++;
$children[$currentChildId] = array(
'attributes' => array(
'label' => 'Mailings',
'name' => 'Mailings',
'url' => 'civicrm/simple-mail#/mailings',
'permission' => NULL,
'operator' => NULL,
'parentID' => $parentId,
'navID' => $currentChildId,
'active' => 1
)
);
$currentChildId++;
$children[$currentChildId] = array(
'attributes' => array(
'label' => 'Headers',
'name' => 'Headers',
'url' => 'civicrm/admin/simple-mail#/headers',
'permission' => NULL,
'operator' => NULL,
'parentID' => $parentId,
'navID' => $currentChildId,
'active' => 1
)
);
$currentChildId++;
$children[$currentChildId] = array(
'attributes' => array(
'label' => 'Campaign Messages',
'name' => 'Campaign Messages',
'url' => 'civicrm/admin/simple-mail#/messages',
'permission' => NULL,
'operator' => NULL,
'parentID' => $parentId,
'navID' => $currentChildId,
'active' => 1
)
);
$parent['child'] = $children;
$params[$parentId] = $parent;
}
/**
* @param $permissions
*/
function simplemail_civicrm_permission(&$permissions) {
// Name of extension
$prefix = ts('CiviSimpleMail') . ': ';
$permissions[SM_PERMISSION_ACCESS] = $prefix . ts(SM_PERMISSION_ACCESS);
$permissions[SM_PERMISSION_DELETE] = $prefix . ts(SM_PERMISSION_DELETE);
$permissions[SM_PERMISSION_ACCESS_ADMIN] = $prefix . ts(SM_PERMISSION_ACCESS_ADMIN);
$permissions[SM_PERMISSION_ACCESS_LEGACY] = $prefix . ts(SM_PERMISSION_ACCESS_LEGACY);
$permissions[SM_PERMISSION_MANAGE_ALL] = $prefix . ts(SM_PERMISSION_MANAGE_ALL);
$permissions[SM_PERMISSION_ADD_GROUPS] = $prefix . ts(SM_PERMISSION_ADD_GROUPS);
}
/**
* Return the maximum key from the menu items array
*
* @param $menuArray
*
* @return mixed
*/
function _getMenuKeyMax($menuArray) {
$max = array(max(array_keys($menuArray)));
foreach ($menuArray as $v) {
if (!empty($v['child'])) {
$max[] = _getMenuKeyMax($v['child']);
}
}
return max($max);
}
/**
* Implementation of hook_civicrm_alterAPIPermissions
*
* This has been used here to add permissions for various entities necessary for the correct working of SimpleMail
*
* @param $entity
* @param $action
* @param $params
* @param $permissions
*/
function simplemail_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
if (CRM_Core_Permission::check('access CiviSimpleMail')) {
$permissionKeys = array('access CiviSimpleMail');
$standardActions = array(
'create' => $permissionKeys,
'delete' => $permissionKeys,
'get' => $permissionKeys,
'update' => $permissionKeys,
);
$permissions['simple_mail'] = array(
'submitmassemail' => $permissionKeys,
'cancelmassemail' => $permissionKeys,
'sendtestemail' => $permissionKeys,
'duplicatemassemail' => $permissionKeys,
'iscreatedfromsearch' => $permissionKeys,
'canaddgroups' => $permissionKeys, // ?
'uploadinlineattachment' => $permissionKeys,
'clearsearchcontacts' => $permissionKeys
) + $standardActions;
$permissions['simple_mail_header'] = array(
'uploadimage' => $permissionKeys,
'deleteimage' => $permissionKeys
) + $standardActions;
$permissions['simple_mail_message'] = array(
'create' => $permissionKeys,
'delete' => $permissionKeys,
'get' => $permissionKeys,
'update' => $permissionKeys
);
$permissions['simple_mail_inline_attachment'] = array(
'remove' => $permissionKeys,
'getall' => $permissionKeys
);
//$permissions['simple_mail_inline']
$permissions['option_group'] = $permissions['option_value'] = array('get' => $permissionKeys);
}
}
/////////////
// Helpers //
/////////////
/**
* Initialise the extension
*/
function simplemail_civicrm_init() {
define('SM_SESSION_SCOPE_PREFIX', 'SimpleMail_');
/**
* The value of the option value for name 'mailing_category', part of the option group 'group_type'
*/
define('SM_MAILING_CATEGORY_GROUP_TYPE_VALUE', 'mailing_category');
// Permission
define('SM_PERMISSION_ACCESS', 'access CiviSimpleMail');
define('SM_PERMISSION_EDIT', 'edit CiviSimpleMail');
define('SM_PERMISSION_DELETE', 'delete CiviSimpleMail');
define('SM_PERMISSION_ACCESS_ADMIN', 'access admin CiviSimpleMail');
define('SM_PERMISSION_ACCESS_LEGACY', 'access LegacyMailUi');
define('SM_PERMISSION_MANAGE_ALL', 'manage all CiviSimpleMail mails');
define('SM_PERMISSION_ADD_GROUPS', 'add groups to new CiviSimpleMail mails');
/** @var SimpleXMLElement[] $infoXml */
$infoXml = CRM_Utils_XML::parseFile(simplemail_civicrm_getExtensionDir() . 'info.xml');
foreach ($infoXml as $element) {
if ($element instanceof SimpleXMLElement && $element->getName() == 'extension') {
$attributes = $element->attributes();
/**
* Name of the extension
*/
define('SM_EXT_NAME', (string) $element->name);
/**
* Key of the extension (also the name of the extension's directory)
*/
define('SM_EXT_KEY', (string) $attributes['key']);
/**
* Path to the assets directory
*/
define('SM_ASSETS_URL', simplemail_civicrm_getExtensionUrl() . '/assets');
}
}
/**
* Whether emails should have SSL linked content or not
* Generally set this to false because SSL linked content can break mail clients
*/
define('SM_CONTENT_SSL', false);
}
/**
* Get the absolute path of the extension directory, with a trailing slash
* TODO (robin): Probably add the extension directory and url as constants as well
*
* @return string
*/
function simplemail_civicrm_getExtensionDir() {
return str_replace('simplemail.php', '', simplemail_civicrm_getExtensionFile());
}
/**
* Get the absolute path of this file
*
* @return string
*/
function simplemail_civicrm_getExtensionFile() {
$files = simplemail_civicrm_getActiveModuleFiles();
$extFile = '';
foreach ($files as $file) {
if ($file['prefix'] === 'simplemail') {
$extFile = $file['filePath'];
break;
}
}
return $extFile;
}
/**
* Get the URL to the extension directory
*
* @return string
*/
function simplemail_civicrm_getExtensionUrl() {
$url = '';
$urls = simplemail_civicrm_getActiveModuleUrls();
if (array_key_exists(SM_EXT_KEY, $urls)) {
$url = $urls[SM_EXT_KEY];
}
return $url;
}
/**
* Get an array of all active module URLs
*
* @return array
*/
function simplemail_civicrm_getActiveModuleUrls() {
$mapper = CRM_Extension_System::singleton()->getMapper();
$urls = array();
$urls['civicrm'] = $mapper->keyToUrl('civicrm');
foreach ($mapper->getModules() as $module) {
/** @var $module CRM_Core_Module */
if ($module->is_active) {
$urls[$module->name] = $mapper->keyToUrl($module->name);
}
}
return $urls;
}
/**
* Get an array of all active module files
*
* @return array
*/
function simplemail_civicrm_getActiveModuleFiles() {
return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles();
}
/**
* @param $key
* @param $value
*
* @return $this
*/
function simplemail_civicrm_addToSessionScope($key, $value) {
CRM_Core_Session::singleton()
->set($key, $value, simplemail_civicrm_getSessionScopeName());
}
/**
* @param $key
*
* @return mixed
*/
function simplemail_civicrm_getFromSessionScope($key) {
return CRM_Core_Session::singleton()
->get($key, simplemail_civicrm_getSessionScopeName());
}
/**
* @return string
*/
function simplemail_civicrm_getSessionScopeName() {
return SM_SESSION_SCOPE_PREFIX . CRM_Core_Session::singleton()->get('userID');
}
/**
* Clear the session scope used by Simple Mail
*/
function simplemail_civicrm_clearSessionScope() {
$session = CRM_Core_Session::singleton();
$sessionScope = simplemail_civicrm_getSessionScopeName();
$session->resetScope($sessionScope);
}