Merge pull request #13170 from elisseck/dev/core/485
[civicrm-core.git] / CRM / Contact / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for custom data.
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.
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 /**
51 * Entity type of the table id
52 *
53 * @var string
54 */
55 protected $_entityType;
56
57 /**
58 * Entity sub type of the table id
59 *
60 * @var string
61 */
62 protected $_entitySubType;
63
64 /**
65 * The group tree data
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 /**
93 * Custom group id
94 *
95 * @int
96 */
97 public $_groupID;
98
99 public $_multiRecordDisplay;
100
101 public $_copyValueId;
102
103 /**
104 * Pre processing work done here.
105 *
106 * Gets session variables for table name, id of entity in table, type of entity and stores them.
107 */
108 public function preProcess() {
109 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
110 $this->assign('cdType', FALSE);
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
119 $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this);
120 if (!empty($entityId)) {
121 $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ',');
122 }
123 CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
124 if ($this->_multiRecordDisplay) {
125 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this);
126 $this->_tableID = $this->_entityId;
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 }
133 $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this);
134
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);
138 CRM_Utils_System::setTitle(ts('%1 %2 Record', array(1 => $mode, 2 => $groupTitle)));
139
140 if (!empty($_POST['hidden_custom'])) {
141 $this->assign('postedInfo', TRUE);
142 }
143 }
144 return;
145 }
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
157 if (!empty($_POST['hidden_custom'])) {
158 for ($i = 1; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) {
159 CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_contactSubType, $i, $this->_contactType, $this->_tableID);
160 CRM_Custom_Form_CustomData::buildQuickForm($this);
161 CRM_Custom_Form_CustomData::setDefaultValues($this);
162 }
163 }
164 }
165
166 /**
167 * Build the form object.
168 */
169 public function buildQuickForm() {
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);
179 $saveButtonName = $this->_copyValueId ? ts('Save a Copy') : ts('Save');
180 $this->addButtons(array(
181 array(
182 'type' => 'upload',
183 'name' => $saveButtonName,
184 'isDefault' => TRUE,
185 ),
186 array(
187 'type' => 'upload',
188 'name' => ts('Save and New'),
189 'subName' => 'new',
190 ),
191 array(
192 'type' => 'cancel',
193 'name' => ts('Cancel'),
194 ),
195 )
196 );
197 }
198 }
199 return CRM_Custom_Form_CustomData::buildQuickForm($this);
200 }
201
202 //need to assign custom data type and subtype to the template
203 $this->assign('entityID', $this->_tableID);
204 $this->assign('groupID', $this->_groupID);
205
206 // make this form an upload since we dont know if the custom data injected dynamically
207 // is of type file etc
208 $this->addButtons(array(
209 array(
210 'type' => 'upload',
211 'name' => ts('Save'),
212 'isDefault' => TRUE,
213 ),
214 array(
215 'type' => 'cancel',
216 'name' => ts('Cancel'),
217 ),
218 )
219 );
220 }
221
222 /**
223 * Set the default form values.
224 *
225 *
226 * @return array
227 * the default array reference
228 */
229 public function setDefaultValues() {
230 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
231 if ($this->_copyValueId) {
232 // cached tree is fetched
233 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_type,
234 NULL,
235 $this->_entityId,
236 $this->_groupID,
237 array(),
238 NULL,
239 TRUE,
240 NULL,
241 FALSE,
242 TRUE,
243 $this->_copyValueId
244 );
245 $valueIdDefaults = array();
246 $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this);
247 CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
248 $tableId = $groupTreeValueId[$this->_groupID]['table_id'];
249 foreach ($valueIdDefaults as $valueIdElementName => $value) {
250 // build defaults for COPY action for new record saving
251 $valueIdElementNamePieces = explode('_', $valueIdElementName);
252 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
253 $elementName = implode('_', $valueIdElementNamePieces);
254 $customDefaultValue[$elementName] = $value;
255 }
256 }
257 else {
258 $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this);
259 }
260 return $customDefaultValue;
261 }
262
263 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType,
264 NULL,
265 $this->_tableID,
266 $this->_groupID,
267 $this->_contactSubType
268 );
269
270 if (empty($_POST['hidden_custom_group_count'])) {
271 // custom data building in edit mode (required to handle multi-value)
272 $groupTree = CRM_Core_BAO_CustomGroup::getTree($this->_contactType, NULL, $this->_tableID,
273 $this->_groupID, $this->_contactSubType
274 );
275 $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID, NULL, NULL, $this->_tableID);
276 }
277 else {
278 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID];
279 }
280
281 $this->assign('customValueCount', $customValueCount);
282
283 $defaults = array();
284 return $defaults;
285 }
286
287 /**
288 * Process the user submitted custom data values.
289 */
290 public function postProcess() {
291 // Get the form values and groupTree
292 //CRM-18183
293 $params = $this->controller->exportValues($this->_name);
294
295 CRM_Core_BAO_CustomValueTable::postProcess($params,
296 'civicrm_contact',
297 $this->_tableID,
298 $this->_entityType
299 );
300 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name');
301 $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE);
302 $cgcount += 1;
303 $buttonName = $this->controller->getButtonName();
304 if ($buttonName == $this->getButtonName('upload', 'new')) {
305 CRM_Core_Session::singleton()
306 ->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"));
307 }
308
309 // Add entry in the log table
310 CRM_Core_BAO_Log::register($this->_tableID,
311 'civicrm_contact',
312 $this->_tableID
313 );
314
315 if (CRM_Core_Resources::isAjaxMode()) {
316 $this->ajaxResponse += CRM_Contact_Form_Inline::renderFooter($this->_tableID);
317 }
318
319 CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
320 }
321
322 }