forked from compucorp/uk.co.compucorp.civicase
-
Notifications
You must be signed in to change notification settings - Fork 2
/
civicase.php
510 lines (458 loc) · 15.4 KB
/
civicase.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
<?php
/**
* @file
* Extension file.
*/
use Civi\Angular\Manager;
require_once 'civicase.civix.php';
/**
* Implements hook_civicrm_tabset().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_tabset
*/
function civicase_civicrm_tabset($tabsetName, &$tabs, $context) {
$useAng = FALSE;
$hooks = [
new CRM_Civicase_Hook_Tabset_CaseTabModifier(),
new CRM_Civicase_Hook_Tabset_CaseCategoryTabAdd(),
new CRM_Civicase_Hook_Tabset_ActivityTabModifier(),
];
foreach ($hooks as $hook) {
$hook->run($tabsetName, $tabs, $context, $useAng);
}
if ($useAng) {
$loader = Civi::service('angularjs.loader');
$loader->addModules('civicase');
}
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function civicase_civicrm_config(&$config) {
_civicase_civix_civicrm_config($config);
if (isset(Civi::$statics[__FUNCTION__])) {
return;
}
Civi::$statics[__FUNCTION__] = 1;
Civi::dispatcher()->addListener(
'civi.api.prepare',
['CRM_Civicase_Event_Listener_ActivityFilter', 'onPrepare'],
10
);
Civi::dispatcher()->addListener(
'civi.api.respond',
[
'CRM_Civicase_Event_Listener_CaseTypeCategoryIsActiveToggler',
'onRespond',
],
10
);
Civi::dispatcher()->addListener(
'civi.api.respond',
['CRM_Civicase_Event_Listener_CaseRoleCreation', 'onRespond'],
10
);
Civi::dispatcher()->addListener(
'civi.api.prepare',
['CRM_Civicase_Event_Listener_CaseRoleCreation', 'onPrepare'],
10
);
Civi::dispatcher()->addListener(
'civi.api.prepare',
['CRM_Civicase_Event_Listener_CaseCustomFields', 'loadOnDemand'],
10
);
Civi::dispatcher()->addListener(
'hook_civicrm_buildAsset',
['CRM_Civicase_Event_Listener_AssetBuilder', 'addWordReplacements']
);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function civicase_civicrm_install() {
_civicase_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function civicase_civicrm_enable() {
_civicase_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_alterMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterMenu
*/
function civicase_civicrm_alterMenu(&$items) {
$items['civicrm/case/activity']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/activity']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/activity/email/add']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/case/email/add']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/activity/pdf/add']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/case/cd/edit']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/export/standalone']['ids_arguments']['json'][] = 'civicase_reload';
}
/**
* Implements hook_civicrm_buildForm().
*/
function civicase_civicrm_buildForm($formName, &$form) {
$hooks = [
new CRM_Civicase_Hook_BuildForm_CaseClientPopulator(),
new CRM_Civicase_Hook_BuildForm_FilterCaseTypesByCategoryForNewCase(),
new CRM_Civicase_Hook_BuildForm_FilterByCaseCategoryOnChangeCaseType(),
new CRM_Civicase_Hook_BuildForm_CaseCategoryFormLabelTranslationForNewCase(),
new CRM_Civicase_Hook_BuildForm_CaseCategoryFormLabelTranslationForChangeCase(),
new CRM_Civicase_Hook_BuildForm_EnableCaseCategoryIconField(),
new CRM_Civicase_Hook_BuildForm_CaseCategoryCustomGroupDisplay(),
new CRM_Civicase_Hook_BuildForm_ModifyCaseTypesForAdvancedSearch(),
new CRM_Civicase_Hook_BuildForm_AddCaseCategoryInstanceField(),
new CRM_Civicase_Hook_BuildForm_RemoveExportActionFromReports(),
new CRM_Civicase_Hook_BuildForm_RestrictCaseEmailContacts(),
new CRM_Civicase_Hook_BuildForm_LimitRecipientFieldsToOnlySelectedContacts(),
new CRM_Civicase_Hook_BuildForm_TokenTree(),
new CRM_Civicase_Hook_BuildForm_LinkCaseActivityDefaultStatus(),
new CRM_Civicase_Hook_BuildForm_HandleDraftActivities(),
new CRM_Civicase_Hook_BuildForm_AddCaseCategoryCustomFields(),
new CRM_Civicase_Hook_BuildForm_MakePdfFormSubjectRequired(),
new CRM_Civicase_Hook_BuildForm_PdfFormButtonsLabelChange(),
new CRM_Civicase_Hook_BuildForm_AddScriptToCreatePdfForm(),
];
foreach ($hooks as $hook) {
$hook->run($form, $formName);
}
// Display category option for activity types and activity statuses.
if ($formName == 'CRM_Admin_Form_Options'
&& in_array($form->getVar('_gName'), [
'activity_type',
'activity_status',
])) {
$options = civicrm_api3('optionValue', 'get', [
'option_group_id' => 'activity_category',
'is_active' => 1,
'options' => ['limit' => 0, 'sort' => 'weight'],
]);
$opts = [];
if ($form->getVar('_gName') == 'activity_status') {
$placeholder = ts('All');
// Activity status can also apply to uncategorized activities.
$opts[] = [
'id' => 'none',
'text' => ts('Uncategorized'),
];
}
else {
$placeholder = ts('Uncategorized');
}
foreach ($options['values'] as $opt) {
$opts[] = [
'id' => $opt['name'],
'text' => $opt['label'],
];
}
$form->add('select2', 'grouping', ts('Activity Category'), $opts, FALSE, [
'class' => 'crm-select2',
'multiple' => TRUE,
'placeholder' => $placeholder,
]);
}
// Only show relevant statuses when editing an activity.
if (is_a($form, 'CRM_Activity_Form_Activity') && $form->_action & (CRM_Core_Action::ADD + CRM_Core_Action::UPDATE)) {
if (!empty($form->_activityTypeId) && $form->elementExists('status_id')) {
$el = $form->getElement('status_id');
$cat = civicrm_api3('OptionValue', 'getsingle', [
'return' => 'grouping',
'option_group_id' => "activity_type",
'value' => $form->_activityTypeId,
]);
$cat = !empty($cat['grouping']) ? explode(',', $cat['grouping']) : ['none'];
$options = civicrm_api3('OptionValue', 'get', [
'return' => ['label', 'value', 'grouping'],
'option_group_id' => "activity_status",
'options' => ['limit' => 0, 'sort' => 'weight'],
]);
$newOptions = $el->_options = [];
$newOptions[''] = ts('- select -');
foreach ($options['values'] as $option) {
if (empty($option['grouping']) || array_intersect($cat, explode(',', $option['grouping']))) {
$newOptions[$option['value']] = $option['label'];
}
}
$el->loadArray($newOptions);
}
}
// If js requests a refresh of case data pass that request along.
if (!empty($_REQUEST['civicase_reload'])) {
$form->civicase_reload = json_decode($_REQUEST['civicase_reload'], TRUE);
}
}
/**
* Implements hook_civicrm_validateForm().
*/
function civicase_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
$hooks = [
new CRM_Civicase_Hook_ValidateForm_SaveActivityDraft(),
new CRM_Civicase_Hook_ValidateForm_SaveCaseTypeCategory(),
new CRM_Civicase_Hook_ValidateForm_SendBulkEmail(),
];
foreach ($hooks as $hook) {
$hook->run($formName, $fields, $files, $form, $errors);
}
}
/**
* Implements hook_civicrm_post().
*/
function civicase_civicrm_post($op, $objectName, $objectId, &$objectRef) {
$hooks = [
new CRM_Civicase_Hook_Post_PopulateCaseCategoryForCaseType(),
new CRM_Civicase_Hook_Post_CaseCategoryCustomGroupSaver(),
new CRM_Civicase_Hook_Post_UpdateCaseTypeListForCaseCategoryCustomGroup(),
];
foreach ($hooks as $hook) {
$hook->run($op, $objectName, $objectId, $objectRef);
}
}
/**
* Implements hook_civicrm_postProcess().
*/
function civicase_civicrm_postProcess($formName, &$form) {
$hooks = [
new CRM_Civicase_Hook_PostProcess_SetUserContextForSaveAndNewCase(),
new CRM_Civicase_Hook_PostProcess_SaveCaseCategoryInstance(),
new CRM_Civicase_Hook_PostProcess_CaseCategoryPostProcessor(),
new CRM_Civicase_Hook_PostProcess_ActivityFormStatusWordReplacement(),
new CRM_Civicase_Hook_PostProcess_RedirectToCaseDetails(),
new CRM_Civicase_Hook_PostProcess_AttachEmailActivityToAllCases(),
new CRM_Civicase_Hook_PostProcess_HandleDraftActivity(),
new CRM_Civicase_Hook_PostProcess_SaveCaseCategoryCustomFields(),
];
foreach ($hooks as $hook) {
$hook->run($formName, $form);
}
if (!empty($form->civicase_reload)) {
$api = civicrm_api3('Case', 'getdetails', ['check_permissions' => 1] + $form->civicase_reload);
$form->ajaxResponse['civicase_reload'] = $api['values'];
}
}
/**
* Implements hook_civicrm_permission().
*/
function civicase_civicrm_permission(&$permissions) {
$hooks = [
new CRM_Civicase_Hook_Permissions_CaseCategory($permissions),
new CRM_Civicase_Hook_Permissions_ExportCasesAndReports($permissions),
];
foreach ($hooks as $hook) {
$hook->run();
}
}
/**
* Implements hook_civicrm_apiWrappers().
*/
function civicase_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if ($apiRequest['entity'] == 'Case') {
$wrappers[] = new CRM_Civicase_Api_Wrapper_CaseList();
$wrappers[] = new CRM_Civicase_Api_Wrapper_CaseGetList();
}
}
/**
* Implements hook_civicrm_alterAPIPermissions().
*
* @link https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_alterAPIPermissions/
*/
function civicase_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
$hooks = [
new CRM_Civicase_Hook_alterAPIPermissions_CaseCategory(),
new CRM_Civicase_Hook_alterAPIPermissions_CaseGetList(),
];
foreach ($hooks as $hook) {
$hook->run($entity, $action, $params, $permissions);
}
}
/**
* Implements hook_civicrm_tokens().
*/
function civicase_civicrm_tokens(&$tokens) {
$contactFieldsService = new CRM_Civicase_Service_ContactFieldsProvider();
$contactCustomFieldsService = new CRM_Civicase_Service_ContactCustomFieldsProvider();
$hooks = [
new CRM_Civicase_Hook_Tokens_AddContactTokens($contactFieldsService, $contactCustomFieldsService),
new CRM_Civicase_Hook_Tokens_AddCaseTokenCategory(),
];
foreach ($hooks as &$hook) {
$hook->run($tokens);
}
}
/**
* Implements hook_civicrm_tokenValues().
*/
function civicase_civicrm_tokenValues(&$values, $cids, $job = NULL, $tokens = [], $context = NULL) {
$contactFieldsService = new CRM_Civicase_Service_ContactFieldsProvider();
$contactCustomFieldsService = new CRM_Civicase_Service_ContactCustomFieldsProvider();
$caseTokenValuesHelper = new CRM_Civicase_Hook_Tokens_Helper_CaseTokenValues();
$hooks = [
new CRM_Civicase_Hook_Tokens_AddContactTokensValues($contactFieldsService, $contactCustomFieldsService),
new CRM_Civicase_Hook_Tokens_AddCaseCustomFieldsTokenValues($caseTokenValuesHelper),
];
foreach ($hooks as &$hook) {
$hook->run($values, $cids, $job, $tokens, $context);
}
}
/**
* Implements hook_civicrm_pageRun().
*/
function civicase_civicrm_pageRun(&$page) {
$hooks = [
new CRM_Civicase_Hook_PageRun_ViewCasePageRedirect(),
new CRM_Civicase_Hook_PageRun_AddCaseAngularPageResources(),
new CRM_Civicase_Hook_PageRun_AddContactPageSummaryResources(),
new CRM_Civicase_Hook_PageRun_CaseCategoryCustomGroupListing(),
];
foreach ($hooks as $hook) {
$hook->run($page);
}
}
/**
* Implements hook_civicrm_check().
*/
function civicase_civicrm_check(&$messages) {
$check = new CRM_Civicase_Check();
$newMessages = $check->checkAll();
$messages = array_merge($messages, $newMessages);
}
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*/
function civicase_civicrm_navigationMenu(&$menu) {
$hooks = [
new CRM_Civicase_Hook_NavigationMenu_AlterForCaseMenu(),
];
foreach ($hooks as $hook) {
$hook->run($menu);
}
}
/**
* Implements hook_civicrm_selectWhereClause().
*/
function civicase_civicrm_selectWhereClause($entity, &$clauses) {
if ($entity === 'Case') {
unset($clauses['id']);
}
$hooks = [
new CRM_Civicase_Hook_SelectWhereClause_LimitCaseQueryToAccessibleCaseCategories(),
];
foreach ($hooks as $hook) {
$hook->run($entity, $clauses);
}
}
/**
* Implements hook_civicrm_entityTypes().
*/
function civicase_civicrm_entityTypes(&$entityTypes) {
_civicase_add_case_category_case_type_entity($entityTypes);
}
/**
* Implements hook_civicrm_queryObjects().
*/
function civicase_civicrm_queryObjects(&$queryObjects, $type) {
if ($type == 'Contact') {
$queryObjects[] = new CRM_Civicase_BAO_Query_ContactLock();
}
}
/**
* Implements hook_civicrm_permission_check().
*/
function civicase_civicrm_permission_check($permission, &$granted, $contactId) {
$hooks = [
new CRM_Civicase_Hook_PermissionCheck_ActivityPageView(),
new CRM_Civicase_Hook_PermissionCheck_CaseCategory(),
];
foreach ($hooks as $hook) {
$hook->run($permission, $granted, $contactId);
}
}
/**
* Implements hook_civicrm_preProcess().
*/
function civicase_civicrm_preProcess($formName, &$form) {
$hooks = [
new CRM_Civicase_Hook_PreProcess_CaseCategoryWordReplacementsForNewCase(),
new CRM_Civicase_Hook_PreProcess_CaseCategoryWordReplacementsForChangeCase(),
new CRM_Civicase_Hook_PreProcess_AddCaseAdminSettings(),
new CRM_Civicase_Hook_PreProcess_CaseTypeCategoryWebFormRedirect(),
];
foreach ($hooks as $hook) {
$hook->run($formName, $form);
}
}
/**
* Implements hook_civicrm_summaryActions().
*/
function civicase_civicrm_summaryActions(&$actions, $contactID) {
$hooks = [
new CRM_Civicase_Hook_SummaryActions_AlterAddCaseAction(),
];
foreach ($hooks as $hook) {
$hook->run($actions, $contactID);
}
}
/**
* Implements hook_civicrm_alterAngular().
*/
function civicase_civicrm_alterAngular(Manager $angular) {
if (CRM_Core_Permission::check(
[['administer CiviCase', 'administer CiviCRM']]
)) {
$angular->add(CRM_Civicase_Hook_alterAngular_AngularChangeSet::getForCaseTypeCategoryField());
$angular->add(CRM_Civicase_Hook_alterAngular_AngularChangeSet::getForHidingNewCaseTypeButton());
}
}
/**
* Adds Case Type Category field to the Case Type entity DAO.
*
* @param array $entityTypes
* Entity types array.
*/
function _civicase_add_case_category_case_type_entity(array &$entityTypes) {
$caseTypeEntityName = isset($entityTypes['CRM_Case_DAO_CaseType']) ? 'CRM_Case_DAO_CaseType' : 'CaseType';
$entityTypes[$caseTypeEntityName]['fields_callback'][] = function ($class, &$fields) {
$fields['case_type_category'] = [
'name' => 'case_type_category',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Case Type Category'),
'description' => ts('FK to a civicrm_option_value (case_type_categories)'),
'required' => FALSE,
'where' => 'civicrm_case_type.case_type_category',
'table_name' => 'civicrm_case_type',
'entity' => 'CaseType',
'bao' => 'CRM_Case_BAO_CaseType',
'localizable' => 1,
'html' => [
'type' => 'Select',
],
'pseudoconstant' => [
'optionGroupName' => 'case_type_categories',
'optionEditPath' => 'civicrm/admin/options/case_type_categories',
],
];
};
}
/**
* Implements hook_civicrm_alterMailParams().
*/
function civicase_civicrm_alterMailParams(&$params, $context) {
$hooks = [
new CRM_Civicase_Hook_alterMailParams_SubjectCaseTypeCategoryProcessor(),
];
foreach ($hooks as &$hook) {
$hook->run($params, $context);
}
}