Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
c037736a | 35 | * This class generates form components for custom data. |
6a488035 TO |
36 | * |
37 | * It delegates the work to lower level subclasses and integrates the changes | |
38 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
39 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
6a488035 TO |
40 | */ |
41 | class CRM_Contact_Form_CustomData extends CRM_Core_Form { | |
42 | ||
43 | /** | |
44 | * The table id, used when editing/creating custom data | |
45 | * | |
46 | * @var int | |
47 | */ | |
48 | protected $_tableId; | |
49 | ||
50 | /** | |
100fef9d | 51 | * Entity type of the table id |
6a488035 TO |
52 | * |
53 | * @var string | |
54 | */ | |
55 | protected $_entityType; | |
56 | ||
57 | /** | |
100fef9d | 58 | * Entity sub type of the table id |
6a488035 TO |
59 | * |
60 | * @var string | |
6a488035 TO |
61 | */ |
62 | protected $_entitySubType; | |
63 | ||
64 | /** | |
100fef9d | 65 | * The group tree data |
6a488035 TO |
66 | * |
67 | * @var array | |
68 | */ | |
69 | //protected $_groupTree; | |
70 | ||
71 | /** | |
72 | * Which blocks should we show and hide. | |
73 | * | |
74 | * @var CRM_Core_ShowHideBlocks | |
75 | */ | |
76 | protected $_showHide; | |
77 | ||
78 | /** | |
79 | * Array group titles. | |
80 | * | |
81 | * @var array | |
82 | */ | |
83 | protected $_groupTitle; | |
84 | ||
85 | /** | |
86 | * Array group display status. | |
87 | * | |
88 | * @var array | |
89 | */ | |
90 | protected $_groupCollapseDisplay; | |
91 | ||
92 | /** | |
100fef9d | 93 | * Custom group id |
6a488035 | 94 | * |
69078420 | 95 | * @var int |
6a488035 TO |
96 | */ |
97 | public $_groupID; | |
98 | ||
e41f4660 | 99 | public $_multiRecordDisplay; |
1dde099b PJ |
100 | |
101 | public $_copyValueId; | |
353ffa53 | 102 | |
6a488035 | 103 | /** |
100fef9d | 104 | * Pre processing work done here. |
6a488035 | 105 | * |
c037736a | 106 | * Gets session variables for table name, id of entity in table, type of entity and stores them. |
8ef12e64 | 107 | */ |
00be9182 | 108 | public function preProcess() { |
6a488035 | 109 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); |
6a488035 | 110 | $this->assign('cdType', FALSE); |
e41f4660 PJ |
111 | $this->_multiRecordDisplay = CRM_Utils_Request::retrieve('multiRecordDisplay', 'String', $this); |
112 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { | |
113 | if ($this->_cdType) { | |
114 | $this->assign('cdType', TRUE); | |
115 | } | |
116 | // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func | |
117 | // this is due to some condition inside it which restricts it from saving in session | |
118 | // so doing this for multi record edit action | |
9710b8d1 | 119 | $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this); |
22e263ad | 120 | if (!empty($entityId)) { |
9710b8d1 RK |
121 | $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ','); |
122 | } | |
123 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId); | |
e41f4660 PJ |
124 | if ($this->_multiRecordDisplay) { |
125 | $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this); | |
126 | $this->_tableID = $this->_entityId; | |
bf076628 PJ |
127 | $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID); |
128 | $mode = CRM_Utils_Request::retrieve('mode', 'String', $this); | |
129 | $hasReachedMax = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($this->_groupID, $this->_tableID); | |
130 | if ($hasReachedMax && $mode == 'add') { | |
131 | CRM_Core_Error::statusBounce(ts('The maximum record limit is reached')); | |
132 | } | |
1dde099b | 133 | $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this); |
9cb02ab3 | 134 | |
525faea3 PJ |
135 | $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupID); |
136 | $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET'); | |
137 | $mode = ucfirst($mode); | |
be2fb01f | 138 | CRM_Utils_System::setTitle(ts('%1 %2 Record', [1 => $mode, 2 => $groupTitle])); |
525faea3 | 139 | |
5f5a6693 PJ |
140 | if (!empty($_POST['hidden_custom'])) { |
141 | $this->assign('postedInfo', TRUE); | |
142 | } | |
e41f4660 PJ |
143 | } |
144 | return; | |
6a488035 | 145 | } |
6a488035 TO |
146 | $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE); |
147 | $this->_tableID = CRM_Utils_Request::retrieve('tableId', 'Positive', $this, TRUE); | |
148 | ||
149 | $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID); | |
150 | $this->_contactSubType = CRM_Contact_BAO_Contact::getContactSubType($this->_tableID, ','); | |
151 | $this->assign('contact_type', $this->_contactType); | |
152 | $this->assign('contact_subtype', $this->_contactSubType); | |
153 | list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_tableID); | |
154 | CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName); | |
155 | ||
156 | // when custom data is included in this page | |
a7488080 | 157 | if (!empty($_POST['hidden_custom'])) { |
88d45a96 | 158 | for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) { |
ecc6bd60 | 159 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_contactSubType, $i, $this->_contactType, $this->_tableID); |
6a488035 TO |
160 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
161 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | /** | |
fe482240 | 167 | * Build the form object. |
6a488035 TO |
168 | */ |
169 | public function buildQuickForm() { | |
e41f4660 PJ |
170 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { |
171 | // buttons display for multi-valued fields to perform independednt actions | |
172 | if ($this->_multiRecordDisplay) { | |
173 | $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
174 | $this->_groupID, | |
175 | 'is_multiple' | |
176 | ); | |
177 | if ($isMultiple) { | |
178 | $this->assign('multiRecordDisplay', $this->_multiRecordDisplay); | |
bbb98b08 | 179 | $saveButtonName = $this->_copyValueId ? ts('Save a Copy') : ts('Save'); |
be2fb01f | 180 | $this->addButtons([ |
69078420 SL |
181 | [ |
182 | 'type' => 'upload', | |
183 | 'name' => $saveButtonName, | |
184 | 'isDefault' => TRUE, | |
185 | ], | |
186 | [ | |
187 | 'type' => 'upload', | |
188 | 'name' => ts('Save and New'), | |
189 | 'subName' => 'new', | |
190 | ], | |
191 | [ | |
192 | 'type' => 'cancel', | |
193 | 'name' => ts('Cancel'), | |
194 | ], | |
195 | ]); | |
e41f4660 PJ |
196 | } |
197 | } | |
6a488035 TO |
198 | return CRM_Custom_Form_CustomData::buildQuickForm($this); |
199 | } | |
200 | ||
201 | //need to assign custom data type and subtype to the template | |
202 | $this->assign('entityID', $this->_tableID); | |
203 | $this->assign('groupID', $this->_groupID); | |
204 | ||
205 | // make this form an upload since we dont know if the custom data injected dynamically | |
206 | // is of type file etc | |
be2fb01f | 207 | $this->addButtons([ |
69078420 SL |
208 | [ |
209 | 'type' => 'upload', | |
210 | 'name' => ts('Save'), | |
211 | 'isDefault' => TRUE, | |
212 | ], | |
213 | [ | |
214 | 'type' => 'cancel', | |
215 | 'name' => ts('Cancel'), | |
216 | ], | |
217 | ]); | |
6a488035 TO |
218 | } |
219 | ||
220 | /** | |
fe482240 | 221 | * Set the default form values. |
6a488035 | 222 | * |
6a488035 | 223 | * |
a6c01b45 CW |
224 | * @return array |
225 | * the default array reference | |
6a488035 | 226 | */ |
00be9182 | 227 | public function setDefaultValues() { |
e41f4660 | 228 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { |
1dde099b PJ |
229 | if ($this->_copyValueId) { |
230 | // cached tree is fetched | |
517755e0 | 231 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type, |
0b330e6d | 232 | NULL, |
1dde099b | 233 | $this->_entityId, |
e1d48b72 | 234 | $this->_groupID, |
be2fb01f | 235 | [], |
e1d48b72 | 236 | NULL, |
237 | TRUE, | |
238 | NULL, | |
239 | FALSE, | |
240 | TRUE, | |
241 | $this->_copyValueId | |
1dde099b | 242 | ); |
be2fb01f | 243 | $valueIdDefaults = []; |
1dde099b PJ |
244 | $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this); |
245 | CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action')); | |
246 | $tableId = $groupTreeValueId[$this->_groupID]['table_id']; | |
247 | foreach ($valueIdDefaults as $valueIdElementName => $value) { | |
248 | // build defaults for COPY action for new record saving | |
249 | $valueIdElementNamePieces = explode('_', $valueIdElementName); | |
250 | $valueIdElementNamePieces[2] = "-{$this->_groupCount}"; | |
251 | $elementName = implode('_', $valueIdElementNamePieces); | |
252 | $customDefaultValue[$elementName] = $value; | |
253 | } | |
254 | } | |
255 | else { | |
256 | $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this); | |
257 | } | |
6a488035 TO |
258 | return $customDefaultValue; |
259 | } | |
260 | ||
517755e0 | 261 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, |
0b330e6d | 262 | NULL, |
6a488035 TO |
263 | $this->_tableID, |
264 | $this->_groupID, | |
265 | $this->_contactSubType | |
266 | ); | |
267 | ||
a7488080 | 268 | if (empty($_POST['hidden_custom_group_count'])) { |
6a488035 | 269 | // custom data building in edit mode (required to handle multi-value) |
0b330e6d | 270 | $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, NULL, $this->_tableID, |
6a488035 TO |
271 | $this->_groupID, $this->_contactSubType |
272 | ); | |
e7cf1b90 | 273 | $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID, NULL, NULL, $this->_tableID); |
6a488035 TO |
274 | } |
275 | else { | |
276 | $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID]; | |
277 | } | |
278 | ||
279 | $this->assign('customValueCount', $customValueCount); | |
280 | ||
be2fb01f | 281 | $defaults = []; |
6a488035 TO |
282 | return $defaults; |
283 | } | |
284 | ||
285 | /** | |
286 | * Process the user submitted custom data values. | |
6a488035 TO |
287 | */ |
288 | public function postProcess() { | |
289 | // Get the form values and groupTree | |
e7cf1b90 WA |
290 | //CRM-18183 |
291 | $params = $this->controller->exportValues($this->_name); | |
bf076628 | 292 | |
6a488035 | 293 | CRM_Core_BAO_CustomValueTable::postProcess($params, |
6a488035 TO |
294 | 'civicrm_contact', |
295 | $this->_tableID, | |
296 | $this->_entityType | |
297 | ); | |
bf076628 PJ |
298 | $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name'); |
299 | $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE); | |
300 | $cgcount += 1; | |
301 | $buttonName = $this->controller->getButtonName(); | |
302 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
353ffa53 TO |
303 | CRM_Core_Session::singleton() |
304 | ->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 | 305 | } |
b4efde7a CW |
306 | |
307 | // Add entry in the log table | |
308 | CRM_Core_BAO_Log::register($this->_tableID, | |
309 | 'civicrm_contact', | |
310 | $this->_tableID | |
311 | ); | |
312 | ||
313 | if (CRM_Core_Resources::isAjaxMode()) { | |
314 | $this->ajaxResponse += CRM_Contact_Form_Inline::renderFooter($this->_tableID); | |
315 | } | |
316 | ||
2b68a50c | 317 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 | 318 | } |
96025800 | 319 | |
ef10e0b5 | 320 | } |