| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 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-2013 |
| 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 extends CRM_Core_Form { |
| 45 | CONST |
| 46 | MODE_REGISTER = 1, |
| 47 | MODE_SEARCH = 2, |
| 48 | MODE_CREATE = 4, |
| 49 | MODE_EDIT = 8; |
| 50 | |
| 51 | protected $_mode; |
| 52 | |
| 53 | protected $_skipPermission = FALSE; |
| 54 | |
| 55 | /** |
| 56 | * The contact id that we are editing |
| 57 | * |
| 58 | * @var int |
| 59 | */ |
| 60 | protected $_id; |
| 61 | |
| 62 | /** |
| 63 | * The group id that we are editing |
| 64 | * |
| 65 | * @var int |
| 66 | */ |
| 67 | protected $_gid; |
| 68 | |
| 69 | /** |
| 70 | * The group id that we are editing |
| 71 | * |
| 72 | * @var string |
| 73 | */ |
| 74 | protected $_ufGroupName = 'unknown'; |
| 75 | |
| 76 | /** |
| 77 | * The group id that we are passing in url |
| 78 | * |
| 79 | * @var int |
| 80 | */ |
| 81 | public $_grid; |
| 82 | |
| 83 | /** |
| 84 | * The title of the category we are editing |
| 85 | * |
| 86 | * @var string |
| 87 | */ |
| 88 | protected $_title; |
| 89 | |
| 90 | /** |
| 91 | * the fields needed to build this form |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
| 95 | public $_fields; |
| 96 | |
| 97 | /** |
| 98 | * to store contact details |
| 99 | * |
| 100 | * @var array |
| 101 | */ |
| 102 | protected $_contact; |
| 103 | |
| 104 | /** |
| 105 | * to store group_id of the group which is to be assigned to the contact |
| 106 | * |
| 107 | * @var int |
| 108 | */ |
| 109 | protected $_addToGroupID; |
| 110 | |
| 111 | /** |
| 112 | * Do we allow updates of the contact |
| 113 | * |
| 114 | * @var int |
| 115 | */ |
| 116 | public $_isUpdateDupe = 0; |
| 117 | |
| 118 | public $_isAddCaptcha = FALSE; |
| 119 | |
| 120 | protected $_isPermissionedChecksum = FALSE; |
| 121 | |
| 122 | /** |
| 123 | * THe context from which we came from, allows us to go there if redirect not set |
| 124 | * |
| 125 | * @var string |
| 126 | */ |
| 127 | protected $_context; |
| 128 | |
| 129 | /** |
| 130 | * THe contact type for registration case |
| 131 | * |
| 132 | * @var string |
| 133 | */ |
| 134 | protected $_ctype = NULL; |
| 135 | |
| 136 | protected $_defaults = NULL; |
| 137 | |
| 138 | /** |
| 139 | * Store profile ids if multiple profile ids are passed using comma separated. |
| 140 | * Currently lets implement this functionality only for dialog mode |
| 141 | */ |
| 142 | protected $_profileIds = array(); |
| 143 | |
| 144 | /** |
| 145 | * Contact profile having activity fields? |
| 146 | * |
| 147 | * @var string |
| 148 | */ |
| 149 | protected $_isContactActivityProfile = FALSE; |
| 150 | |
| 151 | /** |
| 152 | * Activity Id connected to the profile |
| 153 | * |
| 154 | * @var string |
| 155 | */ |
| 156 | protected $_activityId = NULL; |
| 157 | |
| 158 | |
| 159 | protected $_multiRecordFields = NULL; |
| 160 | |
| 161 | protected $_recordId = NULL; |
| 162 | |
| 163 | /** |
| 164 | * action for multi record profile (create/edit/delete) |
| 165 | * |
| 166 | * @var string |
| 167 | */ |
| 168 | protected $_multiRecord = NULL; |
| 169 | |
| 170 | protected $_multiRecordProfile = FALSE; |
| 171 | |
| 172 | protected $_recordExists = TRUE; |
| 173 | |
| 174 | protected $_customGroupTitle = NULL; |
| 175 | |
| 176 | protected $_deleteButtonName = NULL; |
| 177 | |
| 178 | protected $_customGroupId = NULL; |
| 179 | |
| 180 | protected $_currentUserID = NULL; |
| 181 | protected $_session = NULL; |
| 182 | |
| 183 | /** |
| 184 | * pre processing work done here. |
| 185 | * |
| 186 | * gets session variables for table name, id of entity in table, type of entity and stores them. |
| 187 | * |
| 188 | * @param |
| 189 | * |
| 190 | * @return void |
| 191 | * |
| 192 | * @access public |
| 193 | */ |
| 194 | function preProcess() { |
| 195 | $this->_id = $this->get('id'); |
| 196 | $this->_gid = $this->get('gid'); |
| 197 | $this->_profileIds = $this->get('profileIds'); |
| 198 | $this->_grid = CRM_Utils_Request::retrieve('grid', 'Integer', $this); |
| 199 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); |
| 200 | |
| 201 | //unset from session when $_GET doesn't have it |
| 202 | //except when the form is submitted |
| 203 | if (empty($_POST)) { |
| 204 | if (!array_key_exists('multiRecord', $_GET)) { |
| 205 | $this->set('multiRecord', NULL); |
| 206 | } |
| 207 | if (!array_key_exists('recordId', $_GET)) { |
| 208 | $this->set('recordId', NULL); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | $this->_session = CRM_Core_Session::singleton(); |
| 213 | $this->_currentUserID = $this->_session->get('userID'); |
| 214 | |
| 215 | if ($this->_mode == self::MODE_EDIT) { |
| 216 | //specifies the action being done on a multi record field |
| 217 | $multiRecordAction = CRM_Utils_Request::retrieve('multiRecord', 'String', $this); |
| 218 | $this->_multiRecord = (!is_numeric($multiRecordAction)) ? |
| 219 | CRM_Core_Action::resolve($multiRecordAction) : $multiRecordAction; |
| 220 | if ($this->_multiRecord) { |
| 221 | $this->set('multiRecord', $this->_multiRecord); |
| 222 | } |
| 223 | |
| 224 | if ($this->_multiRecord && |
| 225 | !in_array($this->_multiRecord, array(CRM_Core_Action::UPDATE, CRM_Core_Action::ADD, CRM_Core_Action::DELETE))) { |
| 226 | CRM_Core_Error::fatal(ts('Proper action not specified for this custom value record profile')); |
| 227 | } |
| 228 | } |
| 229 | $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate'); |
| 230 | |
| 231 | $gids = explode(',', CRM_Utils_Request::retrieve('gid', 'String', CRM_Core_DAO::$_nullObject, FALSE, 0, 'GET')); |
| 232 | |
| 233 | if ((count($gids) > 1) && !$this->_profileIds && empty($this->_profileIds)) { |
| 234 | if (!empty($gids)) { |
| 235 | foreach ($gids as $pfId) { |
| 236 | $this->_profileIds[] = CRM_Utils_Type::escape($pfId, 'Positive'); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // check if we are rendering mixed profiles |
| 241 | if (CRM_Core_BAO_UFGroup::checkForMixProfiles($this->_profileIds)) { |
| 242 | CRM_Core_Error::fatal(ts('You cannot combine profiles of multiple types.')); |
| 243 | } |
| 244 | |
| 245 | // for now consider 1'st profile as primary profile and validate it |
| 246 | // i.e check for profile type etc. |
| 247 | // FIX ME: validations for other than primary |
| 248 | $this->_gid = $this->_profileIds[0]; |
| 249 | $this->set('gid', $this->_gid); |
| 250 | $this->set('profileIds', $this->_profileIds); |
| 251 | } |
| 252 | |
| 253 | if (!$this->_gid) { |
| 254 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this, FALSE, 0, 'GET'); |
| 255 | } |
| 256 | |
| 257 | $this->_activityId = CRM_Utils_Request::retrieve('aid', 'Positive', $this, FALSE, 0, 'GET'); |
| 258 | if (is_numeric($this->_activityId)) { |
| 259 | $latestRevisionId = CRM_Activity_BAO_Activity::getLatestActivityId($this->_activityId); |
| 260 | if ($latestRevisionId) { |
| 261 | $this->_activityId = $latestRevisionId; |
| 262 | } |
| 263 | } |
| 264 | $this->_isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($this->_gid); |
| 265 | |
| 266 | //get values for ufGroupName, captch and dupe update. |
| 267 | if ($this->_gid) { |
| 268 | $dao = new CRM_Core_DAO_UFGroup(); |
| 269 | $dao->id = $this->_gid; |
| 270 | if ($dao->find(TRUE)) { |
| 271 | $this->_isUpdateDupe = $dao->is_update_dupe; |
| 272 | $this->_isAddCaptcha = $dao->add_captcha; |
| 273 | if (!empty($dao->name)) { |
| 274 | $this->_ufGroupName = $dao->name; |
| 275 | } |
| 276 | } |
| 277 | $dao->free(); |
| 278 | } |
| 279 | $this->assign('ufGroupName', $this->_ufGroupName); |
| 280 | |
| 281 | $gids = empty($this->_profileIds) ? $this->_gid : $this->_profileIds; |
| 282 | |
| 283 | // if we dont have a gid use the default, else just use that specific gid |
| 284 | if (($this->_mode == self::MODE_REGISTER || $this->_mode == self::MODE_CREATE) && !$this->_gid) { |
| 285 | $this->_ctype = CRM_Utils_Request::retrieve('ctype', 'String', $this, FALSE, 'Individual', 'REQUEST'); |
| 286 | $this->_fields = CRM_Core_BAO_UFGroup::getRegistrationFields($this->_action, $this->_mode, $this->_ctype); |
| 287 | } |
| 288 | elseif ($this->_mode == self::MODE_SEARCH) { |
| 289 | $this->_fields = CRM_Core_BAO_UFGroup::getListingFields($this->_action, |
| 290 | CRM_Core_BAO_UFGroup::PUBLIC_VISIBILITY | CRM_Core_BAO_UFGroup::LISTINGS_VISIBILITY, |
| 291 | FALSE, |
| 292 | $gids, |
| 293 | TRUE, NULL, |
| 294 | $this->_skipPermission, |
| 295 | CRM_Core_Permission::SEARCH |
| 296 | ); |
| 297 | } |
| 298 | else { |
| 299 | $this->_fields = CRM_Core_BAO_UFGroup::getFields($gids, FALSE, NULL, |
| 300 | NULL, NULL, |
| 301 | FALSE, NULL, |
| 302 | $this->_skipPermission, |
| 303 | NULL, |
| 304 | ($this->_action == CRM_Core_Action::ADD) ? CRM_Core_Permission::CREATE : CRM_Core_Permission::EDIT |
| 305 | ); |
| 306 | $multiRecordFieldListing = FALSE; |
| 307 | //using selector for listing of multirecord fields |
| 308 | if ($this->_mode == self::MODE_EDIT && $this->_gid) { |
| 309 | CRM_Core_BAO_UFGroup::shiftMultiRecordFields($this->_fields, $this->_multiRecordFields); |
| 310 | |
| 311 | if ($this->_multiRecord) { |
| 312 | if ($this->_multiRecord != CRM_Core_Action::ADD) { |
| 313 | $this->_recordId = CRM_Utils_Request::retrieve('recordId', 'Positive', $this); |
| 314 | } else { |
| 315 | $this->_recordId = NULL; |
| 316 | $this->set('recordId', NULL); |
| 317 | } |
| 318 | //record id is neccessary for _multiRecord view and update/edit action |
| 319 | if (!$this->_recordId |
| 320 | && ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE)) { |
| 321 | CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) requires record id while performing this action', |
| 322 | array(1 => $this->_gid) |
| 323 | )); |
| 324 | } elseif (empty($this->_multiRecordFields)) { |
| 325 | CRM_Core_Error::fatal(ts('No Multi-Record Fields configured for this profile (gid=%1)', |
| 326 | array(1 => $this->_gid) |
| 327 | )); |
| 328 | } |
| 329 | |
| 330 | $fieldId = CRM_Core_BAO_CustomField::getKeyID(key($this->_multiRecordFields)); |
| 331 | $customGroupDetails = CRM_Core_BAO_CustomGroup::getGroupTitles(array($fieldId)); |
| 332 | $this->_customGroupTitle = $customGroupDetails[$fieldId]['groupTitle']; |
| 333 | $this->_customGroupId = $customGroupDetails[$fieldId]['groupID']; |
| 334 | |
| 335 | if ($this->_multiRecord == CRM_Core_Action::UPDATE || $this->_multiRecord == CRM_Core_Action::DELETE) { |
| 336 | //record exists check |
| 337 | foreach ($this->_multiRecordFields as $key => $field) { |
| 338 | $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); |
| 339 | } |
| 340 | $getValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); |
| 341 | |
| 342 | if (array_key_exists($this->_recordId, $getValues)) { |
| 343 | $this->_recordExists = TRUE; |
| 344 | } else { |
| 345 | $this->_recordExists = FALSE; |
| 346 | if ($this->_multiRecord & CRM_Core_Action::UPDATE) { |
| 347 | CRM_Core_Session::setStatus(ts('Note: The record %1 doesnot exists. Upon save a new record will be create', array(1 => $this->_recordId)), ts('Record doesnot exist'), 'alert'); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | if ($this->_multiRecord & CRM_Core_Action::ADD) { |
| 352 | $this->_maxRecordLimit = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupDetails[$fieldId]['groupID'], $this->_id); |
| 353 | if ($this->_maxRecordLimit) { |
| 354 | CRM_Core_Session::setStatus(ts('You cannot add a new record as maximum allowed limit is reached'), ts('Sorry'), 'error'); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | } elseif (!empty($this->_multiRecordFields) |
| 359 | && (!$this->_multiRecord || !in_array($this->_multiRecord, array(CRM_Core_Action::DELETE, CRM_Core_Action::UPDATE)) )) { |
| 360 | //multirecord listing page |
| 361 | $multiRecordFieldListing = TRUE; |
| 362 | $page = new CRM_Profile_Page_MultipleRecordFieldsListing(); |
| 363 | $cs = $this->get('cs'); |
| 364 | $page->set('pageCheckSum', $cs); |
| 365 | $page->set('contactId', $this->_id); |
| 366 | $page->set('profileId', $this->_gid); |
| 367 | $page->set('action', CRM_Core_Action::BROWSE); |
| 368 | $page->set('multiRecordFieldListing', $multiRecordFieldListing); |
| 369 | $page->run(); |
| 370 | } |
| 371 | } |
| 372 | $this->assign('multiRecordFieldListing', $multiRecordFieldListing); |
| 373 | |
| 374 | // is profile double-opt in? |
| 375 | if (CRM_Utils_Array::value('group', $this->_fields) && |
| 376 | CRM_Core_BAO_UFGroup::isProfileDoubleOptin() |
| 377 | ) { |
| 378 | $emailField = FALSE; |
| 379 | foreach ($this->_fields as $name => $values) { |
| 380 | if (substr($name, 0, 6) == 'email-') { |
| 381 | $emailField = TRUE; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (!$emailField) { |
| 386 | $status = ts("Email field should be included in profile if you want to use Group(s) when Profile double-opt in process is enabled."); |
| 387 | $this->_session->setStatus($status); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | //transfering all the multirecord custom fields in _fields |
| 392 | if ($this->_multiRecord && !empty($this->_multiRecordFields)) { |
| 393 | $this->_fields = $this->_multiRecordFields; |
| 394 | $this->_multiRecordProfile = TRUE; |
| 395 | } elseif ($this->_multiRecord && empty($this->_multiRecordFields)) { |
| 396 | CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); |
| 397 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (!is_array($this->_fields)) { |
| 402 | CRM_Core_Session::setStatus(ts('This feature is not currently available.'), ts('Sorry'), 'error'); |
| 403 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm', 'reset=1')); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * This function sets the default values for the form. Note that in edit/view mode |
| 409 | * the default values are retrieved from the database |
| 410 | * |
| 411 | * @access public |
| 412 | * |
| 413 | * @return void |
| 414 | */ |
| 415 | function setDefaultsValues() { |
| 416 | $this->_defaults = array(); |
| 417 | if ($this->_multiRecordProfile && ($this->_multiRecord == CRM_Core_Action::DELETE)) { |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | if ($this->_mode != self::MODE_SEARCH) { |
| 422 | // set default values for country / state to start with |
| 423 | CRM_Core_BAO_UFGroup::setRegisterDefaults($this->_fields, $this->_defaults); |
| 424 | } |
| 425 | |
| 426 | if ($this->_id && !$this->_multiRecordProfile) { |
| 427 | if ($this->_isContactActivityProfile) { |
| 428 | $contactFields = $activityFields = array(); |
| 429 | foreach ($this->_fields as $fieldName => $field) { |
| 430 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { |
| 431 | $activityFields[$fieldName] = $field; |
| 432 | } |
| 433 | else { |
| 434 | $contactFields[$fieldName] = $field; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_id, $contactFields, $this->_defaults, TRUE); |
| 439 | if ($this->_activityId) { |
| 440 | CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $this->_activityId, 'Activity', $this->_defaults, TRUE); |
| 441 | } |
| 442 | } |
| 443 | else { |
| 444 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_id, $this->_fields, $this->_defaults, TRUE); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | //set custom field defaults |
| 449 | if ($this->_multiRecordProfile) { |
| 450 | foreach ($this->_multiRecordFields as $key => $field) { |
| 451 | $fieldIds[] = CRM_Core_BAO_CustomField::getKeyID($key); |
| 452 | } |
| 453 | |
| 454 | $defaultValues = array( ); |
| 455 | if ($this->_multiRecord && $this->_multiRecord == CRM_Core_Action::UPDATE) { |
| 456 | $defaultValues = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_id, NULL, $fieldIds, TRUE); |
| 457 | if ($this->_recordExists == TRUE) { |
| 458 | $defaultValues = $defaultValues[$this->_recordId]; |
| 459 | } else { |
| 460 | $defaultValues = NULL; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (!empty($defaultValues)) { |
| 465 | foreach ($defaultValues as $key => $value) { |
| 466 | $name = "custom_{$key}"; |
| 467 | $htmlType = $this->_multiRecordFields[$name]['html_type']; |
| 468 | if ($htmlType != 'File') { |
| 469 | if (isset($value)) { |
| 470 | CRM_Core_BAO_CustomField::setProfileDefaults($key, |
| 471 | $name, |
| 472 | $this->_defaults, |
| 473 | $this->_id, |
| 474 | $this->_mode, |
| 475 | $value |
| 476 | ); |
| 477 | } else { |
| 478 | $this->_defaults[$name] = ""; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if ($htmlType == 'File') { |
| 483 | $entityId = $this->_id; |
| 484 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity' && |
| 485 | $this->_activityId |
| 486 | ) { |
| 487 | $entityId = $this->_activityId; |
| 488 | } |
| 489 | $url = CRM_Core_BAO_CustomField::getFileURL($entityId, $key); |
| 490 | |
| 491 | if ($url) { |
| 492 | $customFiles[$name]['displayURL'] = ts("Attached File") . ": {$url['file_url']}"; |
| 493 | |
| 494 | $deleteExtra = ts("Are you sure you want to delete attached file?"); |
| 495 | $fileId = $url['file_id']; |
| 496 | $deleteURL = CRM_Utils_System::url('civicrm/file', |
| 497 | "reset=1&id={$fileId}&eid=$entityId&fid={$key}&action=delete" |
| 498 | ); |
| 499 | $text = ts("Delete Attached File"); |
| 500 | $customFiles[$field['name']]['deleteURL'] = "<a href=\"{$deleteURL}\" onclick = \"if (confirm( ' $deleteExtra ' )) this.href+='&confirmed=1'; else return false;\">$text</a>"; |
| 501 | |
| 502 | // also delete the required rule that we've set on the form element |
| 503 | $this->removeFileRequiredRules($name); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | else { |
| 510 | foreach ($this->_fields as $name => $field) { |
| 511 | if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) { |
| 512 | $htmlType = $field['html_type']; |
| 513 | if ((!isset($this->_defaults[$name]) || $htmlType == 'File') && |
| 514 | (CRM_Utils_Array::value('field_type', $field) != 'Activity') |
| 515 | ) { |
| 516 | CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, |
| 517 | $name, |
| 518 | $this->_defaults, |
| 519 | $this->_id, |
| 520 | $this->_mode |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | if ($htmlType == 'File') { |
| 525 | $entityId = $this->_id; |
| 526 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity' && $this->_activityId) { |
| 527 | $entityId = $this->_activityId; |
| 528 | } |
| 529 | $url = CRM_Core_BAO_CustomField::getFileURL($entityId, $customFieldID); |
| 530 | |
| 531 | if ($url) { |
| 532 | $customFiles[$field['name']]['displayURL'] = ts("Attached File") . ": {$url['file_url']}"; |
| 533 | |
| 534 | $deleteExtra = ts("Are you sure you want to delete attached file?"); |
| 535 | $fileId = $url['file_id']; |
| 536 | $deleteURL = CRM_Utils_System::url('civicrm/file', |
| 537 | "reset=1&id={$fileId}&eid=$entityId&fid={$customFieldID}&action=delete" |
| 538 | ); |
| 539 | $text = ts("Delete Attached File"); |
| 540 | $customFiles[$field['name']]['deleteURL'] = "<a href=\"{$deleteURL}\" onclick = \"if (confirm( ' $deleteExtra ' )) this.href+='&confirmed=1'; else return false;\">$text</a>"; |
| 541 | |
| 542 | // also delete the required rule that we've set on the form element |
| 543 | $this->removeFileRequiredRules($field['name']); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | if (isset($customFiles)) { |
| 550 | $this->assign('customFiles', $customFiles); |
| 551 | } |
| 552 | |
| 553 | if ($this->_multiRecordProfile) { |
| 554 | $this->setDefaults($this->_defaults); |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (CRM_Utils_Array::value('image_URL', $this->_defaults)) { |
| 559 | list($imageWidth, $imageHeight) = getimagesize($this->_defaults['image_URL']); |
| 560 | list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight); |
| 561 | $this->assign("imageWidth", $imageWidth); |
| 562 | $this->assign("imageHeight", $imageHeight); |
| 563 | $this->assign("imageThumbWidth", $imageThumbWidth); |
| 564 | $this->assign("imageThumbHeight", $imageThumbHeight); |
| 565 | $this->assign("imageURL", $this->_defaults['image_URL']); |
| 566 | $this->removeFileRequiredRules('image_URL'); |
| 567 | } |
| 568 | |
| 569 | if (array_key_exists('contact_sub_type', $this->_defaults) && |
| 570 | !empty($this->_defaults['contact_sub_type']) |
| 571 | ) { |
| 572 | $this->_defaults['contact_sub_type'] = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 573 | trim($this->_defaults['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR) |
| 574 | ); |
| 575 | } |
| 576 | |
| 577 | $this->setDefaults($this->_defaults); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Function to actually build the form |
| 582 | * |
| 583 | * @return void |
| 584 | * @access public |
| 585 | */ |
| 586 | public function buildQuickForm() { |
| 587 | switch ($this->_mode) { |
| 588 | case self::MODE_CREATE: |
| 589 | case self::MODE_EDIT: |
| 590 | case self::MODE_REGISTER: |
| 591 | CRM_Utils_Hook::buildProfile($this->_ufGroupName); |
| 592 | break; |
| 593 | case self::MODE_SEARCH: |
| 594 | CRM_Utils_Hook::searchProfile($this->_ufGroupName); |
| 595 | break; |
| 596 | default: |
| 597 | } |
| 598 | |
| 599 | //lets have single status message, CRM-4363 |
| 600 | $return = FALSE; |
| 601 | $statusMessage = NULL; |
| 602 | if (($this->_multiRecord & CRM_Core_Action::ADD) && $this->_maxRecordLimit) { |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | if (($this->_multiRecord & CRM_Core_Action::DELETE)) { |
| 607 | if (!$this->_recordExists) { |
| 608 | CRM_Core_Session::setStatus(ts('The record %1 doesnot exists', array(1 => $this->_recordId)), ts('Record doesnot exists'), 'alert'); |
| 609 | } |
| 610 | else { |
| 611 | $this->assign('deleteRecord', TRUE); |
| 612 | } |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | CRM_Core_BAO_Address::checkContactSharedAddressFields($this->_fields, $this->_id); |
| 617 | |
| 618 | // we should not allow component and mix profiles in search mode |
| 619 | if ($this->_mode != self::MODE_REGISTER) { |
| 620 | //check for mix profile fields (eg: individual + other contact type) |
| 621 | if (CRM_Core_BAO_UFField::checkProfileType($this->_gid)) { |
| 622 | if (($this->_mode & self::MODE_EDIT) && $this->_isContactActivityProfile) { |
| 623 | $errors = self::validateContactActivityProfile($this->_activityId, $this->_id, $this->_gid); |
| 624 | if (!empty($errors)) { |
| 625 | $statusMessage = array_pop($errors); |
| 626 | $return = TRUE; |
| 627 | } |
| 628 | } |
| 629 | else { |
| 630 | $statusMessage = ts('Profile search, view and edit are not supported for Profiles which include fields for more than one record type.'); |
| 631 | $return = TRUE; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); |
| 636 | |
| 637 | if ($this->_id) { |
| 638 | $contactTypes = CRM_Contact_BAO_Contact::getContactTypes($this->_id); |
| 639 | $contactType = $contactTypes[0]; |
| 640 | |
| 641 | array_shift($contactTypes); |
| 642 | $contactSubtypes = $contactTypes; |
| 643 | |
| 644 | $profileSubType = FALSE; |
| 645 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { |
| 646 | $profileSubType = $profileType; |
| 647 | $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType); |
| 648 | } |
| 649 | |
| 650 | if ( |
| 651 | ($profileType != 'Contact' && !$this->_isContactActivityProfile) && |
| 652 | (($profileSubType && !empty($contactSubtypes) && (!in_array($profileSubType,$contactSubtypes))) || |
| 653 | ($profileType != $contactType)) |
| 654 | ) { |
| 655 | $return = TRUE; |
| 656 | if (!$statusMessage) { |
| 657 | $statusMessage = |
| 658 | ts("This profile is configured for contact type '%1'. It cannot be used to edit contacts of other types.", |
| 659 | array(1 => $profileSubType ? $profileSubType : $profileType)); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | if ( |
| 665 | in_array( |
| 666 | $profileType, |
| 667 | array("Membership", "Participant", "Contribution") |
| 668 | ) |
| 669 | ) { |
| 670 | $return = TRUE; |
| 671 | if (!$statusMessage) { |
| 672 | $statusMessage = ts('Profile is not configured for the selected action.'); |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | //lets have sigle status message, |
| 678 | $this->assign('statusMessage', $statusMessage); |
| 679 | if ($return) { |
| 680 | return FALSE; |
| 681 | } |
| 682 | |
| 683 | $this->assign('id', $this->_id); |
| 684 | $this->assign('mode', $this->_mode); |
| 685 | $this->assign('action', $this->_action); |
| 686 | $this->assign('fields', $this->_fields); |
| 687 | $this->assign('fieldset', (isset($this->_fieldset)) ? $this->_fieldset : ""); |
| 688 | |
| 689 | // should we restrict what we display |
| 690 | $admin = TRUE; |
| 691 | if ($this->_mode == self::MODE_EDIT) { |
| 692 | $admin = FALSE; |
| 693 | // show all fields that are visibile: |
| 694 | // if we are a admin OR the same user OR acl-user with access to the profile |
| 695 | // or we have checksum access to this contact (i.e. the user without a login) - CRM-5909 |
| 696 | if ( |
| 697 | CRM_Core_Permission::check('administer users') || |
| 698 | $this->_id == $this->_currentUserID || |
| 699 | $this->_isPermissionedChecksum || |
| 700 | in_array( |
| 701 | $this->_gid, |
| 702 | CRM_ACL_API::group( |
| 703 | CRM_Core_Permission::EDIT, |
| 704 | NULL, |
| 705 | 'civicrm_uf_group', |
| 706 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id') |
| 707 | ) |
| 708 | ) |
| 709 | ) { |
| 710 | $admin = TRUE; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // if false, user is not logged-in. |
| 715 | $anonUser = FALSE; |
| 716 | if (!$this->_currentUserID) { |
| 717 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
| 718 | $primaryLocationType = $defaultLocationType->id; |
| 719 | $anonUser = TRUE; |
| 720 | } |
| 721 | $this->assign('anonUser', $anonUser); |
| 722 | |
| 723 | $addCaptcha = array(); |
| 724 | $emailPresent = FALSE; |
| 725 | |
| 726 | // cache the state country fields. based on the results, we could use our javascript solution |
| 727 | // in create or register mode |
| 728 | $stateCountryMap = array(); |
| 729 | |
| 730 | // add the form elements |
| 731 | foreach ($this->_fields as $name => $field) { |
| 732 | // make sure that there is enough permission to expose this field |
| 733 | if (!$admin && $field['visibility'] == 'User and User Admin Only') { |
| 734 | unset($this->_fields[$name]); |
| 735 | continue; |
| 736 | } |
| 737 | |
| 738 | // since the CMS manages the email field, suppress the email display if in |
| 739 | // register mode which occur within the CMS form |
| 740 | if ($this->_mode == self::MODE_REGISTER && substr($name, 0, 5) == 'email') { |
| 741 | unset($this->_fields[$name]); |
| 742 | continue; |
| 743 | } |
| 744 | |
| 745 | list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); |
| 746 | if ($prefixName == 'state_province' || $prefixName == 'country' || $prefixName == 'county') { |
| 747 | if (!array_key_exists($index, $stateCountryMap)) { |
| 748 | $stateCountryMap[$index] = array(); |
| 749 | } |
| 750 | $stateCountryMap[$index][$prefixName] = $name; |
| 751 | } |
| 752 | |
| 753 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, $this->_mode); |
| 754 | |
| 755 | if ($field['add_to_group_id']) { |
| 756 | $addToGroupId = $field['add_to_group_id']; |
| 757 | } |
| 758 | |
| 759 | //build array for captcha |
| 760 | if ($field['add_captcha']) { |
| 761 | $addCaptcha[$field['group_id']] = $field['add_captcha']; |
| 762 | } |
| 763 | |
| 764 | if (($name == 'email-Primary') || ($name == 'email-' . isset($primaryLocationType) ? $primaryLocationType : "")) { |
| 765 | $emailPresent = TRUE; |
| 766 | $this->_mail = $name; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | // add captcha only for create mode. |
| 771 | if ($this->_mode == self::MODE_CREATE) { |
| 772 | // suppress captcha for logged in users only |
| 773 | if ($this->_currentUserID) { |
| 774 | $this->_isAddCaptcha = FALSE; |
| 775 | } |
| 776 | else if (!$this->_isAddCaptcha && !empty($addCaptcha)) { |
| 777 | $this->_isAddCaptcha = TRUE; |
| 778 | } |
| 779 | |
| 780 | if ($this->_gid) { |
| 781 | $dao = new CRM_Core_DAO_UFGroup(); |
| 782 | $dao->id = $this->_gid; |
| 783 | $dao->addSelect(); |
| 784 | $dao->addSelect('is_update_dupe'); |
| 785 | if ($dao->find(TRUE)) { |
| 786 | if ($dao->is_update_dupe) { |
| 787 | $this->_isUpdateDupe = $dao->is_update_dupe; |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | else { |
| 793 | $this->_isAddCaptcha = FALSE; |
| 794 | } |
| 795 | |
| 796 | //finally add captcha to form. |
| 797 | if ($this->_isAddCaptcha) { |
| 798 | $captcha = CRM_Utils_ReCAPTCHA::singleton(); |
| 799 | $captcha->add($this); |
| 800 | } |
| 801 | $this->assign("isCaptcha", $this->_isAddCaptcha); |
| 802 | |
| 803 | if ($this->_mode != self::MODE_SEARCH) { |
| 804 | if (isset($addToGroupId)) { |
| 805 | $this->add('hidden', "add_to_group", $addToGroupId); |
| 806 | $this->_addToGroupID = $addToGroupId; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | // lets do the defaults, so we can use it for the below state country routines |
| 811 | $this->setDefaultsValues(); |
| 812 | |
| 813 | // also do state country js |
| 814 | CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap, $this->_defaults); |
| 815 | |
| 816 | $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, NULL); |
| 817 | if ($this->_mode == self::MODE_CREATE) { |
| 818 | CRM_Core_BAO_CMSUser::buildForm($this, $this->_gid, $emailPresent, $action); |
| 819 | } |
| 820 | else { |
| 821 | $this->assign('showCMS', FALSE); |
| 822 | } |
| 823 | |
| 824 | $this->assign('groupId', $this->_gid); |
| 825 | |
| 826 | // now fix all state country selectors |
| 827 | CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults); |
| 828 | |
| 829 | // if view mode pls freeze it with the done button. |
| 830 | if ($this->_action & CRM_Core_Action::VIEW) { |
| 831 | $this->freeze(); |
| 832 | } |
| 833 | |
| 834 | if ($this->_context == 'dialog') { |
| 835 | $this->addElement( |
| 836 | 'submit', |
| 837 | $this->_duplicateButtonName, |
| 838 | ts('Save Matching Contact') |
| 839 | ); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Function to validate profile and provided activity Id |
| 845 | * |
| 846 | * @params Integer $activityId Activity Id |
| 847 | * @params Integer $gid Profile Id |
| 848 | * |
| 849 | * @return Array $errors Errors ( if any ). |
| 850 | */ |
| 851 | static function validateContactActivityProfile($activityId, $contactId, $gid) { |
| 852 | $errors = array(); |
| 853 | if (!$activityId) { |
| 854 | $errors[] = 'Profile is using one or more activity fields, and is missing the activity Id (aid) in the URL.'; |
| 855 | return $errors; |
| 856 | } |
| 857 | |
| 858 | $activityDetails = array(); |
| 859 | $activityParams = array('id' => $activityId); |
| 860 | CRM_Activity_BAO_Activity::retrieve($activityParams, $activityDetails); |
| 861 | |
| 862 | if (empty($activityDetails)) { |
| 863 | $errors[] = 'Invalid Activity Id (aid).'; |
| 864 | return $errors; |
| 865 | } |
| 866 | |
| 867 | $profileActivityTypes = CRM_Core_BAO_UFGroup::groupTypeValues($gid, 'Activity'); |
| 868 | |
| 869 | if ((CRM_Utils_Array::value('Activity', $profileActivityTypes) && |
| 870 | !in_array($activityDetails['activity_type_id'], $profileActivityTypes['Activity']) |
| 871 | ) || |
| 872 | (!in_array($contactId, $activityDetails['assignee_contact']) && |
| 873 | !in_array($contactId, $activityDetails['target_contact']) |
| 874 | ) |
| 875 | ) { |
| 876 | $errors[] = 'This activity cannot be edited or viewed via this profile.'; |
| 877 | } |
| 878 | |
| 879 | return $errors; |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * global form rule |
| 884 | * |
| 885 | * @param array $fields the input form values |
| 886 | * @param array $files the uploaded files if any |
| 887 | * @param object $form the form object |
| 888 | * |
| 889 | * @return true if no errors, else array of errors |
| 890 | * @access public |
| 891 | * @static |
| 892 | */ |
| 893 | static function formRule($fields, $files, $form) { |
| 894 | CRM_Utils_Hook::validateProfile($form->_ufGroupName); |
| 895 | |
| 896 | $errors = array(); |
| 897 | // if no values, return |
| 898 | if (empty($fields)) { |
| 899 | return TRUE; |
| 900 | } |
| 901 | |
| 902 | $register = NULL; |
| 903 | |
| 904 | // hack we use a -1 in options to indicate that its registration |
| 905 | if ($form->_id) { |
| 906 | $form->_isUpdateDupe = 1; |
| 907 | } |
| 908 | |
| 909 | if ($form->_mode == CRM_Profile_Form::MODE_REGISTER) { |
| 910 | $register = TRUE; |
| 911 | } |
| 912 | |
| 913 | // dont check for duplicates during registration validation: CRM-375 |
| 914 | if (!$register && !CRM_Utils_Array::value('_qf_Edit_upload_duplicate', $fields)) { |
| 915 | // fix for CRM-3240 |
| 916 | if (CRM_Utils_Array::value('email-Primary', $fields)) { |
| 917 | $fields['email'] = CRM_Utils_Array::value('email-Primary', $fields); |
| 918 | } |
| 919 | |
| 920 | // fix for CRM-6141 |
| 921 | if (CRM_Utils_Array::value('phone-Primary-1', $fields) && |
| 922 | !CRM_Utils_Array::value('phone-Primary', $fields) |
| 923 | ) { |
| 924 | $fields['phone-Primary'] = $fields['phone-Primary-1']; |
| 925 | } |
| 926 | |
| 927 | $ctype = CRM_Core_BAO_UFGroup::getContactType($form->_gid); |
| 928 | // If all profile fields is of Contact Type then consider |
| 929 | // profile is of Individual type(default). |
| 930 | if (!$ctype) { |
| 931 | $ctype = 'Individual'; |
| 932 | } |
| 933 | $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, $ctype); |
| 934 | if ($form->_mode == CRM_Profile_Form::MODE_CREATE) { |
| 935 | // fix for CRM-2888 |
| 936 | $exceptions = array(); |
| 937 | } |
| 938 | else { |
| 939 | // for edit mode we need to allow our own record to be a dupe match! |
| 940 | $exceptions = array($form->_session->get('userID')); |
| 941 | } |
| 942 | |
| 943 | // for dialog mode we should always use fuzzy rule. |
| 944 | $ruleType = 'Unsupervised'; |
| 945 | if ($form->_context == 'dialog') { |
| 946 | $ruleType = 'Supervised'; |
| 947 | } |
| 948 | |
| 949 | $dedupeParams['check_permission'] = FALSE; |
| 950 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, |
| 951 | $ctype, |
| 952 | $ruleType, |
| 953 | $exceptions |
| 954 | ); |
| 955 | if ($ids) { |
| 956 | if ($form->_isUpdateDupe == 2) { |
| 957 | CRM_Core_Session::setStatus(ts('Note: this contact may be a duplicate of an existing record.'), ts('Possible Duplicate Detected'), 'alert'); |
| 958 | } |
| 959 | elseif ($form->_isUpdateDupe == 1) { |
| 960 | if (!$form->_id) { |
| 961 | $form->_id = $ids[0]; |
| 962 | } |
| 963 | } |
| 964 | else { |
| 965 | if ($form->_context == 'dialog') { |
| 966 | $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE); |
| 967 | |
| 968 | $duplicateContactsLinks = '<div class="matching-contacts-found">'; |
| 969 | $duplicateContactsLinks .= ts('One matching contact was found. ', array('count' => count($contactLinks['rows']), 'plural' => '%count matching contacts were found.<br />')); |
| 970 | if ($contactLinks['msg'] == 'view') { |
| 971 | $duplicateContactsLinks .= ts('You can View the existing contact.', array('count' => count($contactLinks['rows']), 'plural' => 'You can View the existing contacts.')); |
| 972 | } |
| 973 | else { |
| 974 | $duplicateContactsLinks .= ts('You can View or Edit the existing contact.', array('count' => count($contactLinks['rows']), 'plural' => 'You can View or Edit the existing contacts.')); |
| 975 | } |
| 976 | $duplicateContactsLinks .= '</div>'; |
| 977 | $duplicateContactsLinks .= '<table class="matching-contacts-actions">'; |
| 978 | $row = ''; |
| 979 | for ($i = 0; $i < sizeof($contactLinks['rows']); $i++) { |
| 980 | $row .= ' <tr> '; |
| 981 | $row .= ' <td class="matching-contacts-name"> '; |
| 982 | $row .= $contactLinks['rows'][$i]['display_name']; |
| 983 | $row .= ' </td>'; |
| 984 | $row .= ' <td class="matching-contacts-email"> '; |
| 985 | $row .= $contactLinks['rows'][$i]['primary_email']; |
| 986 | $row .= ' </td>'; |
| 987 | $row .= ' <td class="action-items"> '; |
| 988 | $row .= $contactLinks['rows'][$i]['view'] . ' '; |
| 989 | $row .= $contactLinks['rows'][$i]['edit']; |
| 990 | $row .= ' </td>'; |
| 991 | $row .= ' </tr> '; |
| 992 | } |
| 993 | |
| 994 | $duplicateContactsLinks .= $row . '</table>'; |
| 995 | $duplicateContactsLinks .= "If you're sure this record is not a duplicate, click the 'Save Matching Contact' button below."; |
| 996 | |
| 997 | $errors['_qf_default'] = $duplicateContactsLinks; |
| 998 | |
| 999 | // let smarty know that there are duplicates |
| 1000 | $template = CRM_Core_Smarty::singleton(); |
| 1001 | $template->assign('isDuplicate', 1); |
| 1002 | } |
| 1003 | else { |
| 1004 | $errors['_qf_default'] = ts('A record already exists with the same information.'); |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | foreach ($fields as $key => $value) { |
| 1011 | list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $key, 3); |
| 1012 | if ($fieldName == 'state_province' && !empty($fields["country-{$locTypeId}"])) { |
| 1013 | // Validate Country - State list |
| 1014 | $countryId = $fields["country-{$locTypeId}"]; |
| 1015 | $stateProvinceId = $value; |
| 1016 | |
| 1017 | if ($stateProvinceId && $countryId) { |
| 1018 | $stateProvinceDAO = new CRM_Core_DAO_StateProvince(); |
| 1019 | $stateProvinceDAO->id = $stateProvinceId; |
| 1020 | $stateProvinceDAO->find(TRUE); |
| 1021 | |
| 1022 | if ($stateProvinceDAO->country_id != $countryId) { |
| 1023 | // country mismatch hence display error |
| 1024 | $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); |
| 1025 | $countries = CRM_Core_PseudoConstant::country(); |
| 1026 | $errors[$key] = "State/Province " . $stateProvinces[$stateProvinceId] . " is not part of " . $countries[$countryId] . ". It belongs to " . $countries[$stateProvinceDAO->country_id] . "."; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if ($fieldName == 'county' && $fields["state_province-{$locTypeId}"]) { |
| 1032 | // Validate County - State list |
| 1033 | $stateProvinceId = $fields["state_province-{$locTypeId}"]; |
| 1034 | $countyId = $value; |
| 1035 | |
| 1036 | if ($countyId && $stateProvinceId) { |
| 1037 | $countyDAO = new CRM_Core_DAO_County(); |
| 1038 | $countyDAO->id = $countyId; |
| 1039 | $countyDAO->find(TRUE); |
| 1040 | |
| 1041 | if ($countyDAO->state_province_id != $stateProvinceId) { |
| 1042 | // state province mismatch hence display error |
| 1043 | $stateProvinces = CRM_Core_PseudoConstant::stateProvince(); |
| 1044 | $counties = CRM_Core_PseudoConstant::county(); |
| 1045 | $errors[$key] = "County " . $counties[$countyId] . " is not part of " . $stateProvinces[$stateProvinceId] . ". It belongs to " . $stateProvinces[$countyDAO->state_province_id] . "."; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { |
| 1051 | if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { |
| 1052 | $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); |
| 1053 | if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) { |
| 1054 | $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', |
| 1055 | array(1 => ucwords(str_replace('_', ' ', $greeting))) |
| 1056 | ); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | return empty($errors) ? TRUE : $errors; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * Process the user submitted custom data values. |
| 1066 | * |
| 1067 | * @access public |
| 1068 | * |
| 1069 | * @return void |
| 1070 | */ |
| 1071 | public function postProcess() { |
| 1072 | $params = $this->controller->exportValues($this->_name); |
| 1073 | |
| 1074 | //if the delete record button is clicked |
| 1075 | if ($this->_deleteButtonName) { |
| 1076 | if (CRM_Utils_Array::value($this->_deleteButtonName, $_POST) && $this->_recordId) { |
| 1077 | $filterParams['id'] = $this->_customGroupId; |
| 1078 | $returnProperities = array('is_multiple', 'table_name'); |
| 1079 | CRM_Core_DAO::commonRetrieve("CRM_Core_DAO_CustomGroup", $filterParams, $returnValues, $returnProperities); |
| 1080 | if (CRM_Utils_Array::value('is_multiple', $returnValues)) { |
| 1081 | if ($tableName = CRM_Utils_Array::value('table_name', $returnValues)) { |
| 1082 | $sql = "DELETE FROM {$tableName} WHERE id = %1 AND entity_id = %2"; |
| 1083 | $sqlParams = array( |
| 1084 | 1 => array($this->_recordId, 'Integer'), |
| 1085 | 2 => array($this->_id, 'Integer') |
| 1086 | ); |
| 1087 | CRM_Core_DAO::executeQuery($sql, $sqlParams); |
| 1088 | CRM_Core_Session::setStatus(ts('Your record has been deleted.'), ts('Deleted'), 'success'); |
| 1089 | } |
| 1090 | } |
| 1091 | return; |
| 1092 | } |
| 1093 | } |
| 1094 | CRM_Utils_Hook::processProfile($this->_ufGroupName); |
| 1095 | if (CRM_Utils_Array::value('image_URL', $params)) { |
| 1096 | CRM_Contact_BAO_Contact::processImageParams($params); |
| 1097 | } |
| 1098 | |
| 1099 | $greetingTypes = array( |
| 1100 | 'addressee' => 'addressee_id', |
| 1101 | 'email_greeting' => 'email_greeting_id', |
| 1102 | 'postal_greeting' => 'postal_greeting_id', |
| 1103 | ); |
| 1104 | |
| 1105 | $details = array(); |
| 1106 | if ($this->_id) { |
| 1107 | $contactDetails = CRM_Contact_BAO_Contact::getHierContactDetails($this->_id, |
| 1108 | $greetingTypes |
| 1109 | ); |
| 1110 | $details = $contactDetails[0][$this->_id]; |
| 1111 | } |
| 1112 | if (!(CRM_Utils_Array::value('addressee_id', $details) || |
| 1113 | CRM_Utils_Array::value('email_greeting_id', $details) || |
| 1114 | CRM_Utils_Array::value('postal_greeting_id', $details) |
| 1115 | )) { |
| 1116 | |
| 1117 | $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid); |
| 1118 | //Though Profile type is contact we need |
| 1119 | //Individual/Household/Organization for setting Greetings. |
| 1120 | if ($profileType == 'Contact') { |
| 1121 | $profileType = 'Individual'; |
| 1122 | //if we editing Household/Organization. |
| 1123 | if ($this->_id) { |
| 1124 | $profileType = CRM_Contact_BAO_Contact::getContactType($this->_id); |
| 1125 | } |
| 1126 | } |
| 1127 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { |
| 1128 | $profileType = CRM_Contact_BAO_ContactType::getBasicType($profileType); |
| 1129 | } |
| 1130 | |
| 1131 | foreach ($greetingTypes as $key => $value) { |
| 1132 | if (!array_key_exists($key, $params)) { |
| 1133 | $params[$key] = CRM_Contact_BAO_Contact_Utils::defaultGreeting($profileType, $key); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | $transaction = new CRM_Core_Transaction(); |
| 1139 | |
| 1140 | //used to send subcribe mail to the group which user want. |
| 1141 | //if the profile double option in is enabled |
| 1142 | $mailingType = array(); |
| 1143 | $config = CRM_Core_Config::singleton(); |
| 1144 | |
| 1145 | $result = NULL; |
| 1146 | foreach ($params as $name => $values) { |
| 1147 | if (substr($name, 0, 6) == 'email-') { |
| 1148 | $result['email'] = $values; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | //array of group id, subscribed by contact |
| 1153 | $contactGroup = array(); |
| 1154 | if (CRM_Utils_Array::value('group', $params) && |
| 1155 | CRM_Core_BAO_UFGroup::isProfileDoubleOptin() |
| 1156 | ) { |
| 1157 | $groupSubscribed = array(); |
| 1158 | if (CRM_Utils_Array::value('email', $result)) { |
| 1159 | if ($this->_id) { |
| 1160 | $contactGroups = new CRM_Contact_DAO_GroupContact(); |
| 1161 | $contactGroups->contact_id = $this->_id; |
| 1162 | $contactGroups->status = 'Added'; |
| 1163 | $contactGroups->find(); |
| 1164 | $contactGroup = array(); |
| 1165 | while ($contactGroups->fetch()) { |
| 1166 | $contactGroup[] = $contactGroups->group_id; |
| 1167 | $groupSubscribed[$contactGroups->group_id] = 1; |
| 1168 | } |
| 1169 | } |
| 1170 | foreach ($params['group'] as $key => $val) { |
| 1171 | if (!$val) { |
| 1172 | unset($params['group'][$key]); |
| 1173 | continue; |
| 1174 | } |
| 1175 | $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', |
| 1176 | $key, 'group_type', 'id' |
| 1177 | ); |
| 1178 | $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 1179 | substr($groupTypes, 1, -1) |
| 1180 | ); |
| 1181 | //filter group of mailing type and unset it from params |
| 1182 | if (in_array(2, $groupType)) { |
| 1183 | //if group is already subscribed , ignore it |
| 1184 | $groupExist = CRM_Utils_Array::key($key, $contactGroup); |
| 1185 | if (!isset($groupExist)) { |
| 1186 | $mailingType[] = $key; |
| 1187 | unset($params['group'][$key]); |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | $addToGroupId = NULL; |
| 1195 | if (CRM_Utils_Array::value('add_to_group', $params)) { |
| 1196 | $addToGroupId = $params['add_to_group']; |
| 1197 | |
| 1198 | //run same check whether group is a mailing list |
| 1199 | $groupTypes = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', |
| 1200 | $addToGroupId, 'group_type', 'id' |
| 1201 | ); |
| 1202 | $groupType = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 1203 | substr($groupTypes, 1, -1) |
| 1204 | ); |
| 1205 | //filter group of mailing type and unset it from params |
| 1206 | if (in_array(2, $groupType) && |
| 1207 | CRM_Utils_Array::value('email', $result) && |
| 1208 | CRM_Core_BAO_UFGroup::isProfileAddToGroupDoubleOptin() |
| 1209 | ) { |
| 1210 | if (!count($contactGroup)) { |
| 1211 | //array of group id, subscribed by contact |
| 1212 | $contactGroup = array(); |
| 1213 | if ($this->_id) { |
| 1214 | $contactGroups = new CRM_Contact_DAO_GroupContact(); |
| 1215 | $contactGroups->contact_id = $this->_id; |
| 1216 | $contactGroups->status = 'Added'; |
| 1217 | $contactGroups->find(); |
| 1218 | $contactGroup = array(); |
| 1219 | while ($contactGroups->fetch()) { |
| 1220 | $contactGroup[] = $contactGroups->group_id; |
| 1221 | $groupSubscribed[$contactGroups->group_id] = 1; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | //if group is already subscribed , ignore it |
| 1226 | $groupExist = CRM_Utils_Array::key($addToGroupId, $contactGroup); |
| 1227 | if (!isset($groupExist)) { |
| 1228 | $mailingType[] = $addToGroupId; |
| 1229 | $addToGroupId = NULL; |
| 1230 | } |
| 1231 | } |
| 1232 | else { |
| 1233 | // since we are directly adding contact to group lets unset it from mailing |
| 1234 | if ($key = array_search($addToGroupId, $mailingType)) { |
| 1235 | unset($mailingType[$key]); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | if ($this->_grid) { |
| 1241 | $params['group'] = $groupSubscribed; |
| 1242 | } |
| 1243 | |
| 1244 | // commenting below code, since we potentially |
| 1245 | // triggered maximum name field formatting cases during CRM-4430. |
| 1246 | // CRM-4343 |
| 1247 | // $params['preserveDBName'] = true; |
| 1248 | |
| 1249 | $profileFields = $this->_fields; |
| 1250 | if (($this->_mode & self::MODE_EDIT) && $this->_activityId && $this->_isContactActivityProfile) { |
| 1251 | $profileFields = $activityParams = array(); |
| 1252 | foreach ($this->_fields as $fieldName => $field) { |
| 1253 | if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { |
| 1254 | if (isset($params[$fieldName])) { |
| 1255 | $activityParams[$fieldName] = $params[$fieldName]; |
| 1256 | } |
| 1257 | if (isset($params['activity_date_time'])) { |
| 1258 | $activityParams['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time'], $params['activity_date_time_time']); |
| 1259 | } |
| 1260 | if (CRM_Utils_Array::value($fieldName, $params) && isset($params["{$fieldName}_id"])) { |
| 1261 | $activityParams[$fieldName] = $params["{$fieldName}_id"]; |
| 1262 | } |
| 1263 | } |
| 1264 | else { |
| 1265 | $profileFields[$fieldName] = $field; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | if (!empty($activityParams)) { |
| 1270 | $activityParams['version'] = 3; |
| 1271 | $activityParams['id'] = $this->_activityId; |
| 1272 | $activityParams['skipRecentView'] = TRUE; |
| 1273 | civicrm_api('Activity', 'create', $activityParams); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | if ($this->_multiRecord && $this->_recordId && $this->_multiRecordFields && $this->_recordExists) { |
| 1278 | $params['customRecordValues'][$this->_recordId] = array_keys($this->_multiRecordFields); |
| 1279 | } |
| 1280 | |
| 1281 | $this->_id = CRM_Contact_BAO_Contact::createProfileContact( |
| 1282 | $params, |
| 1283 | $profileFields, |
| 1284 | $this->_id, |
| 1285 | $addToGroupId, |
| 1286 | $this->_gid, |
| 1287 | $this->_ctype, |
| 1288 | TRUE |
| 1289 | ); |
| 1290 | |
| 1291 | //mailing type group |
| 1292 | if (!empty($mailingType)) { |
| 1293 | // we send in the contactID so we match the same groups and are exact, rather than relying on email |
| 1294 | // CRM-8710 |
| 1295 | CRM_Mailing_Event_BAO_Subscribe::commonSubscribe($mailingType, $result, $this->_id, 'profile'); |
| 1296 | } |
| 1297 | |
| 1298 | $ufGroups = array(); |
| 1299 | if ($this->_gid) { |
| 1300 | $ufGroups[$this->_gid] = 1; |
| 1301 | } |
| 1302 | elseif ($this->_mode == self::MODE_REGISTER) { |
| 1303 | $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('User Registration'); |
| 1304 | } |
| 1305 | |
| 1306 | foreach ($ufGroups as $gId => $val) { |
| 1307 | if ($notify = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $gId, 'notify')) { |
| 1308 | $values = CRM_Core_BAO_UFGroup::checkFieldsEmptyValues($gId, $this->_id, NULL); |
| 1309 | CRM_Core_BAO_UFGroup::commonSendMail($this->_id, $values); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | //create CMS user (if CMS user option is selected in profile) |
| 1314 | if (CRM_Utils_Array::value('cms_create_account', $params) && |
| 1315 | $this->_mode == self::MODE_CREATE |
| 1316 | ) { |
| 1317 | $params['contactID'] = $this->_id; |
| 1318 | if (!CRM_Core_BAO_CMSUser::create($params, $this->_mail)) { |
| 1319 | CRM_Core_Session::setStatus(ts('Your profile is not saved and Account is not created.'), ts('Profile Error'), 'error'); |
| 1320 | CRM_Core_Error::debug_log_message("Rolling back transaction as CMSUser Create failed in Profile_Form for contact " . $params['contactID']); |
| 1321 | $transaction->rollback(); |
| 1322 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/profile/create', |
| 1323 | 'reset=1&gid=' . $this->_gid |
| 1324 | )); |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | $transaction->commit(); |
| 1329 | } |
| 1330 | |
| 1331 | function checkTemplateFileExists($suffix = NULL) { |
| 1332 | if ($this->_gid) { |
| 1333 | $templateFile = "CRM/Profile/Form/{$this->_gid}/{$this->_name}.{$suffix}tpl"; |
| 1334 | $template = CRM_Core_Form::getTemplate(); |
| 1335 | if ($template->template_exists($templateFile)) { |
| 1336 | return $templateFile; |
| 1337 | } |
| 1338 | |
| 1339 | // lets see if we have customized by name |
| 1340 | $ufGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'name'); |
| 1341 | if ($ufGroupName) { |
| 1342 | $templateFile = "CRM/Profile/Form/{$ufGroupName}/{$this->_name}.{$suffix}tpl"; |
| 1343 | if ($template->template_exists($templateFile)) { |
| 1344 | return $templateFile; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | |
| 1351 | function getTemplateFileName() { |
| 1352 | $fileName = $this->checkTemplateFileExists(); |
| 1353 | return $fileName ? $fileName : parent::getTemplateFileName(); |
| 1354 | } |
| 1355 | |
| 1356 | function overrideExtraTemplateFileName() { |
| 1357 | $fileName = $this->checkTemplateFileExists('extra.'); |
| 1358 | return $fileName ? $fileName : parent::overrideExtraTemplateFileName(); |
| 1359 | } |
| 1360 | } |
| 1361 | |