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