INFRA-132 - CRM/Profile - phpcbf
[civicrm-core.git] / CRM / Profile / Form / Edit.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 *
60 */
61 public function preProcess() {
62 $this->_mode = CRM_Profile_Form::MODE_CREATE;
63
64 $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this);
65 $this->assign('onPopupClose', $this->_onPopupClose);
66
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 // and also the profile is of type 'Profile'
116 $query = "
117 SELECT module,is_reserved
118 FROM civicrm_uf_group
119 LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id
120 WHERE civicrm_uf_group.id = %1
121 ";
122
123 $params = array(1 => array($this->_gid, 'Integer'));
124 $dao = CRM_Core_DAO::executeQuery($query, $params);
125
126 $isProfile = FALSE;
127 while ($dao->fetch()) {
128 $isProfile = ($isProfile || ($dao->module == "Profile"));
129 }
130
131 //Check that the user has the "add contacts" Permission
132 $canAdd = CRM_Core_Permission::check("add contacts");
133
134 //Remove need for Profile module type when using reserved profiles [CRM-14488]
135 if (!$dao->N || (!$isProfile && !($dao->is_reserved && $canAdd))) {
136 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.',
137 array(1 => $this->_gid)
138 ));
139 }
140 }
141
142 /**
143 * Build the form object
144 *
145 * @return void
146 */
147 public function buildQuickForm() {
148 if (empty($this->_ufGroup['id'])) {
149 CRM_Core_Error::fatal();
150 }
151
152 // set the title
153 if ($this->_multiRecord && $this->_customGroupTitle) {
154 $groupTitle = ($this->_multiRecord & CRM_Core_Action::UPDATE) ? 'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle;
155
156 } else {
157 $groupTitle = $this->_ufGroup['title'];
158 }
159 CRM_Utils_System::setTitle($groupTitle);
160 $this->assign('recentlyViewed', FALSE);
161
162 if ($this->_context != 'dialog') {
163 $this->_postURL = $this->_ufGroup['post_URL'];
164 $this->_cancelURL = $this->_ufGroup['cancel_URL'];
165
166 $gidString = $this->_gid;
167 if (!empty($this->_profileIds)) {
168 $gidString = implode(',', $this->_profileIds);
169 }
170
171 if (!$this->_postURL) {
172 if ($this->_context == 'Search') {
173 $this->_postURL = CRM_Utils_System::url('civicrm/contact/search');
174 }
175 elseif ($this->_id && $this->_gid) {
176 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
177 if ($this->_isContactActivityProfile && $this->_activityId) {
178 $urlParams .= "&aid={$this->_activityId}";
179 }
180 // get checksum if present
181 if ($this->get('cs')) {
182 $urlParams .= "&cs=" . $this->get('cs');
183 }
184 $this->_postURL = CRM_Utils_System::url('civicrm/profile/view', $urlParams);
185 }
186 }
187
188 if (!$this->_cancelURL) {
189 $this->_cancelURL = CRM_Utils_System::url('civicrm/profile',
190 "reset=1&gid={$gidString}"
191 );
192 }
193
194 // we do this gross hack since qf also does entity replacement
195 $this->_postURL = str_replace('&amp;', '&', $this->_postURL);
196 $this->_cancelURL = str_replace('&amp;', '&', $this->_cancelURL);
197
198 // also retain error URL if set
199 $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST);
200 if ($this->_errorURL) {
201 // we do this gross hack since qf also does entity replacement
202 $this->_errorURL = str_replace('&amp;', '&', $this->_errorURL);
203 $this->addElement('hidden', 'errorURL', $this->_errorURL);
204 }
205
206 // replace the session stack in case user cancels (and we dont go into postProcess)
207 $session = CRM_Core_Session::singleton();
208 $session->replaceUserContext($this->_postURL);
209 }
210
211 parent::buildQuickForm();
212
213 $this->assign('cancelURL', $this->_cancelURL);
214
215 if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) {
216 $this->_deleteButtonName = $this->getButtonName('upload', 'delete');
217
218 $this->addElement('submit', $this->_deleteButtonName, ts('Delete'));
219
220 return;
221 }
222
223 //get the value from session, this is set if there is any file
224 //upload field
225 $uploadNames = $this->get('uploadNames');
226
227 if (!empty($uploadNames)) {
228 $buttonName = 'upload';
229 }
230 else {
231 $buttonName = 'next';
232 }
233
234 $buttons[] = array(
235 'type' => $buttonName,
236 'name' => ts('Save'),
237 'isDefault' => TRUE,
238 );
239
240 $this->addButtons($buttons);
241
242 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
243 }
244
245 /**
246 * Process the user submitted custom data values.
247 *
248 *
249 * @return void
250 */
251 public function postProcess() {
252 parent::postProcess();
253
254 $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name');
255 $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name');
256 $this->ajaxResponse['label'] = $sortName;
257
258 // When saving (not deleting) and not in an ajax popup
259 if (empty($_POST[$this->_deleteButtonName]) && $this->_context != 'dialog') {
260 CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
261 }
262
263 $session = CRM_Core_Session::singleton();
264 // only replace user context if we do not have a postURL
265 if (!$this->_postURL) {
266 $gidString = $this->_gid;
267 if (!empty($this->_profileIds)) {
268 $gidString = implode(',', $this->_profileIds);
269 }
270
271 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
272 if ($this->_isContactActivityProfile && $this->_activityId) {
273 $urlParams .= "&aid={$this->_activityId}";
274 }
275 // Get checksum if present
276 if ($this->get('cs')) {
277 $urlParams .= "&cs=" . $this->get('cs');
278 }
279 // Generate one if needed
280 elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) {
281 $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id);
282 }
283 $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams);
284 }
285 else {
286 // Replace tokens from post URL
287 $contactParams = array(
288 'contact_id' => $this->_id,
289 'version' => 3,
290 );
291
292 $contact = civicrm_api('contact', 'get', $contactParams);
293 $contact = reset($contact['values']);
294
295 $dummyMail = new CRM_Mailing_BAO_Mailing();
296 $dummyMail->body_text = $this->_postURL;
297 $tokens = $dummyMail->getTokens();
298
299 $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens));
300 }
301
302 $session->replaceUserContext($url);
303 }
304
305 /**
306 * Intercept QF validation and do our own redirection
307 *
308 * We use this to send control back to the user for a user formatted page
309 * This allows the user to maintain the same state and display the error messages
310 * in their own theme along with any modifications
311 *
312 * This is a first version and will be tweaked over a period of time
313 *
314 *
315 * @return boolean true if no error found
316 */
317 public function validate() {
318 $errors = parent::validate();
319
320 if (!$errors && !empty($_POST['errorURL'])) {
321 $message = NULL;
322 foreach ($this->_errors as $name => $mess) {
323 $message .= $mess;
324 $message .= '<p>';
325 }
326
327 CRM_Utils_System::setUFMessage($message);
328
329 $message = urlencode($message);
330
331 $errorURL = $_POST['errorURL'];
332 if (strpos($errorURL, '?') !== FALSE) {
333 $errorURL .= '&';
334 }
335 else {
336 $errorURL .= '?';
337 }
338 $errorURL .= "gid={$this->_gid}&msg=$message";
339 CRM_Utils_System::redirect($errorURL);
340 }
341
342 return $errors;
343 }
344 }