This repository has been archived by the owner on Jul 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathactivitytypeacl.php
443 lines (403 loc) · 17.3 KB
/
activitytypeacl.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
<?php
require_once 'activitytypeacl.civix.php';
/**
* Implementation of hook_civicrm_config
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function activitytypeacl_civicrm_config(&$config) {
_activitytypeacl_civix_civicrm_config($config);
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'civicrm/case/report') !== false) {
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($activityOptions, CRM_Core_Action::VIEW, FALSE, TRUE);
$flip = array_flip($activityOptions);
foreach ([
[TRUE, FALSE],
[TRUE, TRUE],
[FALSE, TRUE],
] as $value) {
list($all, $indexName) = $value;
CRM_Case_PseudoConstant::caseActivityType($indexName, $all);
$cache = (int) $indexName . '_' . (int) $all;
if ($cache == "0_1") {
CRM_Case_PseudoConstant::$activityTypeList[$cache] = array_intersect_key(CRM_Case_PseudoConstant::$activityTypeList[$cache], $activityOptions);
}
else {
CRM_Case_PseudoConstant::$activityTypeList[$cache] = array_intersect_key(CRM_Case_PseudoConstant::$activityTypeList[$cache], $flip);
}
}
}
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function activitytypeacl_civicrm_xmlMenu(&$files) {
_activitytypeacl_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function activitytypeacl_civicrm_install() {
_activitytypeacl_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function activitytypeacl_civicrm_uninstall() {
_activitytypeacl_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function activitytypeacl_civicrm_enable() {
_activitytypeacl_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function activitytypeacl_civicrm_disable() {
_activitytypeacl_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 activitytypeacl_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _activitytypeacl_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 activitytypeacl_civicrm_managed(&$entities) {
_activitytypeacl_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 activitytypeacl_civicrm_caseTypes(&$caseTypes) {
_activitytypeacl_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implementation of hook_civicrm_alterSettingsFolders
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function activitytypeacl_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_activitytypeacl_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implementation of hook_civicrm_permission
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_permission
*/
function activitytypeacl_civicrm_permission(&$permissions) {
$activities = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name', TRUE);
$prefix = ts('CiviCRM') . ': ';
$actions = array('add', 'view', 'edit', 'delete');
foreach ($activities as $id => $type) {
$label = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $id);
foreach ($actions as $action) {
$permissions[$action . ' activities of type ' . $type] = $prefix . ts($action . ' activities of type ') . $label;
}
}
}
/**
* Implementation of hook_civicrm_queryObjects
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_queryObjects
*/
function activitytypeacl_civicrm_queryObjects(&$queryObjects, $type) {
if ($type == "Contact") {
$queryObjects[] = new CRM_ActivityTypeACL_BAO_Query();
}
}
function activitytypeacl_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if (!empty($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], 'civicrm/a') !== false) {
if ($apiRequest['entity'] == 'OptionValue' && $apiRequest['action'] == 'get') {
$wrappers[] = new CRM_ActivityTypeACL_APIWrappers_ACL();
}
}
if ($apiRequest['entity'] == 'CaseType' && $apiRequest['action'] == 'get') {
$wrappers[] = new CRM_ActivityTypeACL_APIWrappers_ACL();
}
}
/**
* Implementation of hook_civicrm_buildForm
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*/
function activitytypeacl_civicrm_buildForm($formName, &$form) {
// Restrict activity types available in the "New Activity" creation list on contact summary page.
if ($formName == "CRM_Activity_Form_ActivityLinks") {
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($activityOptions, CRM_Core_Action::ADD, FALSE, TRUE);
$activityTypes = CRM_Core_Smarty::singleton()->get_template_vars('activityTypes');
foreach ($activityTypes as $key => $activity) {
if (!array_key_exists($activity['value'], $activityOptions)) {
unset($activityTypes[$key]);
}
}
$form->assign('activityTypes', $activityTypes);
}
// Restrict activity types available in the filters on activity tab on contact summary page.
if ($formName == "CRM_Activity_Form_ActivityFilter") {
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($activityOptions, CRM_Core_Action::VIEW, FALSE, TRUE);
asort($activityOptions);
$form->add('select', 'activity_type_filter_id', ts('Include'), array('' => ts('- all activity type(s) -')) + $activityOptions);
$form->add('select', 'activity_type_exclude_filter_id', ts('Exclude'), array('' => ts('- select activity type -')) + $activityOptions);
}
// Restrict activity types available in the filters on activity searches.
if ($formName == "CRM_Activity_Form_Search" || $formName == "CRM_Contact_Form_Search_Advanced") {
$form->addSelect('activity_type_id', array(
'entity' => 'activity',
'label' => ts('Activity Type(s)'),
'multiple' => 'multiple',
'option_url' => NULL,
'placeholder' => ts('- any -'),
'options' => CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($activityOptions, CRM_Core_Action::VIEW, FALSE, TRUE))
);
}
// Restrict view for activities with unpermissioned activity types.
if ($formName == "CRM_Activity_Form_ActivityView") {
$activityTypeId = CRM_Utils_Request::retrieve('atype', 'Integer');
$activityType = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, " AND v.value = {$activityTypeId}", "name");
if (!CRM_Core_Permission::check('view activities of type ' . $activityType[$activityTypeId])) {
CRM_Utils_System::permissionDenied();
CRM_Utils_System::civiExit();
}
}
if ($formName == "CRM_Report_Form_Contact_Detail") {
CRM_Core_Session::singleton()->set('isConstituent', TRUE);
}
else {
CRM_Core_Session::singleton()->set('isConstituent', FALSE);
}
// Restrict activity types for forms.
if ($formName == "CRM_Activity_Form_Activity") {
// Restrict list of activity types available on activity creation form.
if ($form->_action & CRM_Core_Action::ADD) {
$unwanted = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, "AND v.name = 'Print PDF Letter'");
$fActivityTypes = array_diff_key(CRM_Core_PseudoConstant::ActivityType(FALSE), $unwanted);
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($allowedActivities, CRM_Core_Action::ADD, FALSE, TRUE);
$fActivityTypes = array_intersect_key($allowedActivities, $fActivityTypes);
$form->add('select', 'activity_type_id', ts('Activity Type'),
array('' => '- ' . ts('select') . ' -') + $fActivityTypes,
FALSE, array(
'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
'class' => 'crm-select2 required',
)
);
// Restrict follow up activities too.
$form->add('select', 'followup_activity_type_id', ts('Followup Activity'),
array('' => '- ' . ts('select') . ' -') + $fActivityTypes,
FALSE, array(
'class' => 'crm-select2',
)
);
}
if (!empty($form->_activityTypeId)) {
$activityType = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, " AND v.value = {$form->_activityTypeId}", "name");
}
// Restrict view for activities with unpermissioned activity types.
if ($form->_action & CRM_Core_Action::VIEW) {
if (!CRM_Core_Permission::check('view activities of type ' . $activityType[$form->_activityTypeId])) {
CRM_Utils_System::permissionDenied();
CRM_Utils_System::civiExit();
}
// Permit edit button display for activities.
if (CRM_Core_Permission::check('edit activities of type ' . $activityType[$form->_activityTypeId])) {
$form->assign('canEdit', TRUE);
}
// Permit delete button display for activities.
if (CRM_Core_Permission::check('delete activities of type ' . $activityType[$form->_activityTypeId])) {
$form->assign('canDelete', TRUE);
}
}
// Restrict delete for activities with unpermissioned activity types.
if ($form->_action & CRM_Core_Action::DELETE) {
if (!CRM_Core_Permission::check('delete activities of type ' . $activityType[$form->_activityTypeId])) {
CRM_Utils_System::permissionDenied();
CRM_Utils_System::civiExit();
}
}
// Restrict edit for activities with unpermissioned activity types.
if (($form->_action & CRM_Core_Action::UPDATE) && isset($form->_activityTypeId)) {
if (!CRM_Core_Permission::check('edit activities of type ' . $activityType[$form->_activityTypeId])) {
CRM_Utils_System::permissionDenied();
CRM_Utils_System::civiExit();
}
else {
// Restrict available activities for edit.
$unwanted = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, "AND v.name = 'Print PDF Letter'");
$fActivityTypes = array_diff_key(CRM_Core_PseudoConstant::ActivityType(FALSE), $unwanted);
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($allowedActivities, CRM_Core_Action::UPDATE, FALSE, TRUE);
$fActivityTypes = array_intersect_key($allowedActivities, $fActivityTypes);
$form->add('select', 'activity_type_id', ts('Activity Type'),
array('' => '- ' . ts('select') . ' -') + $allowedActivities,
FALSE, array(
'onchange' => "CRM.buildCustomData( 'Activity', this.value );",
'class' => 'crm-select2 required',
)
);
// Restrict follow up activities too.
$form->add('select', 'followup_activity_type_id', ts('Followup Activity'),
array('' => '- ' . ts('select') . ' -') + $fActivityTypes,
FALSE, array(
'class' => 'crm-select2',
)
);
}
}
}
if ($formName == "CRM_Case_Form_CaseView") {
$xmlProcessor = new CRM_Case_XMLProcessor_Process();
$aTypes = $xmlProcessor->get($form->_caseType, 'ActivityTypes', TRUE);
$allActTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'validate');
$allCaseActTypes = CRM_Case_PseudoConstant::caseActivityType();
$emailActivityType = array_search('Email', $allActTypes);
$pdfActivityType = array_search('Print PDF Letter', $allActTypes);
// For the add activity widget.
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($allowedActivities, CRM_Core_Action::ADD, FALSE, TRUE);
// For the activity search form.
CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($viewActivities, CRM_Core_Action::VIEW, FALSE, TRUE);
foreach ($allCaseActTypes as $typeDetails) {
if (!in_array($typeDetails['name'], array('Open Case'))) {
$aTypesFilter[$typeDetails['id']] = CRM_Utils_Array::value('label', $typeDetails);
}
}
$aTypes = array_intersect_key($allowedActivities, $aTypes);
$aTypesFilter = array_intersect_key($viewActivities, $aTypesFilter);
//CRM_Case_Form_CaseView::activityForm($form, $aTypes);
$form->add('select', 'activity_type_filter_id', ts('Activity Type'), array('' => ts('- select activity type -')) + $aTypesFilter, FALSE, array('id' => 'activity_type_filter_id_' . $form->_caseID));
// remove Open Case activity type since we're inside an existing case
if ($openActTypeId = array_search('Open Case', $allActTypes)) {
unset($aTypes[$openActTypeId]);
}
// Only show "link cases" activity if other cases exist.
$linkActTypeId = array_search('Link Cases', $allActTypes);
if ($linkActTypeId) {
$count = civicrm_api3('Case', 'getcount', array(
'check_permissions' => TRUE,
'id' => array('!=' => $form->_caseID),
'is_deleted' => 0,
));
if (!$count) {
unset($aTypes[$linkActTypeId]);
}
}
if (!$xmlProcessor->getNaturalActivityTypeSort()) {
asort($aTypes);
}
$activityLinks = array('' => ts('Add Activity'));
foreach ($aTypes as $type => $label) {
if ($type == $emailActivityType) {
$url = CRM_Utils_System::url('civicrm/activity/email/add',
"action=add&context=standalone&reset=1&caseid={$form->_caseID}&atype=$type",
FALSE, NULL, FALSE
);
}
elseif ($type == $pdfActivityType) {
$url = CRM_Utils_System::url('civicrm/activity/pdf/add',
"action=add&context=standalone&reset=1&cid={$form->_contactID}&caseid={$form->_caseID}&atype=$type",
FALSE, NULL, FALSE);
}
else {
$url = CRM_Utils_System::url('civicrm/case/activity',
"action=add&reset=1&cid={$form->_contactID}&caseid={$form->_caseID}&atype=$type",
FALSE, NULL, FALSE
);
}
$activityLinks[$url] = $label;
}
$form->add('select', 'add_activity_type_id', '', $activityLinks, FALSE, array('class' => 'crm-select2 crm-action-menu fa-calendar-check-o twenty'));
}
}
/**
* Implements hook_civicrm_postProcess().
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function activitytypeacl_civicrm_postProcess($formName, &$form) {
if ($formName == "CRM_Admin_Form_Options" && $form->getVar('_gName') == 'activity_type' && ($form->_action & CRM_Core_Action::ADD)) {
$message = "Please review permissions for the new activity type <a href='%1'>here</a>, or contact your System Administrator.";
$url = CRM_Utils_System::url('admin/people/permissions', NULL, TRUE);
$status = ts($message, array(1 => $url));
CRM_Core_Session::setStatus($status, ts('Activity Type ACL Notice'));
}
}
/**
* Implementation of hook_civicrm_alterReportVar
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterReportVar
*/
function activitytypeacl_civicrm_alterReportVar($varType, &$var, &$object) {
if ($varType == 'sql' && get_class($object) == 'CRM_Report_Form_Contact_Detail') {
CRM_ActivityTypeACL_BAO_ACL::getAdditionalActivityClause($var->_formComponent['activity_civireport'], "constituent");
}
if ($varType == 'columns') {
if (isset($var['civicrm_activity']['filters']['activity_type_id'])) {
$var['civicrm_activity']['filters']['activity_type_id'] = array(
'title' => ts('Activity Type'),
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_ActivityTypeACL_BAO_ACL::getPermissionedActivities($activityOptions, CRM_Core_Action::VIEW, FALSE, TRUE),
);
}
}
if ($varType == 'sql' && get_class($object) == 'CRM_Report_Form_ActivitySummary') {
CRM_ActivityTypeACL_BAO_ACL::getAdditionalActivityClause($var, "summary");
}
if ($varType == 'sql' && get_class($object) == 'CRM_Report_Form_Case_TimeSpent') {
CRM_ActivityTypeACL_BAO_ACL::getAdditionalActivityClause($var, "summary");
}
if ($varType == 'sql' && get_class($object) == 'CRM_Report_Form_Case_Detail') {
CRM_ActivityTypeACL_BAO_ACL::getAdditionalActivityClause($var, "case");
}
}
/**
* Implementation of hook_civicrm_selectWhereClause
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_selectWhereClause
*/
function activitytypeacl_civicrm_selectWhereClause($entity, &$clauses) {
if ($entity == "Activity") {
$constituent = CRM_Core_Session::singleton()->get('isConstituent');
if (!$constituent) {
$whereClause = CRM_ActivityTypeACL_BAO_ACL::getAdditionalActivityClause($where, "search");
if (!empty($clauses['activity_type_id'])) {
$clauses['activity_type_id'] .= $whereClause;
}
else {
$clauses['activity_type_id'] = $whereClause;
}
}
}
}