Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | /** | |
54 | * entity type of the table id | |
55 | * | |
56 | * @var string | |
57 | */ | |
58 | protected $_entityType; | |
59 | ||
60 | /** | |
61 | * entity sub type of the table id | |
62 | * | |
63 | * @var string | |
64 | * @access protected | |
65 | */ | |
66 | protected $_entitySubType; | |
67 | ||
68 | /** | |
69 | * the group tree data | |
70 | * | |
71 | * @var array | |
72 | */ | |
73 | //protected $_groupTree; | |
74 | ||
75 | /** | |
76 | * Which blocks should we show and hide. | |
77 | * | |
78 | * @var CRM_Core_ShowHideBlocks | |
79 | */ | |
80 | protected $_showHide; | |
81 | ||
82 | /** | |
83 | * Array group titles. | |
84 | * | |
85 | * @var array | |
86 | */ | |
87 | protected $_groupTitle; | |
88 | ||
89 | /** | |
90 | * Array group display status. | |
91 | * | |
92 | * @var array | |
93 | */ | |
94 | protected $_groupCollapseDisplay; | |
95 | ||
96 | /** | |
8ef12e64 | 97 | * custom group id |
6a488035 TO |
98 | * |
99 | * @int | |
100 | * @access public | |
101 | */ | |
102 | public $_groupID; | |
103 | ||
e41f4660 | 104 | public $_multiRecordDisplay; |
1dde099b PJ |
105 | |
106 | public $_copyValueId; | |
6a488035 TO |
107 | /** |
108 | * pre processing work done here. | |
109 | * | |
110 | * gets session variables for table name, id of entity in table, type of entity and stores them. | |
111 | * | |
112 | * @param | |
113 | * | |
114 | * @return void | |
115 | * | |
116 | * @access public | |
117 | * | |
8ef12e64 | 118 | */ |
6a488035 TO |
119 | function preProcess() { |
120 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
6a488035 | 121 | $this->assign('cdType', FALSE); |
e41f4660 PJ |
122 | $this->_multiRecordDisplay = CRM_Utils_Request::retrieve('multiRecordDisplay', 'String', $this); |
123 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { | |
124 | if ($this->_cdType) { | |
125 | $this->assign('cdType', TRUE); | |
126 | } | |
127 | // NOTE : group id is not stored in session from within CRM_Custom_Form_CustomData::preProcess func | |
128 | // this is due to some condition inside it which restricts it from saving in session | |
129 | // so doing this for multi record edit action | |
130 | CRM_Custom_Form_CustomData::preProcess($this); | |
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 TO |
165 | for ($i = 0; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) { |
166 | CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, $i); | |
167 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
168 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | /** | |
174 | * Function to actually build the form | |
175 | * | |
176 | * @return void | |
177 | * @access public | |
178 | */ | |
179 | public function buildQuickForm() { | |
e41f4660 PJ |
180 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { |
181 | // buttons display for multi-valued fields to perform independednt actions | |
182 | if ($this->_multiRecordDisplay) { | |
183 | $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', | |
184 | $this->_groupID, | |
185 | 'is_multiple' | |
186 | ); | |
187 | if ($isMultiple) { | |
188 | $this->assign('multiRecordDisplay', $this->_multiRecordDisplay); | |
1dde099b | 189 | $saveButtonName = $this->_copyValueId ? 'Save a Copy': 'Save'; |
e41f4660 PJ |
190 | $this->addButtons(array( |
191 | array( | |
192 | 'type' => 'upload', | |
1dde099b | 193 | 'name' => ts('%1', array(1 => $saveButtonName)), |
e41f4660 PJ |
194 | 'isDefault' => TRUE, |
195 | ), | |
bf076628 PJ |
196 | array( |
197 | 'type' => 'upload', | |
198 | 'name' => ts('Save and New'), | |
199 | 'subName' => 'new', | |
200 | ), | |
e41f4660 PJ |
201 | array( |
202 | 'type' => 'cancel', | |
203 | 'name' => ts('Cancel'), | |
204 | ), | |
205 | ) | |
206 | ); | |
207 | } | |
208 | } | |
6a488035 TO |
209 | return CRM_Custom_Form_CustomData::buildQuickForm($this); |
210 | } | |
211 | ||
212 | //need to assign custom data type and subtype to the template | |
213 | $this->assign('entityID', $this->_tableID); | |
214 | $this->assign('groupID', $this->_groupID); | |
215 | ||
216 | // make this form an upload since we dont know if the custom data injected dynamically | |
217 | // is of type file etc | |
218 | $this->addButtons(array( | |
219 | array( | |
220 | 'type' => 'upload', | |
221 | 'name' => ts('Save'), | |
222 | 'isDefault' => TRUE, | |
223 | ), | |
224 | array( | |
225 | 'type' => 'cancel', | |
226 | 'name' => ts('Cancel'), | |
227 | ), | |
228 | ) | |
229 | ); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Set the default form values | |
234 | * | |
235 | * @access protected | |
236 | * | |
237 | * @return array the default array reference | |
238 | */ | |
239 | function setDefaultValues() { | |
e41f4660 | 240 | if ($this->_cdType || $this->_multiRecordDisplay == 'single') { |
1dde099b PJ |
241 | if ($this->_copyValueId) { |
242 | // cached tree is fetched | |
243 | $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_type, | |
244 | $this, | |
245 | $this->_entityId, | |
246 | $this->_groupID | |
247 | ); | |
248 | $valueIdDefaults = array(); | |
249 | $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this); | |
250 | CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action')); | |
251 | $tableId = $groupTreeValueId[$this->_groupID]['table_id']; | |
252 | foreach ($valueIdDefaults as $valueIdElementName => $value) { | |
253 | // build defaults for COPY action for new record saving | |
254 | $valueIdElementNamePieces = explode('_', $valueIdElementName); | |
255 | $valueIdElementNamePieces[2] = "-{$this->_groupCount}"; | |
256 | $elementName = implode('_', $valueIdElementNamePieces); | |
257 | $customDefaultValue[$elementName] = $value; | |
258 | } | |
259 | } | |
260 | else { | |
261 | $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this); | |
262 | } | |
6a488035 TO |
263 | return $customDefaultValue; |
264 | } | |
265 | ||
266 | $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType, | |
267 | $this, | |
268 | $this->_tableID, | |
269 | $this->_groupID, | |
270 | $this->_contactSubType | |
271 | ); | |
272 | ||
a7488080 | 273 | if (empty($_POST['hidden_custom_group_count'])) { |
6a488035 TO |
274 | // custom data building in edit mode (required to handle multi-value) |
275 | $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType, $this, $this->_tableID, | |
276 | $this->_groupID, $this->_contactSubType | |
277 | ); | |
278 | $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID); | |
279 | } | |
280 | else { | |
281 | $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID]; | |
282 | } | |
283 | ||
284 | $this->assign('customValueCount', $customValueCount); | |
285 | ||
286 | $defaults = array(); | |
287 | return $defaults; | |
288 | } | |
289 | ||
290 | /** | |
291 | * Process the user submitted custom data values. | |
292 | * | |
293 | * @access public | |
294 | * | |
295 | * @return void | |
296 | */ | |
297 | public function postProcess() { | |
298 | // Get the form values and groupTree | |
299 | $params = $this->controller->exportValues($this->_name); | |
bf076628 | 300 | |
6a488035 TO |
301 | CRM_Core_BAO_CustomValueTable::postProcess($params, |
302 | $this->_groupTree[$this->_groupID]['fields'], | |
303 | 'civicrm_contact', | |
304 | $this->_tableID, | |
305 | $this->_entityType | |
306 | ); | |
bf076628 PJ |
307 | $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name'); |
308 | $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE); | |
309 | $cgcount += 1; | |
310 | $buttonName = $this->controller->getButtonName(); | |
311 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
312 | CRM_Core_Session::singleton()->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")); | |
313 | } | |
6a488035 TO |
314 | // reset the group contact cache for this group |
315 | CRM_Contact_BAO_GroupContactCache::remove(); | |
316 | } | |
e41f4660 | 317 | } |