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