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