Merge pull request #22599 from civicrm/5.46
[civicrm-core.git] / CRM / Case / Form / CustomData.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 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for custom data
20 *
21 * It delegates the work to lower level subclasses and integrates the changes
22 * back in. It also uses a lot of functionality with the CRM API's, so any change
23 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
24 */
25 class CRM_Case_Form_CustomData extends CRM_Core_Form {
26
27 /**
28 * The entity id, used when editing/creating custom data
29 *
30 * @var int
31 */
32 protected $_entityID;
33
34 /**
35 * Entity sub type of the table id.
36 *
37 * @var string
38 */
39 protected $_subTypeID;
40
41 /**
42 * Pre processing work done here.
43 *
44 * gets session variables for table name, id of entity in table, type of entity and stores them.
45 */
46 public function preProcess() {
47 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
48 $this->_entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', $this, TRUE);
49 $this->_subTypeID = CRM_Utils_Request::retrieve('subType', 'Positive', $this, TRUE);
50 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
51
52 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Case',
53 NULL,
54 $this->_entityID,
55 $this->_groupID,
56 $this->_subTypeID
57 );
58 // simplified formatted groupTree
59 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this);
60 // Array contains only one item
61 foreach ($groupTree as $groupValues) {
62 $this->_customTitle = $groupValues['title'];
63 $this->setTitle(ts('Edit %1', [1 => $groupValues['title']]));
64 }
65
66 $this->_defaults = [];
67 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $this->_defaults);
68 $this->setDefaults($this->_defaults);
69
70 CRM_Core_BAO_CustomGroup::buildQuickForm($this, $groupTree);
71
72 //need to assign custom data type and subtype to the template
73 $this->assign('entityID', $this->_entityID);
74 $this->assign('groupID', $this->_groupID);
75 $this->assign('subType', $this->_subTypeID);
76 $this->assign('contactID', $this->_contactID);
77 $this->assign('cgCount');
78 }
79
80 /**
81 * Build the form object.
82 */
83 public function buildQuickForm() {
84 // make this form an upload since we dont know if the custom data injected dynamically
85 // is of type file etc
86 $this->addButtons([
87 [
88 'type' => 'upload',
89 'name' => ts('Save'),
90 'isDefault' => TRUE,
91 ],
92 [
93 'type' => 'cancel',
94 'name' => ts('Cancel'),
95 ],
96 ]);
97 }
98
99 /**
100 * Process the user submitted custom data values.
101 */
102 public function postProcess() {
103 $params = $this->controller->exportValues($this->_name);
104
105 $transaction = new CRM_Core_Transaction();
106
107 CRM_Core_BAO_CustomValueTable::postProcess($params,
108 'civicrm_case',
109 $this->_entityID,
110 'Case'
111 );
112
113 $session = CRM_Core_Session::singleton();
114 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/case', "reset=1&id={$this->_entityID}&cid={$this->_contactID}&action=view"));
115
116 $formattedDetails = $this->formatCustomDataChangesForDetail($params);
117 if (!empty($formattedDetails)) {
118 $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Custom Data');
119 $activityParams = [
120 'activity_type_id' => $activityTypeID,
121 'source_contact_id' => $session->get('userID'),
122 'is_auto' => TRUE,
123 'subject' => $this->_customTitle . " : change data",
124 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
125 'target_contact_id' => $this->_contactID,
126 'details' => $formattedDetails,
127 'activity_date_time' => date('YmdHis'),
128 ];
129 $activity = CRM_Activity_BAO_Activity::create($activityParams);
130
131 $caseParams = [
132 'activity_id' => $activity->id,
133 'case_id' => $this->_entityID,
134 ];
135 CRM_Case_BAO_Case::processCaseActivity($caseParams);
136 }
137
138 $transaction->commit();
139 }
140
141 /**
142 * Format the custom data changes as [label]: [old value] => [new value]
143 *
144 * @param array $params New custom field values from form
145 *
146 * @return string
147 * @throws \CiviCRM_API3_Exception
148 */
149 public function formatCustomDataChangesForDetail($params) {
150 $formattedDetails = [];
151 foreach ($params as $customField => $newCustomValue) {
152 if (substr($customField, 0, 7) === 'custom_') {
153 if (($this->_defaults[$customField] ?? '') === $newCustomValue) {
154 // Don't show values that did not change
155 continue;
156 }
157 // We need custom field ID from custom_XX_1
158 list($_, $customFieldId, $_) = explode('_', $customField);
159
160 if (!empty($customFieldId) && is_numeric($customFieldId)) {
161 // Got a custom field ID
162 $label = civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldId, 'return' => 'label']);
163
164 // Convert dropdown and other machine values to human labels.
165 // Money is special for non-US locales because at this point it's in human format so we don't
166 // want to try to convert it.
167 $oldValue = $this->_defaults[$customField] ?? '';
168 $newValue = $newCustomValue;
169 if ('Money' !== civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldId, 'return' => 'data_type'])) {
170 $oldValue = civicrm_api3('CustomValue', 'getdisplayvalue', [
171 'custom_field_id' => $customFieldId,
172 'entity_id' => $this->_entityID,
173 'custom_field_value' => $oldValue,
174 ]);
175 $oldValue = $oldValue['values'][$customFieldId]['display'];
176 $newValue = civicrm_api3('CustomValue', 'getdisplayvalue', [
177 'custom_field_id' => $customFieldId,
178 'entity_id' => $this->_entityID,
179 'custom_field_value' => $newCustomValue,
180 ]);
181 $newValue = $newValue['values'][$customFieldId]['display'];
182 }
183 $formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue;
184 }
185
186 }
187 }
188
189 return implode('<br/>', $formattedDetails);
190 }
191
192 }