| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 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. | |
| 13 | | | |
| 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. | |
| 18 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class generates form components for custom data |
| 38 | * |
| 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. |
| 42 | * |
| 43 | */ |
| 44 | class CRM_Profile_Form_Edit extends CRM_Profile_Form { |
| 45 | protected $_postURL = NULL; |
| 46 | protected $_cancelURL = NULL; |
| 47 | protected $_errorURL = NULL; |
| 48 | protected $_context; |
| 49 | protected $_blockNo; |
| 50 | protected $_prefix; |
| 51 | |
| 52 | /** |
| 53 | * Pre processing work done here. |
| 54 | * |
| 55 | * @param |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function preProcess() { |
| 60 | $this->_mode = CRM_Profile_Form::MODE_CREATE; |
| 61 | |
| 62 | $this->_onPopupClose = CRM_Utils_Request::retrieve('onPopupClose', 'String', $this); |
| 63 | $this->assign('onPopupClose', $this->_onPopupClose); |
| 64 | |
| 65 | //set the context for the profile |
| 66 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 67 | |
| 68 | //set the block no |
| 69 | $this->_blockNo = CRM_Utils_Request::retrieve('blockNo', 'String', $this); |
| 70 | |
| 71 | //set the prefix |
| 72 | $this->_prefix = CRM_Utils_Request::retrieve('prefix', 'String', $this); |
| 73 | |
| 74 | $this->assign('context', $this->_context); |
| 75 | |
| 76 | if ($this->_blockNo) { |
| 77 | $this->assign('blockNo', $this->_blockNo); |
| 78 | $this->assign('prefix', $this->_prefix); |
| 79 | } |
| 80 | |
| 81 | $this->assign('createCallback', CRM_Utils_Request::retrieve('createCallback', 'String', $this)); |
| 82 | |
| 83 | if ($this->get('skipPermission')) { |
| 84 | $this->_skipPermission = TRUE; |
| 85 | } |
| 86 | |
| 87 | if ($this->get('edit')) { |
| 88 | // make sure we have right permission to edit this user |
| 89 | $session = CRM_Core_Session::singleton(); |
| 90 | $userID = $session->get('userID'); |
| 91 | |
| 92 | // Set the ID from the query string, otherwise default to the current user |
| 93 | $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, $userID); |
| 94 | |
| 95 | if ($id) { |
| 96 | // this is edit mode. |
| 97 | $this->_mode = CRM_Profile_Form::MODE_EDIT; |
| 98 | |
| 99 | if ($id != $userID) { |
| 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); |
| 104 | } |
| 105 | else { |
| 106 | CRM_Contact_BAO_Contact_Permission::validateChecksumContact($id, $this); |
| 107 | } |
| 108 | $this->_isPermissionedChecksum = TRUE; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // CRM-16784: If there is no ID then this can't be an 'edit' |
| 113 | else { |
| 114 | CRM_Core_Error::fatal(ts('No user/contact ID was specified, so the Profile cannot be used in edit mode.')); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | parent::preProcess(); |
| 120 | |
| 121 | // and also the profile is of type 'Profile' |
| 122 | $query = " |
| 123 | SELECT module,is_reserved |
| 124 | FROM civicrm_uf_group |
| 125 | LEFT JOIN civicrm_uf_join ON uf_group_id = civicrm_uf_group.id |
| 126 | WHERE civicrm_uf_group.id = %1 |
| 127 | "; |
| 128 | |
| 129 | $params = array(1 => array($this->_gid, 'Integer')); |
| 130 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 131 | |
| 132 | $isProfile = FALSE; |
| 133 | while ($dao->fetch()) { |
| 134 | $isProfile = ($isProfile || ($dao->module == "Profile")); |
| 135 | } |
| 136 | |
| 137 | //Check that the user has the "add contacts" Permission |
| 138 | $canAdd = CRM_Core_Permission::check("add contacts"); |
| 139 | |
| 140 | //Remove need for Profile module type when using reserved profiles [CRM-14488] |
| 141 | if (!$dao->N || (!$isProfile && !($dao->is_reserved && $canAdd))) { |
| 142 | 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.', |
| 143 | array(1 => $this->_gid) |
| 144 | )); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Build the form object. |
| 150 | * |
| 151 | * @return void |
| 152 | */ |
| 153 | public function buildQuickForm() { |
| 154 | if (empty($this->_ufGroup['id'])) { |
| 155 | CRM_Core_Error::fatal(); |
| 156 | } |
| 157 | |
| 158 | // set the title |
| 159 | if ($this->_multiRecord && $this->_customGroupTitle) { |
| 160 | $groupTitle = ($this->_multiRecord & CRM_Core_Action::UPDATE) ? 'Edit ' . $this->_customGroupTitle . ' Record' : $this->_customGroupTitle; |
| 161 | |
| 162 | } |
| 163 | else { |
| 164 | $groupTitle = $this->_ufGroup['title']; |
| 165 | } |
| 166 | CRM_Utils_System::setTitle($groupTitle); |
| 167 | $this->assign('recentlyViewed', FALSE); |
| 168 | |
| 169 | if ($this->_context != 'dialog') { |
| 170 | $this->_postURL = $this->_ufGroup['post_URL']; |
| 171 | $this->_cancelURL = $this->_ufGroup['cancel_URL']; |
| 172 | |
| 173 | $gidString = $this->_gid; |
| 174 | if (!empty($this->_profileIds)) { |
| 175 | $gidString = implode(',', $this->_profileIds); |
| 176 | } |
| 177 | |
| 178 | if (!$this->_postURL) { |
| 179 | if ($this->_context == 'Search') { |
| 180 | $this->_postURL = CRM_Utils_System::url('civicrm/contact/search'); |
| 181 | } |
| 182 | elseif ($this->_id && $this->_gid) { |
| 183 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; |
| 184 | if ($this->_isContactActivityProfile && $this->_activityId) { |
| 185 | $urlParams .= "&aid={$this->_activityId}"; |
| 186 | } |
| 187 | // get checksum if present |
| 188 | if ($this->get('cs')) { |
| 189 | $urlParams .= "&cs=" . $this->get('cs'); |
| 190 | } |
| 191 | $this->_postURL = CRM_Utils_System::url('civicrm/profile/view', $urlParams); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (!$this->_cancelURL) { |
| 196 | $this->_cancelURL = CRM_Utils_System::url('civicrm/profile', |
| 197 | "reset=1&gid={$gidString}" |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | // we do this gross hack since qf also does entity replacement |
| 202 | $this->_postURL = str_replace('&', '&', $this->_postURL); |
| 203 | $this->_cancelURL = str_replace('&', '&', $this->_cancelURL); |
| 204 | |
| 205 | // also retain error URL if set |
| 206 | $this->_errorURL = CRM_Utils_Array::value('errorURL', $_POST); |
| 207 | if ($this->_errorURL) { |
| 208 | // we do this gross hack since qf also does entity replacement |
| 209 | $this->_errorURL = str_replace('&', '&', $this->_errorURL); |
| 210 | $this->addElement('hidden', 'errorURL', $this->_errorURL); |
| 211 | } |
| 212 | |
| 213 | // replace the session stack in case user cancels (and we dont go into postProcess) |
| 214 | $session = CRM_Core_Session::singleton(); |
| 215 | $session->replaceUserContext($this->_postURL); |
| 216 | } |
| 217 | |
| 218 | parent::buildQuickForm(); |
| 219 | |
| 220 | $this->assign('cancelURL', $this->_cancelURL); |
| 221 | |
| 222 | if (($this->_multiRecord & CRM_Core_Action::DELETE) && $this->_recordExists) { |
| 223 | $this->_deleteButtonName = $this->getButtonName('upload', 'delete'); |
| 224 | |
| 225 | $this->addElement('submit', $this->_deleteButtonName, ts('Delete')); |
| 226 | |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | //get the value from session, this is set if there is any file |
| 231 | //upload field |
| 232 | $uploadNames = $this->get('uploadNames'); |
| 233 | |
| 234 | if (!empty($uploadNames)) { |
| 235 | $buttonName = 'upload'; |
| 236 | } |
| 237 | else { |
| 238 | $buttonName = 'next'; |
| 239 | } |
| 240 | |
| 241 | $buttons[] = array( |
| 242 | 'type' => $buttonName, |
| 243 | 'name' => ts('Save'), |
| 244 | 'isDefault' => TRUE, |
| 245 | ); |
| 246 | |
| 247 | $this->addButtons($buttons); |
| 248 | |
| 249 | $this->addFormRule(array('CRM_Profile_Form', 'formRule'), $this); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Process the user submitted custom data values. |
| 254 | * |
| 255 | * |
| 256 | * @return void |
| 257 | */ |
| 258 | public function postProcess() { |
| 259 | parent::postProcess(); |
| 260 | |
| 261 | $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'display_name'); |
| 262 | $sortName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_id, 'sort_name'); |
| 263 | $this->ajaxResponse['label'] = $sortName; |
| 264 | |
| 265 | // When saving (not deleting) and not in an ajax popup |
| 266 | if (empty($_POST[$this->_deleteButtonName]) && $this->_context != 'dialog') { |
| 267 | CRM_Core_Session::setStatus(ts('Your information has been saved.'), ts('Thank you.'), 'success'); |
| 268 | } |
| 269 | |
| 270 | $session = CRM_Core_Session::singleton(); |
| 271 | // only replace user context if we do not have a postURL |
| 272 | if (!$this->_postURL) { |
| 273 | $gidString = $this->_gid; |
| 274 | if (!empty($this->_profileIds)) { |
| 275 | $gidString = implode(',', $this->_profileIds); |
| 276 | } |
| 277 | |
| 278 | $urlParams = "reset=1&id={$this->_id}&gid={$gidString}"; |
| 279 | if ($this->_isContactActivityProfile && $this->_activityId) { |
| 280 | $urlParams .= "&aid={$this->_activityId}"; |
| 281 | } |
| 282 | // Get checksum if present |
| 283 | if ($this->get('cs')) { |
| 284 | $urlParams .= "&cs=" . $this->get('cs'); |
| 285 | } |
| 286 | // Generate one if needed |
| 287 | elseif (!CRM_Contact_BAO_Contact_Permission::allow($this->_id)) { |
| 288 | $urlParams .= "&cs=" . CRM_Contact_BAO_Contact_Utils::generateChecksum($this->_id); |
| 289 | } |
| 290 | $url = CRM_Utils_System::url('civicrm/profile/view', $urlParams); |
| 291 | } |
| 292 | else { |
| 293 | // Replace tokens from post URL |
| 294 | $contactParams = array( |
| 295 | 'contact_id' => $this->_id, |
| 296 | 'version' => 3, |
| 297 | ); |
| 298 | |
| 299 | $contact = civicrm_api('contact', 'get', $contactParams); |
| 300 | $contact = reset($contact['values']); |
| 301 | |
| 302 | $dummyMail = new CRM_Mailing_BAO_Mailing(); |
| 303 | $dummyMail->body_text = $this->_postURL; |
| 304 | $tokens = $dummyMail->getTokens(); |
| 305 | |
| 306 | $url = CRM_Utils_Token::replaceContactTokens($this->_postURL, $contact, FALSE, CRM_Utils_Array::value('text', $tokens)); |
| 307 | } |
| 308 | |
| 309 | $session->replaceUserContext($url); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Intercept QF validation and do our own redirection. |
| 314 | * |
| 315 | * We use this to send control back to the user for a user formatted page |
| 316 | * This allows the user to maintain the same state and display the error messages |
| 317 | * in their own theme along with any modifications |
| 318 | * |
| 319 | * This is a first version and will be tweaked over a period of time |
| 320 | * |
| 321 | * |
| 322 | * @return bool |
| 323 | * true if no error found |
| 324 | */ |
| 325 | public function validate() { |
| 326 | $errors = parent::validate(); |
| 327 | |
| 328 | if (!$errors && !empty($_POST['errorURL'])) { |
| 329 | $message = NULL; |
| 330 | foreach ($this->_errors as $name => $mess) { |
| 331 | $message .= $mess; |
| 332 | $message .= '<p>'; |
| 333 | } |
| 334 | |
| 335 | CRM_Utils_System::setUFMessage($message); |
| 336 | |
| 337 | $message = urlencode($message); |
| 338 | |
| 339 | $errorURL = $_POST['errorURL']; |
| 340 | if (strpos($errorURL, '?') !== FALSE) { |
| 341 | $errorURL .= '&'; |
| 342 | } |
| 343 | else { |
| 344 | $errorURL .= '?'; |
| 345 | } |
| 346 | $errorURL .= "gid={$this->_gid}&msg=$message"; |
| 347 | CRM_Utils_System::redirect($errorURL); |
| 348 | } |
| 349 | |
| 350 | return $errors; |
| 351 | } |
| 352 | |
| 353 | } |