-
Notifications
You must be signed in to change notification settings - Fork 35
/
civicase.php
557 lines (515 loc) · 18.9 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
require_once 'civicase.civix.php';
use CRM_Civicase_ExtensionUtil as E;
/**
* Implements hook_civicrm_tabset().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_tabset
*/
function civicase_civicrm_tabset($tabsetName, &$tabs, $context) {
$useAng = FALSE;
switch ($tabsetName) {
case 'civicrm/contact/view':
$caseTabPresent = FALSE;
foreach ($tabs as &$tab) {
if ($tab['id'] === 'case') {
$caseTabPresent = TRUE;
$useAng = TRUE;
$tab['url'] = CRM_Utils_System::url('civicrm/case/contact-case-tab', array(
'cid' => $context['contact_id'],
));
}
if ($tab['id'] === 'activity') {
$useAng = TRUE;
$tab['url'] = CRM_Utils_System::url('civicrm/case/contact-act-tab', array(
'cid' => $context['contact_id'],
));
}
}
if (!$caseTabPresent && CRM_Core_Permission::check('basic case information')) {
$useAng = TRUE;
$tabs[] = array(
'id' => 'case',
'url' => CRM_Utils_System::url('civicrm/case/contact-case-tab', array(
'cid' => $context['contact_id'],
)),
'title' => E::ts('Cases'),
'weight' => 20,
'count' => CRM_Contact_BAO_Contact::getCountComponent('case', $context['contact_id']),
'class' => 'livePage',
);
}
break;
}
if ($useAng) {
$loader = \Civi::service('angularjs.loader');
$loader->setModules(array('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', array('CRM_Civicase_ActivityFilter', 'onPrepare'), 10);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
*/
function civicase_civicrm_xmlMenu(&$files) {
_civicase_civix_civicrm_xmlMenu($files);
}
/**
* 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_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function civicase_civicrm_postInstall() {
_civicase_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function civicase_civicrm_uninstall() {
_civicase_civix_civicrm_uninstall();
}
/**
* 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_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function civicase_civicrm_disable() {
_civicase_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function civicase_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _civicase_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements 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 civicase_civicrm_managed(&$entities) {
_civicase_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function civicase_civicrm_caseTypes(&$caseTypes) {
_civicase_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
*/
function civicase_civicrm_angularModules(&$angularModules) {
_civicase_civix_civicrm_angularModules($angularModules);
}
/**
* 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/activity/pdf/add']['ids_arguments']['json'][] = 'civicase_reload';
$items['civicrm/case/cd/edit']['ids_arguments']['json'][] = 'civicase_reload';
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
*/
function civicase_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_civicase_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_buildForm().
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function civicase_civicrm_buildForm($formName, &$form) {
// Display category option for activity types and activity statuses
if ($formName == 'CRM_Admin_Form_Options' && in_array($form->getVar('_gName'), array('activity_type', 'activity_status'))) {
$options = civicrm_api3('optionValue', 'get', array(
'option_group_id' => 'activity_category',
'is_active' => 1,
'options' => array('limit' => 0, 'sort' => 'weight'),
));
$opts = array();
if ($form->getVar('_gName') == 'activity_status') {
$placeholder = E::ts('All');
// Activity status can also apply to uncategorized activities
$opts[] = array(
'id' => 'none',
'text' => E::ts('Uncategorized'),
);
}
else {
$placeholder = E::ts('Uncategorized');
}
foreach ($options['values'] as $opt) {
$opts[] = array(
'id' => $opt['name'],
'text' => $opt['label'],
);
}
$form->add('select2', 'grouping', E::ts('Activity Category'), $opts, FALSE, array('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', array(
'return' => 'grouping',
'option_group_id' => "activity_type",
'value' => $form->_activityTypeId,
));
$cat = !empty($cat['grouping']) ? explode(',', $cat['grouping']) : array('none');
$options = civicrm_api3('OptionValue', 'get', array(
'return' => array('label', 'value', 'grouping'),
'option_group_id' => "activity_status",
'options' => array('limit' => 0, 'sort' => 'weight'),
));
$newOptions = $el->_options = array();
$newOptions[''] = E::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);
}
// Add save draft button to Communication activities
$specialForms = array('CRM_Contact_Form_Task_PDF', 'CRM_Contact_Form_Task_Email');
$specialTypes = array('Print PDF Letter', 'Email');
if (is_a($form, 'CRM_Activity_Form_Activity') || in_array($formName, $specialForms)) {
$activityTypeId = $form->getVar('_activityTypeId');
if ($activityTypeId) {
$activityType = civicrm_api3('OptionValue', 'getvalue', array(
'return' => "name",
'option_group_id' => "activity_type",
'value' => $activityTypeId,
));
}
else {
$activityType = $formName == 'CRM_Contact_Form_Task_PDF' ? 'Print PDF Letter' : 'Email';
}
$id = $form->getVar('_activityId');
$status = NULL;
if ($id) {
$status = civicrm_api3('Activity', 'getsingle', array('id' => $id, 'return' => 'status_id.name'));
$status = $status['status_id.name'];
}
$checkParams = array('option_group_id' => 'activity_type', 'grouping' => array('LIKE' => '%communication%'), 'value' => $activityTypeId);
if (in_array($activityType, $specialTypes) || ($activityTypeId && civicrm_api3('OptionValue', 'getcount', $checkParams))) {
if ($form->_action & (CRM_Core_Action::ADD + CRM_Core_Action::UPDATE)) {
$buttonGroup = $form->getElement('buttons');
$buttons = $buttonGroup->getElements();
$buttons[] = $form->createElement('submit', $form->getButtonName('refresh'), E::ts('Save Draft'), array(
'crm-icon' => 'fa-pencil-square-o',
'class' => 'crm-form-submit',
));
$buttonGroup->setElements($buttons);
$form->addGroup($buttons, 'buttons');
$form->setDefaults(array('status_id' => 2));
}
if ($status == 'Draft' && ($form->_action & CRM_Core_Action::VIEW)) {
if (in_array($activityType, $specialTypes)) {
$atype = $activityType == 'Email' ? 'email' : 'pdf';
$caseId = civicrm_api3('Activity', 'getsingle', array('id' => $id, 'return' => 'case_id'));
$composeUrl = CRM_Utils_System::url("civicrm/activity/$atype/add", array(
'action' => 'add',
'reset' => 1,
'caseId' => $caseId['case_id'][0],
'context' => 'standalone',
'draft_id' => $id,
));
$buttonMarkup = '<a class="button" href="' . $composeUrl . '"><i class="crm-i fa-pencil-square-o"></i> ' . E::ts('Continue Editing') . '</a>';
$form->assign('activityTypeDescription', $buttonMarkup);
}
else {
$form->assign('activityTypeDescription', '<i class="crm-i fa-pencil-square-o"></i> ' . E::ts('Saved as a Draft'));
}
}
}
// Form email/print activities, set defaults from the original draft activity (which will be deleted on submit)
if (in_array($formName, $specialForms) && !empty($_GET['draft_id'])) {
$draft = civicrm_api3('Activity', 'get', array('id' => $_GET['draft_id'], 'check_permissions' => TRUE, 'sequential' => TRUE));
$form->setVar('_activityId', $_GET['draft_id']);
if (isset($draft['values'][0])) {
$draft = $draft['values'][0];
if (in_array($formName, $specialForms)) {
$draft['html_message'] = CRM_Utils_Array::value('details', $draft);
}
// Set defaults for to email addresses
if ($formName == 'CRM_Contact_Form_Task_Email') {
$cids = CRM_Utils_Array::value('target_contact_id', civicrm_api3('Activity', 'getsingle', array('id' => $draft['id'], 'return' => 'target_contact_id')));
if ($cids) {
$toContacts = civicrm_api3('Contact', 'get', array('id' => array('IN' => $cids), 'return' => array('email', 'sort_name')));
$toArray = array();
foreach ($toContacts['values'] as $cid => $contact) {
$toArray[] = array(
'text' => '"' . $contact['sort_name'] . '" <' . $contact['email'] . '>',
'id' => "$cid::{$contact['email']}",
);
}
$form->assign('toContact', json_encode($toArray));
}
}
$form->setDefaults($draft);
}
}
}
}
/**
* Implements hook_civicrm_validateForm().
*
* @param string $formName
* @param array $fields
* @param array $files
* @param CRM_Core_Form $form
* @param array $errors
*/
function civicase_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
// Save draft feature
// The validate stage provides an opportunity to bypass normal form processing, save the draft & return early
$specialForms = array('CRM_Contact_Form_Task_PDF', 'CRM_Contact_Form_Task_Email');
if (is_a($form, 'CRM_Activity_Form_Activity') || in_array($formName, $specialForms)) {
if (array_key_exists($form->getButtonName('refresh'), $fields['buttons'])) {
$activityType = $form->getVar('_activityTypeId');
$caseId = $form->getVar('_caseId');
if (!$activityType) {
$activityType = $formName == 'CRM_Contact_Form_Task_PDF' ? 'Print PDF Letter' : 'Email';
}
$params = array(
'activity_type_id' => $activityType,
'status_id' => 'Draft',
'case_id' => $caseId,
'id' => $form->getVar('_activityId'),
);
if (in_array($formName, $specialForms)) {
$params['details'] = CRM_Utils_Array::value('html_message', $fields);
}
if ($formName == 'CRM_Contact_Form_Task_Email') {
$params['target_contact_id'] = explode(',', CRM_Utils_Array::value('to', $fields));
$params['target_contact_id'] = array_map('intval', $params['target_contact_id']);
}
$newActivity = civicrm_api3('Activity', 'create', $params + $fields);
$url = CRM_Utils_System::url('civicrm/contact/view/case',
"reset=1&action=view&cid={$form->_currentlyViewedContactId}&id={$caseId}&show=1"
);
$session = CRM_Core_Session::singleton();
$session->pushUserContext($url);
CRM_Core_Session::setStatus('Activity saved as a draft', E::ts('Saved'), 'success');
if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
$response = array();
if (!empty($form->civicase_reload)) {
$api = civicrm_api3('Case', 'getdetails', array('check_permissions' => 1) + $form->civicase_reload);
$response['civicase_reload'] = $api['values'];
}
CRM_Core_Page_AJAX::returnJsonResponse($response);
}
CRM_Utils_System::redirect($url);
}
}
}
/**
* Implements hook_civicrm_postProcess().
* @param string $formName
* @param CRM_Core_Form $form
* @throws \CiviCRM_API3_Exception
*/
function civicase_civicrm_postProcess($formName, &$form) {
if (!empty($form->civicase_reload)) {
$api = civicrm_api3('Case', 'getdetails', array('check_permissions' => 1) + $form->civicase_reload);
$form->ajaxResponse['civicase_reload'] = $api['values'];
}
// When emailing/printing - delete draft.
$specialForms = array('CRM_Contact_Form_Task_PDF', 'CRM_Contact_Form_Task_Email');
if (in_array($formName, $specialForms)) {
$urlParams = parse_url(htmlspecialchars_decode($form->controller->_entryURL), PHP_URL_QUERY);
parse_str($urlParams, $urlParams);
if (!empty($urlParams['draft_id'])) {
civicrm_api3('Activity', 'delete', array('id' => $urlParams['draft_id']));
}
}
}
/**
* Implements hook_civicrm_permission()
*
* @param array $permissions
* Array of permissions defined on extensions
*/
function civicase_civicrm_permission(&$permissions) {
$permissions['basic case information'] = array(
'Civicase: basic case information',
E::ts('Allows a user to view only basic information of cases.')
);
}
/**
* Implements hook_civicrm_apiWrappers
*/
function civicase_civicrm_apiWrappers(&$wrappers, $apiRequest) {
if ($apiRequest['entity'] == 'Case') {
$wrappers[] = new CRM_Civicase_APIHelpers_CaseList();
}
}
/**
* Implements hook_civicrm_alterAPIPermissions().
*
* @link https://docs.civicrm.org/dev/en/master/hooks/hook_civicrm_alterAPIPermissions/
*/
function civicase_civicrm_alterAPIPermissions($entity, $action, &$params, &$permissions) {
$permissions['case']['getfiles'] = array(
array('access my cases and activities', 'access all cases and activities'),
'access uploaded files',
);
$permissions['case']['get'] = array(
array(
'access my cases and activities',
'access all cases and activities',
'basic case information',
)
);
$permissions['case']['getcount'] = array(
array(
'access my cases and activities',
'access all cases and activities',
'basic case information',
)
);
$permissions['case_type']['get'] = $permissions['casetype']['getcount'] = array(
array(
'access my cases and activities',
'access all cases and activities',
'basic case information',
)
);
}
/**
* Implements hook_civicrm_pageRun().
*/
function civicase_civicrm_pageRun(&$page) {
if ($page instanceof CRM_Case_Page_Tab) {
// OLD: http://localhost/civicrm/contact/view/case?reset=1&action=view&cid=129&id=51
// NEW: http://localhost/civicrm/case/a/#/case/list?sf=contact_id.sort_name&sd=ASC&focus=0&cf=%7B%7D&caseId=51&tab=summary&sx=0
$caseId = CRM_Utils_Request::retrieve('id', 'Positive');
if ($caseId) {
$case = civicrm_api3('Case', 'getsingle', array(
'id' => $caseId,
'return' => array('case_type_id.name', 'status_id.name'),
));
$url = CRM_Utils_System::url('civicrm/case/a/', NULL, TRUE,
"/case/list?sf=id&sd=DESC&caseId={$caseId}&cf=%7B%22status_id%22:%5B%22{$case['status_id.name']}%22%5D,%22case_type_id%22:%5B%22{$case['case_type_id.name']}%22%5D%7D",
FALSE);
CRM_Utils_System::redirect($url);
}
}
}
/**
* 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) {
/**
* @var array
* Array(string $oldUrl => string $newUrl).
*/
$rewriteMap = array(
'civicrm/case?reset=1' => 'civicrm/case/a/#/case',
'civicrm/case/search?reset=1' => 'civicrm/case/a/#/case/list?sx=1',
);
_civicase_menu_walk($menu, function(&$item) use ($rewriteMap) {
if (isset($item['url']) && isset($rewriteMap[$item['url']])) {
$item['url'] = $rewriteMap[$item['url']];
}
});
}
/**
* Visit every link in the navigation menu, and alter it using $callback.
*
* @param array $menu
* Tree of menu items, per hook_civicrm_navigationMenu.
* @param callable $callback
* Function(&$item).
*/
function _civicase_menu_walk(&$menu, $callback) {
foreach (array_keys($menu) as $key) {
if (isset($menu[$key]['attributes'])) {
$callback($menu[$key]['attributes']);
}
if (isset($menu[$key]['child'])) {
_civicase_menu_walk($menu[$key]['child'], $callback);
}
}
}
/**
* Implements hook_civicrm_selectWhereClause
*/
function civicase_civicrm_selectWhereClause($entity, &$clauses) {
if ($entity === 'Case' && CRM_Core_Permission::check('basic case information')) {
unset($clauses['id']);
}
}