Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-14-22-39-05
[civicrm-core.git] / CRM / Profile / Form / Edit.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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 */
44class CRM_Profile_Form_Edit extends CRM_Profile_Form {
45 protected $_postURL = NULL;
46 protected $_cancelURL = NULL;
47 protected $_errorURL = NULL;
48 protected $_context;
49 protected $_blockNo;
50 protected $_prefix;
51
52 /**
53 * pre processing work done here.
54 *
55 * @param
56 *
57 * @return void
58 *
59 * @access public
60 *
61 */
62 function preProcess() {
63 $this->_mode = CRM_Profile_Form::MODE_CREATE;
64
e5e3f1ad 65 $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this);
ac79535c 66
6a488035
TO
67 //set the context for the profile
68 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
69
70 //set the block no
71 $this->_blockNo = CRM_Utils_Request::retrieve('blockNo', 'String', $this);
72
73 //set the prefix
74 $this->_prefix = CRM_Utils_Request::retrieve('prefix', 'String', $this);
75
76 $this->assign('context', $this->_context);
77
78 if ($this->_blockNo) {
79 $this->assign('blockNo', $this->_blockNo);
80 $this->assign('prefix', $this->_prefix);
81 }
82
83 $this->assign('createCallback', CRM_Utils_Request::retrieve('createCallback', 'String', $this));
84
85 if ($this->get('skipPermission')) {
86 $this->_skipPermission = TRUE;
87 }
88
89 if ($this->get('edit')) {
90 // make sure we have right permission to edit this user
91 $session = CRM_Core_Session::singleton();
92 $userID = $session->get('userID');
93 $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, $userID);
94
95 if ($id) {
96 // this is edit mode.
97 $this->_mode = CRM_Profile_Form::MODE_EDIT;
98
99 if ($id != $userID) {
100 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
101 $config = CRM_Core_Config::singleton();
102 if ($config->userFrameworkFrontend) {
103 CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum($id, $this);
104 }
105 else {
106 CRM_Contact_BAO_Contact_Permission::validateChecksumContact($id, $this);
107 }
108 $this->_isPermissionedChecksum = TRUE;
109 }
110 }
111 }
112
113 parent::preProcess();
114
115 // make sure the gid is set and valid
116 if (!$this->_gid) {
117 CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) is disabled, OR there is no Profile with that ID, OR a valid \'gid=\' integer value is missing from the URL. Contact the site administrator if you need assistance.',
118 array(1 => $this->_gid)
119 ));
120 }
121
122 // and also the profile is of type 'Profile'
123 $query = "
124SELECT module
125 FROM civicrm_uf_join
126 WHERE module = 'Profile'
127 AND uf_group_id = %1
128";
129 $params = array(1 => array($this->_gid, 'Integer'));
130 $dao = CRM_Core_DAO::executeQuery($query, $params);
131 if (!$dao->fetch()) {
132 CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) is not configured to be used for \'Profile\' edit and view forms in its Settings. Contact the site administrator if you need assistance.',
133 array(1 => $this->_gid)
134 ));
135 }
136 }
137
138 /**
139 * Function to actually build the form
140 *
141 * @return void
142 * @access public
143 */
144 public function buildQuickForm() {
145 // add the hidden field to redirect the postProcess from
146 $ufGroup = new CRM_Core_DAO_UFGroup();
147
148 $ufGroup->id = $this->_gid;
149 if (!$ufGroup->find(TRUE)) {
150 CRM_Core_Error::fatal();
151 }
152
153 // set the title
154 if ($this->_multiRecord && $this->_customGroupTitle) {
155 $groupTitle = ($this->_multiRecord & CRM_Core_Action::UPDATE) ?
156 'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle;
157
158 } else {
159 $groupTitle = $ufGroup->title;
160 }
161 CRM_Utils_System::setTitle($groupTitle);
162 $this->assign('recentlyViewed', FALSE);
163
164 if ($this->_context != 'dialog') {
165 $this->_postURL = CRM_Utils_Array::value('postURL', $_POST);
166 $this->_cancelURL = CRM_Utils_Array::value('cancelURL', $_POST);
167
168 $gidString = $this->_gid;
169 if (!empty($this->_profileIds)) {
170 $gidString = implode(',', $this->_profileIds);
171 }
172
173 if (!$this->_postURL) {
174 $this->_postURL = $ufGroup->post_URL;
175 }
176
177 if (!$this->_postURL) {
178 if ($this->_context == 'Search') {
179 $this->_postURL = CRM_Utils_System::url('civicrm/contact/search');
180 }
181 elseif ($this->_id && $this->_gid) {
182 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
183 if ($this->_isContactActivityProfile && $this->_activityId) {
184 $urlParams .= "&aid={$this->_activityId}";
185 }
186 // get checksum if present
187 if ($this->get('cs')) {
188 $urlParams .= "&cs=" . $this->get('cs');
189 }
190 $this->_postURL = CRM_Utils_System::url('civicrm/profile/view', $urlParams);
191 }
192 }
193
194 if (!$this->_cancelURL) {
195 if ($ufGroup->cancel_URL) {
196 $this->_cancelURL = $ufGroup->cancel_URL;
197 }
198 else {
199 $this->_cancelURL = CRM_Utils_System::url('civicrm/profile',
200 "reset=1&gid={$gidString}"
201 );
202 }
203 }
204
205 if ($this->_multiRecordProfile) {
206 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
207
208 // get checksum if present
209 if ($this->get('cs')) {
210 $urlParams .= "&cs=" . $this->get('cs');
211 }
212 $this->_postURL = CRM_Utils_System::url('civicrm/profile/edit', $urlParams);
213 $this->_cancelURL = CRM_Utils_System::url('civicrm/profile/edit', $urlParams);
214
215 //passing the post url to template so the popup form does
216 //proper redirection and proccess form errors if any
136127f2
RN
217 if (!isset($this->_onPopupClose) || $this->_onPopupClose == 'redirectToProfile') {
218 $popupRedirect = CRM_Utils_System::url('civicrm/profile/edit', $urlParams, FALSE, NULL, FALSE);
219 }
220 elseif ($this->_onPopupClose == 'redirectToTab') {
ac79535c
RN
221 $popupRedirect = CRM_Utils_System::url('civicrm/contact/view',
222 "reset=1&cid={$this->_id}&selectedChild=custom_{$this->_customGroupId}", FALSE, NULL, FALSE);
223 }
224
6a488035
TO
225 $this->assign('urlParams', $urlParams);
226 $this->assign('postUrl', $popupRedirect);
227 }
228
229 // we do this gross hack since qf also does entity replacement
230 $this->_postURL = str_replace('&amp;', '&', $this->_postURL);
231 $this->_cancelURL = str_replace('&amp;', '&', $this->_cancelURL);
232
233 $this->addElement('hidden', 'postURL', $this->_postURL);
234 if ($this->_cancelURL) {
235 $this->addElement('hidden', 'cancelURL', $this->_cancelURL);
236 }
237
238 // also retain error URL if set
239 $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST);
240 if ($this->_errorURL) {
241 // we do this gross hack since qf also does entity replacement
242 $this->_errorURL = str_replace('&amp;', '&', $this->_errorURL);
243 $this->addElement('hidden', 'errorURL', $this->_errorURL);
244 }
245
246 // replace the session stack in case user cancels (and we dont go into postProcess)
247 $session = CRM_Core_Session::singleton();
248 $session->replaceUserContext($this->_postURL);
249 }
250
251 parent::buildQuickForm();
252
253 if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) {
254 $this->_deleteButtonName = $this->getButtonName('upload', 'delete');
255
256 $this->addElement('submit',
257 $this->_deleteButtonName,
258 ts('Delete')
259 );
260
261 $buttons[] = array(
262 'type' => 'cancel',
263 'name' => ts('Cancel'),
264 'isDefault' => TRUE,
265 );
266 $this->addButtons($buttons);
267 return;
268 }
269
270 //get the value from session, this is set if there is any file
271 //upload field
272 $uploadNames = $this->get('uploadNames');
273
274 if (!empty($uploadNames)) {
275 $buttonName = 'upload';
276 }
277 else {
278 $buttonName = 'next';
279 }
280
281 $buttons[] = array(
282 'type' => $buttonName,
283 'name' => ts('Save'),
284 'isDefault' => TRUE,
285 );
286
287 if ($this->_context != 'dialog') {
288 $buttons[] = array(
289 'type' => 'cancel',
290 'name' => ts('Cancel'),
291 'isDefault' => TRUE,
292 );
293 }
294
295 $this->addButtons($buttons);
296
297 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
298 }
299
300 /**
301 * Process the user submitted custom data values.
302 *
303 * @access public
304 *
305 * @return void
306 */
307 public function postProcess() {
308 parent::postProcess();
309
310 // this is special case when we create contact using Dialog box
311 if ($this->_context == 'dialog') {
abc8b55b 312 $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name');
6a488035
TO
313 $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name');
314 $returnArray = array(
315 'contactID' => $this->_id,
abc8b55b 316 'displayName' => $displayName,
6a488035
TO
317 'sortName' => $sortName,
318 'newContactSuccess' => TRUE,
319 );
320
321 echo json_encode($returnArray);
322 CRM_Utils_System::civiExit();
323 }
324
325 //for delete record handling
326 if (!CRM_Utils_Array::value($this->_deleteButtonName, $_POST)) {
327 CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
328 }
329
330 $session = CRM_Core_Session::singleton();
331 // only replace user context if we do not have a postURL
332 if (!$this->_postURL) {
333 $gidString = $this->_gid;
334 if (!empty($this->_profileIds)) {
335 $gidString = implode(',', $this->_profileIds);
336 }
337
338 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
339 if ($this->_isContactActivityProfile && $this->_activityId) {
340 $urlParams .= "&aid={$this->_activityId}";
341 }
342 // Get checksum if present
343 if ($this->get('cs')) {
344 $urlParams .= "&cs=" . $this->get('cs');
345 }
346 // Generate one if needed
347 elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) {
348 $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id);
349 }
350 $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams);
351 }
352 else {
353 // Replace tokens from post URL
354 $contactParams = array(
355 'contact_id' => $this->_id,
356 'version' => 3,
357 );
358
359 $contact = civicrm_api('contact', 'get', $contactParams);
360 $contact = reset($contact['values']);
361
362 $dummyMail = new CRM_Mailing_BAO_Mailing();
363 $dummyMail->body_text = $this->_postURL;
364 $tokens = $dummyMail->getTokens();
365
366 $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens));
367 }
368
369 $session->replaceUserContext($url);
370 }
371
372 /**
373 * Function to intercept QF validation and do our own redirection
374 *
375 * We use this to send control back to the user for a user formatted page
376 * This allows the user to maintain the same state and display the error messages
377 * in their own theme along with any modifications
378 *
379 * This is a first version and will be tweaked over a period of time
380 *
381 * @access public
382 *
383 * @return boolean true if no error found
384 */
385 function validate() {
386 $errors = parent::validate();
387
388 if (!$errors &&
389 CRM_Utils_Array::value('errorURL', $_POST)
390 ) {
391 $message = NULL;
392 foreach ($this->_errors as $name => $mess) {
393 $message .= $mess;
394 $message .= '<p>';
395 }
396
397 CRM_Utils_System::setUFMessage($message);
398
399 $message = urlencode($message);
400
401 $errorURL = $_POST['errorURL'];
402 if (strpos($errorURL, '?') !== FALSE) {
403 $errorURL .= '&';
404 }
405 else {
406 $errorURL .= '?';
407 }
408 $errorURL .= "gid={$this->_gid}&msg=$message";
409 CRM_Utils_System::redirect($errorURL);
410 }
411
412 return $errors;
413 }
414}
415