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 CRM_Utils_System
::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
);
80 * Build the form object.
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
93 'name' => ts('Cancel'),
99 * Process the user submitted custom data values.
101 public function postProcess() {
102 $params = $this->controller
->exportValues($this->_name
);
104 $transaction = new CRM_Core_Transaction();
106 CRM_Core_BAO_CustomValueTable
::postProcess($params,
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"));
115 $activityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Custom Data');
117 'activity_type_id' => $activityTypeID,
118 'source_contact_id' => $session->get('userID'),
120 'subject' => $this->_customTitle
. " : change data",
121 'status_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'),
122 'target_contact_id' => $this->_contactID
,
123 'details' => $this->formatCustomDataChangesForDetail($params),
124 'activity_date_time' => date('YmdHis'),
126 $activity = CRM_Activity_BAO_Activity
::create($activityParams);
129 'activity_id' => $activity->id
,
130 'case_id' => $this->_entityID
,
132 CRM_Case_BAO_Case
::processCaseActivity($caseParams);
134 $transaction->commit();
138 * Format the custom data changes as [label]: [old value] => [new value]
140 * @param array $params New custom field values from form
143 * @throws \CiviCRM_API3_Exception
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
153 // We need custom field ID from custom_XX_1
154 list($_, $customFieldId, $_) = explode('_', $customField);
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],
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,
170 $newValue = $newValue['values'][$customFieldId]['display'];
171 $formattedDetails[] = $label . ': ' . $oldValue . ' => ' . $newValue;
177 return implode('<br/>', $formattedDetails);