forked from agileware/au.com.agileware.mailingteams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailingteams.php
executable file
·430 lines (371 loc) · 15.8 KB
/
mailingteams.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
<?php
require_once 'mailingteams.civix.php';
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function mailingteams_civicrm_config(&$config) {
_mailingteams_civix_civicrm_config($config);
$arg = array();
if(isset($_GET[$config->userFrameworkURLVar])) {
$arg = explode('/', $_GET[$config->userFrameworkURLVar]);
}
if (!(CRM_Core_Config::singleton()->userPermissionTemp)) {
CRM_Core_Config::singleton()->userPermissionTemp = new CRM_MailingTeam_Permission($arg);
}
Civi::dispatcher()->addListener(\Civi\API\Events::PREPARE, ['CRM_MailingTeam_APIWrapper', 'PREPARE'], -100);
Civi::dispatcher()->addListener(\Civi\API\Events::RESPOND, ['CRM_MailingTeam_APIWrapper', 'RESPOND'], -100);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function mailingteams_civicrm_install() {
_mailingteams_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function mailingteams_civicrm_enable() {
_mailingteams_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_preProcess().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
*/
function mailingteams_civicrm_preProcess($formName, &$form) {
if ($formName == 'CRM_Team_Form_Settings') {
/* Add the MailingTeams Group and Elements to the form. */
$form->add_elementGroup('mailingteams', ts('Mailing'));
$form->add('checkbox', 'mailingteams_restricted', ts('Restricted'));
$form->add_elementName(
'mailingteams_restricted', 'mailingteams',
ts('A restricted Team cannot load or copy Mailings created by another Team.')
);
$form->addEntityRef('mailingteams_from_addresses', ts('From Email Addresses'),
array (
'entity' => 'option_value',
'api' => array ('params' => array ('option_group_id' => 'from_email_address','domain_id' => CRM_Core_Config::domainID())),
'select' => array ('minimumInputLength' => 0),
'multiple' => TRUE,
));
$form->add_elementName(
'mailingteams_from_addresses', 'mailingteams',
ts('From Email Addresses that this Team is allowed to create Mailings with. The list of available email addresses is administered from <a href="%1">%2</a>',
array(
1 => CRM_Utils_System::url('civicrm/admin/options/from_email_address', 'reset=1'),
2 => ts('the CiviMail "From Email Addresses" page.')
)));
// Generate the magic incantation to find only mailing type groups.
$mailing_type = civicrm_api3('OptionValue', 'getvalue',
array('option_group_id' => 'group_type', 'name' => 'Mailing List', 'return' => 'value')
);
$mailing_type = "%\x01{$mailing_type}\x01%";
$form->addEntityRef('mailingteams_groups_draft', ts('Draft Groups'),
array (
'entity' => 'group',
'api' => array('params' => array( 'group_type' => array('LIKE' => $mailing_type))),
'select' => array ('minimumInputLength' => 0),
'multiple' => TRUE,
));
$form->add_elementName('mailingteams_groups_draft', 'mailingteams', ts('Groups that this Team can Draft Mailings for.'));
$form->addEntityRef('mailingteams_groups_publish', ts('Publish Groups'),
array (
'entity' => 'group',
'api' => array('params' => array( 'group_type' => array('LIKE' => $mailing_type))),
'select' => array ('minimumInputLength' => 0),
'multiple' => TRUE,
));
$form->add_elementName('mailingteams_groups_publish', 'mailingteams', ts('Groups that this Team can Approve and Schedule Mailings for; the Team will also be able to Draft Mailings for these Groups.'));
// Get and set the MailingTeam defaults.
if($form->team_id) {
$team = civicrm_api3('Team','getsingle', array('id' => $form->team_id));
$tfea = civicrm_api3('TeamMailingFromAddress', 'get', array('team_id' => $form->team_id, 'options' => array('limit' => 0)));
$tgr = civicrm_api3('TeamMailingGroup', 'get', array('team_id' => $form->team_id, 'options' => array('limit' => 0)));
$defaults['mailingteams_from_addresses'] = array();
$defaults['mailingteams_groups_draft'] = array();
$defaults['mailingteams_groups_publish'] = array();
foreach($tfea['values'] as $e) {
$defaults['mailingteams_from_addresses'][] = $e['from_email_address_id'];
}
foreach($tgr['values'] as $g) {
if($g['role'] == 'draft') {
$defaults['mailingteams_groups_draft'][] = $g['group_id'];
}
elseif($g['role'] == 'publish') {
$defaults['mailingteams_groups_publish'][] = $g['group_id'];
}
}
$defaults['mailingteams_restricted'] = !empty($team['data']['mailingteams']['restricted']);
$defaults['mailingteams_from_addresses'] = implode(',', $defaults['mailingteams_from_addresses']);
$defaults['mailingteams_groups_draft'] = implode(',', $defaults['mailingteams_groups_draft']);
$defaults['mailingteams_groups_publish'] = implode(',', $defaults['mailingteams_groups_publish']);
$form->setDefaults($defaults);
}
}
elseif ($formName == 'CRM_Team_Form_Teams') {
/* Add columns to Team Listing. */
$selector =& $form->selector();
$optgroupID = civicrm_api3('OptionGroup', 'getvalue', array('name' => 'from_email_address', 'return' => 'id'));
$selector->join('tme', 'LEFT JOIN (
SELECT tme1.team_id, GROUP_CONCAT(ov.label ORDER BY ov.label ASC SEPARATOR ", ") `emails`
FROM civicrm_option_value ov
INNER JOIN civicrm_team_mailing_email tme1 ON (ov.value = tme1.from_email_address_id AND ov.option_group_id = @optgroupID)
GROUP BY tme1.team_id
) tme ON tme.team_id = t.id',
array('optgroupID' => $optgroupID));
$selector->addColumn('from_email_addresses', ts('From Email Addresses'), 'tme.emails `from_email_addresses`');
$selector->join('tmg', 'LEFT JOIN (
SELECT tmg1.team_id, GROUP_CONCAT(DISTINCT g.title ORDER BY g.title ASC SEPARATOR ", ") `groups`
FROM civicrm_group g
INNER JOIN civicrm_team_mailing_group tmg1 ON(g.id = tmg1.group_id)
GROUP BY tmg1.team_id
) tmg ON tmg.team_id = t.id');
$selector->addColumn('groups', ts('Groups'), 'tmg.groups');
}
}
function mailingteams_civicrm_buildForm($formName, &$form) {
if($formName == 'CRM_Team_Form_Teams') {
$form->addEntityRef('mailingteams_from_address', ts('From Email Address'),
array (
'entity' => 'option_value',
'api' => array ('params' => array ('option_group_id' => 'from_email_address','domain_id' => CRM_Core_Config::domainID())),
'select' => array ('minimumInputLength' => 0),
));
$mailing_type = civicrm_api3('OptionValue', 'getvalue',
array('option_group_id' => 'group_type', 'name' => 'Mailing List', 'return' => 'value')
);
$mailing_type = "%\x01{$mailing_type}\x01%";
$form->addEntityRef('mailingteams_groups', ts('Groups'),
array (
'entity' => 'group',
'api' => array('params' => array( 'group_type' => array('LIKE' => $mailing_type))),
'select' => array ('minimumInputLength' => 0),
'multiple' => TRUE,
));
$form->addSearchElement(array(
'title' => ts('Mailing'),
'children' => array('mailingteams_from_address', 'mailingteams_groups')
));
}
}
function mailingteams_civicrm_postProcess($formName, &$form) {
if ($formName == 'CRM_Team_Form_Settings') {
$values = $form->exportValues();
$team = civicrm_api3('Team', 'getsingle', array('id' => $form->team_id));
$data = &$team['data'];
// Flag if we need to update the data field of the Team
$update_data = FALSE;
if((!empty($values['mailingteams_restricted']) && empty($data['mailingteams']['restricted'])) ||
empty($values['mailingteams_restricted']) && !empty($data['mailingteams']['restricted'])) {
$data['mailingteams']['restricted'] = !empty($values['mailingteams_restricted']);
$update_data = TRUE;
}
if($update_data) {
// Yes we have data updates! Apply them.
civicrm_api3('Team', 'create', array('id' => $form->team_id, 'data' => $data));
}
$from_addresses = (!empty($values['mailingteams_from_addresses'])
? explode(',', $values['mailingteams_from_addresses'])
: array());
_mailingteams_update_from_addresses($form->team_id, $from_addresses);
$draft_groups = (!empty($values['mailingteams_groups_draft'])
? explode(',', $values['mailingteams_groups_draft'])
: array());
$publish_groups = (!empty($values['mailingteams_groups_publish'])
? explode(',', $values['mailingteams_groups_publish'])
: array());
_mailingteams_update_groups($form->team_id, $draft_groups, $publish_groups);
}
elseif ($formName == 'CRM_Team_Form_Teams') {
$selector =& $form->selector();
if ($group_ids = CRM_Utils_Request::retrieve('mailingteams_groups', 'String')) {
$selector->where(
'EXISTS (SELECT 1 FROM civicrm_team_mailing_group WHERE group_id IN (@group_id) AND team_id = t.id)',
array('group_id' => $group_ids)
);
}
if ($email_id = CRM_Utils_Request::retrieve('mailingteams_from_address', 'String')) {
$selector->where(
'EXISTS (SELECT 1 FROM civicrm_team_mailing_email WHERE from_email_address_id IN (@email_id) AND team_id = t.id)',
array('email_id' => $email_id)
);
}
}
}
/**
* "Internal" function for updating from addresses from a list of from address ids.
*
* @param int $team_id
* @param array $from_addresses
*
**/
function _mailingteams_update_from_addresses($team_id, $from_addresses) {
try {
$team_from_addresses = civicrm_api3('TeamMailingFromAddress', 'get', array('team_id' => $team_id));
$team_from_addresses = $team_from_addresses['values'];
}
catch(CiviCRM_API3_Exception $e){
$team_from_addresses = array();
}
$tfa_match = array();
// Start with known address ids.
foreach($team_from_addresses as $id => $tfa) {
if (!in_array($tfa['from_email_address_id'], $from_addresses)) {
// Delete those that do not match input.
civicrm_api3('TeamMailingFromAddress', 'delete', array('id' => $id));
}
else {
// Mark those that match input as not requiring update.
$tfa_match[$tfa['from_email_address_id']] = TRUE;
}
}
// Loop through input to find new addresses ids
foreach($from_addresses as $eid) {
if(empty($tfa_match[$eid])) {
civicrm_api3('TeamMailingFromAddress', 'create', array('team_id' => $team_id, 'from_email_address_id' => $eid));
}
}
}
/**
* "Internal" function for updating groups links from lists of from group ids.
*
* @param int $team_id
* @param array $draft_groups
* @param array $publish_groups
*
**/
function _mailingteams_update_groups($team_id, $draft_groups, $publish_groups) {
try {
$team_groups = civicrm_api3('TeamMailingGroup', 'get', array('team_id' => $team_id));
$team_groups = $team_groups['values'];
}
catch(CiviCRM_API3_Exception $e){
$team_groups = array();
}
$dgr_match = array();
$pgr_match = array();
// Start with known groups.
foreach($team_groups as $id => $tgr) {
switch($tgr['role']){
case 'draft':
if (!in_array($tgr['group_id'], $draft_groups)) {
// Delete those that do not match input.
civicrm_api3('TeamMailingGroup', 'delete', array('id' => $id));
}
else {
// Mark as not requiring update.
$dgr_match[$tgr['group_id']] = TRUE;
}
break;
case 'publish':
if (!in_array($tgr['group_id'], $publish_groups)) {
// Delete those that do not match input.
civicrm_api3('TeamMailingGroup', 'delete', array('id' => $id));
}
else {
// Mark as not requiring update.
$pgr_match[$tgr['group_id']] = TRUE;
}
break;
default:
break;
}
}
// Loop through input to find new groups' ids
foreach($draft_groups as $gid) {
if(empty($dgr_match[$gid])) {
civicrm_api3('TeamMailingGroup', 'create', array('team_id' => $team_id, 'group_id' => $gid, 'role' => 'draft'));
}
}
foreach($publish_groups as $gid) {
if(empty($pgr_match[$gid])) {
civicrm_api3('TeamMailingGroup', 'create', array('team_id' => $team_id, 'group_id' => $gid, 'role' => 'publish'));
}
}
}
function mailingteams_civicrm_selectWhereClause($entity, &$clauses) {
static $contact_id = NULL;
if($contact_id === NULL){
$contact_id = CRM_Core_Session::getLoggedInContactID();
}
if(CRM_Core_Permission::check('access CiviMail')) {
return;
}
if(CRM_MailingTeam_BAO_TeamMailingGroup::$doAclCheck && $entity == 'Group') {
$clauses['id'][] = sprintf('IN (SELECT tmg.group_id FROM civicrm_team_mailing_group tmg INNER JOIN civicrm_team_contact tc USING(team_id) WHERE tc.contact_id = %1$d AND EXISTS (SELECT 1 FROM civicrm_team t WHERE t.id = tc.team_id AND (domain_id = %2$d OR domain_id IS NULL)))', $contact_id, CRM_Core_Config::domainID());
}
if($entity == 'Mailing' && !CRM_Core_Permission::check('administer teams')) {
// @TODO expand to restrict by team that saved the mailing when ready (no interface yet )
$sqlstr = <<<'EOS'
IN (SELECT m.id FROM civicrm_mailing m
LEFT JOIN civicrm_mailing_group mg ON m.id = mg.mailing_id AND mg.entity_table = 'civicrm_group'
LEFT JOIN civicrm_team_mailing_group tmg ON tmg.group_id = mg.entity_id
LEFT JOIN civicrm_team_contact tc ON tmg.team_id = tc.team_id
WHERE mg.id is NULL OR tc.contact_id = %1$d
AND EXISTS (SELECT 1 FROM civicrm_team t WHERE t.id = tc.team_id AND (domain_id = %2$d OR domain_id IS NULL))
)
EOS;
$clauses['id'][] = sprintf($sqlstr, $contact_id, CRM_Core_Config::domainID());
}
}
function _mailingteams_fea_id($option_value_id) {
static $fea_id = NULL;
if(empty($fea_id)) {
$bao = new CRM_Core_BAO_OptionGroup;
$bao->name = 'from_email_address';
$bao->find(1);
$fea_id = $bao->id;
}
return ($option_value_id == 'from_email_address') || ($option_value_id == $fea_id);
}
function mailingteams_civicrm_team_permissions($entity_table, $entity_id, $action, $contact_id, &$permissions) {
if($entity_table == 'from_email_address' && !CRM_Core_Permission::check('access CiviMail')) {
$addresses = civicrm_api3(
'TeamMailingFromAddress', 'get',
['from_email_address_id' => $entity_id, 'options' => ['limit' => 0]]
);
$count = civicrm_api3(
'TeamContact', 'getcount',
[ 'contact_id' => $contact_id,
'team_id' => [ 'IN' => array_map(function($v){ return $v['team_id']; }, $addresses['values'])]
]
);
$permissions[] = !!$count;
}
if ($entity_table == 'civicrm_mailing' && !CRM_Core_Permission::check('access CiviMail')) {
$permissions[] = CRM_MailingTeam_Permission::canMail($entity_id, $action, $contact_id);
}
}
/**
* Functions below this ship commented out. Uncomment as required.
*
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function mailingteams_civicrm_navigationMenu(&$menu) {
_mailingteams_civix_insert_navigation_menu($menu, NULL, array(
'label' => ts('The Page', array('domain' => 'au.com.agileware.mailingteams')),
'name' => 'the_page',
'url' => 'civicrm/the-page',
'permission' => 'access CiviReport,access CiviContribute',
'operator' => 'OR',
'separator' => 0,
));
_mailingteams_civix_navigationMenu($menu);
} // */
// /**
// * Implements hook_civicrm_entityTypes().
// *
// * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
// */
// function mailingteams_civicrm_entityTypes(&$entityTypes) {
// _mailingteams_civix_civicrm_entityTypes($entityTypes);
// }