3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for custom data
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.
25 class CRM_Case_Form_CustomData
extends CRM_Core_Form
{
28 * The entity id, used when editing/creating custom data
35 * Entity sub type of the table id.
39 protected $_subTypeID;
42 * Pre processing work done here.
44 * gets session variables for table name, id of entity in table, type of entity and stores them.
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);
52 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Case',
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']]));
66 $this->_defaults
= [];
67 CRM_Core_BAO_CustomGroup
::setDefaults($groupTree, $this->_defaults
);
68 $this->setDefaults($this->_defaults
);
70 CRM_Core_BAO_CustomGroup
::buildQuickForm($this, $groupTree);
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');
81 * Build the form object.
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
94 'name' => ts('Cancel'),
100 * Process the user submitted custom data values.
102 public function postProcess() {
103 $params = $this->controller
->exportValues($this->_name
);
105 $transaction = new CRM_Core_Transaction();
107 CRM_Core_BAO_CustomValueTable
::postProcess($params,
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"));
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');
120 'activity_type_id' => $activityTypeID,
121 'source_contact_id' => $session->get('userID'),
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'),
129 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
132 'activity_id' => $activity->id
,
133 'case_id' => $this->_entityID
,
135 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
138 $transaction->commit();
142 * Format the custom data changes as [label]: [old value] => [new value]
144 * @param array $params New custom field values from form
147 * @throws \CiviCRM_API3_Exception
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
157 // We need custom field ID from custom_XX_1
158 list($_, $customFieldId, $_) = explode('_', $customField);
160 if (!empty($customFieldId) && is_numeric($customFieldId)) {
161 // Got a custom field ID
162 $label = civicrm_api3('CustomField', 'getvalue', ['id' => $customFieldId, 'return' => 'label']);
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,
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,
181 $newValue = $newValue['values'][$customFieldId]['display'];
183 $formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue;
189 return implode('<br/>', $formattedDetails);