Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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); |
3a2e7d2b | 66 | $this->assign('onPopupClose', $this->_onPopupClose); |
ac79535c | 67 | |
6a488035 TO |
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 | ||
cacc57d7 TO |
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 | ||
6a488035 TO |
123 | // and also the profile is of type 'Profile' |
124 | $query = " | |
125 | SELECT module | |
126 | FROM civicrm_uf_join | |
127 | WHERE module = 'Profile' | |
128 | AND uf_group_id = %1 | |
129 | "; | |
130 | $params = array(1 => array($this->_gid, 'Integer')); | |
131 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
132 | if (!$dao->fetch()) { | |
133 | 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.', | |
134 | array(1 => $this->_gid) | |
135 | )); | |
136 | } | |
137 | } | |
138 | ||
139 | /** | |
140 | * Function to actually build the form | |
141 | * | |
142 | * @return void | |
143 | * @access public | |
144 | */ | |
145 | public function buildQuickForm() { | |
a3dbd1bc | 146 | if (empty($this->_ufGroup['id'])) { |
6a488035 TO |
147 | CRM_Core_Error::fatal(); |
148 | } | |
149 | ||
150 | // set the title | |
151 | if ($this->_multiRecord && $this->_customGroupTitle) { | |
152 | $groupTitle = ($this->_multiRecord & CRM_Core_Action::UPDATE) ? | |
153 | 'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle; | |
154 | ||
155 | } else { | |
a3dbd1bc | 156 | $groupTitle = $this->_ufGroup['title']; |
6a488035 TO |
157 | } |
158 | CRM_Utils_System::setTitle($groupTitle); | |
159 | $this->assign('recentlyViewed', FALSE); | |
160 | ||
161 | if ($this->_context != 'dialog') { | |
a3dbd1bc TO |
162 | $this->_postURL = $this->_ufGroup['post_URL']; |
163 | $this->_cancelURL = $this->_ufGroup['cancel_URL']; | |
6a488035 TO |
164 | |
165 | $gidString = $this->_gid; | |
166 | if (!empty($this->_profileIds)) { | |
167 | $gidString = implode(',', $this->_profileIds); | |
168 | } | |
169 | ||
6a488035 TO |
170 | if (!$this->_postURL) { |
171 | if ($this->_context == 'Search') { | |
172 | $this->_postURL = CRM_Utils_System::url('civicrm/contact/search'); | |
173 | } | |
174 | elseif ($this->_id && $this->_gid) { | |
175 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
176 | if ($this->_isContactActivityProfile && $this->_activityId) { | |
177 | $urlParams .= "&aid={$this->_activityId}"; | |
178 | } | |
179 | // get checksum if present | |
180 | if ($this->get('cs')) { | |
181 | $urlParams .= "&cs=" . $this->get('cs'); | |
182 | } | |
183 | $this->_postURL = CRM_Utils_System::url('civicrm/profile/view', $urlParams); | |
184 | } | |
185 | } | |
186 | ||
187 | if (!$this->_cancelURL) { | |
a3dbd1bc TO |
188 | $this->_cancelURL = CRM_Utils_System::url('civicrm/profile', |
189 | "reset=1&gid={$gidString}" | |
190 | ); | |
6a488035 TO |
191 | } |
192 | ||
193 | if ($this->_multiRecordProfile) { | |
194 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
195 | ||
196 | // get checksum if present | |
197 | if ($this->get('cs')) { | |
198 | $urlParams .= "&cs=" . $this->get('cs'); | |
199 | } | |
200 | $this->_postURL = CRM_Utils_System::url('civicrm/profile/edit', $urlParams); | |
201 | $this->_cancelURL = CRM_Utils_System::url('civicrm/profile/edit', $urlParams); | |
202 | ||
203 | //passing the post url to template so the popup form does | |
204 | //proper redirection and proccess form errors if any | |
136127f2 RN |
205 | if (!isset($this->_onPopupClose) || $this->_onPopupClose == 'redirectToProfile') { |
206 | $popupRedirect = CRM_Utils_System::url('civicrm/profile/edit', $urlParams, FALSE, NULL, FALSE); | |
207 | } | |
208 | elseif ($this->_onPopupClose == 'redirectToTab') { | |
2efcf0c2 | 209 | $popupRedirect = CRM_Utils_System::url('civicrm/contact/view', |
ac79535c RN |
210 | "reset=1&cid={$this->_id}&selectedChild=custom_{$this->_customGroupId}", FALSE, NULL, FALSE); |
211 | } | |
212 | ||
6a488035 TO |
213 | $this->assign('urlParams', $urlParams); |
214 | $this->assign('postUrl', $popupRedirect); | |
215 | } | |
216 | ||
217 | // we do this gross hack since qf also does entity replacement | |
218 | $this->_postURL = str_replace('&', '&', $this->_postURL); | |
219 | $this->_cancelURL = str_replace('&', '&', $this->_cancelURL); | |
220 | ||
6a488035 TO |
221 | // also retain error URL if set |
222 | $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST); | |
223 | if ($this->_errorURL) { | |
224 | // we do this gross hack since qf also does entity replacement | |
225 | $this->_errorURL = str_replace('&', '&', $this->_errorURL); | |
226 | $this->addElement('hidden', 'errorURL', $this->_errorURL); | |
227 | } | |
228 | ||
229 | // replace the session stack in case user cancels (and we dont go into postProcess) | |
230 | $session = CRM_Core_Session::singleton(); | |
231 | $session->replaceUserContext($this->_postURL); | |
232 | } | |
233 | ||
234 | parent::buildQuickForm(); | |
235 | ||
236 | if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) { | |
237 | $this->_deleteButtonName = $this->getButtonName('upload', 'delete'); | |
238 | ||
239 | $this->addElement('submit', | |
240 | $this->_deleteButtonName, | |
241 | ts('Delete') | |
242 | ); | |
243 | ||
244 | $buttons[] = array( | |
245 | 'type' => 'cancel', | |
246 | 'name' => ts('Cancel'), | |
247 | 'isDefault' => TRUE, | |
a29a45d4 | 248 | 'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"), |
6a488035 TO |
249 | ); |
250 | $this->addButtons($buttons); | |
251 | return; | |
252 | } | |
253 | ||
254 | //get the value from session, this is set if there is any file | |
255 | //upload field | |
256 | $uploadNames = $this->get('uploadNames'); | |
257 | ||
258 | if (!empty($uploadNames)) { | |
259 | $buttonName = 'upload'; | |
260 | } | |
261 | else { | |
262 | $buttonName = 'next'; | |
263 | } | |
264 | ||
265 | $buttons[] = array( | |
266 | 'type' => $buttonName, | |
267 | 'name' => ts('Save'), | |
268 | 'isDefault' => TRUE, | |
269 | ); | |
270 | ||
271 | if ($this->_context != 'dialog') { | |
272 | $buttons[] = array( | |
273 | 'type' => 'cancel', | |
274 | 'name' => ts('Cancel'), | |
275 | 'isDefault' => TRUE, | |
a29a45d4 | 276 | 'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"), |
6a488035 TO |
277 | ); |
278 | } | |
279 | ||
280 | $this->addButtons($buttons); | |
281 | ||
282 | $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this); | |
283 | } | |
284 | ||
285 | /** | |
286 | * Process the user submitted custom data values. | |
287 | * | |
288 | * @access public | |
289 | * | |
290 | * @return void | |
291 | */ | |
292 | public function postProcess() { | |
293 | parent::postProcess(); | |
294 | ||
295 | // this is special case when we create contact using Dialog box | |
296 | if ($this->_context == 'dialog') { | |
abc8b55b | 297 | $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name'); |
6a488035 TO |
298 | $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name'); |
299 | $returnArray = array( | |
300 | 'contactID' => $this->_id, | |
abc8b55b | 301 | 'displayName' => $displayName, |
6a488035 TO |
302 | 'sortName' => $sortName, |
303 | 'newContactSuccess' => TRUE, | |
304 | ); | |
305 | ||
306 | echo json_encode($returnArray); | |
307 | CRM_Utils_System::civiExit(); | |
308 | } | |
309 | ||
310 | //for delete record handling | |
311 | if (!CRM_Utils_Array::value($this->_deleteButtonName, $_POST)) { | |
312 | CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success'); | |
313 | } | |
314 | ||
315 | $session = CRM_Core_Session::singleton(); | |
316 | // only replace user context if we do not have a postURL | |
317 | if (!$this->_postURL) { | |
318 | $gidString = $this->_gid; | |
319 | if (!empty($this->_profileIds)) { | |
320 | $gidString = implode(',', $this->_profileIds); | |
321 | } | |
322 | ||
323 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
324 | if ($this->_isContactActivityProfile && $this->_activityId) { | |
325 | $urlParams .= "&aid={$this->_activityId}"; | |
326 | } | |
327 | // Get checksum if present | |
328 | if ($this->get('cs')) { | |
329 | $urlParams .= "&cs=" . $this->get('cs'); | |
330 | } | |
331 | // Generate one if needed | |
332 | elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) { | |
333 | $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id); | |
334 | } | |
335 | $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams); | |
336 | } | |
337 | else { | |
338 | // Replace tokens from post URL | |
339 | $contactParams = array( | |
340 | 'contact_id' => $this->_id, | |
341 | 'version' => 3, | |
342 | ); | |
343 | ||
344 | $contact = civicrm_api('contact', 'get', $contactParams); | |
345 | $contact = reset($contact['values']); | |
346 | ||
347 | $dummyMail = new CRM_Mailing_BAO_Mailing(); | |
348 | $dummyMail->body_text = $this->_postURL; | |
349 | $tokens = $dummyMail->getTokens(); | |
350 | ||
351 | $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens)); | |
352 | } | |
353 | ||
354 | $session->replaceUserContext($url); | |
355 | } | |
356 | ||
357 | /** | |
358 | * Function to intercept QF validation and do our own redirection | |
359 | * | |
360 | * We use this to send control back to the user for a user formatted page | |
361 | * This allows the user to maintain the same state and display the error messages | |
362 | * in their own theme along with any modifications | |
363 | * | |
364 | * This is a first version and will be tweaked over a period of time | |
365 | * | |
366 | * @access public | |
367 | * | |
368 | * @return boolean true if no error found | |
369 | */ | |
370 | function validate() { | |
371 | $errors = parent::validate(); | |
372 | ||
373 | if (!$errors && | |
374 | CRM_Utils_Array::value('errorURL', $_POST) | |
375 | ) { | |
376 | $message = NULL; | |
377 | foreach ($this->_errors as $name => $mess) { | |
378 | $message .= $mess; | |
379 | $message .= '<p>'; | |
380 | } | |
381 | ||
382 | CRM_Utils_System::setUFMessage($message); | |
383 | ||
384 | $message = urlencode($message); | |
385 | ||
386 | $errorURL = $_POST['errorURL']; | |
387 | if (strpos($errorURL, '?') !== FALSE) { | |
388 | $errorURL .= '&'; | |
389 | } | |
390 | else { | |
391 | $errorURL .= '?'; | |
392 | } | |
393 | $errorURL .= "gid={$this->_gid}&msg=$message"; | |
394 | CRM_Utils_System::redirect($errorURL); | |
395 | } | |
396 | ||
397 | return $errors; | |
398 | } | |
399 | } | |
400 |