Merge pull request #15649 from JMAConsulting/core-1346
[civicrm-core.git] / CRM / Case / Form / Task / Batch.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17 /**
18 * This class provides the functionality for batch profile update for cases
19 */
20 class CRM_Case_Form_Task_Batch extends CRM_Core_Form_Task_Batch {
21
22 /**
23 * Must be set to entity table name (eg. civicrm_participant) by child class
24 * @var string
25 */
26 public static $tableName = 'civicrm_case';
27 /**
28 * Must be set to entity shortname (eg. event)
29 * @var string
30 */
31 public static $entityShortname = 'case';
32
33 /**
34 * Process the form after the input has been submitted and validated.
35 *
36 * @return void
37 */
38 public function postProcess() {
39 $params = $this->exportValues();
40
41 if (!isset($params['field'])) {
42 CRM_Core_Session::setStatus(ts('No updates have been saved.'), ts('Not Saved'), 'alert');
43 return;
44 }
45
46 $customFields = [];
47 $dateFields = [
48 'case_created_date',
49 'case_start_date',
50 'case_end_date',
51 'case_modified_date',
52 ];
53 foreach ($params['field'] as $key => $value) {
54 $value['id'] = $key;
55
56 if (!empty($value['case_type'])) {
57 $caseTypeId = $value['case_type_id'] = $value['case_type'][1];
58 }
59 unset($value['case_type']);
60
61 // Get the case status
62 $daoClass = 'CRM_Case_DAO_Case';
63 $caseStatus = CRM_Utils_Array::value('case_status', $value);
64 if (!$caseStatus) {
65 // default to existing status ID
66 $caseStatus = CRM_Core_DAO::getFieldValue($daoClass, $key, 'status_id');
67 }
68 $value['status_id'] = $caseStatus;
69 unset($value['case_status']);
70
71 foreach ($dateFields as $val) {
72 if (isset($value[$val])) {
73 $value[$val] = CRM_Utils_Date::processDate($value[$val]);
74 }
75 }
76 if (empty($customFields)) {
77 if (empty($value['case_type_id'])) {
78 $caseTypeId = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_Case', $key, 'case_type_id');
79 }
80
81 // case type custom data
82 $customFields = CRM_Core_BAO_CustomField::getFields('Case', FALSE, FALSE, $caseTypeId);
83
84 $customFields = CRM_Utils_Array::crmArrayMerge($customFields,
85 CRM_Core_BAO_CustomField::getFields('Case',
86 FALSE, FALSE, NULL, NULL, TRUE
87 )
88 );
89 }
90 //check for custom data
91 // @todo extract submit functions &
92 // extend CRM_Event_Form_Task_BatchTest::testSubmit with a data provider to test
93 // handling of custom data, specifically checkbox fields.
94 $value['custom'] = CRM_Core_BAO_CustomField::postProcess($params['field'][$key],
95 $key,
96 'Case',
97 $caseTypeId
98 );
99
100 $case = CRM_Case_BAO_Case::add($value);
101
102 // add custom field values
103 if (!empty($value['custom']) && is_array($value['custom'])) {
104 CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_case', $case->id);
105 }
106 }
107
108 CRM_Core_Session::setStatus(ts('Your updates have been saved.'), ts('Saved'), 'success');
109 }
110
111 }