Merge pull request #4550 from jitendrapurohit/CRM-15568
[civicrm-core.git] / CRM / Contact / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 /**
97 * custom group id
98 *
99 * @int
100 * @access public
101 */
102 public $_groupID;
103
104 public $_multiRecordDisplay;
105
106 public $_copyValueId;
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 *
118 */
119 function preProcess() {
120 $this->_cdType = CRM_Utils_Array::value('type', $_GET);
121 $this->assign('cdType', FALSE);
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 $entityId = CRM_Utils_Request::retrieve('entityID', 'Positive', $this);
131 if(!empty($entityId)) {
132 $subType = CRM_Contact_BAO_Contact::getContactSubType($entityId, ',');
133 }
134 CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, NULL, NULL, $entityId);
135 if ($this->_multiRecordDisplay) {
136 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this);
137 $this->_tableID = $this->_entityId;
138 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
139 $mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
140 $hasReachedMax = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($this->_groupID, $this->_tableID);
141 if ($hasReachedMax && $mode == 'add') {
142 CRM_Core_Error::statusBounce(ts('The maximum record limit is reached'));
143 }
144 $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this);
145
146 $groupTitle = CRM_Core_BAO_CustomGroup::getTitle($this->_groupID);
147 $mode = CRM_Utils_Request::retrieve('mode', 'String', CRM_Core_DAO::$_nullObject, FALSE, NULL, 'GET');
148 $mode = ucfirst($mode);
149 CRM_Utils_System::setTitle(ts('%1 %2 Record', array(1 => $mode, 2 => $groupTitle)));
150
151 if (!empty($_POST['hidden_custom'])) {
152 $this->assign('postedInfo', TRUE);
153 }
154 }
155 return;
156 }
157 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
158 $this->_tableID = CRM_Utils_Request::retrieve('tableId', 'Positive', $this, TRUE);
159
160 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
161 $this->_contactSubType = CRM_Contact_BAO_Contact::getContactSubType($this->_tableID, ',');
162 $this->assign('contact_type', $this->_contactType);
163 $this->assign('contact_subtype', $this->_contactSubType);
164 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_tableID);
165 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
166
167 // when custom data is included in this page
168 if (!empty($_POST['hidden_custom'])) {
169 for ($i = 0; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) {
170 CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, $i);
171 CRM_Custom_Form_CustomData::buildQuickForm($this);
172 CRM_Custom_Form_CustomData::setDefaultValues($this);
173 }
174 }
175 }
176
177 /**
178 * Function to actually build the form
179 *
180 * @return void
181 * @access public
182 */
183 public function buildQuickForm() {
184 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
185 // buttons display for multi-valued fields to perform independednt actions
186 if ($this->_multiRecordDisplay) {
187 $isMultiple = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
188 $this->_groupID,
189 'is_multiple'
190 );
191 if ($isMultiple) {
192 $this->assign('multiRecordDisplay', $this->_multiRecordDisplay);
193 $saveButtonName = $this->_copyValueId ? 'Save a Copy': 'Save';
194 $this->addButtons(array(
195 array(
196 'type' => 'upload',
197 'name' => ts('%1', array(1 => $saveButtonName)),
198 'isDefault' => TRUE,
199 ),
200 array(
201 'type' => 'upload',
202 'name' => ts('Save and New'),
203 'subName' => 'new',
204 ),
205 array(
206 'type' => 'cancel',
207 'name' => ts('Cancel'),
208 ),
209 )
210 );
211 }
212 }
213 return CRM_Custom_Form_CustomData::buildQuickForm($this);
214 }
215
216 //need to assign custom data type and subtype to the template
217 $this->assign('entityID', $this->_tableID);
218 $this->assign('groupID', $this->_groupID);
219
220 // make this form an upload since we dont know if the custom data injected dynamically
221 // is of type file etc
222 $this->addButtons(array(
223 array(
224 'type' => 'upload',
225 'name' => ts('Save'),
226 'isDefault' => TRUE,
227 ),
228 array(
229 'type' => 'cancel',
230 'name' => ts('Cancel'),
231 ),
232 )
233 );
234 }
235
236 /**
237 * Set the default form values
238 *
239 * @access protected
240 *
241 * @return array the default array reference
242 */
243 function setDefaultValues() {
244 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
245 if ($this->_copyValueId) {
246 // cached tree is fetched
247 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_type,
248 $this,
249 $this->_entityId,
250 $this->_groupID
251 );
252 $valueIdDefaults = array();
253 $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this);
254 CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
255 $tableId = $groupTreeValueId[$this->_groupID]['table_id'];
256 foreach ($valueIdDefaults as $valueIdElementName => $value) {
257 // build defaults for COPY action for new record saving
258 $valueIdElementNamePieces = explode('_', $valueIdElementName);
259 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
260 $elementName = implode('_', $valueIdElementNamePieces);
261 $customDefaultValue[$elementName] = $value;
262 }
263 }
264 else {
265 $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this);
266 }
267 return $customDefaultValue;
268 }
269
270 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType,
271 $this,
272 $this->_tableID,
273 $this->_groupID,
274 $this->_contactSubType
275 );
276
277 if (empty($_POST['hidden_custom_group_count'])) {
278 // custom data building in edit mode (required to handle multi-value)
279 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType, $this, $this->_tableID,
280 $this->_groupID, $this->_contactSubType
281 );
282 $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID);
283 }
284 else {
285 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID];
286 }
287
288 $this->assign('customValueCount', $customValueCount);
289
290 $defaults = array();
291 return $defaults;
292 }
293
294 /**
295 * Process the user submitted custom data values.
296 *
297 * @access public
298 *
299 * @return void
300 */
301 public function postProcess() {
302 // Get the form values and groupTree
303 $params = $this->controller->exportValues($this->_name);
304
305 CRM_Core_BAO_CustomValueTable::postProcess($params,
306 $this->_groupTree[$this->_groupID]['fields'],
307 'civicrm_contact',
308 $this->_tableID,
309 $this->_entityType
310 );
311 $table = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $this->_groupID, 'table_name');
312 $cgcount = CRM_Core_BAO_CustomGroup::customGroupDataExistsForEntity($this->_tableID, $table, TRUE);
313 $cgcount += 1;
314 $buttonName = $this->controller->getButtonName();
315 if ($buttonName == $this->getButtonName('upload', 'new')) {
316 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"));
317 }
318 // reset the group contact cache for this group
319 CRM_Contact_BAO_GroupContactCache::remove();
320 }
321 }