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