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