Merge pull request #17312 from totten/url3
[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 * 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 /**
70 * Custom group id
71 *
72 * @var int
73 */
74 public $_groupID;
75
76 public $_multiRecordDisplay;
77
78 public $_copyValueId;
79
80 /**
81 * Pre processing work done here.
82 *
83 * Gets session variables for table name, id of entity in table, type of entity and stores them.
84 */
85 public function preProcess() {
86 $this->_cdType = $_GET['type'] ?? NULL;
87 $this->assign('cdType', FALSE);
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
96 $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this);
97 if (!empty($entityId)) {
98 $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ',');
99 }
100 CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
101 if ($this->_multiRecordDisplay) {
102 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this);
103 $this->_tableID = $this->_entityId;
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);
107 if ($hasReachedMax && $mode == 'add') {
108 CRM_Core_Error::statusBounce(ts('The maximum record limit is reached'));
109 }
110 $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this);
111
112 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupID);
113 $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET');
114 $mode = ucfirst($mode);
115 CRM_Utils_System::setTitle(ts('%1 %2 Record', [1 => $mode, 2 => $groupTitle]));
116
117 if (!empty($_POST['hidden_custom'])) {
118 $this->assign('postedInfo', TRUE);
119 }
120 }
121 return;
122 }
123 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
124 $this->_tableID = CRM_Utils_Request::retrieve('tableId', 'Positive', $this, TRUE);
125
126 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
127 $this->_contactSubType = CRM_Contact_BAO_Contact::getContactSubType($this->_tableID, ',');
128 $this->assign('contact_type', $this->_contactType);
129 $this->assign('contact_subtype', $this->_contactSubType);
130 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_tableID);
131 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
132
133 // when custom data is included in this page
134 if (!empty($_POST['hidden_custom'])) {
135 for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) {
136 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_contactSubType, $i, $this->_contactType, $this->_tableID);
137 CRM_Custom_Form_CustomData::buildQuickForm($this);
138 CRM_Custom_Form_CustomData::setDefaultValues($this);
139 }
140 }
141 }
142
143 /**
144 * Build the form object.
145 */
146 public function buildQuickForm() {
147 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
148 // buttons display for multi-valued fields to perform independednt actions
149 if ($this->_multiRecordDisplay) {
150 $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
151 $this->_groupID,
152 'is_multiple'
153 );
154 if ($isMultiple) {
155 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay);
156 $saveButtonName = $this->_copyValueId ? ts('Save a Copy') : ts('Save');
157 $this->addButtons([
158 [
159 'type' => 'upload',
160 'name' => $saveButtonName,
161 'isDefault' => TRUE,
162 ],
163 [
164 'type' => 'upload',
165 'name' => ts('Save and New'),
166 'subName' => 'new',
167 ],
168 [
169 'type' => 'cancel',
170 'name' => ts('Cancel'),
171 ],
172 ]);
173 }
174 }
175 return CRM_Custom_Form_CustomData::buildQuickForm($this);
176 }
177
178 //need to assign custom data type and subtype to the template
179 $this->assign('entityID', $this->_tableID);
180 $this->assign('groupID', $this->_groupID);
181
182 // make this form an upload since we dont know if the custom data injected dynamically
183 // is of type file etc
184 $this->addButtons([
185 [
186 'type' => 'upload',
187 'name' => ts('Save'),
188 'isDefault' => TRUE,
189 ],
190 [
191 'type' => 'cancel',
192 'name' => ts('Cancel'),
193 ],
194 ]);
195 }
196
197 /**
198 * Set the default form values.
199 *
200 *
201 * @return array
202 * the default array reference
203 */
204 public function setDefaultValues() {
205 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
206 if ($this->_copyValueId) {
207 // cached tree is fetched
208 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type,
209 NULL,
210 $this->_entityId,
211 $this->_groupID,
212 [],
213 NULL,
214 TRUE,
215 NULL,
216 FALSE,
217 TRUE,
218 $this->_copyValueId
219 );
220 $valueIdDefaults = [];
221 $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this);
222 CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
223 $tableId = $groupTreeValueId[$this->_groupID]['table_id'];
224 foreach ($valueIdDefaults as $valueIdElementName => $value) {
225 // build defaults for COPY action for new record saving
226 $valueIdElementNamePieces = explode('_', $valueIdElementName);
227 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
228 $elementName = implode('_', $valueIdElementNamePieces);
229 $customDefaultValue[$elementName] = $value;
230 }
231 }
232 else {
233 $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this);
234 }
235 return $customDefaultValue;
236 }
237
238 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType,
239 NULL,
240 $this->_tableID,
241 $this->_groupID,
242 $this->_contactSubType
243 );
244
245 if (empty($_POST['hidden_custom_group_count'])) {
246 // custom data building in edit mode (required to handle multi-value)
247 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, NULL, $this->_tableID,
248 $this->_groupID, $this->_contactSubType
249 );
250 $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID, NULL, NULL, $this->_tableID);
251 }
252 else {
253 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID];
254 }
255
256 $this->assign('customValueCount', $customValueCount);
257
258 $defaults = [];
259 return $defaults;
260 }
261
262 /**
263 * Process the user submitted custom data values.
264 */
265 public function postProcess() {
266 // Get the form values and groupTree
267 //CRM-18183
268 $params = $this->controller->exportValues($this->_name);
269
270 CRM_Core_BAO_CustomValueTable::postProcess($params,
271 'civicrm_contact',
272 $this->_tableID,
273 $this->_entityType
274 );
275 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name');
276 $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE);
277 $cgcount += 1;
278 $buttonName = $this->controller->getButtonName();
279 if ($buttonName == $this->getButtonName('upload', 'new')) {
280 CRM_Core_Session::singleton()
281 ->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"));
282 }
283
284 // Add entry in the log table
285 CRM_Core_BAO_Log::register($this->_tableID,
286 'civicrm_contact',
287 $this->_tableID
288 );
289
290 if (CRM_Core_Resources::isAjaxMode()) {
291 $this->ajaxResponse += CRM_Contact_Form_Inline::renderFooter($this->_tableID);
292 }
293
294 CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
295 }
296
297 }