-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathCustomData.php
204 lines (180 loc) · 6.72 KB
/
CustomData.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
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
/**
* This class generates form components for custom data
*
* It delegates the work to lower level subclasses and integrates the changes
* back in. It also uses a lot of functionality with the CRM API's, so any change
* made here could potentially affect the API etc. Be careful, be aware, use unit tests.
*/
class CRM_Case_Form_CustomData extends CRM_Core_Form {
/**
* The entity id, used when editing/creating custom data
*
* @var int
*/
protected $_entityID;
/**
* Entity sub type of the table id.
*
* @var string
*/
protected $_subTypeID;
/**
* @var string
*/
private $customGroupTitle;
/**
* Pre processing work done here.
*
* gets session variables for table name, id of entity in table, type of
* entity and stores them.
*
* @throws \CRM_Core_Exception
*/
public function preProcess(): void {
$groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
$this->_entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', $this, TRUE);
$this->_subTypeID = CRM_Utils_Request::retrieve('subType', 'Positive', $this, TRUE);
$contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
$groupTree = CRM_Core_BAO_CustomGroup::getTree('Case',
NULL,
$this->_entityID,
$groupID,
$this->_subTypeID
);
// simplified formatted groupTree
$groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this);
// Array contains only one item
foreach ($groupTree as $groupValues) {
$this->customGroupTitle = $groupValues['title'];
$this->setTitle(ts('Edit %1', [1 => $groupValues['title']]));
}
$this->_defaults = [];
CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $this->_defaults);
$this->setDefaults($this->_defaults);
CRM_Core_BAO_CustomGroup::buildQuickForm($this, $groupTree);
//need to assign custom data type and subtype to the template
$this->assign('entityID', $this->_entityID);
$this->assign('groupID', $groupID);
$this->assign('subType', $this->_subTypeID);
$this->assign('contactID', $contactID);
$this->assign('cgCount');
}
/**
* Build the form object.
*/
public function buildQuickForm(): void {
// make this form an upload since we dont know if the custom data injected dynamically
// is of type file etc
$this->addButtons([
[
'type' => 'upload',
'name' => ts('Save'),
'isDefault' => TRUE,
],
[
'type' => 'cancel',
'name' => ts('Cancel'),
],
]);
}
/**
* Process the user submitted custom data values.
*
* @throws \CRM_Core_Exception
*/
public function postProcess(): void {
$params = $this->getSubmittedValues();
$transaction = new CRM_Core_Transaction();
CRM_Core_BAO_CustomValueTable::postProcess($params,
'civicrm_case',
$this->_entityID,
'Case'
);
$contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/case', "reset=1&id={$this->_entityID}&cid={$contactID}&action=view"));
$formattedDetails = $this->formatCustomDataChangesForDetail($params);
if (!empty($formattedDetails)) {
$activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Custom Data');
$activityParams = [
'activity_type_id' => $activityTypeID,
'source_contact_id' => $session->get('userID'),
'is_auto' => TRUE,
'subject' => $this->customGroupTitle . ' : change data',
'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
'target_contact_id' => $contactID,
'details' => $formattedDetails,
'activity_date_time' => date('YmdHis'),
];
$activity = CRM_Activity_BAO_Activity::create($activityParams);
$caseParams = [
'activity_id' => $activity->id,
'case_id' => $this->_entityID,
];
CRM_Case_BAO_Case::processCaseActivity($caseParams);
}
$transaction->commit();
}
/**
* Format the custom data changes as [label]: [old value] => [new value]
*
* @param array $params New custom field values from form
*
* @return string
* @throws \CRM_Core_Exception
*/
public function formatCustomDataChangesForDetail(array $params): string {
$formattedDetails = [];
foreach ($params as $fieldKey => $newCustomValue) {
if (str_starts_with($fieldKey, 'custom_')) {
if (($this->_defaults[$fieldKey] ?? '') === $newCustomValue) {
// Don't show values that did not change
continue;
}
// We need custom field ID from custom_XX_1
[, $customFieldId] = explode('_', $fieldKey);
if (!empty($customFieldId) && is_numeric($customFieldId)) {
// Got a custom field ID
$customField = CRM_Core_BAO_CustomField::getField($customFieldId);
$label = $customField['label'];
// Convert dropdown and other machine values to human labels.
// Money is special for non-US locales because at this point it's in human format so we don't
// want to try to convert it.
$oldValue = $this->_defaults[$fieldKey] ?? '';
$newValue = $newCustomValue;
if ('Money' !== $customField['data_type']) {
$oldValue = civicrm_api3('CustomValue', 'getdisplayvalue', [
'custom_field_id' => $customFieldId,
'entity_id' => $this->_entityID,
'custom_field_value' => $oldValue,
]);
$oldValue = $oldValue['values'][$customFieldId]['display'];
$newValue = civicrm_api3('CustomValue', 'getdisplayvalue', [
'custom_field_id' => $customFieldId,
'entity_id' => $this->_entityID,
'custom_field_value' => $newCustomValue,
]);
$newValue = $newValue['values'][$customFieldId]['display'];
}
$formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue;
}
}
}
return implode('<br/>', $formattedDetails);
}
}