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