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