Replace CRM_Utils_Array::value with ?? in variable assignments
[civicrm-core.git] / CRM / Contact / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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.
24 */
25 class CRM_Contact_Form_CustomData extends CRM_Core_Form {
26
27 /**
28 * The table id, used when editing/creating custom data
29 *
30 * @var int
31 */
32 protected $_tableId;
33
34 /**
35 * Entity type of the table id
36 *
37 * @var string
38 */
39 protected $_entityType;
40
41 /**
42 * Entity sub type of the table id
43 *
44 * @var string
45 */
46 protected $_entitySubType;
47
48 /**
49 * The group tree data
50 *
51 * @var array
52 */
53 //protected $_groupTree;
54
55 /**
56 * Which blocks should we show and hide.
57 *
58 * @var CRM_Core_ShowHideBlocks
59 */
60 protected $_showHide;
61
62 /**
63 * Array group titles.
64 *
65 * @var array
66 */
67 protected $_groupTitle;
68
69 /**
70 * Array group display status.
71 *
72 * @var array
73 */
74 protected $_groupCollapseDisplay;
75
76 /**
77 * Custom group id
78 *
79 * @var int
80 */
81 public $_groupID;
82
83 public $_multiRecordDisplay;
84
85 public $_copyValueId;
86
87 /**
88 * Pre processing work done here.
89 *
90 * Gets session variables for table name, id of entity in table, type of entity and stores them.
91 */
92 public function preProcess() {
93 $this->_cdType = $_GET['type'] ?? NULL;
94 $this->assign('cdType', FALSE);
95 $this->_multiRecordDisplay = CRM_Utils_Request::retrieve('multiRecordDisplay', 'String', $this);
96 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
97 if ($this->_cdType) {
98 $this->assign('cdType', TRUE);
99 }
100 // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func
101 // this is due to some condition inside it which restricts it from saving in session
102 // so doing this for multi record edit action
103 $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this);
104 if (!empty($entityId)) {
105 $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ',');
106 }
107 CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
108 if ($this->_multiRecordDisplay) {
109 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this);
110 $this->_tableID = $this->_entityId;
111 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
112 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
113 $hasReachedMax = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($this->_groupID, $this->_tableID);
114 if ($hasReachedMax && $mode == 'add') {
115 CRM_Core_Error::statusBounce(ts('The maximum record limit is reached'));
116 }
117 $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this);
118
119 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupID);
120 $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET');
121 $mode = ucfirst($mode);
122 CRM_Utils_System::setTitle(ts('%1 %2 Record', [1 => $mode, 2 => $groupTitle]));
123
124 if (!empty($_POST['hidden_custom'])) {
125 $this->assign('postedInfo', TRUE);
126 }
127 }
128 return;
129 }
130 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
131 $this->_tableID = CRM_Utils_Request::retrieve('tableId', 'Positive', $this, TRUE);
132
133 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
134 $this->_contactSubType = CRM_Contact_BAO_Contact::getContactSubType($this->_tableID, ',');
135 $this->assign('contact_type', $this->_contactType);
136 $this->assign('contact_subtype', $this->_contactSubType);
137 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_tableID);
138 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
139
140 // when custom data is included in this page
141 if (!empty($_POST['hidden_custom'])) {
142 for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) {
143 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_contactSubType, $i, $this->_contactType, $this->_tableID);
144 CRM_Custom_Form_CustomData::buildQuickForm($this);
145 CRM_Custom_Form_CustomData::setDefaultValues($this);
146 }
147 }
148 }
149
150 /**
151 * Build the form object.
152 */
153 public function buildQuickForm() {
154 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
155 // buttons display for multi-valued fields to perform independednt actions
156 if ($this->_multiRecordDisplay) {
157 $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
158 $this->_groupID,
159 'is_multiple'
160 );
161 if ($isMultiple) {
162 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay);
163 $saveButtonName = $this->_copyValueId ? ts('Save a Copy') : ts('Save');
164 $this->addButtons([
165 [
166 'type' => 'upload',
167 'name' => $saveButtonName,
168 'isDefault' => TRUE,
169 ],
170 [
171 'type' => 'upload',
172 'name' => ts('Save and New'),
173 'subName' => 'new',
174 ],
175 [
176 'type' => 'cancel',
177 'name' => ts('Cancel'),
178 ],
179 ]);
180 }
181 }
182 return CRM_Custom_Form_CustomData::buildQuickForm($this);
183 }
184
185 //need to assign custom data type and subtype to the template
186 $this->assign('entityID', $this->_tableID);
187 $this->assign('groupID', $this->_groupID);
188
189 // make this form an upload since we dont know if the custom data injected dynamically
190 // is of type file etc
191 $this->addButtons([
192 [
193 'type' => 'upload',
194 'name' => ts('Save'),
195 'isDefault' => TRUE,
196 ],
197 [
198 'type' => 'cancel',
199 'name' => ts('Cancel'),
200 ],
201 ]);
202 }
203
204 /**
205 * Set the default form values.
206 *
207 *
208 * @return array
209 * the default array reference
210 */
211 public function setDefaultValues() {
212 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
213 if ($this->_copyValueId) {
214 // cached tree is fetched
215 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type,
216 NULL,
217 $this->_entityId,
218 $this->_groupID,
219 [],
220 NULL,
221 TRUE,
222 NULL,
223 FALSE,
224 TRUE,
225 $this->_copyValueId
226 );
227 $valueIdDefaults = [];
228 $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this);
229 CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
230 $tableId = $groupTreeValueId[$this->_groupID]['table_id'];
231 foreach ($valueIdDefaults as $valueIdElementName => $value) {
232 // build defaults for COPY action for new record saving
233 $valueIdElementNamePieces = explode('_', $valueIdElementName);
234 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
235 $elementName = implode('_', $valueIdElementNamePieces);
236 $customDefaultValue[$elementName] = $value;
237 }
238 }
239 else {
240 $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this);
241 }
242 return $customDefaultValue;
243 }
244
245 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType,
246 NULL,
247 $this->_tableID,
248 $this->_groupID,
249 $this->_contactSubType
250 );
251
252 if (empty($_POST['hidden_custom_group_count'])) {
253 // custom data building in edit mode (required to handle multi-value)
254 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, NULL, $this->_tableID,
255 $this->_groupID, $this->_contactSubType
256 );
257 $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID, NULL, NULL, $this->_tableID);
258 }
259 else {
260 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID];
261 }
262
263 $this->assign('customValueCount', $customValueCount);
264
265 $defaults = [];
266 return $defaults;
267 }
268
269 /**
270 * Process the user submitted custom data values.
271 */
272 public function postProcess() {
273 // Get the form values and groupTree
274 //CRM-18183
275 $params = $this->controller->exportValues($this->_name);
276
277 CRM_Core_BAO_CustomValueTable::postProcess($params,
278 'civicrm_contact',
279 $this->_tableID,
280 $this->_entityType
281 );
282 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name');
283 $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE);
284 $cgcount += 1;
285 $buttonName = $this->controller->getButtonName();
286 if ($buttonName == $this->getButtonName('upload', 'new')) {
287 CRM_Core_Session::singleton()
288 ->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/cd/edit', "reset=1&type={$this->_contactType}&groupID={$this->_groupID}&entityID={$this->_tableID}&cgcount={$cgcount}&multiRecordDisplay=single&mode=add"));
289 }
290
291 // Add entry in the log table
292 CRM_Core_BAO_Log::register($this->_tableID,
293 'civicrm_contact',
294 $this->_tableID
295 );
296
297 if (CRM_Core_Resources::isAjaxMode()) {
298 $this->ajaxResponse += CRM_Contact_Form_Inline::renderFooter($this->_tableID);
299 }
300
301 CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
302 }
303
304 }