Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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. | |
6a488035 TO |
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 | ||
6a488035 | 34 | /** |
fe482240 | 35 | * Entity sub type of the table id. |
6a488035 TO |
36 | * |
37 | * @var string | |
6a488035 TO |
38 | */ |
39 | protected $_subTypeID; | |
40 | ||
41 | /** | |
100fef9d | 42 | * Pre processing work done here. |
6a488035 TO |
43 | * |
44 | * gets session variables for table name, id of entity in table, type of entity and stores them. | |
6a488035 | 45 | */ |
00be9182 | 46 | public function preProcess() { |
353ffa53 TO |
47 | $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE); |
48 | $this->_entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', $this, TRUE); | |
6a488035 TO |
49 | $this->_subTypeID = CRM_Utils_Request::retrieve('subType', 'Positive', $this, TRUE); |
50 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); | |
51 | ||
517755e0 | 52 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Case', |
0b330e6d | 53 | NULL, |
6a488035 TO |
54 | $this->_entityID, |
55 | $this->_groupID, | |
56 | $this->_subTypeID | |
57 | ); | |
58 | // simplified formatted groupTree | |
59 | $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this); | |
e08129f7 | 60 | // Array contains only one item |
6a488035 TO |
61 | foreach ($groupTree as $groupValues) { |
62 | $this->_customTitle = $groupValues['title']; | |
be2fb01f | 63 | CRM_Utils_System::setTitle(ts('Edit %1', [1 => $groupValues['title']])); |
6a488035 TO |
64 | } |
65 | ||
be2fb01f | 66 | $this->_defaults = []; |
6a488035 TO |
67 | CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $this->_defaults); |
68 | $this->setDefaults($this->_defaults); | |
69 | ||
7b226831 | 70 | CRM_Core_BAO_CustomGroup::buildQuickForm($this, $groupTree); |
6a488035 TO |
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 | } | |
78 | ||
79 | /** | |
fe482240 | 80 | * Build the form object. |
6a488035 TO |
81 | */ |
82 | public function buildQuickForm() { | |
83 | // make this form an upload since we dont know if the custom data injected dynamically | |
84 | // is of type file etc | |
be2fb01f | 85 | $this->addButtons([ |
5d4fcf54 TO |
86 | [ |
87 | 'type' => 'upload', | |
88 | 'name' => ts('Save'), | |
89 | 'isDefault' => TRUE, | |
90 | ], | |
91 | [ | |
92 | 'type' => 'cancel', | |
93 | 'name' => ts('Cancel'), | |
94 | ], | |
95 | ]); | |
6a488035 TO |
96 | } |
97 | ||
98 | /** | |
99 | * Process the user submitted custom data values. | |
6a488035 TO |
100 | */ |
101 | public function postProcess() { | |
102 | $params = $this->controller->exportValues($this->_name); | |
6a488035 TO |
103 | |
104 | $transaction = new CRM_Core_Transaction(); | |
105 | ||
106 | CRM_Core_BAO_CustomValueTable::postProcess($params, | |
6a488035 TO |
107 | 'civicrm_case', |
108 | $this->_entityID, | |
109 | 'Case' | |
110 | ); | |
111 | ||
112 | $session = CRM_Core_Session::singleton(); | |
113 | $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/case', "reset=1&id={$this->_entityID}&cid={$this->_contactID}&action=view")); | |
114 | ||
9c248a42 | 115 | $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Custom Data'); |
be2fb01f | 116 | $activityParams = [ |
6a488035 TO |
117 | 'activity_type_id' => $activityTypeID, |
118 | 'source_contact_id' => $session->get('userID'), | |
119 | 'is_auto' => TRUE, | |
120 | 'subject' => $this->_customTitle . " : change data", | |
9c248a42 | 121 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
6a488035 | 122 | 'target_contact_id' => $this->_contactID, |
6437dda5 | 123 | 'details' => $this->formatCustomDataChangesForDetail($params), |
6a488035 | 124 | 'activity_date_time' => date('YmdHis'), |
be2fb01f | 125 | ]; |
6a488035 TO |
126 | $activity = CRM_Activity_BAO_Activity::create($activityParams); |
127 | ||
be2fb01f | 128 | $caseParams = [ |
6a488035 TO |
129 | 'activity_id' => $activity->id, |
130 | 'case_id' => $this->_entityID, | |
be2fb01f | 131 | ]; |
6a488035 TO |
132 | CRM_Case_BAO_Case::processCaseActivity($caseParams); |
133 | ||
134 | $transaction->commit(); | |
135 | } | |
96025800 | 136 | |
6437dda5 MWMC |
137 | /** |
138 | * Format the custom data changes as [label]: [old value] => [new value] | |
139 | * | |
140 | * @param array $params New custom field values from form | |
141 | * | |
142 | * @return string | |
143 | * @throws \CiviCRM_API3_Exception | |
144 | */ | |
145 | public function formatCustomDataChangesForDetail($params) { | |
146 | $formattedDetails = []; | |
147 | foreach ($params as $customField => $newCustomValue) { | |
148 | if (substr($customField, 0, 7) == 'custom_') { | |
149 | if ($this->_defaults[$customField] == $newCustomValue) { | |
150 | // Don't show values that did not change | |
151 | continue; | |
152 | } | |
153 | // We need custom field ID from custom_XX_1 | |
154 | list($_, $customFieldId, $_) = explode('_', $customField); | |
155 | ||
156 | if (!empty($customFieldId) && is_numeric($customFieldId)) { | |
157 | // Got a custom field ID | |
158 | $label = civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldId, 'return' => 'label']); | |
159 | $oldValue = civicrm_api3('CustomValue', 'getdisplayvalue', [ | |
160 | 'custom_field_id' => $customFieldId, | |
161 | 'entity_id' => $this->_entityID, | |
162 | 'custom_field_value' => $this->_defaults[$customField], | |
163 | ]); | |
164 | $oldValue = $oldValue['values'][$customFieldId]['display']; | |
165 | $newValue = civicrm_api3('CustomValue', 'getdisplayvalue', [ | |
166 | 'custom_field_id' => $customFieldId, | |
167 | 'entity_id' => $this->_entityID, | |
168 | 'custom_field_value' => $newCustomValue, | |
169 | ]); | |
170 | $newValue = $newValue['values'][$customFieldId]['display']; | |
171 | $formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue; | |
172 | } | |
173 | ||
174 | } | |
175 | } | |
176 | ||
177 | return implode('<br/>', $formattedDetails); | |
178 | } | |
179 | ||
6a488035 | 180 | } |