Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | * |
33 | */ | |
34 | ||
35 | /** | |
36 | * This class generates form components for custom data | |
37 | * | |
38 | * It delegates the work to lower level subclasses and integrates the changes | |
39 | * back in. It also uses a lot of functionality with the CRM API's, so any change | |
40 | * made here could potentially affect the API etc. Be careful, be aware, use unit tests. | |
41 | * | |
42 | */ | |
43 | class CRM_Profile_Form_Edit extends CRM_Profile_Form { | |
44 | protected $_postURL = NULL; | |
45 | protected $_cancelURL = NULL; | |
46 | protected $_errorURL = NULL; | |
47 | protected $_context; | |
48 | protected $_blockNo; | |
49 | protected $_prefix; | |
2b63c577 | 50 | protected $returnExtra; |
6a488035 TO |
51 | |
52 | /** | |
100fef9d | 53 | * Pre processing work done here. |
6a488035 TO |
54 | * |
55 | * @param | |
56 | * | |
6a488035 | 57 | */ |
00be9182 | 58 | public function preProcess() { |
6a488035 TO |
59 | $this->_mode = CRM_Profile_Form::MODE_CREATE; |
60 | ||
e5e3f1ad | 61 | $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this); |
3a2e7d2b | 62 | $this->assign('onPopupClose', $this->_onPopupClose); |
ac79535c | 63 | |
6a488035 | 64 | //set the context for the profile |
edc80cda | 65 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
66 | |
67 | //set the block no | |
68 | $this->_blockNo = CRM_Utils_Request::retrieve('blockNo', 'String', $this); | |
69 | ||
70 | //set the prefix | |
71 | $this->_prefix = CRM_Utils_Request::retrieve('prefix', 'String', $this); | |
72 | ||
882a1be2 | 73 | // Fields for the EntityRef widget |
2b63c577 | 74 | $this->returnExtra = CRM_Utils_Request::retrieve('returnExtra', 'String', $this); |
882a1be2 | 75 | |
6a488035 TO |
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 | |
34f493fb | 91 | $userID = CRM_Core_Session::getLoggedInContactID(); |
1a447f39 J |
92 | |
93 | // Set the ID from the query string, otherwise default to the current user | |
353ffa53 | 94 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, $userID); |
6a488035 TO |
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 | |
541d99d7 | 102 | // see also CRM-19079 for modifications to the condition |
6a488035 | 103 | $config = CRM_Core_Config::singleton(); |
541d99d7 | 104 | if ($config->userFrameworkFrontend && $config->userSystem->is_joomla) { |
6a488035 TO |
105 | CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum($id, $this); |
106 | } | |
107 | else { | |
108 | CRM_Contact_BAO_Contact_Permission::validateChecksumContact($id, $this); | |
109 | } | |
110 | $this->_isPermissionedChecksum = TRUE; | |
111 | } | |
112 | } | |
1a447f39 J |
113 | |
114 | // CRM-16784: If there is no ID then this can't be an 'edit' | |
115 | else { | |
116 | CRM_Core_Error::fatal(ts('No user/contact ID was specified, so the Profile cannot be used in edit mode.')); | |
117 | } | |
118 | ||
6a488035 TO |
119 | } |
120 | ||
121 | parent::preProcess(); | |
122 | ||
6a488035 TO |
123 | // and also the profile is of type 'Profile' |
124 | $query = " | |
2e57a21b DG |
125 | SELECT module,is_reserved |
126 | FROM civicrm_uf_group | |
127 | LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id | |
74ee0fba | 128 | WHERE civicrm_uf_group.id = %1 |
6a488035 | 129 | "; |
74ee0fba | 130 | |
be2fb01f | 131 | $params = [1 => [$this->_gid, 'Integer']]; |
6a488035 | 132 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
2e57a21b | 133 | |
192e8d1a | 134 | $isProfile = FALSE; |
135 | while ($dao->fetch()) { | |
136 | $isProfile = ($isProfile || ($dao->module == "Profile")); | |
137 | } | |
2e57a21b | 138 | |
192e8d1a | 139 | //Check that the user has the "add contacts" Permission |
140 | $canAdd = CRM_Core_Permission::check("add contacts"); | |
2e57a21b | 141 | |
192e8d1a | 142 | //Remove need for Profile module type when using reserved profiles [CRM-14488] |
143 | if (!$dao->N || (!$isProfile && !($dao->is_reserved && $canAdd))) { | |
6a488035 | 144 | 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.', |
be2fb01f | 145 | [1 => $this->_gid] |
192e8d1a | 146 | )); |
6a488035 TO |
147 | } |
148 | } | |
149 | ||
150 | /** | |
fe482240 | 151 | * Build the form object. |
6a488035 | 152 | * |
6a488035 TO |
153 | */ |
154 | public function buildQuickForm() { | |
a3dbd1bc | 155 | if (empty($this->_ufGroup['id'])) { |
6a488035 TO |
156 | CRM_Core_Error::fatal(); |
157 | } | |
158 | ||
159 | // set the title | |
160 | if ($this->_multiRecord && $this->_customGroupTitle) { | |
dc98079b | 161 | $groupTitle = ($this->_multiRecord & CRM_Core_Action::UPDATE) ? 'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle; |
6a488035 | 162 | |
0db6c3e1 TO |
163 | } |
164 | else { | |
a3dbd1bc | 165 | $groupTitle = $this->_ufGroup['title']; |
6a488035 TO |
166 | } |
167 | CRM_Utils_System::setTitle($groupTitle); | |
168 | $this->assign('recentlyViewed', FALSE); | |
169 | ||
170 | if ($this->_context != 'dialog') { | |
a3dbd1bc TO |
171 | $this->_postURL = $this->_ufGroup['post_URL']; |
172 | $this->_cancelURL = $this->_ufGroup['cancel_URL']; | |
6a488035 TO |
173 | |
174 | $gidString = $this->_gid; | |
175 | if (!empty($this->_profileIds)) { | |
176 | $gidString = implode(',', $this->_profileIds); | |
177 | } | |
178 | ||
6a488035 TO |
179 | if (!$this->_postURL) { |
180 | if ($this->_context == 'Search') { | |
181 | $this->_postURL = CRM_Utils_System::url('civicrm/contact/search'); | |
182 | } | |
183 | elseif ($this->_id && $this->_gid) { | |
184 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
185 | if ($this->_isContactActivityProfile && $this->_activityId) { | |
186 | $urlParams .= "&aid={$this->_activityId}"; | |
187 | } | |
188 | // get checksum if present | |
189 | if ($this->get('cs')) { | |
190 | $urlParams .= "&cs=" . $this->get('cs'); | |
191 | } | |
192 | $this->_postURL = CRM_Utils_System::url('civicrm/profile/view', $urlParams); | |
193 | } | |
194 | } | |
195 | ||
196 | if (!$this->_cancelURL) { | |
a3dbd1bc TO |
197 | $this->_cancelURL = CRM_Utils_System::url('civicrm/profile', |
198 | "reset=1&gid={$gidString}" | |
199 | ); | |
6a488035 TO |
200 | } |
201 | ||
6a488035 TO |
202 | // we do this gross hack since qf also does entity replacement |
203 | $this->_postURL = str_replace('&', '&', $this->_postURL); | |
204 | $this->_cancelURL = str_replace('&', '&', $this->_cancelURL); | |
205 | ||
6a488035 TO |
206 | // also retain error URL if set |
207 | $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST); | |
208 | if ($this->_errorURL) { | |
209 | // we do this gross hack since qf also does entity replacement | |
210 | $this->_errorURL = str_replace('&', '&', $this->_errorURL); | |
211 | $this->addElement('hidden', 'errorURL', $this->_errorURL); | |
212 | } | |
213 | ||
214 | // replace the session stack in case user cancels (and we dont go into postProcess) | |
215 | $session = CRM_Core_Session::singleton(); | |
216 | $session->replaceUserContext($this->_postURL); | |
217 | } | |
218 | ||
219 | parent::buildQuickForm(); | |
220 | ||
fc942baa CW |
221 | $this->assign('cancelURL', $this->_cancelURL); |
222 | ||
c48f851a SL |
223 | $cancelButtonValue = !empty($this->_ufGroup['cancel_button_text']) ? $this->_ufGroup['cancel_button_text'] : ts('Cancel'); |
224 | $this->assign('cancelButtonText', $cancelButtonValue); | |
b3f1028c | 225 | $this->assign('includeCancelButton', CRM_Utils_Array::value('add_cancel_button', $this->_ufGroup)); |
c48f851a | 226 | |
6a488035 TO |
227 | if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) { |
228 | $this->_deleteButtonName = $this->getButtonName('upload', 'delete'); | |
7ce5cba4 | 229 | $this->addElement('submit', $this->_deleteButtonName, ts('Delete')); |
6a488035 | 230 | |
6a488035 TO |
231 | return; |
232 | } | |
233 | ||
234 | //get the value from session, this is set if there is any file | |
235 | //upload field | |
236 | $uploadNames = $this->get('uploadNames'); | |
237 | ||
238 | if (!empty($uploadNames)) { | |
239 | $buttonName = 'upload'; | |
240 | } | |
241 | else { | |
242 | $buttonName = 'next'; | |
243 | } | |
244 | ||
be2fb01f | 245 | $buttons[] = [ |
6a488035 | 246 | 'type' => $buttonName, |
c48f851a | 247 | 'name' => !empty($this->_ufGroup['submit_button_text']) ? $this->_ufGroup['submit_button_text'] : ts('Save'), |
6a488035 | 248 | 'isDefault' => TRUE, |
be2fb01f | 249 | ]; |
6a488035 | 250 | |
6a488035 TO |
251 | $this->addButtons($buttons); |
252 | ||
be2fb01f | 253 | $this->addFormRule(['CRM_Profile_Form', 'formRule'], $this); |
6a488035 TO |
254 | } |
255 | ||
256 | /** | |
257 | * Process the user submitted custom data values. | |
258 | * | |
6a488035 TO |
259 | */ |
260 | public function postProcess() { | |
261 | parent::postProcess(); | |
262 | ||
882a1be2 | 263 | // Send back data for the EntityRef widget |
2b63c577 | 264 | if ($this->returnExtra) { |
be2fb01f | 265 | $contact = civicrm_api3('Contact', 'getsingle', [ |
2b63c577 CW |
266 | 'id' => $this->_id, |
267 | 'return' => $this->returnExtra, | |
be2fb01f | 268 | ]); |
2b63c577 CW |
269 | foreach (explode(',', $this->returnExtra) as $field) { |
270 | $field = trim($field); | |
271 | $this->ajaxResponse['extra'][$field] = CRM_Utils_Array::value($field, $contact); | |
272 | } | |
882a1be2 | 273 | } |
79ae07d9 | 274 | |
f90f3ac1 CW |
275 | // When saving (not deleting) and not in an ajax popup |
276 | if (empty($_POST[$this->_deleteButtonName]) && $this->_context != 'dialog') { | |
6a488035 TO |
277 | CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success'); |
278 | } | |
279 | ||
280 | $session = CRM_Core_Session::singleton(); | |
281 | // only replace user context if we do not have a postURL | |
282 | if (!$this->_postURL) { | |
283 | $gidString = $this->_gid; | |
284 | if (!empty($this->_profileIds)) { | |
285 | $gidString = implode(',', $this->_profileIds); | |
286 | } | |
287 | ||
288 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
289 | if ($this->_isContactActivityProfile && $this->_activityId) { | |
290 | $urlParams .= "&aid={$this->_activityId}"; | |
291 | } | |
292 | // Get checksum if present | |
293 | if ($this->get('cs')) { | |
294 | $urlParams .= "&cs=" . $this->get('cs'); | |
295 | } | |
296 | // Generate one if needed | |
297 | elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) { | |
298 | $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id); | |
299 | } | |
300 | $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams); | |
301 | } | |
302 | else { | |
303 | // Replace tokens from post URL | |
be2fb01f | 304 | $contactParams = [ |
6a488035 TO |
305 | 'contact_id' => $this->_id, |
306 | 'version' => 3, | |
be2fb01f | 307 | ]; |
6a488035 TO |
308 | |
309 | $contact = civicrm_api('contact', 'get', $contactParams); | |
310 | $contact = reset($contact['values']); | |
311 | ||
312 | $dummyMail = new CRM_Mailing_BAO_Mailing(); | |
313 | $dummyMail->body_text = $this->_postURL; | |
314 | $tokens = $dummyMail->getTokens(); | |
315 | ||
316 | $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens)); | |
317 | } | |
318 | ||
319 | $session->replaceUserContext($url); | |
320 | } | |
321 | ||
322 | /** | |
fe482240 | 323 | * Intercept QF validation and do our own redirection. |
6a488035 TO |
324 | * |
325 | * We use this to send control back to the user for a user formatted page | |
326 | * This allows the user to maintain the same state and display the error messages | |
327 | * in their own theme along with any modifications | |
328 | * | |
329 | * This is a first version and will be tweaked over a period of time | |
330 | * | |
6a488035 | 331 | * |
317fceb4 | 332 | * @return bool |
a6c01b45 | 333 | * true if no error found |
6a488035 | 334 | */ |
00be9182 | 335 | public function validate() { |
6a488035 TO |
336 | $errors = parent::validate(); |
337 | ||
8cc574cf | 338 | if (!$errors && !empty($_POST['errorURL'])) { |
6a488035 TO |
339 | $message = NULL; |
340 | foreach ($this->_errors as $name => $mess) { | |
341 | $message .= $mess; | |
342 | $message .= '<p>'; | |
343 | } | |
344 | ||
345 | CRM_Utils_System::setUFMessage($message); | |
346 | ||
347 | $message = urlencode($message); | |
348 | ||
349 | $errorURL = $_POST['errorURL']; | |
350 | if (strpos($errorURL, '?') !== FALSE) { | |
351 | $errorURL .= '&'; | |
352 | } | |
353 | else { | |
354 | $errorURL .= '?'; | |
355 | } | |
356 | $errorURL .= "gid={$this->_gid}&msg=$message"; | |
357 | CRM_Utils_System::redirect($errorURL); | |
358 | } | |
359 | ||
360 | return $errors; | |
361 | } | |
96025800 | 362 | |
6a488035 | 363 | } |