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