Commit | Line | Data |
---|---|---|
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 | */ | |
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 | ||
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 = " | |
124 | SELECT 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 | |
217 | $popupRedirect = CRM_Utils_System::url('civicrm/profile/edit', $urlParams, FALSE, NULL, FALSE); | |
ac79535c | 218 | |
e5e3f1ad | 219 | if ($this->_onPopupClose == 'redirectToTab') { |
ac79535c RN |
220 | $popupRedirect = CRM_Utils_System::url('civicrm/contact/view', |
221 | "reset=1&cid={$this->_id}&selectedChild=custom_{$this->_customGroupId}", FALSE, NULL, FALSE); | |
222 | } | |
223 | ||
6a488035 TO |
224 | $this->assign('urlParams', $urlParams); |
225 | $this->assign('postUrl', $popupRedirect); | |
226 | } | |
227 | ||
228 | // we do this gross hack since qf also does entity replacement | |
229 | $this->_postURL = str_replace('&', '&', $this->_postURL); | |
230 | $this->_cancelURL = str_replace('&', '&', $this->_cancelURL); | |
231 | ||
232 | $this->addElement('hidden', 'postURL', $this->_postURL); | |
233 | if ($this->_cancelURL) { | |
234 | $this->addElement('hidden', 'cancelURL', $this->_cancelURL); | |
235 | } | |
236 | ||
237 | // also retain error URL if set | |
238 | $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST); | |
239 | if ($this->_errorURL) { | |
240 | // we do this gross hack since qf also does entity replacement | |
241 | $this->_errorURL = str_replace('&', '&', $this->_errorURL); | |
242 | $this->addElement('hidden', 'errorURL', $this->_errorURL); | |
243 | } | |
244 | ||
245 | // replace the session stack in case user cancels (and we dont go into postProcess) | |
246 | $session = CRM_Core_Session::singleton(); | |
247 | $session->replaceUserContext($this->_postURL); | |
248 | } | |
249 | ||
250 | parent::buildQuickForm(); | |
251 | ||
252 | if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) { | |
253 | $this->_deleteButtonName = $this->getButtonName('upload', 'delete'); | |
254 | ||
255 | $this->addElement('submit', | |
256 | $this->_deleteButtonName, | |
257 | ts('Delete') | |
258 | ); | |
259 | ||
260 | $buttons[] = array( | |
261 | 'type' => 'cancel', | |
262 | 'name' => ts('Cancel'), | |
263 | 'isDefault' => TRUE, | |
264 | ); | |
265 | $this->addButtons($buttons); | |
266 | return; | |
267 | } | |
268 | ||
269 | //get the value from session, this is set if there is any file | |
270 | //upload field | |
271 | $uploadNames = $this->get('uploadNames'); | |
272 | ||
273 | if (!empty($uploadNames)) { | |
274 | $buttonName = 'upload'; | |
275 | } | |
276 | else { | |
277 | $buttonName = 'next'; | |
278 | } | |
279 | ||
280 | $buttons[] = array( | |
281 | 'type' => $buttonName, | |
282 | 'name' => ts('Save'), | |
283 | 'isDefault' => TRUE, | |
284 | ); | |
285 | ||
286 | if ($this->_context != 'dialog') { | |
287 | $buttons[] = array( | |
288 | 'type' => 'cancel', | |
289 | 'name' => ts('Cancel'), | |
290 | 'isDefault' => TRUE, | |
291 | ); | |
292 | } | |
293 | ||
294 | $this->addButtons($buttons); | |
295 | ||
296 | $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this); | |
297 | } | |
298 | ||
299 | /** | |
300 | * Process the user submitted custom data values. | |
301 | * | |
302 | * @access public | |
303 | * | |
304 | * @return void | |
305 | */ | |
306 | public function postProcess() { | |
307 | parent::postProcess(); | |
308 | ||
309 | // this is special case when we create contact using Dialog box | |
310 | if ($this->_context == 'dialog') { | |
311 | $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name'); | |
312 | $returnArray = array( | |
313 | 'contactID' => $this->_id, | |
314 | 'sortName' => $sortName, | |
315 | 'newContactSuccess' => TRUE, | |
316 | ); | |
317 | ||
318 | echo json_encode($returnArray); | |
319 | CRM_Utils_System::civiExit(); | |
320 | } | |
321 | ||
322 | //for delete record handling | |
323 | if (!CRM_Utils_Array::value($this->_deleteButtonName, $_POST)) { | |
324 | CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success'); | |
325 | } | |
326 | ||
327 | $session = CRM_Core_Session::singleton(); | |
328 | // only replace user context if we do not have a postURL | |
329 | if (!$this->_postURL) { | |
330 | $gidString = $this->_gid; | |
331 | if (!empty($this->_profileIds)) { | |
332 | $gidString = implode(',', $this->_profileIds); | |
333 | } | |
334 | ||
335 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
336 | if ($this->_isContactActivityProfile && $this->_activityId) { | |
337 | $urlParams .= "&aid={$this->_activityId}"; | |
338 | } | |
339 | // Get checksum if present | |
340 | if ($this->get('cs')) { | |
341 | $urlParams .= "&cs=" . $this->get('cs'); | |
342 | } | |
343 | // Generate one if needed | |
344 | elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) { | |
345 | $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id); | |
346 | } | |
347 | $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams); | |
348 | } | |
349 | else { | |
350 | // Replace tokens from post URL | |
351 | $contactParams = array( | |
352 | 'contact_id' => $this->_id, | |
353 | 'version' => 3, | |
354 | ); | |
355 | ||
356 | $contact = civicrm_api('contact', 'get', $contactParams); | |
357 | $contact = reset($contact['values']); | |
358 | ||
359 | $dummyMail = new CRM_Mailing_BAO_Mailing(); | |
360 | $dummyMail->body_text = $this->_postURL; | |
361 | $tokens = $dummyMail->getTokens(); | |
362 | ||
363 | $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens)); | |
364 | } | |
365 | ||
366 | $session->replaceUserContext($url); | |
367 | } | |
368 | ||
369 | /** | |
370 | * Function to intercept QF validation and do our own redirection | |
371 | * | |
372 | * We use this to send control back to the user for a user formatted page | |
373 | * This allows the user to maintain the same state and display the error messages | |
374 | * in their own theme along with any modifications | |
375 | * | |
376 | * This is a first version and will be tweaked over a period of time | |
377 | * | |
378 | * @access public | |
379 | * | |
380 | * @return boolean true if no error found | |
381 | */ | |
382 | function validate() { | |
383 | $errors = parent::validate(); | |
384 | ||
385 | if (!$errors && | |
386 | CRM_Utils_Array::value('errorURL', $_POST) | |
387 | ) { | |
388 | $message = NULL; | |
389 | foreach ($this->_errors as $name => $mess) { | |
390 | $message .= $mess; | |
391 | $message .= '<p>'; | |
392 | } | |
393 | ||
394 | CRM_Utils_System::setUFMessage($message); | |
395 | ||
396 | $message = urlencode($message); | |
397 | ||
398 | $errorURL = $_POST['errorURL']; | |
399 | if (strpos($errorURL, '?') !== FALSE) { | |
400 | $errorURL .= '&'; | |
401 | } | |
402 | else { | |
403 | $errorURL .= '?'; | |
404 | } | |
405 | $errorURL .= "gid={$this->_gid}&msg=$message"; | |
406 | CRM_Utils_System::redirect($errorURL); | |
407 | } | |
408 | ||
409 | return $errors; | |
410 | } | |
411 | } | |
412 |