Merge pull request #2708 from yashodha/sms-reminder
[civicrm-core.git] / CRM / Contact / Form / CustomData.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 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;
134 $this->_copyValueId = CRM_Utils_Request::retrieve('copyValueId', 'Positive', $this);
135 $backUrl = CRM_Core_Session::singleton()->readUserContext();
136 $this->assign('backUrl', $backUrl);
137 if (!empty($_POST['hidden_custom'])) {
138 $this->assign('postedInfo', TRUE);
139 }
140 }
141 return;
142 }
143 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
144 $this->_tableID = CRM_Utils_Request::retrieve('tableId', 'Positive', $this, TRUE);
145
146 $this->_contactType = CRM_Contact_BAO_Contact::getContactType($this->_tableID);
147 $this->_contactSubType = CRM_Contact_BAO_Contact::getContactSubType($this->_tableID, ',');
148 $this->assign('contact_type', $this->_contactType);
149 $this->assign('contact_subtype', $this->_contactSubType);
150 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($this->_tableID);
151 CRM_Utils_System::setTitle($displayName, $contactImage . ' ' . $displayName);
152
153 // when custom data is included in this page
154 if (!empty($_POST['hidden_custom'])) {
155 for ($i = 0; $i <= $_POST['hidden_custom_group_count'][$this->_groupID]; $i++) {
156 CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, $i);
157 CRM_Custom_Form_CustomData::buildQuickForm($this);
158 CRM_Custom_Form_CustomData::setDefaultValues($this);
159 }
160 }
161 }
162
163 /**
164 * Function to actually build the form
165 *
166 * @return void
167 * @access public
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 ? 'Save a Copy': 'Save';
180 $this->addButtons(array(
181 array(
182 'type' => 'upload',
183 'name' => ts('%1', array(1 => $saveButtonName)),
184 'isDefault' => TRUE,
185 ),
186 array(
187 'type' => 'cancel',
188 'name' => ts('Cancel'),
189 ),
190 )
191 );
192 }
193 }
194 return CRM_Custom_Form_CustomData::buildQuickForm($this);
195 }
196
197 //need to assign custom data type and subtype to the template
198 $this->assign('entityID', $this->_tableID);
199 $this->assign('groupID', $this->_groupID);
200
201 // make this form an upload since we dont know if the custom data injected dynamically
202 // is of type file etc
203 $this->addButtons(array(
204 array(
205 'type' => 'upload',
206 'name' => ts('Save'),
207 'isDefault' => TRUE,
208 ),
209 array(
210 'type' => 'cancel',
211 'name' => ts('Cancel'),
212 ),
213 )
214 );
215 }
216
217 /**
218 * Set the default form values
219 *
220 * @access protected
221 *
222 * @return array the default array reference
223 */
224 function setDefaultValues() {
225 if ($this->_cdType || $this->_multiRecordDisplay == 'single') {
226 if ($this->_copyValueId) {
227 // cached tree is fetched
228 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_type,
229 $this,
230 $this->_entityId,
231 $this->_groupID
232 );
233 $valueIdDefaults = array();
234 $groupTreeValueId = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $this->_copyValueId, $this);
235 CRM_Core_BAO_CustomGroup::setDefaults($groupTreeValueId, $valueIdDefaults, FALSE, FALSE, $this->get('action'));
236 $tableId = $groupTreeValueId[$this->_groupID]['table_id'];
237 foreach ($valueIdDefaults as $valueIdElementName => $value) {
238 // build defaults for COPY action for new record saving
239 $valueIdElementNamePieces = explode('_', $valueIdElementName);
240 $valueIdElementNamePieces[2] = "-{$this->_groupCount}";
241 $elementName = implode('_', $valueIdElementNamePieces);
242 $customDefaultValue[$elementName] = $value;
243 }
244 }
245 else {
246 $customDefaultValue = CRM_Custom_Form_CustomData::setDefaultValues($this);
247 }
248 return $customDefaultValue;
249 }
250
251 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType,
252 $this,
253 $this->_tableID,
254 $this->_groupID,
255 $this->_contactSubType
256 );
257
258 if (empty($_POST['hidden_custom_group_count'])) {
259 // custom data building in edit mode (required to handle multi-value)
260 $groupTree = &CRM_Core_BAO_CustomGroup::getTree($this->_contactType, $this, $this->_tableID,
261 $this->_groupID, $this->_contactSubType
262 );
263 $customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, TRUE, $this->_groupID);
264 }
265 else {
266 $customValueCount = $_POST['hidden_custom_group_count'][$this->_groupID];
267 }
268
269 $this->assign('customValueCount', $customValueCount);
270
271 $defaults = array();
272 return $defaults;
273 }
274
275 /**
276 * Process the user submitted custom data values.
277 *
278 * @access public
279 *
280 * @return void
281 */
282 public function postProcess() {
283 // Get the form values and groupTree
284 $params = $this->controller->exportValues($this->_name);
285 CRM_Core_BAO_CustomValueTable::postProcess($params,
286 $this->_groupTree[$this->_groupID]['fields'],
287 'civicrm_contact',
288 $this->_tableID,
289 $this->_entityType
290 );
291
292 // reset the group contact cache for this group
293 CRM_Contact_BAO_GroupContactCache::remove();
294 }
295 }