3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
36 * This class generates form components for custom data
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.
43 class CRM_Profile_Form_Edit
extends CRM_Profile_Form
{
44 protected $_postURL = NULL;
45 protected $_cancelURL = NULL;
46 protected $_errorURL = NULL;
50 protected $returnExtra;
53 * Pre processing work done here.
58 public function preProcess() {
59 $this->_mode
= CRM_Profile_Form
::MODE_CREATE
;
61 $this->_onPopupClose
= CRM_Utils_Request
::retrieve('onPopupClose', 'String', $this);
62 $this->assign('onPopupClose', $this->_onPopupClose
);
64 //set the context for the profile
65 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
68 $this->_blockNo
= CRM_Utils_Request
::retrieve('blockNo', 'String', $this);
71 $this->_prefix
= CRM_Utils_Request
::retrieve('prefix', 'String', $this);
73 // Fields for the EntityRef widget
74 $this->returnExtra
= CRM_Utils_Request
::retrieve('returnExtra', 'String', $this);
76 $this->assign('context', $this->_context
);
78 if ($this->_blockNo
) {
79 $this->assign('blockNo', $this->_blockNo
);
80 $this->assign('prefix', $this->_prefix
);
83 $this->assign('createCallback', CRM_Utils_Request
::retrieve('createCallback', 'String', $this));
85 if ($this->get('skipPermission')) {
86 $this->_skipPermission
= TRUE;
89 if ($this->get('edit')) {
90 // make sure we have right permission to edit this user
91 $userID = CRM_Core_Session
::getLoggedInContactID();
93 // Set the ID from the query string, otherwise default to the current user
94 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, $userID);
98 $this->_mode
= CRM_Profile_Form
::MODE_EDIT
;
100 if ($id != $userID) {
101 // do not allow edit for anon users in joomla frontend, CRM-4668, unless u have checksum CRM-5228
102 // see also CRM-19079 for modifications to the condition
103 $config = CRM_Core_Config
::singleton();
104 if ($config->userFrameworkFrontend
&& $config->userSystem
->is_joomla
) {
105 CRM_Contact_BAO_Contact_Permission
::validateOnlyChecksum($id, $this);
108 CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($id, $this);
110 $this->_isPermissionedChecksum
= TRUE;
114 // CRM-16784: If there is no ID then this can't be an 'edit'
116 CRM_Core_Error
::fatal(ts('No user/contact ID was specified, so the Profile cannot be used in edit mode.'));
121 parent
::preProcess();
123 // and also the profile is of type 'Profile'
125 SELECT module,is_reserved
126 FROM civicrm_uf_group
127 LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id
128 WHERE civicrm_uf_group.id = %1
131 $params = array(1 => array($this->_gid
, 'Integer'));
132 $dao = CRM_Core_DAO
::executeQuery($query, $params);
135 while ($dao->fetch()) {
136 $isProfile = ($isProfile ||
($dao->module
== "Profile"));
139 //Check that the user has the "add contacts" Permission
140 $canAdd = CRM_Core_Permission
::check("add contacts");
142 //Remove need for Profile module type when using reserved profiles [CRM-14488]
143 if (!$dao->N ||
(!$isProfile && !($dao->is_reserved
&& $canAdd))) {
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.',
145 array(1 => $this->_gid
)
151 * Build the form object.
154 public function buildQuickForm() {
155 if (empty($this->_ufGroup
['id'])) {
156 CRM_Core_Error
::fatal();
160 if ($this->_multiRecord
&& $this->_customGroupTitle
) {
161 $groupTitle = ($this->_multiRecord
& CRM_Core_Action
::UPDATE
) ?
'Edit ' . $this->_customGroupTitle
. ' Record' : $this->_customGroupTitle
;
165 $groupTitle = $this->_ufGroup
['title'];
167 CRM_Utils_System
::setTitle($groupTitle);
168 $this->assign('recentlyViewed', FALSE);
170 if ($this->_context
!= 'dialog') {
171 $this->_postURL
= $this->_ufGroup
['post_URL'];
172 $this->_cancelURL
= $this->_ufGroup
['cancel_URL'];
174 $gidString = $this->_gid
;
175 if (!empty($this->_profileIds
)) {
176 $gidString = implode(',', $this->_profileIds
);
179 if (!$this->_postURL
) {
180 if ($this->_context
== 'Search') {
181 $this->_postURL
= CRM_Utils_System
::url('civicrm/contact/search');
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}";
188 // get checksum if present
189 if ($this->get('cs')) {
190 $urlParams .= "&cs=" . $this->get('cs');
192 $this->_postURL
= CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
196 if (!$this->_cancelURL
) {
197 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/profile',
198 "reset=1&gid={$gidString}"
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
);
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
);
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
);
219 parent
::buildQuickForm();
221 $this->assign('cancelURL', $this->_cancelURL
);
223 $cancelButtonValue = !empty($this->_ufGroup
['cancel_button_text']) ?
$this->_ufGroup
['cancel_button_text'] : ts('Cancel');
224 $this->assign('cancelButtonText', $cancelButtonValue);
226 if (($this->_multiRecord
& CRM_Core_Action
::DELETE
) && $this->_recordExists
) {
227 $this->_deleteButtonName
= $this->getButtonName('upload', 'delete');
228 $this->addElement('submit', $this->_deleteButtonName
, ts('Delete'));
233 //get the value from session, this is set if there is any file
235 $uploadNames = $this->get('uploadNames');
237 if (!empty($uploadNames)) {
238 $buttonName = 'upload';
241 $buttonName = 'next';
245 'type' => $buttonName,
246 'name' => !empty($this->_ufGroup
['submit_button_text']) ?
$this->_ufGroup
['submit_button_text'] : ts('Save'),
250 $this->addButtons($buttons);
252 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
256 * Process the user submitted custom data values.
259 public function postProcess() {
260 parent
::postProcess();
262 // Send back data for the EntityRef widget
263 if ($this->returnExtra
) {
264 $contact = civicrm_api3('Contact', 'getsingle', array(
266 'return' => $this->returnExtra
,
268 foreach (explode(',', $this->returnExtra
) as $field) {
269 $field = trim($field);
270 $this->ajaxResponse
['extra'][$field] = CRM_Utils_Array
::value($field, $contact);
274 // When saving (not deleting) and not in an ajax popup
275 if (empty($_POST[$this->_deleteButtonName
]) && $this->_context
!= 'dialog') {
276 CRM_Core_Session
::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
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
);
287 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
288 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
289 $urlParams .= "&aid={$this->_activityId}";
291 // Get checksum if present
292 if ($this->get('cs')) {
293 $urlParams .= "&cs=" . $this->get('cs');
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
);
299 $url = CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
302 // Replace tokens from post URL
303 $contactParams = array(
304 'contact_id' => $this->_id
,
308 $contact = civicrm_api('contact', 'get', $contactParams);
309 $contact = reset($contact['values']);
311 $dummyMail = new CRM_Mailing_BAO_Mailing();
312 $dummyMail->body_text
= $this->_postURL
;
313 $tokens = $dummyMail->getTokens();
315 $url = CRM_Utils_Token
::replaceContactTokens($this->_postURL
, $contact, FALSE, CRM_Utils_Array
::value('text', $tokens));
318 $session->replaceUserContext($url);
322 * Intercept QF validation and do our own redirection.
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
328 * This is a first version and will be tweaked over a period of time
332 * true if no error found
334 public function validate() {
335 $errors = parent
::validate();
337 if (!$errors && !empty($_POST['errorURL'])) {
339 foreach ($this->_errors
as $name => $mess) {
344 CRM_Utils_System
::setUFMessage($message);
346 $message = urlencode($message);
348 $errorURL = $_POST['errorURL'];
349 if (strpos($errorURL, '?') !== FALSE) {
355 $errorURL .= "gid={$this->_gid}&msg=$message";
356 CRM_Utils_System
::redirect($errorURL);