Commit | Line | Data |
---|---|---|
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 | */ |
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 | /** | |
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 | ||
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 | /** | |
100fef9d | 77 | * Custom group id |
6a488035 | 78 | * |
69078420 | 79 | * @var int |
6a488035 TO |
80 | */ |
81 | public $_groupID; | |
82 | ||
e41f4660 | 83 | public $_multiRecordDisplay; |
1dde099b PJ |
84 | |
85 | public $_copyValueId; | |
353ffa53 | 86 | |
6a488035 | 87 | /** |
100fef9d | 88 | * Pre processing work done here. |
6a488035 | 89 | * |
c037736a | 90 | * Gets session variables for table name, id of entity in table, type of entity and stores them. |
8ef12e64 | 91 | */ |
00be9182 | 92 | public function preProcess() { |
9c1bc317 | 93 | $this->_cdType = $_GET['type'] ?? NULL; |
6a488035 | 94 | $this->assign('cdType', FALSE); |
e41f4660 PJ |
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 | |
9710b8d1 | 103 | $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this); |
22e263ad | 104 | if (!empty($entityId)) { |
9710b8d1 RK |
105 | $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ','); |
106 | } | |
107 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId); | |
e41f4660 PJ |
108 | if ($this->_multiRecordDisplay) { |
109 | $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this); | |
110 | $this->_tableID = $this->_entityId; | |
bf076628 PJ |
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 | } | |
1dde099b | 117 | $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this); |
9cb02ab3 | 118 | |
525faea3 PJ |
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); | |
be2fb01f | 122 | CRM_Utils_System::setTitle(ts('%1 %2 Record', [1 => $mode, 2 => $groupTitle])); |
525faea3 | 123 | |
5f5a6693 PJ |
124 | if (!empty($_POST['hidden_custom'])) { |
125 | $this->assign('postedInfo', TRUE); | |
126 | } | |
e41f4660 PJ |
127 | } |
128 | return; | |
6a488035 | 129 | } |
6a488035 TO |
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 | |
a7488080 | 141 | if (!empty($_POST['hidden_custom'])) { |
88d45a96 | 142 | for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) { |
ecc6bd60 | 143 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_contactSubType, $i, $this->_contactType, $this->_tableID); |
6a488035 TO |
144 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
145 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
fe482240 | 151 | * Build the form object. |
6a488035 TO |
152 | */ |
153 | public function buildQuickForm() { | |
e41f4660 PJ |
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); | |
bbb98b08 | 163 | $saveButtonName = $this->_copyValueId ? ts('Save a Copy') : ts('Save'); |
be2fb01f | 164 | $this->addButtons([ |
69078420 SL |
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 | ]); | |
e41f4660 PJ |
180 | } |
181 | } | |
6a488035 TO |
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 | |
be2fb01f | 191 | $this->addButtons([ |
69078420 SL |
192 | [ |
193 | 'type' => 'upload', | |
194 | 'name' => ts('Save'), | |
195 | 'isDefault' => TRUE, | |
196 | ], | |
197 | [ | |
198 | 'type' => 'cancel', | |
199 | 'name' => ts('Cancel'), | |
200 | ], | |
201 | ]); | |
6a488035 TO |
202 | } |
203 | ||
204 | /** | |
fe482240 | 205 | * Set the default form values. |
6a488035 | 206 | * |
6a488035 | 207 | * |
a6c01b45 CW |
208 | * @return array |
209 | * the default array reference | |
6a488035 | 210 | */ |
00be9182 | 211 | public function setDefaultValues() { |
e41f4660 | 212 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { |
1dde099b PJ |
213 | if ($this->_copyValueId) { |
214 | // cached tree is fetched | |
517755e0 | 215 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type, |
0b330e6d | 216 | NULL, |
1dde099b | 217 | $this->_entityId, |
e1d48b72 | 218 | $this->_groupID, |
be2fb01f | 219 | [], |
e1d48b72 | 220 | NULL, |
221 | TRUE, | |
222 | NULL, | |
223 | FALSE, | |
224 | TRUE, | |
225 | $this->_copyValueId | |
1dde099b | 226 | ); |
be2fb01f | 227 | $valueIdDefaults = []; |
1dde099b PJ |
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 | } | |
6a488035 TO |
242 | return $customDefaultValue; |
243 | } | |
244 | ||
517755e0 | 245 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, |
0b330e6d | 246 | NULL, |
6a488035 TO |
247 | $this->_tableID, |
248 | $this->_groupID, | |
249 | $this->_contactSubType | |
250 | ); | |
251 | ||
a7488080 | 252 | if (empty($_POST['hidden_custom_group_count'])) { |
6a488035 | 253 | // custom data building in edit mode (required to handle multi-value) |
0b330e6d | 254 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, NULL, $this->_tableID, |
6a488035 TO |
255 | $this->_groupID, $this->_contactSubType |
256 | ); | |
e7cf1b90 | 257 | $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID, NULL, NULL, $this->_tableID); |
6a488035 TO |
258 | } |
259 | else { | |
260 | $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID]; | |
261 | } | |
262 | ||
263 | $this->assign('customValueCount', $customValueCount); | |
264 | ||
be2fb01f | 265 | $defaults = []; |
6a488035 TO |
266 | return $defaults; |
267 | } | |
268 | ||
269 | /** | |
270 | * Process the user submitted custom data values. | |
6a488035 TO |
271 | */ |
272 | public function postProcess() { | |
273 | // Get the form values and groupTree | |
e7cf1b90 WA |
274 | //CRM-18183 |
275 | $params = $this->controller->exportValues($this->_name); | |
bf076628 | 276 | |
6a488035 | 277 | CRM_Core_BAO_CustomValueTable::postProcess($params, |
6a488035 TO |
278 | 'civicrm_contact', |
279 | $this->_tableID, | |
280 | $this->_entityType | |
281 | ); | |
bf076628 PJ |
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')) { | |
353ffa53 TO |
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")); | |
bf076628 | 289 | } |
b4efde7a CW |
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 | ||
2b68a50c | 301 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 | 302 | } |
96025800 | 303 | |
ef10e0b5 | 304 | } |