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
;
158 $groupTitle = $this->_ufGroup
['title'];
160 CRM_Utils_System
::setTitle($groupTitle);
161 $this->assign('recentlyViewed', FALSE);
163 if ($this->_context
!= 'dialog') {
164 $this->_postURL
= $this->_ufGroup
['post_URL'];
165 $this->_cancelURL
= $this->_ufGroup
['cancel_URL'];
167 $gidString = $this->_gid
;
168 if (!empty($this->_profileIds
)) {
169 $gidString = implode(',', $this->_profileIds
);
172 if (!$this->_postURL
) {
173 if ($this->_context
== 'Search') {
174 $this->_postURL
= CRM_Utils_System
::url('civicrm/contact/search');
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}";
181 // get checksum if present
182 if ($this->get('cs')) {
183 $urlParams .= "&cs=" . $this->get('cs');
185 $this->_postURL
= CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
189 if (!$this->_cancelURL
) {
190 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/profile',
191 "reset=1&gid={$gidString}"
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
);
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
);
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
);
212 parent
::buildQuickForm();
214 $this->assign('cancelURL', $this->_cancelURL
);
216 if (($this->_multiRecord
& CRM_Core_Action
::DELETE
) && $this->_recordExists
) {
217 $this->_deleteButtonName
= $this->getButtonName('upload', 'delete');
219 $this->addElement('submit', $this->_deleteButtonName
, ts('Delete'));
224 //get the value from session, this is set if there is any file
226 $uploadNames = $this->get('uploadNames');
228 if (!empty($uploadNames)) {
229 $buttonName = 'upload';
232 $buttonName = 'next';
236 'type' => $buttonName,
237 'name' => ts('Save'),
241 $this->addButtons($buttons);
243 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
247 * Process the user submitted custom data values.
252 public function postProcess() {
253 parent
::postProcess();
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;
259 // When saving (not deleting) and not in an ajax popup
260 if (empty($_POST[$this->_deleteButtonName
]) && $this->_context
!= 'dialog') {
261 CRM_Core_Session
::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
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
);
272 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
273 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
274 $urlParams .= "&aid={$this->_activityId}";
276 // Get checksum if present
277 if ($this->get('cs')) {
278 $urlParams .= "&cs=" . $this->get('cs');
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
);
284 $url = CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
287 // Replace tokens from post URL
288 $contactParams = array(
289 'contact_id' => $this->_id
,
293 $contact = civicrm_api('contact', 'get', $contactParams);
294 $contact = reset($contact['values']);
296 $dummyMail = new CRM_Mailing_BAO_Mailing();
297 $dummyMail->body_text
= $this->_postURL
;
298 $tokens = $dummyMail->getTokens();
300 $url = CRM_Utils_Token
::replaceContactTokens($this->_postURL
, $contact, FALSE, CRM_Utils_Array
::value('text', $tokens));
303 $session->replaceUserContext($url);
307 * Intercept QF validation and do our own redirection
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
313 * This is a first version and will be tweaked over a period of time
316 * @return boolean true if no error found
318 public function validate() {
319 $errors = parent
::validate();
321 if (!$errors && !empty($_POST['errorURL'])) {
323 foreach ($this->_errors
as $name => $mess) {
328 CRM_Utils_System
::setUFMessage($message);
330 $message = urlencode($message);
332 $errorURL = $_POST['errorURL'];
333 if (strpos($errorURL, '?') !== FALSE) {
339 $errorURL .= "gid={$this->_gid}&msg=$message";
340 CRM_Utils_System
::redirect($errorURL);