3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class generates form components for custom data
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.
44 class CRM_Profile_Form_Edit
extends CRM_Profile_Form
{
45 protected $_postURL = NULL;
46 protected $_cancelURL = NULL;
47 protected $_errorURL = NULL;
53 * pre processing work done here.
62 function preProcess() {
63 $this->_mode
= CRM_Profile_Form
::MODE_CREATE
;
65 $this->_onPopupClose
= CRM_Utils_Request
::retrieve('onPopupClose', 'String', $this);
66 $this->assign('onPopupClose', $this->_onPopupClose
);
68 //set the context for the profile
69 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
72 $this->_blockNo
= CRM_Utils_Request
::retrieve('blockNo', 'String', $this);
75 $this->_prefix
= CRM_Utils_Request
::retrieve('prefix', 'String', $this);
77 $this->assign('context', $this->_context
);
79 if ($this->_blockNo
) {
80 $this->assign('blockNo', $this->_blockNo
);
81 $this->assign('prefix', $this->_prefix
);
84 $this->assign('createCallback', CRM_Utils_Request
::retrieve('createCallback', 'String', $this));
86 if ($this->get('skipPermission')) {
87 $this->_skipPermission
= TRUE;
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);
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 $config = CRM_Core_Config
::singleton();
103 if ($config->userFrameworkFrontend
) {
104 CRM_Contact_BAO_Contact_Permission
::validateOnlyChecksum($id, $this);
107 CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($id, $this);
109 $this->_isPermissionedChecksum
= TRUE;
114 parent
::preProcess();
116 // and also the profile is of type 'Profile'
118 SELECT module,is_reserved
119 FROM civicrm_uf_group
120 LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id
121 WHERE civicrm_uf_group.id = %1
124 $params = array(1 => array($this->_gid
, 'Integer'));
125 $dao = CRM_Core_DAO
::executeQuery($query, $params);
128 while ($dao->fetch()) {
129 $isProfile = ($isProfile ||
($dao->module
== "Profile"));
133 //Check that the user has the "add contacts" Permission
134 $canAdd = CRM_Core_Permission
::check("add contacts");
136 //Remove need for Profile module type when using reserved profiles [CRM-14488]
137 if( !$dao->N ||
(!$isProfile && !($dao->is_reserved
&& $canAdd))) {
138 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.',
139 array(1 => $this->_gid
)
145 * Function to actually build the form
150 public function buildQuickForm() {
151 if (empty($this->_ufGroup
['id'])) {
152 CRM_Core_Error
::fatal();
156 if ($this->_multiRecord
&& $this->_customGroupTitle
) {
157 $groupTitle = ($this->_multiRecord
& CRM_Core_Action
::UPDATE
) ?
158 'Edit ' . $this->_customGroupTitle
. ' Record' : $this->_customGroupTitle
;
161 $groupTitle = $this->_ufGroup
['title'];
163 CRM_Utils_System
::setTitle($groupTitle);
164 $this->assign('recentlyViewed', FALSE);
166 if ($this->_context
!= 'dialog') {
167 $this->_postURL
= $this->_ufGroup
['post_URL'];
168 $this->_cancelURL
= $this->_ufGroup
['cancel_URL'];
170 $gidString = $this->_gid
;
171 if (!empty($this->_profileIds
)) {
172 $gidString = implode(',', $this->_profileIds
);
175 if (!$this->_postURL
) {
176 if ($this->_context
== 'Search') {
177 $this->_postURL
= CRM_Utils_System
::url('civicrm/contact/search');
179 elseif ($this->_id
&& $this->_gid
) {
180 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
181 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
182 $urlParams .= "&aid={$this->_activityId}";
184 // get checksum if present
185 if ($this->get('cs')) {
186 $urlParams .= "&cs=" . $this->get('cs');
188 $this->_postURL
= CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
192 if (!$this->_cancelURL
) {
193 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/profile',
194 "reset=1&gid={$gidString}"
198 // we do this gross hack since qf also does entity replacement
199 $this->_postURL
= str_replace('&', '&', $this->_postURL
);
200 $this->_cancelURL
= str_replace('&', '&', $this->_cancelURL
);
202 // also retain error URL if set
203 $this->_errorURL
= CRM_Utils_Array
::value('errorURL', $_POST);
204 if ($this->_errorURL
) {
205 // we do this gross hack since qf also does entity replacement
206 $this->_errorURL
= str_replace('&', '&', $this->_errorURL
);
207 $this->addElement('hidden', 'errorURL', $this->_errorURL
);
210 // replace the session stack in case user cancels (and we dont go into postProcess)
211 $session = CRM_Core_Session
::singleton();
212 $session->replaceUserContext($this->_postURL
);
215 parent
::buildQuickForm();
217 if (($this->_multiRecord
& CRM_Core_Action
::DELETE
) && $this->_recordExists
) {
218 $this->_deleteButtonName
= $this->getButtonName('upload', 'delete');
220 $this->addElement('submit', $this->_deleteButtonName
, ts('Delete'));
224 'name' => ts('Cancel'),
226 'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
228 $this->addButtons($buttons);
232 //get the value from session, this is set if there is any file
234 $uploadNames = $this->get('uploadNames');
236 if (!empty($uploadNames)) {
237 $buttonName = 'upload';
240 $buttonName = 'next';
244 'type' => $buttonName,
245 'name' => ts('Save'),
251 'name' => ts('Cancel'),
253 'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
256 $this->addButtons($buttons);
258 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
262 * Process the user submitted custom data values.
268 public function postProcess() {
269 parent
::postProcess();
271 $displayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'display_name');
272 $sortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'sort_name');
273 $this->ajaxResponse
['label'] = $sortName;
275 // When saving (not deleting) and not in an ajax popup
276 if (empty($_POST[$this->_deleteButtonName
]) && $this->_context
!= 'dialog') {
277 CRM_Core_Session
::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
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
);
288 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
289 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
290 $urlParams .= "&aid={$this->_activityId}";
292 // Get checksum if present
293 if ($this->get('cs')) {
294 $urlParams .= "&cs=" . $this->get('cs');
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
);
300 $url = CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
303 // Replace tokens from post URL
304 $contactParams = array(
305 'contact_id' => $this->_id
,
309 $contact = civicrm_api('contact', 'get', $contactParams);
310 $contact = reset($contact['values']);
312 $dummyMail = new CRM_Mailing_BAO_Mailing();
313 $dummyMail->body_text
= $this->_postURL
;
314 $tokens = $dummyMail->getTokens();
316 $url = CRM_Utils_Token
::replaceContactTokens($this->_postURL
, $contact, FALSE, CRM_Utils_Array
::value('text', $tokens));
319 $session->replaceUserContext($url);
323 * Function to intercept QF validation and do our own redirection
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
329 * This is a first version and will be tweaked over a period of time
333 * @return boolean true if no error found
335 function validate() {
336 $errors = parent
::validate();
338 if (!$errors && !empty($_POST['errorURL'])) {
340 foreach ($this->_errors
as $name => $mess) {
345 CRM_Utils_System
::setUFMessage($message);
347 $message = urlencode($message);
349 $errorURL = $_POST['errorURL'];
350 if (strpos($errorURL, '?') !== FALSE) {
356 $errorURL .= "gid={$this->_gid}&msg=$message";
357 CRM_Utils_System
::redirect($errorURL);