3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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'
120 WHERE module = 'Profile'
123 $params = array(1 => array($this->_gid
, 'Integer'));
124 $dao = CRM_Core_DAO
::executeQuery($query, $params);
125 if (!$dao->fetch()) {
126 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.',
127 array(1 => $this->_gid
)
133 * Function to actually build the form
138 public function buildQuickForm() {
139 if (empty($this->_ufGroup
['id'])) {
140 CRM_Core_Error
::fatal();
144 if ($this->_multiRecord
&& $this->_customGroupTitle
) {
145 $groupTitle = ($this->_multiRecord
& CRM_Core_Action
::UPDATE
) ?
146 'Edit ' . $this->_customGroupTitle
. ' Record' : $this->_customGroupTitle
;
149 $groupTitle = $this->_ufGroup
['title'];
151 CRM_Utils_System
::setTitle($groupTitle);
152 $this->assign('recentlyViewed', FALSE);
154 if ($this->_context
!= 'dialog') {
155 $this->_postURL
= $this->_ufGroup
['post_URL'];
156 $this->_cancelURL
= $this->_ufGroup
['cancel_URL'];
158 $gidString = $this->_gid
;
159 if (!empty($this->_profileIds
)) {
160 $gidString = implode(',', $this->_profileIds
);
163 if (!$this->_postURL
) {
164 if ($this->_context
== 'Search') {
165 $this->_postURL
= CRM_Utils_System
::url('civicrm/contact/search');
167 elseif ($this->_id
&& $this->_gid
) {
168 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
169 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
170 $urlParams .= "&aid={$this->_activityId}";
172 // get checksum if present
173 if ($this->get('cs')) {
174 $urlParams .= "&cs=" . $this->get('cs');
176 $this->_postURL
= CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
180 if (!$this->_cancelURL
) {
181 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/profile',
182 "reset=1&gid={$gidString}"
186 if ($this->_multiRecordProfile
) {
187 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
189 // get checksum if present
190 if ($this->get('cs')) {
191 $urlParams .= "&cs=" . $this->get('cs');
193 $this->_postURL
= CRM_Utils_System
::url('civicrm/profile/edit', $urlParams);
194 $this->_cancelURL
= CRM_Utils_System
::url('civicrm/profile/edit', $urlParams);
196 //passing the post url to template so the popup form does
197 //proper redirection and proccess form errors if any
198 if (!isset($this->_onPopupClose
) ||
$this->_onPopupClose
== 'redirectToProfile') {
199 $popupRedirect = CRM_Utils_System
::url('civicrm/profile/edit', $urlParams, FALSE, NULL, FALSE);
201 elseif ($this->_onPopupClose
== 'redirectToTab') {
202 $popupRedirect = CRM_Utils_System
::url('civicrm/contact/view',
203 "reset=1&cid={$this->_id}&selectedChild=custom_{$this->_customGroupId}", FALSE, NULL, FALSE);
206 $this->assign('urlParams', $urlParams);
207 $this->assign('postUrl', $popupRedirect);
210 // we do this gross hack since qf also does entity replacement
211 $this->_postURL
= str_replace('&', '&', $this->_postURL
);
212 $this->_cancelURL
= str_replace('&', '&', $this->_cancelURL
);
214 // also retain error URL if set
215 $this->_errorURL
= CRM_Utils_Array
::value('errorURL', $_POST);
216 if ($this->_errorURL
) {
217 // we do this gross hack since qf also does entity replacement
218 $this->_errorURL
= str_replace('&', '&', $this->_errorURL
);
219 $this->addElement('hidden', 'errorURL', $this->_errorURL
);
222 // replace the session stack in case user cancels (and we dont go into postProcess)
223 $session = CRM_Core_Session
::singleton();
224 $session->replaceUserContext($this->_postURL
);
227 parent
::buildQuickForm();
229 if (($this->_multiRecord
& CRM_Core_Action
::DELETE
) && $this->_recordExists
) {
230 $this->_deleteButtonName
= $this->getButtonName('upload', 'delete');
232 $this->addElement('submit',
233 $this->_deleteButtonName
,
239 'name' => ts('Cancel'),
241 'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
243 $this->addButtons($buttons);
247 //get the value from session, this is set if there is any file
249 $uploadNames = $this->get('uploadNames');
251 if (!empty($uploadNames)) {
252 $buttonName = 'upload';
255 $buttonName = 'next';
259 'type' => $buttonName,
260 'name' => ts('Save'),
264 if ($this->_context
!= 'dialog') {
267 'name' => ts('Cancel'),
269 'js' => array('onclick' => "location.href='{$this->_cancelURL}'; return false;"),
273 $this->addButtons($buttons);
275 $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this);
279 * Process the user submitted custom data values.
285 public function postProcess() {
286 parent
::postProcess();
288 // this is special case when we create contact using Dialog box
289 if ($this->_context
== 'dialog') {
290 $displayName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'display_name');
291 $sortName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_id
, 'sort_name');
292 $returnArray = array(
293 'contactID' => $this->_id
,
294 'displayName' => $displayName,
295 'sortName' => $sortName,
296 'newContactSuccess' => TRUE,
299 echo json_encode($returnArray);
300 CRM_Utils_System
::civiExit();
303 //for delete record handling
304 if (!CRM_Utils_Array
::value($this->_deleteButtonName
, $_POST)) {
305 CRM_Core_Session
::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success');
308 $session = CRM_Core_Session
::singleton();
309 // only replace user context if we do not have a postURL
310 if (!$this->_postURL
) {
311 $gidString = $this->_gid
;
312 if (!empty($this->_profileIds
)) {
313 $gidString = implode(',', $this->_profileIds
);
316 $urlParams = "reset=1&id={$this->_id}&gid={$gidString}";
317 if ($this->_isContactActivityProfile
&& $this->_activityId
) {
318 $urlParams .= "&aid={$this->_activityId}";
320 // Get checksum if present
321 if ($this->get('cs')) {
322 $urlParams .= "&cs=" . $this->get('cs');
324 // Generate one if needed
325 elseif (!CRM_Contact_BAO_Contact_Permission
::allow($this->_id
)) {
326 $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils
::generateChecksum($this->_id
);
328 $url = CRM_Utils_System
::url('civicrm/profile/view', $urlParams);
331 // Replace tokens from post URL
332 $contactParams = array(
333 'contact_id' => $this->_id
,
337 $contact = civicrm_api('contact', 'get', $contactParams);
338 $contact = reset($contact['values']);
340 $dummyMail = new CRM_Mailing_BAO_Mailing();
341 $dummyMail->body_text
= $this->_postURL
;
342 $tokens = $dummyMail->getTokens();
344 $url = CRM_Utils_Token
::replaceContactTokens($this->_postURL
, $contact, FALSE, CRM_Utils_Array
::value('text', $tokens));
347 $session->replaceUserContext($url);
351 * Function to intercept QF validation and do our own redirection
353 * We use this to send control back to the user for a user formatted page
354 * This allows the user to maintain the same state and display the error messages
355 * in their own theme along with any modifications
357 * This is a first version and will be tweaked over a period of time
361 * @return boolean true if no error found
363 function validate() {
364 $errors = parent
::validate();
367 CRM_Utils_Array
::value('errorURL', $_POST)
370 foreach ($this->_errors
as $name => $mess) {
375 CRM_Utils_System
::setUFMessage($message);
377 $message = urlencode($message);
379 $errorURL = $_POST['errorURL'];
380 if (strpos($errorURL, '?') !== FALSE) {
386 $errorURL .= "gid={$this->_gid}&msg=$message";
387 CRM_Utils_System
::redirect($errorURL);