Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
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 | |
6a488035 | 58 | */ |
00be9182 | 59 | public function preProcess() { |
6a488035 TO |
60 | $this->_mode = CRM_Profile_Form::MODE_CREATE; |
61 | ||
e5e3f1ad | 62 | $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this); |
3a2e7d2b | 63 | $this->assign('onPopupClose', $this->_onPopupClose); |
ac79535c | 64 | |
6a488035 TO |
65 | //set the context for the profile |
66 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
67 | ||
68 | //set the block no | |
69 | $this->_blockNo = CRM_Utils_Request::retrieve('blockNo', 'String', $this); | |
70 | ||
71 | //set the prefix | |
72 | $this->_prefix = CRM_Utils_Request::retrieve('prefix', 'String', $this); | |
73 | ||
74 | $this->assign('context', $this->_context); | |
75 | ||
76 | if ($this->_blockNo) { | |
77 | $this->assign('blockNo', $this->_blockNo); | |
78 | $this->assign('prefix', $this->_prefix); | |
79 | } | |
80 | ||
81 | $this->assign('createCallback', CRM_Utils_Request::retrieve('createCallback', 'String', $this)); | |
82 | ||
83 | if ($this->get('skipPermission')) { | |
84 | $this->_skipPermission = TRUE; | |
85 | } | |
86 | ||
87 | if ($this->get('edit')) { | |
88 | // make sure we have right permission to edit this user | |
89 | $session = CRM_Core_Session::singleton(); | |
353ffa53 TO |
90 | $userID = $session->get('userID'); |
91 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, $userID); | |
6a488035 TO |
92 | |
93 | if ($id) { | |
94 | // this is edit mode. | |
95 | $this->_mode = CRM_Profile_Form::MODE_EDIT; | |
96 | ||
97 | if ($id != $userID) { | |
98 | // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228 | |
99 | $config = CRM_Core_Config::singleton(); | |
100 | if ($config->userFrameworkFrontend) { | |
101 | CRM_Contact_BAO_Contact_Permission::validateOnlyChecksum($id, $this); | |
102 | } | |
103 | else { | |
104 | CRM_Contact_BAO_Contact_Permission::validateChecksumContact($id, $this); | |
105 | } | |
106 | $this->_isPermissionedChecksum = TRUE; | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | parent::preProcess(); | |
112 | ||
6a488035 TO |
113 | // and also the profile is of type 'Profile' |
114 | $query = " | |
2e57a21b DG |
115 | SELECT module,is_reserved |
116 | FROM civicrm_uf_group | |
117 | LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id | |
74ee0fba | 118 | WHERE civicrm_uf_group.id = %1 |
6a488035 | 119 | "; |
74ee0fba | 120 | |
6a488035 TO |
121 | $params = array(1 => array($this->_gid, 'Integer')); |
122 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
2e57a21b | 123 | |
192e8d1a | 124 | $isProfile = FALSE; |
125 | while ($dao->fetch()) { | |
126 | $isProfile = ($isProfile || ($dao->module == "Profile")); | |
127 | } | |
2e57a21b | 128 | |
192e8d1a | 129 | //Check that the user has the "add contacts" Permission |
130 | $canAdd = CRM_Core_Permission::check("add contacts"); | |
2e57a21b | 131 | |
192e8d1a | 132 | //Remove need for Profile module type when using reserved profiles [CRM-14488] |
133 | if (!$dao->N || (!$isProfile && !($dao->is_reserved && $canAdd))) { | |
6a488035 | 134 | 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 | 135 | array(1 => $this->_gid) |
136 | )); | |
6a488035 TO |
137 | } |
138 | } | |
139 | ||
140 | /** | |
fe482240 | 141 | * Build the form object. |
6a488035 TO |
142 | * |
143 | * @return void | |
6a488035 TO |
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) { | |
dc98079b | 152 | $groupTitle = ($this->_multiRecord & CRM_Core_Action::UPDATE) ? 'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle; |
6a488035 | 153 | |
0db6c3e1 TO |
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 | ||
6a488035 TO |
193 | // we do this gross hack since qf also does entity replacement |
194 | $this->_postURL = str_replace('&', '&', $this->_postURL); | |
195 | $this->_cancelURL = str_replace('&', '&', $this->_cancelURL); | |
196 | ||
6a488035 TO |
197 | // also retain error URL if set |
198 | $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST); | |
199 | if ($this->_errorURL) { | |
200 | // we do this gross hack since qf also does entity replacement | |
201 | $this->_errorURL = str_replace('&', '&', $this->_errorURL); | |
202 | $this->addElement('hidden', 'errorURL', $this->_errorURL); | |
203 | } | |
204 | ||
205 | // replace the session stack in case user cancels (and we dont go into postProcess) | |
206 | $session = CRM_Core_Session::singleton(); | |
207 | $session->replaceUserContext($this->_postURL); | |
208 | } | |
209 | ||
210 | parent::buildQuickForm(); | |
211 | ||
fc942baa CW |
212 | $this->assign('cancelURL', $this->_cancelURL); |
213 | ||
6a488035 TO |
214 | if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) { |
215 | $this->_deleteButtonName = $this->getButtonName('upload', 'delete'); | |
216 | ||
7ce5cba4 | 217 | $this->addElement('submit', $this->_deleteButtonName, ts('Delete')); |
6a488035 | 218 | |
6a488035 TO |
219 | return; |
220 | } | |
221 | ||
222 | //get the value from session, this is set if there is any file | |
223 | //upload field | |
224 | $uploadNames = $this->get('uploadNames'); | |
225 | ||
226 | if (!empty($uploadNames)) { | |
227 | $buttonName = 'upload'; | |
228 | } | |
229 | else { | |
230 | $buttonName = 'next'; | |
231 | } | |
232 | ||
233 | $buttons[] = array( | |
234 | 'type' => $buttonName, | |
235 | 'name' => ts('Save'), | |
236 | 'isDefault' => TRUE, | |
237 | ); | |
238 | ||
6a488035 TO |
239 | $this->addButtons($buttons); |
240 | ||
241 | $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this); | |
242 | } | |
243 | ||
244 | /** | |
245 | * Process the user submitted custom data values. | |
246 | * | |
6a488035 TO |
247 | * |
248 | * @return void | |
249 | */ | |
250 | public function postProcess() { | |
251 | parent::postProcess(); | |
252 | ||
79ae07d9 CW |
253 | $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name'); |
254 | $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name'); | |
255 | $this->ajaxResponse['label'] = $sortName; | |
256 | ||
f90f3ac1 CW |
257 | // When saving (not deleting) and not in an ajax popup |
258 | if (empty($_POST[$this->_deleteButtonName]) && $this->_context != 'dialog') { | |
6a488035 TO |
259 | CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success'); |
260 | } | |
261 | ||
262 | $session = CRM_Core_Session::singleton(); | |
263 | // only replace user context if we do not have a postURL | |
264 | if (!$this->_postURL) { | |
265 | $gidString = $this->_gid; | |
266 | if (!empty($this->_profileIds)) { | |
267 | $gidString = implode(',', $this->_profileIds); | |
268 | } | |
269 | ||
270 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; | |
271 | if ($this->_isContactActivityProfile && $this->_activityId) { | |
272 | $urlParams .= "&aid={$this->_activityId}"; | |
273 | } | |
274 | // Get checksum if present | |
275 | if ($this->get('cs')) { | |
276 | $urlParams .= "&cs=" . $this->get('cs'); | |
277 | } | |
278 | // Generate one if needed | |
279 | elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) { | |
280 | $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id); | |
281 | } | |
282 | $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams); | |
283 | } | |
284 | else { | |
285 | // Replace tokens from post URL | |
286 | $contactParams = array( | |
287 | 'contact_id' => $this->_id, | |
288 | 'version' => 3, | |
289 | ); | |
290 | ||
291 | $contact = civicrm_api('contact', 'get', $contactParams); | |
292 | $contact = reset($contact['values']); | |
293 | ||
294 | $dummyMail = new CRM_Mailing_BAO_Mailing(); | |
295 | $dummyMail->body_text = $this->_postURL; | |
296 | $tokens = $dummyMail->getTokens(); | |
297 | ||
298 | $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens)); | |
299 | } | |
300 | ||
301 | $session->replaceUserContext($url); | |
302 | } | |
303 | ||
304 | /** | |
fe482240 | 305 | * Intercept QF validation and do our own redirection. |
6a488035 TO |
306 | * |
307 | * We use this to send control back to the user for a user formatted page | |
308 | * This allows the user to maintain the same state and display the error messages | |
309 | * in their own theme along with any modifications | |
310 | * | |
311 | * This is a first version and will be tweaked over a period of time | |
312 | * | |
6a488035 | 313 | * |
317fceb4 | 314 | * @return bool |
a6c01b45 | 315 | * true if no error found |
6a488035 | 316 | */ |
00be9182 | 317 | public function validate() { |
6a488035 TO |
318 | $errors = parent::validate(); |
319 | ||
8cc574cf | 320 | if (!$errors && !empty($_POST['errorURL'])) { |
6a488035 TO |
321 | $message = NULL; |
322 | foreach ($this->_errors as $name => $mess) { | |
323 | $message .= $mess; | |
324 | $message .= '<p>'; | |
325 | } | |
326 | ||
327 | CRM_Utils_System::setUFMessage($message); | |
328 | ||
329 | $message = urlencode($message); | |
330 | ||
331 | $errorURL = $_POST['errorURL']; | |
332 | if (strpos($errorURL, '?') !== FALSE) { | |
333 | $errorURL .= '&'; | |
334 | } | |
335 | else { | |
336 | $errorURL .= '?'; | |
337 | } | |
338 | $errorURL .= "gid={$this->_gid}&msg=$message"; | |
339 | CRM_Utils_System::redirect($errorURL); | |
340 | } | |
341 | ||
342 | return $errors; | |
343 | } | |
96025800 | 344 | |
6a488035 | 345 | } |