Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class generates form components for custom data | |
36 | * | |
37 | * It delegates the work to lower level subclasses and integrates the changes | |
38 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
39 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
6a488035 TO |
40 | */ |
41 | class CRM_Case_Form_CustomData extends CRM_Core_Form { | |
42 | ||
43 | /** | |
44 | * The entity id, used when editing/creating custom data | |
45 | * | |
46 | * @var int | |
47 | */ | |
48 | protected $_entityID; | |
49 | ||
6a488035 | 50 | /** |
fe482240 | 51 | * Entity sub type of the table id. |
6a488035 TO |
52 | * |
53 | * @var string | |
6a488035 TO |
54 | */ |
55 | protected $_subTypeID; | |
56 | ||
57 | /** | |
100fef9d | 58 | * Pre processing work done here. |
6a488035 TO |
59 | * |
60 | * gets session variables for table name, id of entity in table, type of entity and stores them. | |
6a488035 | 61 | */ |
00be9182 | 62 | public function preProcess() { |
353ffa53 TO |
63 | $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE); |
64 | $this->_entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', $this, TRUE); | |
6a488035 TO |
65 | $this->_subTypeID = CRM_Utils_Request::retrieve('subType', 'Positive', $this, TRUE); |
66 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); | |
67 | ||
517755e0 | 68 | $groupTree = CRM_Core_BAO_CustomGroup::getTree('Case', |
0b330e6d | 69 | NULL, |
6a488035 TO |
70 | $this->_entityID, |
71 | $this->_groupID, | |
72 | $this->_subTypeID | |
73 | ); | |
74 | // simplified formatted groupTree | |
75 | $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this); | |
e08129f7 | 76 | // Array contains only one item |
6a488035 TO |
77 | foreach ($groupTree as $groupValues) { |
78 | $this->_customTitle = $groupValues['title']; | |
be2fb01f | 79 | CRM_Utils_System::setTitle(ts('Edit %1', [1 => $groupValues['title']])); |
6a488035 TO |
80 | } |
81 | ||
be2fb01f | 82 | $this->_defaults = []; |
6a488035 TO |
83 | CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $this->_defaults); |
84 | $this->setDefaults($this->_defaults); | |
85 | ||
7b226831 | 86 | CRM_Core_BAO_CustomGroup::buildQuickForm($this, $groupTree); |
6a488035 TO |
87 | |
88 | //need to assign custom data type and subtype to the template | |
89 | $this->assign('entityID', $this->_entityID); | |
90 | $this->assign('groupID', $this->_groupID); | |
91 | $this->assign('subType', $this->_subTypeID); | |
92 | $this->assign('contactID', $this->_contactID); | |
93 | } | |
94 | ||
95 | /** | |
fe482240 | 96 | * Build the form object. |
6a488035 TO |
97 | */ |
98 | public function buildQuickForm() { | |
99 | // make this form an upload since we dont know if the custom data injected dynamically | |
100 | // is of type file etc | |
be2fb01f | 101 | $this->addButtons([ |
5d4fcf54 TO |
102 | [ |
103 | 'type' => 'upload', | |
104 | 'name' => ts('Save'), | |
105 | 'isDefault' => TRUE, | |
106 | ], | |
107 | [ | |
108 | 'type' => 'cancel', | |
109 | 'name' => ts('Cancel'), | |
110 | ], | |
111 | ]); | |
6a488035 TO |
112 | } |
113 | ||
114 | /** | |
115 | * Process the user submitted custom data values. | |
6a488035 TO |
116 | */ |
117 | public function postProcess() { | |
118 | $params = $this->controller->exportValues($this->_name); | |
6a488035 TO |
119 | |
120 | $transaction = new CRM_Core_Transaction(); | |
121 | ||
122 | CRM_Core_BAO_CustomValueTable::postProcess($params, | |
6a488035 TO |
123 | 'civicrm_case', |
124 | $this->_entityID, | |
125 | 'Case' | |
126 | ); | |
127 | ||
128 | $session = CRM_Core_Session::singleton(); | |
129 | $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/case', "reset=1&id={$this->_entityID}&cid={$this->_contactID}&action=view")); | |
130 | ||
9c248a42 | 131 | $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Custom Data'); |
be2fb01f | 132 | $activityParams = [ |
6a488035 TO |
133 | 'activity_type_id' => $activityTypeID, |
134 | 'source_contact_id' => $session->get('userID'), | |
135 | 'is_auto' => TRUE, | |
136 | 'subject' => $this->_customTitle . " : change data", | |
9c248a42 | 137 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
6a488035 | 138 | 'target_contact_id' => $this->_contactID, |
6437dda5 | 139 | 'details' => $this->formatCustomDataChangesForDetail($params), |
6a488035 | 140 | 'activity_date_time' => date('YmdHis'), |
be2fb01f | 141 | ]; |
6a488035 TO |
142 | $activity = CRM_Activity_BAO_Activity::create($activityParams); |
143 | ||
be2fb01f | 144 | $caseParams = [ |
6a488035 TO |
145 | 'activity_id' => $activity->id, |
146 | 'case_id' => $this->_entityID, | |
be2fb01f | 147 | ]; |
6a488035 TO |
148 | CRM_Case_BAO_Case::processCaseActivity($caseParams); |
149 | ||
150 | $transaction->commit(); | |
151 | } | |
96025800 | 152 | |
6437dda5 MWMC |
153 | /** |
154 | * Format the custom data changes as [label]: [old value] => [new value] | |
155 | * | |
156 | * @param array $params New custom field values from form | |
157 | * | |
158 | * @return string | |
159 | * @throws \CiviCRM_API3_Exception | |
160 | */ | |
161 | public function formatCustomDataChangesForDetail($params) { | |
162 | $formattedDetails = []; | |
163 | foreach ($params as $customField => $newCustomValue) { | |
164 | if (substr($customField, 0, 7) == 'custom_') { | |
165 | if ($this->_defaults[$customField] == $newCustomValue) { | |
166 | // Don't show values that did not change | |
167 | continue; | |
168 | } | |
169 | // We need custom field ID from custom_XX_1 | |
170 | list($_, $customFieldId, $_) = explode('_', $customField); | |
171 | ||
172 | if (!empty($customFieldId) && is_numeric($customFieldId)) { | |
173 | // Got a custom field ID | |
174 | $label = civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldId, 'return' => 'label']); | |
175 | $oldValue = civicrm_api3('CustomValue', 'getdisplayvalue', [ | |
176 | 'custom_field_id' => $customFieldId, | |
177 | 'entity_id' => $this->_entityID, | |
178 | 'custom_field_value' => $this->_defaults[$customField], | |
179 | ]); | |
180 | $oldValue = $oldValue['values'][$customFieldId]['display']; | |
181 | $newValue = civicrm_api3('CustomValue', 'getdisplayvalue', [ | |
182 | 'custom_field_id' => $customFieldId, | |
183 | 'entity_id' => $this->_entityID, | |
184 | 'custom_field_value' => $newCustomValue, | |
185 | ]); | |
186 | $newValue = $newValue['values'][$customFieldId]['display']; | |
187 | $formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue; | |
188 | } | |
189 | ||
190 | } | |
191 | } | |
192 | ||
193 | return implode('<br/>', $formattedDetails); | |
194 | } | |
195 | ||
6a488035 | 196 | } |