3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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.
61 public function preProcess() {
62 $this->_mode
= CRM_Profile_Form
::MODE_CREATE
;
64 $this->_onPopupClose
= CRM_Utils_Request
::retrieve('onPopupClose', 'String', $this);
65 $this->assign('onPopupClose', $this->_onPopupClose
);
67 //set the context for the profile
68 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
71 $this->_blockNo
= CRM_Utils_Request
::retrieve('blockNo', 'String', $this);
74 $this->_prefix
= CRM_Utils_Request
::retrieve('prefix', '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 $session = CRM_Core_Session
::singleton();
92 $userID = $session->get('userID');
93 $id = CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE, $userID);
97 $this->_mode
= CRM_Profile_Form
::MODE_EDIT
;
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);
106 CRM_Contact_BAO_Contact_Permission
::validateChecksumContact($id, $this);
108 $this->_isPermissionedChecksum
= TRUE;
113 parent
::preProcess();
115 // and also the profile is of type 'Profile'
117 SELECT module,is_reserved
118 FROM civicrm_uf_group
119 LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id
120 WHERE civicrm_uf_group.id = %1
123 $params = array(1 => array($this->_gid
, 'Integer'));
124 $dao = CRM_Core_DAO
::executeQuery($query, $params);
127 while ($dao->fetch()) {
128 $isProfile = ($isProfile ||
($dao->module
== "Profile"));
131 //Check that the user has the "add contacts" Permission
132 $canAdd = CRM_Core_Permission
::check("add contacts");
134 //Remove need for Profile module type when using reserved profiles [CRM-14488]
135 if (!$dao->N ||
(!$isProfile && !($dao->is_reserved
&& $canAdd))) {
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.',
137 array(1 => $this->_gid
)
143 * Build the form object
147 public function buildQuickForm() {
148 if (empty($this->_ufGroup
['id'])) {
149 CRM_Core_Error
::fatal();
153 if ($this->_multiRecord
&& $this->_customGroupTitle
) {
154 $groupTitle = ($this->_multiRecord
& CRM_Core_Action
::UPDATE
) ?
'Edit ' . $this->_customGroupTitle
. ' Record' : $this->_customGroupTitle
;
157 $groupTitle = $this->_ufGroup
['title'];
159 CRM_Utils_System
::setTitle($groupTitle);
160 $this->assign('recentlyViewed', FALSE);
162 if ($this->_context
!= 'dialog') {
163 $this->_postURL
= $this->_ufGroup
['post_URL'];
164 $this->_cancelURL
= $this->_ufGroup
['cancel_URL'];
166 $gidString = $this->_gid
;
167 if (!empty($this->_profileIds
)) {
168 $gidString = implode(',', $this->_profileIds
);
171 if (!$this->_postURL
) {
172 if ($this->_context
== 'Search') {
173 $this->_postURL
= CRM_Utils_System
::url('civicrm/contact/search');
175 elseif ($this->_id
&& $this->_gid
) {
176 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
177 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
178 $urlParams .= "&aid={$this->_activityId}";
180 // get checksum if present
181 if ($this->get('cs')) {
182 $urlParams .= "&cs=" . $this->get('cs');
184 $this->_postURL
= CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
188 if (!$this->_cancelURL
) {
189 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/profile',
190 "reset=1&gid={$gidString}"
194 // we do this gross hack since qf also does entity replacement
195 $this->_postURL
= str_replace('&', '&', $this->_postURL
);
196 $this->_cancelURL
= str_replace('&', '&', $this->_cancelURL
);
198 // also retain error URL if set
199 $this->_errorURL
= CRM_Utils_Array
::value('errorURL', $_POST);
200 if ($this->_errorURL
) {
201 // we do this gross hack since qf also does entity replacement
202 $this->_errorURL
= str_replace('&', '&', $this->_errorURL
);
203 $this->addElement('hidden', 'errorURL', $this->_errorURL
);
206 // replace the session stack in case user cancels (and we dont go into postProcess)
207 $session = CRM_Core_Session
::singleton();
208 $session->replaceUserContext($this->_postURL
);
211 parent
::buildQuickForm();
213 $this->assign('cancelURL', $this->_cancelURL
);
215 if (($this->_multiRecord
& CRM_Core_Action
::DELETE
) && $this->_recordExists
) {
216 $this->_deleteButtonName
= $this->getButtonName('upload', 'delete');
218 $this->addElement('submit', $this->_deleteButtonName
, ts('Delete'));
223 //get the value from session, this is set if there is any file
225 $uploadNames = $this->get('uploadNames');
227 if (!empty($uploadNames)) {
228 $buttonName = 'upload';
231 $buttonName = 'next';
235 'type' => $buttonName,
236 'name' => ts('Save'),
240 $this->addButtons($buttons);
242 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
246 * Process the user submitted custom data values.
251 public function postProcess() {
252 parent
::postProcess();
254 $displayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'display_name');
255 $sortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'sort_name');
256 $this->ajaxResponse
['label'] = $sortName;
258 // When saving (not deleting) and not in an ajax popup
259 if (empty($_POST[$this->_deleteButtonName
]) && $this->_context
!= 'dialog') {
260 CRM_Core_Session
::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
263 $session = CRM_Core_Session
::singleton();
264 // only replace user context if we do not have a postURL
265 if (!$this->_postURL
) {
266 $gidString = $this->_gid
;
267 if (!empty($this->_profileIds
)) {
268 $gidString = implode(',', $this->_profileIds
);
271 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
272 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
273 $urlParams .= "&aid={$this->_activityId}";
275 // Get checksum if present
276 if ($this->get('cs')) {
277 $urlParams .= "&cs=" . $this->get('cs');
279 // Generate one if needed
280 elseif (!CRM_Contact_BAO_Contact_Permission
::allow($this->_id
)) {
281 $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils
::generateChecksum($this->_id
);
283 $url = CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
286 // Replace tokens from post URL
287 $contactParams = array(
288 'contact_id' => $this->_id
,
292 $contact = civicrm_api('contact', 'get', $contactParams);
293 $contact = reset($contact['values']);
295 $dummyMail = new CRM_Mailing_BAO_Mailing();
296 $dummyMail->body_text
= $this->_postURL
;
297 $tokens = $dummyMail->getTokens();
299 $url = CRM_Utils_Token
::replaceContactTokens($this->_postURL
, $contact, FALSE, CRM_Utils_Array
::value('text', $tokens));
302 $session->replaceUserContext($url);
306 * Intercept QF validation and do our own redirection
308 * We use this to send control back to the user for a user formatted page
309 * This allows the user to maintain the same state and display the error messages
310 * in their own theme along with any modifications
312 * This is a first version and will be tweaked over a period of time
315 * @return boolean true if no error found
317 public function validate() {
318 $errors = parent
::validate();
320 if (!$errors && !empty($_POST['errorURL'])) {
322 foreach ($this->_errors
as $name => $mess) {
327 CRM_Utils_System
::setUFMessage($message);
329 $message = urlencode($message);
331 $errorURL = $_POST['errorURL'];
332 if (strpos($errorURL, '?') !== FALSE) {
338 $errorURL .= "gid={$this->_gid}&msg=$message";
339 CRM_Utils_System
::redirect($errorURL);