| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 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 | class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact { |
| 35 | |
| 36 | /** |
| 37 | * SQL function used to format the phone_numeric field via trigger. |
| 38 | * @see self::triggerInfo() |
| 39 | * |
| 40 | * Note that this is also used by the 4.3 upgrade script. |
| 41 | * @see CRM_Upgrade_Incremental_php_FourThree |
| 42 | */ |
| 43 | const DROP_STRIP_FUNCTION_43 = "DROP FUNCTION IF EXISTS civicrm_strip_non_numeric"; |
| 44 | const CREATE_STRIP_FUNCTION_43 = " |
| 45 | CREATE FUNCTION civicrm_strip_non_numeric(input VARCHAR(255) CHARACTER SET utf8) |
| 46 | RETURNS VARCHAR(255) CHARACTER SET utf8 |
| 47 | DETERMINISTIC |
| 48 | NO SQL |
| 49 | BEGIN |
| 50 | DECLARE output VARCHAR(255) CHARACTER SET utf8 DEFAULT ''; |
| 51 | DECLARE iterator INT DEFAULT 1; |
| 52 | WHILE iterator < (LENGTH(input) + 1) DO |
| 53 | IF SUBSTRING(input, iterator, 1) IN ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') THEN |
| 54 | SET output = CONCAT(output, SUBSTRING(input, iterator, 1)); |
| 55 | END IF; |
| 56 | SET iterator = iterator + 1; |
| 57 | END WHILE; |
| 58 | RETURN output; |
| 59 | END"; |
| 60 | |
| 61 | /** |
| 62 | * The types of communication preferences. |
| 63 | * |
| 64 | * @var array |
| 65 | */ |
| 66 | static $_commPrefs = array( |
| 67 | 'do_not_phone', |
| 68 | 'do_not_email', |
| 69 | 'do_not_mail', |
| 70 | 'do_not_sms', |
| 71 | 'do_not_trade', |
| 72 | ); |
| 73 | |
| 74 | /** |
| 75 | * Types of greetings. |
| 76 | * |
| 77 | * @var array |
| 78 | */ |
| 79 | static $_greetingTypes = array( |
| 80 | 'addressee', |
| 81 | 'email_greeting', |
| 82 | 'postal_greeting', |
| 83 | ); |
| 84 | |
| 85 | /** |
| 86 | * Static field for all the contact information that we can potentially import. |
| 87 | * |
| 88 | * @var array |
| 89 | */ |
| 90 | static $_importableFields = array(); |
| 91 | |
| 92 | /** |
| 93 | * Static field for all the contact information that we can potentially export. |
| 94 | * |
| 95 | * @var array |
| 96 | */ |
| 97 | static $_exportableFields = NULL; |
| 98 | |
| 99 | /** |
| 100 | * Class constructor. |
| 101 | */ |
| 102 | public function __construct() { |
| 103 | parent::__construct(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Takes an associative array and creates a contact object. |
| 108 | * |
| 109 | * The function extracts all the params it needs to initialize the create a |
| 110 | * contact object. the params array could contain additional unused name/value |
| 111 | * pairs |
| 112 | * |
| 113 | * @param array $params |
| 114 | * (reference) an assoc array of name/value pairs. |
| 115 | * |
| 116 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error|NULL |
| 117 | * Created or updated contact object or error object. |
| 118 | * (error objects are being phased out in favour of exceptions) |
| 119 | */ |
| 120 | public static function add(&$params) { |
| 121 | $contact = new CRM_Contact_DAO_Contact(); |
| 122 | |
| 123 | if (empty($params)) { |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | // Fix for validate contact sub type CRM-5143. |
| 128 | if (isset($params['contact_sub_type'])) { |
| 129 | if (empty($params['contact_sub_type'])) { |
| 130 | $params['contact_sub_type'] = 'null'; |
| 131 | } |
| 132 | else { |
| 133 | if (!CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], |
| 134 | $params['contact_type'], TRUE |
| 135 | ) |
| 136 | ) { |
| 137 | // we'll need to fix tests to handle this |
| 138 | // CRM-7925 |
| 139 | CRM_Core_Error::fatal(ts('The Contact Sub Type does not match the Contact type for this record')); |
| 140 | } |
| 141 | if (is_array($params['contact_sub_type'])) { |
| 142 | $params['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['contact_sub_type']) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 143 | } |
| 144 | else { |
| 145 | $params['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . trim($params['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | else { |
| 150 | // Reset the value. |
| 151 | // CRM-101XX. |
| 152 | $params['contact_sub_type'] = 'null'; |
| 153 | } |
| 154 | |
| 155 | // Fixed contact source. |
| 156 | if (isset($params['contact_source'])) { |
| 157 | $params['source'] = $params['contact_source']; |
| 158 | } |
| 159 | |
| 160 | // Fix for preferred communication method. |
| 161 | $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params); |
| 162 | if ($prefComm && is_array($prefComm)) { |
| 163 | unset($params['preferred_communication_method']); |
| 164 | $newPref = array(); |
| 165 | |
| 166 | foreach ($prefComm as $k => $v) { |
| 167 | if ($v) { |
| 168 | $newPref[$k] = $v; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $prefComm = $newPref; |
| 173 | if (is_array($prefComm) && !empty($prefComm)) { |
| 174 | $prefComm = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($prefComm)) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 175 | $contact->preferred_communication_method = $prefComm; |
| 176 | } |
| 177 | else { |
| 178 | $contact->preferred_communication_method = ''; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | $allNull = $contact->copyValues($params); |
| 183 | |
| 184 | $contact->id = CRM_Utils_Array::value('contact_id', $params); |
| 185 | |
| 186 | if ($contact->contact_type == 'Individual') { |
| 187 | $allNull = FALSE; |
| 188 | |
| 189 | // Format individual fields. |
| 190 | CRM_Contact_BAO_Individual::format($params, $contact); |
| 191 | } |
| 192 | elseif ($contact->contact_type == 'Household') { |
| 193 | if (isset($params['household_name'])) { |
| 194 | $allNull = FALSE; |
| 195 | $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, ''); |
| 196 | } |
| 197 | } |
| 198 | elseif ($contact->contact_type == 'Organization') { |
| 199 | if (isset($params['organization_name'])) { |
| 200 | $allNull = FALSE; |
| 201 | $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, ''); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | $privacy = CRM_Utils_Array::value('privacy', $params); |
| 206 | if ($privacy && |
| 207 | is_array($privacy) && |
| 208 | !empty($privacy) |
| 209 | ) { |
| 210 | $allNull = FALSE; |
| 211 | foreach (self::$_commPrefs as $name) { |
| 212 | $contact->$name = CRM_Utils_Array::value($name, $privacy, FALSE); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Since hash was required, make sure we have a 0 value for it (CRM-1063). |
| 217 | // @todo - does this mean we can remove this block? |
| 218 | // Fixed in 1.5 by making hash optional, only do this in create mode, not update. |
| 219 | if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) { |
| 220 | $allNull = FALSE; |
| 221 | $contact->hash = md5(uniqid(rand(), TRUE)); |
| 222 | } |
| 223 | |
| 224 | // Even if we don't need $employerId, it's important to call getFieldValue() before |
| 225 | // the contact is saved because we want the existing value to be cached. |
| 226 | // createCurrentEmployerRelationship() needs the old value not the updated one. CRM-10788 |
| 227 | $employerId = empty($contact->id) ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id'); |
| 228 | |
| 229 | if (!$allNull) { |
| 230 | $contact->save(); |
| 231 | |
| 232 | CRM_Core_BAO_Log::register($contact->id, |
| 233 | 'civicrm_contact', |
| 234 | $contact->id |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | if ($contact->contact_type == 'Individual' && (isset($params['current_employer']) || isset($params['employer_id']))) { |
| 239 | // Create current employer. |
| 240 | $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params); |
| 241 | |
| 242 | $newContact = FALSE; |
| 243 | if (empty($params['contact_id'])) { |
| 244 | $newContact = TRUE; |
| 245 | } |
| 246 | if ($newEmployer) { |
| 247 | CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact); |
| 248 | } |
| 249 | else { |
| 250 | if ($employerId) { |
| 251 | CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Update cached employer name. |
| 257 | if ($contact->contact_type == 'Organization') { |
| 258 | CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id); |
| 259 | } |
| 260 | |
| 261 | return $contact; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Create contact. |
| 266 | * |
| 267 | * takes an associative array and creates a contact object and all the associated |
| 268 | * derived objects (i.e. individual, location, email, phone etc) |
| 269 | * |
| 270 | * This function is invoked from within the web form layer and also from the api layer |
| 271 | * |
| 272 | * @param array $params |
| 273 | * (reference ) an assoc array of name/value pairs. |
| 274 | * @param bool $fixAddress |
| 275 | * If we need to fix address. |
| 276 | * @param bool $invokeHooks |
| 277 | * If we need to invoke hooks. |
| 278 | * |
| 279 | * @param bool $skipDelete |
| 280 | * Unclear parameter, passed to website create |
| 281 | * |
| 282 | * @todo explain this parameter |
| 283 | * |
| 284 | * @throws Exception |
| 285 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error |
| 286 | * Created or updated contribution object. We are deprecating returning an error in |
| 287 | * favour of exceptions |
| 288 | */ |
| 289 | public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE, $skipDelete = FALSE) { |
| 290 | $contact = NULL; |
| 291 | if (empty($params['contact_type']) && empty($params['contact_id'])) { |
| 292 | return $contact; |
| 293 | } |
| 294 | |
| 295 | $isEdit = TRUE; |
| 296 | if ($invokeHooks) { |
| 297 | if (!empty($params['contact_id'])) { |
| 298 | CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params); |
| 299 | } |
| 300 | else { |
| 301 | CRM_Utils_Hook::pre('create', $params['contact_type'], NULL, $params); |
| 302 | $isEdit = FALSE; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | $config = CRM_Core_Config::singleton(); |
| 307 | |
| 308 | // CRM-6942: set preferred language to the current language if it’s unset (and we’re creating a contact). |
| 309 | if (empty($params['contact_id']) && empty($params['preferred_language'])) { |
| 310 | $params['preferred_language'] = $config->lcMessages; |
| 311 | } |
| 312 | |
| 313 | // CRM-9739: set greeting & addressee if unset and we’re creating a contact. |
| 314 | if (empty($params['contact_id'])) { |
| 315 | foreach (self::$_greetingTypes as $greeting) { |
| 316 | if (empty($params[$greeting . '_id'])) { |
| 317 | if ($defaultGreetingTypeId |
| 318 | = CRM_Contact_BAO_Contact_Utils::defaultGreeting($params['contact_type'], $greeting) |
| 319 | ) { |
| 320 | $params[$greeting . '_id'] = $defaultGreetingTypeId; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | $transaction = new CRM_Core_Transaction(); |
| 327 | |
| 328 | $contact = self::add($params); |
| 329 | if (!$contact) { |
| 330 | // Not dying here is stupid, since we get into wierd situation and into a bug that |
| 331 | // is impossible to figure out for the user or for us |
| 332 | // CRM-7925 |
| 333 | CRM_Core_Error::fatal(); |
| 334 | } |
| 335 | |
| 336 | $params['contact_id'] = $contact->id; |
| 337 | |
| 338 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, 'is_enabled')) { |
| 339 | // Enabling multisite causes the contact to be added to the domain group. |
| 340 | $domainGroupID = CRM_Core_BAO_Domain::getGroupId(); |
| 341 | if (!empty($domainGroupID)) { |
| 342 | if (!empty($params['group']) && is_array($params['group'])) { |
| 343 | $params['group'][$domainGroupID] = 1; |
| 344 | } |
| 345 | else { |
| 346 | $params['group'] = array($domainGroupID => 1); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (array_key_exists('group', $params)) { |
| 352 | $contactIds = array($params['contact_id']); |
| 353 | foreach ($params['group'] as $groupId => $flag) { |
| 354 | if ($flag == 1) { |
| 355 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId); |
| 356 | } |
| 357 | elseif ($flag == -1) { |
| 358 | CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $groupId); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Add location Block data. |
| 364 | $blocks = CRM_Core_BAO_Location::create($params, $fixAddress); |
| 365 | foreach ($blocks as $name => $value) { |
| 366 | $contact->$name = $value; |
| 367 | } |
| 368 | |
| 369 | //add website |
| 370 | CRM_Core_BAO_Website::create($params['website'], $contact->id, $skipDelete); |
| 371 | |
| 372 | //get userID from session |
| 373 | $session = CRM_Core_Session::singleton(); |
| 374 | $userID = $session->get('userID'); |
| 375 | // add notes |
| 376 | if (!empty($params['note'])) { |
| 377 | if (is_array($params['note'])) { |
| 378 | foreach ($params['note'] as $note) { |
| 379 | $contactId = $contact->id; |
| 380 | if (isset($note['contact_id'])) { |
| 381 | $contactId = $note['contact_id']; |
| 382 | } |
| 383 | //if logged in user, overwrite contactId |
| 384 | if ($userID) { |
| 385 | $contactId = $userID; |
| 386 | } |
| 387 | |
| 388 | $noteParams = array( |
| 389 | 'entity_id' => $contact->id, |
| 390 | 'entity_table' => 'civicrm_contact', |
| 391 | 'note' => $note['note'], |
| 392 | 'subject' => CRM_Utils_Array::value('subject', $note), |
| 393 | 'contact_id' => $contactId, |
| 394 | ); |
| 395 | CRM_Core_BAO_Note::add($noteParams, CRM_Core_DAO::$_nullArray); |
| 396 | } |
| 397 | } |
| 398 | else { |
| 399 | $contactId = $contact->id; |
| 400 | if (isset($note['contact_id'])) { |
| 401 | $contactId = $note['contact_id']; |
| 402 | } |
| 403 | //if logged in user, overwrite contactId |
| 404 | if ($userID) { |
| 405 | $contactId = $userID; |
| 406 | } |
| 407 | |
| 408 | $noteParams = array( |
| 409 | 'entity_id' => $contact->id, |
| 410 | 'entity_table' => 'civicrm_contact', |
| 411 | 'note' => $params['note'], |
| 412 | 'subject' => CRM_Utils_Array::value('subject', $params), |
| 413 | 'contact_id' => $contactId, |
| 414 | ); |
| 415 | CRM_Core_BAO_Note::add($noteParams, CRM_Core_DAO::$_nullArray); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // update the UF user_unique_id if that has changed |
| 420 | CRM_Core_BAO_UFMatch::updateUFName($contact->id); |
| 421 | |
| 422 | if (!empty($params['custom']) && |
| 423 | is_array($params['custom']) |
| 424 | ) { |
| 425 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contact', $contact->id); |
| 426 | } |
| 427 | |
| 428 | // make a civicrm_subscription_history entry only on contact create (CRM-777) |
| 429 | if (empty($params['contact_id'])) { |
| 430 | $subscriptionParams = array( |
| 431 | 'contact_id' => $contact->id, |
| 432 | 'status' => 'Added', |
| 433 | 'method' => 'Admin', |
| 434 | ); |
| 435 | CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams); |
| 436 | } |
| 437 | |
| 438 | $transaction->commit(); |
| 439 | |
| 440 | // CRM-6367: fetch the right label for contact type’s display |
| 441 | $contact->contact_type_display = CRM_Core_DAO::getFieldValue( |
| 442 | 'CRM_Contact_DAO_ContactType', |
| 443 | $contact->contact_type, |
| 444 | 'label', |
| 445 | 'name' |
| 446 | ); |
| 447 | |
| 448 | if (!$config->doNotResetCache) { |
| 449 | // Note: doNotResetCache flag is currently set by import contact process and merging, |
| 450 | // since resetting and |
| 451 | // rebuilding cache could be expensive (for many contacts). We might come out with better |
| 452 | // approach in future. |
| 453 | CRM_Contact_BAO_Contact_Utils::clearContactCaches($contact->id); |
| 454 | } |
| 455 | |
| 456 | if ($invokeHooks) { |
| 457 | if ($isEdit) { |
| 458 | CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact); |
| 459 | } |
| 460 | else { |
| 461 | CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // process greetings CRM-4575, cache greetings |
| 466 | self::processGreetings($contact); |
| 467 | |
| 468 | return $contact; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Get the display name and image of a contact. |
| 473 | * |
| 474 | * @param int $id |
| 475 | * The contactId. |
| 476 | * |
| 477 | * @param bool $includeTypeInReturnParameters |
| 478 | * Should type be part of the returned array? |
| 479 | * |
| 480 | * @return array |
| 481 | * the displayName and contactImage for this contact |
| 482 | */ |
| 483 | public static function getDisplayAndImage($id, $includeTypeInReturnParameters = FALSE) { |
| 484 | //CRM-14276 added the * on the civicrm_contact table so that we have all the contact info available |
| 485 | $sql = " |
| 486 | SELECT civicrm_contact.*, |
| 487 | civicrm_email.email as email |
| 488 | FROM civicrm_contact |
| 489 | LEFT JOIN civicrm_email ON civicrm_email.contact_id = civicrm_contact.id |
| 490 | AND civicrm_email.is_primary = 1 |
| 491 | WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); |
| 492 | $dao = new CRM_Core_DAO(); |
| 493 | $dao->query($sql); |
| 494 | if ($dao->fetch()) { |
| 495 | $image = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? |
| 496 | $dao->contact_sub_type : $dao->contact_type, FALSE, $id |
| 497 | ); |
| 498 | $imageUrl = CRM_Contact_BAO_Contact_Utils::getImage($dao->contact_sub_type ? |
| 499 | $dao->contact_sub_type : $dao->contact_type, TRUE, $id |
| 500 | ); |
| 501 | |
| 502 | // use email if display_name is empty |
| 503 | if (empty($dao->display_name)) { |
| 504 | $displayName = $dao->email; |
| 505 | } |
| 506 | else { |
| 507 | $displayName = $dao->display_name; |
| 508 | } |
| 509 | |
| 510 | CRM_Utils_Hook::alterDisplayName($displayName, $id, $dao); |
| 511 | |
| 512 | return $includeTypeInReturnParameters ? array( |
| 513 | $displayName, |
| 514 | $image, |
| 515 | $dao->contact_type, |
| 516 | $dao->contact_sub_type, |
| 517 | $imageUrl, |
| 518 | ) : array($displayName, $image, $imageUrl); |
| 519 | } |
| 520 | return NULL; |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Create last viewed link to recently updated contact. |
| 525 | * |
| 526 | * @param array $crudLinkSpec |
| 527 | * - action: int, CRM_Core_Action::UPDATE or CRM_Core_Action::VIEW [default: VIEW] |
| 528 | * - entity_table: string, eg "civicrm_contact" |
| 529 | * - entity_id: int |
| 530 | * |
| 531 | * @return array|NULL |
| 532 | * NULL if unavailable, or |
| 533 | * [path: string, query: string, title: string] |
| 534 | * @see CRM_Utils_System::createDefaultCrudLink |
| 535 | */ |
| 536 | public function createDefaultCrudLink($crudLinkSpec) { |
| 537 | switch ($crudLinkSpec['action']) { |
| 538 | case CRM_Core_Action::VIEW: |
| 539 | $result = array( |
| 540 | 'title' => $this->display_name, |
| 541 | 'path' => 'civicrm/contact/view', |
| 542 | 'query' => array( |
| 543 | 'reset' => 1, |
| 544 | 'cid' => $this->id, |
| 545 | ), |
| 546 | ); |
| 547 | return $result; |
| 548 | |
| 549 | case CRM_Core_Action::UPDATE: |
| 550 | $result = array( |
| 551 | 'title' => $this->display_name, |
| 552 | 'path' => 'civicrm/contact/add', |
| 553 | 'query' => array( |
| 554 | 'reset' => 1, |
| 555 | 'action' => 'update', |
| 556 | 'cid' => $this->id, |
| 557 | ), |
| 558 | ); |
| 559 | return $result; |
| 560 | } |
| 561 | return NULL; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Get the values for pseudoconstants for name->value and reverse. |
| 566 | * |
| 567 | * @param array $defaults |
| 568 | * (reference) the default values, some of which need to be resolved. |
| 569 | * @param bool $reverse |
| 570 | * True if we want to resolve the values in the reverse direction (value -> name). |
| 571 | */ |
| 572 | public static function resolveDefaults(&$defaults, $reverse = FALSE) { |
| 573 | // Hack for birth_date. |
| 574 | if (!empty($defaults['birth_date'])) { |
| 575 | if (is_array($defaults['birth_date'])) { |
| 576 | $defaults['birth_date'] = CRM_Utils_Date::format($defaults['birth_date'], '-'); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | CRM_Utils_Array::lookupValue($defaults, 'prefix', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'), $reverse); |
| 581 | CRM_Utils_Array::lookupValue($defaults, 'suffix', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'), $reverse); |
| 582 | CRM_Utils_Array::lookupValue($defaults, 'gender', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), $reverse); |
| 583 | CRM_Utils_Array::lookupValue($defaults, 'communication_style', CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id'), $reverse); |
| 584 | |
| 585 | //lookup value of email/postal greeting, addressee, CRM-4575 |
| 586 | foreach (self::$_greetingTypes as $greeting) { |
| 587 | $filterCondition = array( |
| 588 | 'contact_type' => CRM_Utils_Array::value('contact_type', $defaults), |
| 589 | 'greeting_type' => $greeting, |
| 590 | ); |
| 591 | CRM_Utils_Array::lookupValue($defaults, $greeting, |
| 592 | CRM_Core_PseudoConstant::greeting($filterCondition), $reverse |
| 593 | ); |
| 594 | } |
| 595 | |
| 596 | $blocks = array('address', 'im', 'phone'); |
| 597 | foreach ($blocks as $name) { |
| 598 | if (!array_key_exists($name, $defaults) || !is_array($defaults[$name])) { |
| 599 | continue; |
| 600 | } |
| 601 | foreach ($defaults[$name] as $count => & $values) { |
| 602 | |
| 603 | //get location type id. |
| 604 | CRM_Utils_Array::lookupValue($values, 'location_type', CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), $reverse); |
| 605 | |
| 606 | if ($name == 'address') { |
| 607 | // FIXME: lookupValue doesn't work for vcard_name |
| 608 | if (!empty($values['location_type_id'])) { |
| 609 | $vcardNames = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array('labelColumn' => 'vcard_name')); |
| 610 | $values['vcard_name'] = $vcardNames[$values['location_type_id']]; |
| 611 | } |
| 612 | |
| 613 | if (!CRM_Utils_Array::lookupValue($values, |
| 614 | 'country', |
| 615 | CRM_Core_PseudoConstant::country(), |
| 616 | $reverse |
| 617 | ) && |
| 618 | $reverse |
| 619 | ) { |
| 620 | CRM_Utils_Array::lookupValue($values, |
| 621 | 'country', |
| 622 | CRM_Core_PseudoConstant::countryIsoCode(), |
| 623 | $reverse |
| 624 | ); |
| 625 | } |
| 626 | |
| 627 | // CRM-7597 |
| 628 | // if we find a country id above, we need to restrict it to that country |
| 629 | // rather than the list of all countries |
| 630 | |
| 631 | if (!empty($values['country_id'])) { |
| 632 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id']); |
| 633 | } |
| 634 | else { |
| 635 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvince(); |
| 636 | } |
| 637 | if (!CRM_Utils_Array::lookupValue($values, |
| 638 | 'state_province', |
| 639 | $stateProvinceList, |
| 640 | $reverse |
| 641 | ) && |
| 642 | $reverse |
| 643 | ) { |
| 644 | |
| 645 | if (!empty($values['country_id'])) { |
| 646 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id'], 'abbreviation'); |
| 647 | } |
| 648 | else { |
| 649 | $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceAbbreviation(); |
| 650 | } |
| 651 | CRM_Utils_Array::lookupValue($values, |
| 652 | 'state_province', |
| 653 | $stateProvinceList, |
| 654 | $reverse |
| 655 | ); |
| 656 | } |
| 657 | |
| 658 | if (!empty($values['state_province_id'])) { |
| 659 | $countyList = CRM_Core_PseudoConstant::countyForState($values['state_province_id']); |
| 660 | } |
| 661 | else { |
| 662 | $countyList = CRM_Core_PseudoConstant::county(); |
| 663 | } |
| 664 | CRM_Utils_Array::lookupValue($values, |
| 665 | 'county', |
| 666 | $countyList, |
| 667 | $reverse |
| 668 | ); |
| 669 | } |
| 670 | |
| 671 | if ($name == 'im') { |
| 672 | CRM_Utils_Array::lookupValue($values, |
| 673 | 'provider', |
| 674 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), |
| 675 | $reverse |
| 676 | ); |
| 677 | } |
| 678 | |
| 679 | if ($name == 'phone') { |
| 680 | CRM_Utils_Array::lookupValue($values, |
| 681 | 'phone_type', |
| 682 | CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
| 683 | $reverse |
| 684 | ); |
| 685 | } |
| 686 | |
| 687 | // Kill the reference. |
| 688 | unset($values); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Fetch object based on array of properties. |
| 695 | * |
| 696 | * @param array $params |
| 697 | * (reference ) an assoc array of name/value pairs. |
| 698 | * @param array $defaults |
| 699 | * (reference ) an assoc array to hold the name / value pairs. |
| 700 | * in a hierarchical manner |
| 701 | * @param bool $microformat |
| 702 | * For location in microformat. |
| 703 | * |
| 704 | * @return CRM_Contact_BAO_Contact |
| 705 | */ |
| 706 | public static function &retrieve(&$params, &$defaults, $microformat = FALSE) { |
| 707 | if (array_key_exists('contact_id', $params)) { |
| 708 | $params['id'] = $params['contact_id']; |
| 709 | } |
| 710 | elseif (array_key_exists('id', $params)) { |
| 711 | $params['contact_id'] = $params['id']; |
| 712 | } |
| 713 | |
| 714 | $contact = self::getValues($params, $defaults); |
| 715 | |
| 716 | unset($params['id']); |
| 717 | |
| 718 | //get the block information for this contact |
| 719 | $entityBlock = array('contact_id' => $params['contact_id']); |
| 720 | $blocks = CRM_Core_BAO_Location::getValues($entityBlock, $microformat); |
| 721 | $defaults = array_merge($defaults, $blocks); |
| 722 | foreach ($blocks as $block => $value) { |
| 723 | $contact->$block = $value; |
| 724 | } |
| 725 | |
| 726 | if (!isset($params['noNotes'])) { |
| 727 | $contact->notes = CRM_Core_BAO_Note::getValues($params, $defaults); |
| 728 | } |
| 729 | |
| 730 | if (!isset($params['noRelationships'])) { |
| 731 | $contact->relationship = CRM_Contact_BAO_Relationship::getValues($params, $defaults); |
| 732 | } |
| 733 | |
| 734 | if (!isset($params['noGroups'])) { |
| 735 | $contact->groupContact = CRM_Contact_BAO_GroupContact::getValues($params, $defaults); |
| 736 | } |
| 737 | |
| 738 | if (!isset($params['noWebsite'])) { |
| 739 | $contact->website = CRM_Core_BAO_Website::getValues($params, $defaults); |
| 740 | } |
| 741 | |
| 742 | return $contact; |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Get the display name of a contact. |
| 747 | * |
| 748 | * @param int $id |
| 749 | * Id of the contact. |
| 750 | * |
| 751 | * @return null|string |
| 752 | * display name of the contact if found |
| 753 | */ |
| 754 | public static function displayName($id) { |
| 755 | $displayName = NULL; |
| 756 | if ($id) { |
| 757 | $displayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'display_name'); |
| 758 | } |
| 759 | |
| 760 | return $displayName; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Delete a contact and all its associated records. |
| 765 | * |
| 766 | * @param int $id |
| 767 | * Id of the contact to delete. |
| 768 | * @param bool $restore |
| 769 | * Whether to actually restore, not delete. |
| 770 | * @param bool $skipUndelete |
| 771 | * Whether to force contact delete or not. |
| 772 | * |
| 773 | * @return bool |
| 774 | * Was contact deleted? |
| 775 | */ |
| 776 | public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALSE) { |
| 777 | |
| 778 | if (!$id) { |
| 779 | return FALSE; |
| 780 | } |
| 781 | // If trash is disabled in system settings then we always skip |
| 782 | if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL, 1)) { |
| 783 | $skipUndelete = TRUE; |
| 784 | } |
| 785 | |
| 786 | // make sure we have edit permission for this contact |
| 787 | // before we delete |
| 788 | if (($skipUndelete && !CRM_Core_Permission::check('delete contacts')) || |
| 789 | ($restore && !CRM_Core_Permission::check('access deleted contacts')) |
| 790 | ) { |
| 791 | return FALSE; |
| 792 | } |
| 793 | |
| 794 | // CRM-12929 |
| 795 | // Restrict contact to be delete if contact has financial trxns |
| 796 | $error = NULL; |
| 797 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($id), $error)) { |
| 798 | return FALSE; |
| 799 | } |
| 800 | |
| 801 | // make sure this contact_id does not have any membership types |
| 802 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', |
| 803 | $id, |
| 804 | 'id', |
| 805 | 'member_of_contact_id' |
| 806 | ); |
| 807 | if ($membershipTypeID) { |
| 808 | return FALSE; |
| 809 | } |
| 810 | |
| 811 | $contact = new CRM_Contact_DAO_Contact(); |
| 812 | $contact->id = $id; |
| 813 | if (!$contact->find(TRUE)) { |
| 814 | return FALSE; |
| 815 | } |
| 816 | |
| 817 | $contactType = $contact->contact_type; |
| 818 | // currently we only clear employer cache. |
| 819 | // we are now deleting inherited membership if any. |
| 820 | if ($contact->contact_type == 'Organization') { |
| 821 | $action = $restore ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE; |
| 822 | $relationshipDtls = CRM_Contact_BAO_Relationship::getRelationship($id); |
| 823 | if (!empty($relationshipDtls)) { |
| 824 | foreach ($relationshipDtls as $rId => $details) { |
| 825 | CRM_Contact_BAO_Relationship::disableEnableRelationship($rId, $action); |
| 826 | } |
| 827 | } |
| 828 | CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id); |
| 829 | } |
| 830 | |
| 831 | if ($restore) { |
| 832 | return self::contactTrashRestore($contact, TRUE); |
| 833 | } |
| 834 | |
| 835 | // start a new transaction |
| 836 | $transaction = new CRM_Core_Transaction(); |
| 837 | |
| 838 | if ($skipUndelete) { |
| 839 | CRM_Utils_Hook::pre('delete', $contactType, $id, CRM_Core_DAO::$_nullArray); |
| 840 | |
| 841 | //delete billing address if exists. |
| 842 | CRM_Contribute_BAO_Contribution::deleteAddress(NULL, $id); |
| 843 | |
| 844 | // delete the log entries since we dont have triggers enabled as yet |
| 845 | $logDAO = new CRM_Core_DAO_Log(); |
| 846 | $logDAO->entity_table = 'civicrm_contact'; |
| 847 | $logDAO->entity_id = $id; |
| 848 | $logDAO->delete(); |
| 849 | |
| 850 | // delete contact participants CRM-12155 |
| 851 | CRM_Event_BAO_Participant::deleteContactParticipant($id); |
| 852 | |
| 853 | // delete contact contributions CRM-12155 |
| 854 | CRM_Contribute_BAO_Contribution::deleteContactContribution($id); |
| 855 | |
| 856 | // do activity cleanup, CRM-5604 |
| 857 | CRM_Activity_BAO_Activity::cleanupActivity($id); |
| 858 | |
| 859 | // delete all notes related to contact |
| 860 | CRM_Core_BAO_Note::cleanContactNotes($id); |
| 861 | |
| 862 | // delete cases related to contact |
| 863 | $contactCases = CRM_Case_BAO_Case::retrieveCaseIdsByContactId($id); |
| 864 | if (!empty($contactCases)) { |
| 865 | foreach ($contactCases as $caseId) { |
| 866 | //check if case is associate with other contact or not. |
| 867 | $caseContactId = CRM_Case_BAO_Case::getCaseClients($caseId); |
| 868 | if (count($caseContactId) <= 1) { |
| 869 | CRM_Case_BAO_Case::deleteCase($caseId); |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | $contact->delete(); |
| 875 | } |
| 876 | else { |
| 877 | self::contactTrashRestore($contact); |
| 878 | } |
| 879 | |
| 880 | //delete the contact id from recently view |
| 881 | CRM_Utils_Recent::delContact($id); |
| 882 | |
| 883 | // Update the group contact cache |
| 884 | if ($restore) { |
| 885 | CRM_Contact_BAO_GroupContactCache::remove(); |
| 886 | } |
| 887 | else { |
| 888 | CRM_Contact_BAO_GroupContactCache::removeContact($id); |
| 889 | } |
| 890 | |
| 891 | // delete any dupe cache entry |
| 892 | CRM_Core_BAO_PrevNextCache::deleteItem($id); |
| 893 | |
| 894 | $transaction->commit(); |
| 895 | |
| 896 | if ($skipUndelete) { |
| 897 | CRM_Utils_Hook::post('delete', $contactType, $contact->id, $contact); |
| 898 | } |
| 899 | |
| 900 | // also reset the DB_DO global array so we can reuse the memory |
| 901 | // http://issues.civicrm.org/jira/browse/CRM-4387 |
| 902 | CRM_Core_DAO::freeResult(); |
| 903 | |
| 904 | return TRUE; |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Delete the image of a contact. |
| 909 | * |
| 910 | * @param int $id |
| 911 | * Id of the contact. |
| 912 | * |
| 913 | * @return bool |
| 914 | * Was contact image deleted? |
| 915 | */ |
| 916 | public static function deleteContactImage($id) { |
| 917 | if (!$id) { |
| 918 | return FALSE; |
| 919 | } |
| 920 | $query = " |
| 921 | UPDATE civicrm_contact |
| 922 | SET image_URL=NULL |
| 923 | WHERE id={$id}; "; |
| 924 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); |
| 925 | return TRUE; |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * Return relative path. |
| 930 | * |
| 931 | * @todo make this a method of $config->userSystem (i.e. UF classes) rather than a static function |
| 932 | * |
| 933 | * @param string $absolutePath |
| 934 | * Absolute path. |
| 935 | * |
| 936 | * @return string |
| 937 | * Relative url of uploaded image |
| 938 | */ |
| 939 | public static function getRelativePath($absolutePath) { |
| 940 | $relativePath = NULL; |
| 941 | $config = CRM_Core_Config::singleton(); |
| 942 | if ($config->userFramework == 'Joomla') { |
| 943 | $userFrameworkBaseURL = trim(str_replace('/administrator/', '', $config->userFrameworkBaseURL)); |
| 944 | $customFileUploadDirectory = strstr(str_replace('\\', '/', $absolutePath), '/media'); |
| 945 | $relativePath = $userFrameworkBaseURL . $customFileUploadDirectory; |
| 946 | } |
| 947 | elseif ($config->userSystem->is_drupal == '1') { |
| 948 | //ideally we would do a bigger re-factoring & move the getRelativePath onto the UF class |
| 949 | $rootPath = $config->userSystem->cmsRootPath(); |
| 950 | $baseUrl = $config->userFrameworkBaseURL; |
| 951 | |
| 952 | //format url for language negotiation, CRM-7135 |
| 953 | $baseUrl = CRM_Utils_System::languageNegotiationURL($baseUrl, FALSE, TRUE); |
| 954 | |
| 955 | $relativePath = str_replace("{$rootPath}/", |
| 956 | $baseUrl, |
| 957 | str_replace('\\', '/', $absolutePath) |
| 958 | ); |
| 959 | } |
| 960 | elseif ($config->userFramework == 'WordPress') { |
| 961 | $userFrameworkBaseURL = trim(str_replace('/wp-admin/', '', $config->userFrameworkBaseURL)); |
| 962 | $customFileUploadDirectory = strstr(str_replace('\\', '/', $absolutePath), '/wp-content/'); |
| 963 | $relativePath = $userFrameworkBaseURL . $customFileUploadDirectory; |
| 964 | } |
| 965 | |
| 966 | return $relativePath; |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Return proportional height and width of the image. |
| 971 | * |
| 972 | * @param int $imageWidth |
| 973 | * Width of image. |
| 974 | * |
| 975 | * @param int $imageHeight |
| 976 | * Height of image. |
| 977 | * |
| 978 | * @return array |
| 979 | * Thumb dimension of image |
| 980 | */ |
| 981 | public static function getThumbSize($imageWidth, $imageHeight) { |
| 982 | $thumbWidth = 100; |
| 983 | if ($imageWidth && $imageHeight) { |
| 984 | $imageRatio = $imageWidth / $imageHeight; |
| 985 | } |
| 986 | else { |
| 987 | $imageRatio = 1; |
| 988 | } |
| 989 | if ($imageRatio > 1) { |
| 990 | $imageThumbWidth = $thumbWidth; |
| 991 | $imageThumbHeight = round($thumbWidth / $imageRatio); |
| 992 | } |
| 993 | else { |
| 994 | $imageThumbHeight = $thumbWidth; |
| 995 | $imageThumbWidth = round($thumbWidth * $imageRatio); |
| 996 | } |
| 997 | |
| 998 | return array($imageThumbWidth, $imageThumbHeight); |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Validate type of contact image. |
| 1003 | * |
| 1004 | * @param array $params |
| 1005 | * @param string $imageIndex |
| 1006 | * Index of image field. |
| 1007 | * @param string $statusMsg |
| 1008 | * Status message to be set after operation. |
| 1009 | * @param string $opType |
| 1010 | * Type of operation like fatal, bounce etc. |
| 1011 | * |
| 1012 | * @return bool |
| 1013 | * true if valid image extension |
| 1014 | */ |
| 1015 | public static function processImageParams( |
| 1016 | &$params, |
| 1017 | $imageIndex = 'image_URL', |
| 1018 | $statusMsg = NULL, |
| 1019 | $opType = 'status' |
| 1020 | ) { |
| 1021 | $mimeType = array( |
| 1022 | 'image/jpeg', |
| 1023 | 'image/jpg', |
| 1024 | 'image/png', |
| 1025 | 'image/bmp', |
| 1026 | 'image/p-jpeg', |
| 1027 | 'image/gif', |
| 1028 | 'image/x-png', |
| 1029 | ); |
| 1030 | |
| 1031 | if (in_array($params[$imageIndex]['type'], $mimeType)) { |
| 1032 | $photo = basename($params[$imageIndex]['name']); |
| 1033 | $params[$imageIndex] = CRM_Utils_System::url('civicrm/contact/imagefile', 'photo=' . $photo, TRUE, NULL, TRUE, TRUE); |
| 1034 | return TRUE; |
| 1035 | } |
| 1036 | else { |
| 1037 | unset($params[$imageIndex]); |
| 1038 | if (!$statusMsg) { |
| 1039 | $statusMsg = ts('Image could not be uploaded due to invalid type extension.'); |
| 1040 | } |
| 1041 | if ($opType == 'status') { |
| 1042 | CRM_Core_Session::setStatus($statusMsg, 'Sorry', 'error'); |
| 1043 | } |
| 1044 | // FIXME: additional support for fatal, bounce etc could be added. |
| 1045 | return FALSE; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | /** |
| 1050 | * Extract contact id from url for deleting contact image. |
| 1051 | */ |
| 1052 | public static function processImage() { |
| 1053 | |
| 1054 | $action = CRM_Utils_Request::retrieve('action', 'String'); |
| 1055 | $cid = CRM_Utils_Request::retrieve('cid', 'Positive'); |
| 1056 | // retrieve contact id in case of Profile context |
| 1057 | $id = CRM_Utils_Request::retrieve('id', 'Positive'); |
| 1058 | $cid = $cid ? $cid : $id; |
| 1059 | if ($action & CRM_Core_Action::DELETE) { |
| 1060 | if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) { |
| 1061 | CRM_Contact_BAO_Contact::deleteContactImage($cid); |
| 1062 | CRM_Core_Session::setStatus(ts('Contact image deleted successfully'), ts('Image Deleted'), 'success'); |
| 1063 | $session = CRM_Core_Session::singleton(); |
| 1064 | $toUrl = $session->popUserContext(); |
| 1065 | CRM_Utils_System::redirect($toUrl); |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * Function to set is_delete true or restore deleted contact. |
| 1072 | * |
| 1073 | * @param CRM_Contact_DAO_Contact $contact |
| 1074 | * Contact DAO object. |
| 1075 | * @param bool $restore |
| 1076 | * True to set the is_delete = 1 else false to restore deleted contact, |
| 1077 | * i.e. is_delete = 0 |
| 1078 | * |
| 1079 | * @return bool |
| 1080 | */ |
| 1081 | public static function contactTrashRestore($contact, $restore = FALSE) { |
| 1082 | $updateParams = array( |
| 1083 | 'id' => $contact->id, |
| 1084 | 'is_deleted' => $restore ? 0 : 1, |
| 1085 | ); |
| 1086 | |
| 1087 | CRM_Utils_Hook::pre('update', $contact->contact_type, $contact->id, $updateParams); |
| 1088 | |
| 1089 | $params = array(1 => array($contact->id, 'Integer')); |
| 1090 | if (!$restore) { |
| 1091 | $query = "DELETE FROM civicrm_uf_match WHERE contact_id = %1"; |
| 1092 | CRM_Core_DAO::executeQuery($query, $params); |
| 1093 | } |
| 1094 | |
| 1095 | $contact->copyValues($updateParams); |
| 1096 | $contact->save(); |
| 1097 | |
| 1098 | CRM_Utils_Hook::post('update', $contact->contact_type, $contact->id, $contact); |
| 1099 | |
| 1100 | return TRUE; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Get contact type for a contact. |
| 1105 | * |
| 1106 | * @param int $id |
| 1107 | * Id of the contact whose contact type is needed. |
| 1108 | * |
| 1109 | * @return string |
| 1110 | * contact_type if $id found else null "" |
| 1111 | */ |
| 1112 | public static function getContactType($id) { |
| 1113 | return CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'contact_type'); |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * Get contact sub type for a contact. |
| 1118 | * |
| 1119 | * @param int $id |
| 1120 | * Id of the contact whose contact sub type is needed. |
| 1121 | * |
| 1122 | * @param string $implodeDelimiter |
| 1123 | * |
| 1124 | * @return string |
| 1125 | * contact_sub_type if $id found else null "" |
| 1126 | */ |
| 1127 | public static function getContactSubType($id, $implodeDelimiter = NULL) { |
| 1128 | $subtype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $id, 'contact_sub_type'); |
| 1129 | if (!$subtype) { |
| 1130 | return $implodeDelimiter ? NULL : array(); |
| 1131 | } |
| 1132 | |
| 1133 | $subtype = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($subtype, CRM_Core_DAO::VALUE_SEPARATOR)); |
| 1134 | |
| 1135 | if ($implodeDelimiter) { |
| 1136 | $subtype = implode($implodeDelimiter, $subtype); |
| 1137 | } |
| 1138 | return $subtype; |
| 1139 | } |
| 1140 | |
| 1141 | /** |
| 1142 | * Get pair of contact-type and sub-type for a contact. |
| 1143 | * |
| 1144 | * @param int $id |
| 1145 | * Id of the contact whose contact sub/contact type is needed. |
| 1146 | * |
| 1147 | * @return array |
| 1148 | */ |
| 1149 | public static function getContactTypes($id) { |
| 1150 | $params = array('id' => $id); |
| 1151 | $details = array(); |
| 1152 | $contact = CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Contact', |
| 1153 | $params, |
| 1154 | $details, |
| 1155 | array('contact_type', 'contact_sub_type') |
| 1156 | ); |
| 1157 | |
| 1158 | if ($contact) { |
| 1159 | $contactTypes = array(); |
| 1160 | if ($contact->contact_sub_type) { |
| 1161 | $contactTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($contact->contact_sub_type, CRM_Core_DAO::VALUE_SEPARATOR)); |
| 1162 | } |
| 1163 | array_unshift($contactTypes, $contact->contact_type); |
| 1164 | |
| 1165 | return $contactTypes; |
| 1166 | } |
| 1167 | else { |
| 1168 | CRM_Core_Error::fatal(); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * Combine all the importable fields from the lower levels object. |
| 1174 | * |
| 1175 | * The ordering is important, since currently we do not have a weight |
| 1176 | * scheme. Adding weight is super important |
| 1177 | * |
| 1178 | * @param int|string $contactType contact Type |
| 1179 | * @param bool $status |
| 1180 | * Status is used to manipulate first title. |
| 1181 | * @param bool $showAll |
| 1182 | * If true returns all fields (includes disabled fields). |
| 1183 | * @param bool $isProfile |
| 1184 | * If its profile mode. |
| 1185 | * @param bool $checkPermission |
| 1186 | * If false, do not include permissioning clause (for custom data). |
| 1187 | * |
| 1188 | * @param bool $withMultiCustomFields |
| 1189 | * |
| 1190 | * @return array |
| 1191 | * array of importable Fields |
| 1192 | */ |
| 1193 | public static function importableFields( |
| 1194 | $contactType = 'Individual', |
| 1195 | $status = FALSE, |
| 1196 | $showAll = FALSE, |
| 1197 | $isProfile = FALSE, |
| 1198 | $checkPermission = TRUE, |
| 1199 | $withMultiCustomFields = FALSE |
| 1200 | ) { |
| 1201 | if (empty($contactType)) { |
| 1202 | $contactType = 'All'; |
| 1203 | } |
| 1204 | |
| 1205 | $cacheKeyString = "importableFields $contactType"; |
| 1206 | $cacheKeyString .= $status ? '_1' : '_0'; |
| 1207 | $cacheKeyString .= $showAll ? '_1' : '_0'; |
| 1208 | $cacheKeyString .= $isProfile ? '_1' : '_0'; |
| 1209 | $cacheKeyString .= $checkPermission ? '_1' : '_0'; |
| 1210 | |
| 1211 | $fields = CRM_Utils_Array::value($cacheKeyString, self::$_importableFields); |
| 1212 | |
| 1213 | if (!$fields) { |
| 1214 | // check if we can retrieve from database cache |
| 1215 | $fields = CRM_Core_BAO_Cache::getItem('contact fields', $cacheKeyString); |
| 1216 | } |
| 1217 | |
| 1218 | if (!$fields) { |
| 1219 | $fields = CRM_Contact_DAO_Contact::import(); |
| 1220 | |
| 1221 | // get the fields thar are meant for contact types |
| 1222 | if (in_array($contactType, array( |
| 1223 | 'Individual', |
| 1224 | 'Household', |
| 1225 | 'Organization', |
| 1226 | 'All', |
| 1227 | ))) { |
| 1228 | $fields = array_merge($fields, CRM_Core_OptionValue::getFields('', $contactType)); |
| 1229 | } |
| 1230 | |
| 1231 | $locationFields = array_merge(CRM_Core_DAO_Address::import(), |
| 1232 | CRM_Core_DAO_Phone::import(), |
| 1233 | CRM_Core_DAO_Email::import(), |
| 1234 | CRM_Core_DAO_IM::import(TRUE), |
| 1235 | CRM_Core_DAO_OpenID::import() |
| 1236 | ); |
| 1237 | |
| 1238 | $locationFields = array_merge($locationFields, |
| 1239 | CRM_Core_BAO_CustomField::getFieldsForImport('Address', |
| 1240 | FALSE, |
| 1241 | FALSE, |
| 1242 | FALSE, |
| 1243 | FALSE |
| 1244 | ) |
| 1245 | ); |
| 1246 | |
| 1247 | foreach ($locationFields as $key => $field) { |
| 1248 | $locationFields[$key]['hasLocationType'] = TRUE; |
| 1249 | } |
| 1250 | |
| 1251 | $fields = array_merge($fields, $locationFields); |
| 1252 | |
| 1253 | $fields = array_merge($fields, CRM_Contact_DAO_Contact::import()); |
| 1254 | $fields = array_merge($fields, CRM_Core_DAO_Note::import()); |
| 1255 | |
| 1256 | //website fields |
| 1257 | $fields = array_merge($fields, CRM_Core_DAO_Website::import()); |
| 1258 | $fields['url']['hasWebsiteType'] = TRUE; |
| 1259 | |
| 1260 | if ($contactType != 'All') { |
| 1261 | $fields = array_merge($fields, |
| 1262 | CRM_Core_BAO_CustomField::getFieldsForImport($contactType, |
| 1263 | $showAll, |
| 1264 | TRUE, |
| 1265 | FALSE, |
| 1266 | FALSE, |
| 1267 | $withMultiCustomFields |
| 1268 | ) |
| 1269 | ); |
| 1270 | //unset the fields, which are not related to their |
| 1271 | //contact type. |
| 1272 | $commonValues = array( |
| 1273 | 'Individual' => array( |
| 1274 | 'household_name', |
| 1275 | 'legal_name', |
| 1276 | 'sic_code', |
| 1277 | 'organization_name', |
| 1278 | ), |
| 1279 | 'Household' => array( |
| 1280 | 'first_name', |
| 1281 | 'middle_name', |
| 1282 | 'last_name', |
| 1283 | 'formal_title', |
| 1284 | 'job_title', |
| 1285 | 'gender_id', |
| 1286 | 'prefix_id', |
| 1287 | 'suffix_id', |
| 1288 | 'birth_date', |
| 1289 | 'organization_name', |
| 1290 | 'legal_name', |
| 1291 | 'legal_identifier', |
| 1292 | 'sic_code', |
| 1293 | 'home_URL', |
| 1294 | 'is_deceased', |
| 1295 | 'deceased_date', |
| 1296 | ), |
| 1297 | 'Organization' => array( |
| 1298 | 'first_name', |
| 1299 | 'middle_name', |
| 1300 | 'last_name', |
| 1301 | 'formal_title', |
| 1302 | 'job_title', |
| 1303 | 'gender_id', |
| 1304 | 'prefix_id', |
| 1305 | 'suffix_id', |
| 1306 | 'birth_date', |
| 1307 | 'household_name', |
| 1308 | 'is_deceased', |
| 1309 | 'deceased_date', |
| 1310 | ), |
| 1311 | ); |
| 1312 | foreach ($commonValues[$contactType] as $value) { |
| 1313 | unset($fields[$value]); |
| 1314 | } |
| 1315 | } |
| 1316 | else { |
| 1317 | foreach (array('Individual', 'Household', 'Organization') as $type) { |
| 1318 | $fields = array_merge($fields, |
| 1319 | CRM_Core_BAO_CustomField::getFieldsForImport($type, |
| 1320 | $showAll, |
| 1321 | FALSE, |
| 1322 | FALSE, |
| 1323 | FALSE, |
| 1324 | $withMultiCustomFields |
| 1325 | ) |
| 1326 | ); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | if ($isProfile) { |
| 1331 | $fields = array_merge($fields, array( |
| 1332 | 'group' => array( |
| 1333 | 'title' => ts('Group(s)'), |
| 1334 | 'name' => 'group', |
| 1335 | ), |
| 1336 | 'tag' => array( |
| 1337 | 'title' => ts('Tag(s)'), |
| 1338 | 'name' => 'tag', |
| 1339 | ), |
| 1340 | 'note' => array( |
| 1341 | 'title' => ts('Note(s)'), |
| 1342 | 'name' => 'note', |
| 1343 | ), |
| 1344 | 'communication_style_id' => array( |
| 1345 | 'title' => ts('Communication Style'), |
| 1346 | 'name' => 'communication_style_id', |
| 1347 | ), |
| 1348 | )); |
| 1349 | } |
| 1350 | |
| 1351 | //Sorting fields in alphabetical order(CRM-1507) |
| 1352 | $fields = CRM_Utils_Array::crmArraySortByField($fields, 'title'); |
| 1353 | |
| 1354 | CRM_Core_BAO_Cache::setItem($fields, 'contact fields', $cacheKeyString); |
| 1355 | } |
| 1356 | |
| 1357 | self::$_importableFields[$cacheKeyString] = $fields; |
| 1358 | |
| 1359 | if (!$isProfile) { |
| 1360 | if (!$status) { |
| 1361 | $fields = array_merge(array('do_not_import' => array('title' => ts('- do not import -'))), |
| 1362 | self::$_importableFields[$cacheKeyString] |
| 1363 | ); |
| 1364 | } |
| 1365 | else { |
| 1366 | $fields = array_merge(array('' => array('title' => ts('- Contact Fields -'))), |
| 1367 | self::$_importableFields[$cacheKeyString] |
| 1368 | ); |
| 1369 | } |
| 1370 | } |
| 1371 | return $fields; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * Combine all the exportable fields from the lower levels object. |
| 1376 | * |
| 1377 | * Currently we are using importable fields as exportable fields |
| 1378 | * |
| 1379 | * @param int|string $contactType contact Type |
| 1380 | * @param bool $status |
| 1381 | * True while exporting primary contacts. |
| 1382 | * @param bool $export |
| 1383 | * True when used during export. |
| 1384 | * @param bool $search |
| 1385 | * True when used during search, might conflict with export param?. |
| 1386 | * |
| 1387 | * @param bool $withMultiRecord |
| 1388 | * |
| 1389 | * @return array |
| 1390 | * array of exportable Fields |
| 1391 | */ |
| 1392 | public static function &exportableFields($contactType = 'Individual', $status = FALSE, $export = FALSE, $search = FALSE, $withMultiRecord = FALSE) { |
| 1393 | if (empty($contactType)) { |
| 1394 | $contactType = 'All'; |
| 1395 | } |
| 1396 | |
| 1397 | $cacheKeyString = "exportableFields $contactType"; |
| 1398 | $cacheKeyString .= $export ? '_1' : '_0'; |
| 1399 | $cacheKeyString .= $status ? '_1' : '_0'; |
| 1400 | $cacheKeyString .= $search ? '_1' : '_0'; |
| 1401 | //CRM-14501 it turns out that the impact of permissioning here is sometimes inconsistent. The field that |
| 1402 | //calculates custom fields takes into account the logged in user & caches that for all users |
| 1403 | //as an interim fix we will cache the fields by contact |
| 1404 | $cacheKeyString .= '_' . CRM_Core_Session::getLoggedInContactID(); |
| 1405 | |
| 1406 | if (!self::$_exportableFields || !CRM_Utils_Array::value($cacheKeyString, self::$_exportableFields)) { |
| 1407 | if (!self::$_exportableFields) { |
| 1408 | self::$_exportableFields = array(); |
| 1409 | } |
| 1410 | |
| 1411 | // check if we can retrieve from database cache |
| 1412 | $fields = CRM_Core_BAO_Cache::getItem('contact fields', $cacheKeyString); |
| 1413 | |
| 1414 | if (!$fields) { |
| 1415 | $fields = CRM_Contact_DAO_Contact::export(); |
| 1416 | |
| 1417 | // The fields are meant for contact types. |
| 1418 | if (in_array($contactType, array( |
| 1419 | 'Individual', |
| 1420 | 'Household', |
| 1421 | 'Organization', |
| 1422 | 'All', |
| 1423 | ) |
| 1424 | )) { |
| 1425 | $fields = array_merge($fields, CRM_Core_OptionValue::getFields('', $contactType)); |
| 1426 | } |
| 1427 | // add current employer for individuals |
| 1428 | $fields = array_merge($fields, array( |
| 1429 | 'current_employer' => |
| 1430 | array( |
| 1431 | 'name' => 'organization_name', |
| 1432 | 'title' => ts('Current Employer'), |
| 1433 | ), |
| 1434 | )); |
| 1435 | |
| 1436 | $locationType = array( |
| 1437 | 'location_type' => array( |
| 1438 | 'name' => 'location_type', |
| 1439 | 'where' => 'civicrm_location_type.name', |
| 1440 | 'title' => ts('Location Type'), |
| 1441 | ), |
| 1442 | ); |
| 1443 | |
| 1444 | $IMProvider = array( |
| 1445 | 'im_provider' => array( |
| 1446 | 'name' => 'im_provider', |
| 1447 | 'where' => 'civicrm_im.provider_id', |
| 1448 | 'title' => ts('IM Provider'), |
| 1449 | ), |
| 1450 | ); |
| 1451 | |
| 1452 | $locationFields = array_merge($locationType, |
| 1453 | CRM_Core_DAO_Address::export(), |
| 1454 | CRM_Core_DAO_Phone::export(), |
| 1455 | CRM_Core_DAO_Email::export(), |
| 1456 | $IMProvider, |
| 1457 | CRM_Core_DAO_IM::export(TRUE), |
| 1458 | CRM_Core_DAO_OpenID::export() |
| 1459 | ); |
| 1460 | |
| 1461 | $locationFields = array_merge($locationFields, |
| 1462 | CRM_Core_BAO_CustomField::getFieldsForImport('Address') |
| 1463 | ); |
| 1464 | |
| 1465 | foreach ($locationFields as $key => $field) { |
| 1466 | $locationFields[$key]['hasLocationType'] = TRUE; |
| 1467 | } |
| 1468 | |
| 1469 | $fields = array_merge($fields, $locationFields); |
| 1470 | |
| 1471 | //add world region |
| 1472 | $fields = array_merge($fields, |
| 1473 | CRM_Core_DAO_Worldregion::export() |
| 1474 | ); |
| 1475 | |
| 1476 | $fields = array_merge($fields, |
| 1477 | CRM_Contact_DAO_Contact::export() |
| 1478 | ); |
| 1479 | |
| 1480 | //website fields |
| 1481 | $fields = array_merge($fields, CRM_Core_DAO_Website::export()); |
| 1482 | |
| 1483 | if ($contactType != 'All') { |
| 1484 | $fields = array_merge($fields, |
| 1485 | CRM_Core_BAO_CustomField::getFieldsForImport($contactType, $status, FALSE, $search, TRUE, $withMultiRecord) |
| 1486 | ); |
| 1487 | } |
| 1488 | else { |
| 1489 | foreach (array( |
| 1490 | 'Individual', |
| 1491 | 'Household', |
| 1492 | 'Organization', |
| 1493 | ) as $type) { |
| 1494 | $fields = array_merge($fields, |
| 1495 | CRM_Core_BAO_CustomField::getFieldsForImport($type, FALSE, FALSE, $search, TRUE, $withMultiRecord) |
| 1496 | ); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | //fix for CRM-791 |
| 1501 | if ($export) { |
| 1502 | $fields = array_merge($fields, array( |
| 1503 | 'groups' => array( |
| 1504 | 'title' => ts('Group(s)'), |
| 1505 | 'name' => 'groups', |
| 1506 | ), |
| 1507 | 'tags' => array( |
| 1508 | 'title' => ts('Tag(s)'), |
| 1509 | 'name' => 'tags', |
| 1510 | ), |
| 1511 | 'notes' => array( |
| 1512 | 'title' => ts('Note(s)'), |
| 1513 | 'name' => 'notes', |
| 1514 | ), |
| 1515 | )); |
| 1516 | } |
| 1517 | else { |
| 1518 | $fields = array_merge($fields, array( |
| 1519 | 'group' => array( |
| 1520 | 'title' => ts('Group(s)'), |
| 1521 | 'name' => 'group', |
| 1522 | ), |
| 1523 | 'tag' => array( |
| 1524 | 'title' => ts('Tag(s)'), |
| 1525 | 'name' => 'tag', |
| 1526 | ), |
| 1527 | 'note' => array( |
| 1528 | 'title' => ts('Note(s)'), |
| 1529 | 'name' => 'note', |
| 1530 | ), |
| 1531 | )); |
| 1532 | } |
| 1533 | |
| 1534 | //Sorting fields in alphabetical order(CRM-1507) |
| 1535 | foreach ($fields as $k => $v) { |
| 1536 | $sortArray[$k] = CRM_Utils_Array::value('title', $v); |
| 1537 | } |
| 1538 | |
| 1539 | $fields = array_merge($sortArray, $fields); |
| 1540 | //unset the field which are not related to their contact type. |
| 1541 | if ($contactType != 'All') { |
| 1542 | $commonValues = array( |
| 1543 | 'Individual' => array( |
| 1544 | 'household_name', |
| 1545 | 'legal_name', |
| 1546 | 'sic_code', |
| 1547 | 'organization_name', |
| 1548 | 'email_greeting_custom', |
| 1549 | 'postal_greeting_custom', |
| 1550 | 'addressee_custom', |
| 1551 | ), |
| 1552 | 'Household' => array( |
| 1553 | 'first_name', |
| 1554 | 'middle_name', |
| 1555 | 'last_name', |
| 1556 | 'formal_title', |
| 1557 | 'job_title', |
| 1558 | 'gender_id', |
| 1559 | 'prefix_id', |
| 1560 | 'suffix_id', |
| 1561 | 'birth_date', |
| 1562 | 'organization_name', |
| 1563 | 'legal_name', |
| 1564 | 'legal_identifier', |
| 1565 | 'sic_code', |
| 1566 | 'home_URL', |
| 1567 | 'is_deceased', |
| 1568 | 'deceased_date', |
| 1569 | 'current_employer', |
| 1570 | 'email_greeting_custom', |
| 1571 | 'postal_greeting_custom', |
| 1572 | 'addressee_custom', |
| 1573 | 'prefix_id', |
| 1574 | 'suffix_id', |
| 1575 | ), |
| 1576 | 'Organization' => array( |
| 1577 | 'first_name', |
| 1578 | 'middle_name', |
| 1579 | 'last_name', |
| 1580 | 'formal_title', |
| 1581 | 'job_title', |
| 1582 | 'gender_id', |
| 1583 | 'prefix_id', |
| 1584 | 'suffix_id', |
| 1585 | 'birth_date', |
| 1586 | 'household_name', |
| 1587 | 'email_greeting_custom', |
| 1588 | 'postal_greeting_custom', |
| 1589 | 'prefix_id', |
| 1590 | 'suffix_id', |
| 1591 | 'gender_id', |
| 1592 | 'addressee_custom', |
| 1593 | 'is_deceased', |
| 1594 | 'deceased_date', |
| 1595 | 'current_employer', |
| 1596 | ), |
| 1597 | ); |
| 1598 | foreach ($commonValues[$contactType] as $value) { |
| 1599 | unset($fields[$value]); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | CRM_Core_BAO_Cache::setItem($fields, 'contact fields', $cacheKeyString); |
| 1604 | } |
| 1605 | self::$_exportableFields[$cacheKeyString] = $fields; |
| 1606 | } |
| 1607 | |
| 1608 | if (!$status) { |
| 1609 | $fields = self::$_exportableFields[$cacheKeyString]; |
| 1610 | } |
| 1611 | else { |
| 1612 | $fields = array_merge(array('' => array('title' => ts('- Contact Fields -'))), |
| 1613 | self::$_exportableFields[$cacheKeyString] |
| 1614 | ); |
| 1615 | } |
| 1616 | |
| 1617 | return $fields; |
| 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * Get the all contact details (Hierarchical). |
| 1622 | * |
| 1623 | * @param int $contactId |
| 1624 | * Contact id. |
| 1625 | * @param array $fields |
| 1626 | * Fields array. |
| 1627 | * |
| 1628 | * @return array |
| 1629 | * Contact details |
| 1630 | */ |
| 1631 | public static function getHierContactDetails($contactId, &$fields) { |
| 1632 | $params = array(array('contact_id', '=', $contactId, 0, 0)); |
| 1633 | $options = array(); |
| 1634 | |
| 1635 | $returnProperties = self::makeHierReturnProperties($fields, $contactId); |
| 1636 | |
| 1637 | // We don't know the contents of return properties, but we need the lower |
| 1638 | // level ids of the contact so add a few fields. |
| 1639 | $returnProperties['first_name'] = 1; |
| 1640 | $returnProperties['organization_name'] = 1; |
| 1641 | $returnProperties['household_name'] = 1; |
| 1642 | $returnProperties['contact_type'] = 1; |
| 1643 | $returnProperties['contact_sub_type'] = 1; |
| 1644 | return list($query, $options) = CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, $options); |
| 1645 | } |
| 1646 | |
| 1647 | /** |
| 1648 | * Given a set of flat profile style field names, create a hierarchy. |
| 1649 | * |
| 1650 | * This is for the query to use, create the right sql. |
| 1651 | * |
| 1652 | * @param $fields |
| 1653 | * @param int $contactId |
| 1654 | * Contact id. |
| 1655 | * |
| 1656 | * @return array |
| 1657 | * A hierarchical property tree if appropriate |
| 1658 | */ |
| 1659 | public static function &makeHierReturnProperties($fields, $contactId = NULL) { |
| 1660 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
| 1661 | |
| 1662 | $returnProperties = array(); |
| 1663 | |
| 1664 | $multipleFields = array('website' => 'url'); |
| 1665 | foreach ($fields as $name => $dontCare) { |
| 1666 | if (strpos($name, '-') !== FALSE) { |
| 1667 | list($fieldName, $id, $type) = CRM_Utils_System::explode('-', $name, 3); |
| 1668 | |
| 1669 | if (!in_array($fieldName, $multipleFields)) { |
| 1670 | if ($id == 'Primary') { |
| 1671 | $locationTypeName = 1; |
| 1672 | } |
| 1673 | else { |
| 1674 | $locationTypeName = CRM_Utils_Array::value($id, $locationTypes); |
| 1675 | if (!$locationTypeName) { |
| 1676 | continue; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | if (empty($returnProperties['location'])) { |
| 1681 | $returnProperties['location'] = array(); |
| 1682 | } |
| 1683 | if (empty($returnProperties['location'][$locationTypeName])) { |
| 1684 | $returnProperties['location'][$locationTypeName] = array(); |
| 1685 | $returnProperties['location'][$locationTypeName]['location_type'] = $id; |
| 1686 | } |
| 1687 | if (in_array($fieldName, array( |
| 1688 | 'phone', |
| 1689 | 'im', |
| 1690 | 'email', |
| 1691 | 'openid', |
| 1692 | 'phone_ext', |
| 1693 | ))) { |
| 1694 | if ($type) { |
| 1695 | $returnProperties['location'][$locationTypeName][$fieldName . '-' . $type] = 1; |
| 1696 | } |
| 1697 | else { |
| 1698 | $returnProperties['location'][$locationTypeName][$fieldName] = 1; |
| 1699 | } |
| 1700 | } |
| 1701 | elseif (substr($fieldName, 0, 14) === 'address_custom') { |
| 1702 | $returnProperties['location'][$locationTypeName][substr($fieldName, 8)] = 1; |
| 1703 | } |
| 1704 | else { |
| 1705 | $returnProperties['location'][$locationTypeName][$fieldName] = 1; |
| 1706 | } |
| 1707 | } |
| 1708 | else { |
| 1709 | $returnProperties['website'][$id][$fieldName] = 1; |
| 1710 | } |
| 1711 | } |
| 1712 | else { |
| 1713 | $returnProperties[$name] = 1; |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | return $returnProperties; |
| 1718 | } |
| 1719 | |
| 1720 | /** |
| 1721 | * Return the primary location type of a contact. |
| 1722 | * |
| 1723 | * $params int $contactId contact_id |
| 1724 | * $params boolean $isPrimaryExist if true, return primary contact location type otherwise null |
| 1725 | * $params boolean $skipDefaultPriamry if true, return primary contact location type otherwise null |
| 1726 | * |
| 1727 | * @param int $contactId |
| 1728 | * @param bool $skipDefaultPriamry |
| 1729 | * @param null $block |
| 1730 | * |
| 1731 | * @return int |
| 1732 | * $locationType location_type_id |
| 1733 | */ |
| 1734 | public static function getPrimaryLocationType($contactId, $skipDefaultPriamry = FALSE, $block = NULL) { |
| 1735 | if ($block) { |
| 1736 | $entityBlock = array('contact_id' => $contactId); |
| 1737 | $blocks = CRM_Core_BAO_Location::getValues($entityBlock); |
| 1738 | foreach ($blocks[$block] as $key => $value) { |
| 1739 | if (!empty($value['is_primary'])) { |
| 1740 | $locationType = CRM_Utils_Array::value('location_type_id', $value); |
| 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | else { |
| 1745 | $query = " |
| 1746 | SELECT |
| 1747 | IF ( civicrm_email.location_type_id IS NULL, |
| 1748 | IF ( civicrm_address.location_type_id IS NULL, |
| 1749 | IF ( civicrm_phone.location_type_id IS NULL, |
| 1750 | IF ( civicrm_im.location_type_id IS NULL, |
| 1751 | IF ( civicrm_openid.location_type_id IS NULL, null, civicrm_openid.location_type_id) |
| 1752 | ,civicrm_im.location_type_id) |
| 1753 | ,civicrm_phone.location_type_id) |
| 1754 | ,civicrm_address.location_type_id) |
| 1755 | ,civicrm_email.location_type_id) as locationType |
| 1756 | FROM civicrm_contact |
| 1757 | LEFT JOIN civicrm_email ON ( civicrm_email.is_primary = 1 AND civicrm_email.contact_id = civicrm_contact.id ) |
| 1758 | LEFT JOIN civicrm_address ON ( civicrm_address.is_primary = 1 AND civicrm_address.contact_id = civicrm_contact.id) |
| 1759 | LEFT JOIN civicrm_phone ON ( civicrm_phone.is_primary = 1 AND civicrm_phone.contact_id = civicrm_contact.id) |
| 1760 | LEFT JOIN civicrm_im ON ( civicrm_im.is_primary = 1 AND civicrm_im.contact_id = civicrm_contact.id) |
| 1761 | LEFT JOIN civicrm_openid ON ( civicrm_openid.is_primary = 1 AND civicrm_openid.contact_id = civicrm_contact.id) |
| 1762 | WHERE civicrm_contact.id = %1 "; |
| 1763 | |
| 1764 | $params = array(1 => array($contactId, 'Integer')); |
| 1765 | |
| 1766 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1767 | |
| 1768 | $locationType = NULL; |
| 1769 | if ($dao->fetch()) { |
| 1770 | $locationType = $dao->locationType; |
| 1771 | } |
| 1772 | } |
| 1773 | if (isset($locationType)) { |
| 1774 | return $locationType; |
| 1775 | } |
| 1776 | elseif ($skipDefaultPriamry) { |
| 1777 | // if there is no primary contact location then return null |
| 1778 | return NULL; |
| 1779 | } |
| 1780 | else { |
| 1781 | // if there is no primart contact location, then return default |
| 1782 | // location type of the system |
| 1783 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
| 1784 | return $defaultLocationType->id; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * Get the display name, primary email and location type of a contact. |
| 1790 | * |
| 1791 | * @param int $id |
| 1792 | * Id of the contact. |
| 1793 | * |
| 1794 | * @return array |
| 1795 | * Array of display_name, email if found, do_not_email or (null,null,null) |
| 1796 | */ |
| 1797 | public static function getContactDetails($id) { |
| 1798 | // check if the contact type |
| 1799 | $contactType = self::getContactType($id); |
| 1800 | |
| 1801 | $nameFields = ($contactType == 'Individual') ? "civicrm_contact.first_name, civicrm_contact.last_name, civicrm_contact.display_name" : "civicrm_contact.display_name"; |
| 1802 | |
| 1803 | $sql = " |
| 1804 | SELECT $nameFields, civicrm_email.email, civicrm_contact.do_not_email, civicrm_email.on_hold, civicrm_contact.is_deceased |
| 1805 | FROM civicrm_contact LEFT JOIN civicrm_email ON (civicrm_contact.id = civicrm_email.contact_id) |
| 1806 | WHERE civicrm_contact.id = %1 |
| 1807 | ORDER BY civicrm_email.is_primary DESC"; |
| 1808 | $params = array(1 => array($id, 'Integer')); |
| 1809 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
| 1810 | |
| 1811 | if ($dao->fetch()) { |
| 1812 | if ($contactType == 'Individual') { |
| 1813 | if ($dao->first_name || $dao->last_name) { |
| 1814 | $name = "{$dao->first_name} {$dao->last_name}"; |
| 1815 | } |
| 1816 | else { |
| 1817 | $name = $dao->display_name; |
| 1818 | } |
| 1819 | } |
| 1820 | else { |
| 1821 | $name = $dao->display_name; |
| 1822 | } |
| 1823 | $email = $dao->email; |
| 1824 | $doNotEmail = $dao->do_not_email ? TRUE : FALSE; |
| 1825 | $onHold = $dao->on_hold ? TRUE : FALSE; |
| 1826 | $isDeceased = $dao->is_deceased ? TRUE : FALSE; |
| 1827 | return array($name, $email, $doNotEmail, $onHold, $isDeceased); |
| 1828 | } |
| 1829 | return array(NULL, NULL, NULL, NULL, NULL); |
| 1830 | } |
| 1831 | |
| 1832 | /** |
| 1833 | * Add/edit/register contacts through profile. |
| 1834 | * |
| 1835 | * @param array $params |
| 1836 | * Array of profile fields to be edited/added. |
| 1837 | * @param array $fields |
| 1838 | * Array of fields from UFGroup. |
| 1839 | * @param int $contactID |
| 1840 | * Id of the contact to be edited/added. |
| 1841 | * @param int $addToGroupID |
| 1842 | * Specifies the default group to which contact is added. |
| 1843 | * @param int $ufGroupId |
| 1844 | * Uf group id (profile id). |
| 1845 | * @param string $ctype |
| 1846 | * @param bool $visibility |
| 1847 | * Basically lets us know where this request is coming from. |
| 1848 | * if via a profile from web, we restrict what groups are changed |
| 1849 | * |
| 1850 | * @return int |
| 1851 | * contact id created/edited |
| 1852 | */ |
| 1853 | public static function createProfileContact( |
| 1854 | &$params, |
| 1855 | &$fields, |
| 1856 | $contactID = NULL, |
| 1857 | $addToGroupID = NULL, |
| 1858 | $ufGroupId = NULL, |
| 1859 | $ctype = NULL, |
| 1860 | $visibility = FALSE |
| 1861 | ) { |
| 1862 | // add ufGroupID to params array ( CRM-2012 ) |
| 1863 | if ($ufGroupId) { |
| 1864 | $params['uf_group_id'] = $ufGroupId; |
| 1865 | } |
| 1866 | |
| 1867 | if ($contactID) { |
| 1868 | $editHook = TRUE; |
| 1869 | CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params); |
| 1870 | } |
| 1871 | else { |
| 1872 | $editHook = FALSE; |
| 1873 | CRM_Utils_Hook::pre('create', 'Profile', NULL, $params); |
| 1874 | } |
| 1875 | |
| 1876 | list($data, $contactDetails) = self::formatProfileContactParams($params, $fields, $contactID, $ufGroupId, $ctype); |
| 1877 | |
| 1878 | // manage is_opt_out |
| 1879 | if (array_key_exists('is_opt_out', $fields) && array_key_exists('is_opt_out', $params)) { |
| 1880 | $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, FALSE); |
| 1881 | $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, FALSE); |
| 1882 | $data['is_opt_out'] = $isOptOut; |
| 1883 | // on change, create new civicrm_subscription_history entry |
| 1884 | if (($wasOptOut != $isOptOut) && !empty($contactDetails['contact_id'])) { |
| 1885 | $shParams = array( |
| 1886 | 'contact_id' => $contactDetails['contact_id'], |
| 1887 | 'status' => $isOptOut ? 'Removed' : 'Added', |
| 1888 | 'method' => 'Web', |
| 1889 | ); |
| 1890 | CRM_Contact_BAO_SubscriptionHistory::create($shParams); |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | $contact = self::create($data); |
| 1895 | |
| 1896 | // contact is null if the profile does not have any contact fields |
| 1897 | if ($contact) { |
| 1898 | $contactID = $contact->id; |
| 1899 | } |
| 1900 | |
| 1901 | if (empty($contactID)) { |
| 1902 | CRM_Core_Error::fatal('Cannot proceed without a valid contact id'); |
| 1903 | } |
| 1904 | |
| 1905 | // Process group and tag |
| 1906 | if (!empty($fields['group'])) { |
| 1907 | $method = 'Admin'; |
| 1908 | // this for sure means we are coming in via profile since i added it to fix |
| 1909 | // removing contacts from user groups -- lobo |
| 1910 | if ($visibility) { |
| 1911 | $method = 'Web'; |
| 1912 | } |
| 1913 | CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method); |
| 1914 | } |
| 1915 | |
| 1916 | if (!empty($fields['tag'])) { |
| 1917 | CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $contactID); |
| 1918 | } |
| 1919 | |
| 1920 | //to add profile in default group |
| 1921 | if (is_array($addToGroupID)) { |
| 1922 | $contactIds = array($contactID); |
| 1923 | foreach ($addToGroupID as $groupId) { |
| 1924 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId); |
| 1925 | } |
| 1926 | } |
| 1927 | elseif ($addToGroupID) { |
| 1928 | $contactIds = array($contactID); |
| 1929 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID); |
| 1930 | } |
| 1931 | |
| 1932 | // reset the group contact cache for this group |
| 1933 | CRM_Contact_BAO_GroupContactCache::remove(); |
| 1934 | |
| 1935 | if ($editHook) { |
| 1936 | CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params); |
| 1937 | } |
| 1938 | else { |
| 1939 | CRM_Utils_Hook::post('create', 'Profile', $contactID, $params); |
| 1940 | } |
| 1941 | return $contactID; |
| 1942 | } |
| 1943 | |
| 1944 | /** |
| 1945 | * Format profile contact parameters. |
| 1946 | * |
| 1947 | * @param array $params |
| 1948 | * @param $fields |
| 1949 | * @param int $contactID |
| 1950 | * @param int $ufGroupId |
| 1951 | * @param null $ctype |
| 1952 | * @param bool $skipCustom |
| 1953 | * |
| 1954 | * @return array |
| 1955 | */ |
| 1956 | public static function formatProfileContactParams( |
| 1957 | &$params, |
| 1958 | &$fields, |
| 1959 | $contactID = NULL, |
| 1960 | $ufGroupId = NULL, |
| 1961 | $ctype = NULL, |
| 1962 | $skipCustom = FALSE |
| 1963 | ) { |
| 1964 | |
| 1965 | $data = $contactDetails = array(); |
| 1966 | |
| 1967 | // get the contact details (hier) |
| 1968 | if ($contactID) { |
| 1969 | list($details, $options) = self::getHierContactDetails($contactID, $fields); |
| 1970 | |
| 1971 | $contactDetails = $details[$contactID]; |
| 1972 | $data['contact_type'] = CRM_Utils_Array::value('contact_type', $contactDetails); |
| 1973 | $data['contact_sub_type'] = CRM_Utils_Array::value('contact_sub_type', $contactDetails); |
| 1974 | } |
| 1975 | else { |
| 1976 | //we should get contact type only if contact |
| 1977 | if ($ufGroupId) { |
| 1978 | $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId); |
| 1979 | |
| 1980 | //special case to handle profile with only contact fields |
| 1981 | if ($data['contact_type'] == 'Contact') { |
| 1982 | $data['contact_type'] = 'Individual'; |
| 1983 | } |
| 1984 | elseif (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) { |
| 1985 | $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']); |
| 1986 | } |
| 1987 | } |
| 1988 | elseif ($ctype) { |
| 1989 | $data['contact_type'] = $ctype; |
| 1990 | } |
| 1991 | else { |
| 1992 | $data['contact_type'] = 'Individual'; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | //fix contact sub type CRM-5125 |
| 1997 | if (array_key_exists('contact_sub_type', $params) && |
| 1998 | !empty($params['contact_sub_type']) |
| 1999 | ) { |
| 2000 | $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $params['contact_sub_type']) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 2001 | } |
| 2002 | elseif (array_key_exists('contact_sub_type_hidden', $params) && |
| 2003 | !empty($params['contact_sub_type_hidden']) |
| 2004 | ) { |
| 2005 | // if profile was used, and had any subtype, we obtain it from there |
| 2006 | $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $params['contact_sub_type_hidden']) . CRM_Core_DAO::VALUE_SEPARATOR; |
| 2007 | } |
| 2008 | |
| 2009 | if ($ctype == 'Organization') { |
| 2010 | $data['organization_name'] = CRM_Utils_Array::value('organization_name', $contactDetails); |
| 2011 | } |
| 2012 | elseif ($ctype == 'Household') { |
| 2013 | $data['household_name'] = CRM_Utils_Array::value('household_name', $contactDetails); |
| 2014 | } |
| 2015 | |
| 2016 | $locationType = array(); |
| 2017 | $count = 1; |
| 2018 | |
| 2019 | if ($contactID) { |
| 2020 | //add contact id |
| 2021 | $data['contact_id'] = $contactID; |
| 2022 | $primaryLocationType = self::getPrimaryLocationType($contactID); |
| 2023 | } |
| 2024 | else { |
| 2025 | $defaultLocation = CRM_Core_BAO_LocationType::getDefault(); |
| 2026 | $defaultLocationId = $defaultLocation->id; |
| 2027 | } |
| 2028 | |
| 2029 | // get the billing location type |
| 2030 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate'); |
| 2031 | $billingLocationTypeId = array_search('Billing', $locationTypes); |
| 2032 | |
| 2033 | $blocks = array('email', 'phone', 'im', 'openid'); |
| 2034 | |
| 2035 | $multiplFields = array('url'); |
| 2036 | // prevent overwritten of formatted array, reset all block from |
| 2037 | // params if it is not in valid format (since import pass valid format) |
| 2038 | foreach ($blocks as $blk) { |
| 2039 | if (array_key_exists($blk, $params) && |
| 2040 | !is_array($params[$blk]) |
| 2041 | ) { |
| 2042 | unset($params[$blk]); |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | $primaryPhoneLoc = NULL; |
| 2047 | $session = CRM_Core_Session::singleton(); |
| 2048 | foreach ($params as $key => $value) { |
| 2049 | $fieldName = $locTypeId = $typeId = NULL; |
| 2050 | list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3); |
| 2051 | |
| 2052 | //store original location type id |
| 2053 | $actualLocTypeId = $locTypeId; |
| 2054 | |
| 2055 | if ($locTypeId == 'Primary') { |
| 2056 | if ($contactID) { |
| 2057 | if (in_array($fieldName, $blocks)) { |
| 2058 | $locTypeId = self::getPrimaryLocationType($contactID, FALSE, $fieldName); |
| 2059 | } |
| 2060 | else { |
| 2061 | $locTypeId = self::getPrimaryLocationType($contactID, FALSE, 'address'); |
| 2062 | } |
| 2063 | $primaryLocationType = $locTypeId; |
| 2064 | } |
| 2065 | else { |
| 2066 | $locTypeId = $defaultLocationId; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | if (is_numeric($locTypeId) && |
| 2071 | !in_array($fieldName, $multiplFields) && |
| 2072 | substr($fieldName, 0, 7) != 'custom_' |
| 2073 | ) { |
| 2074 | $index = $locTypeId; |
| 2075 | |
| 2076 | if (is_numeric($typeId)) { |
| 2077 | $index .= '-' . $typeId; |
| 2078 | } |
| 2079 | if (!in_array($index, $locationType)) { |
| 2080 | $locationType[$count] = $index; |
| 2081 | $count++; |
| 2082 | } |
| 2083 | |
| 2084 | $loc = CRM_Utils_Array::key($index, $locationType); |
| 2085 | |
| 2086 | $blockName = in_array($fieldName, $blocks) ? $fieldName : 'address'; |
| 2087 | |
| 2088 | $data[$blockName][$loc]['location_type_id'] = $locTypeId; |
| 2089 | |
| 2090 | //set is_billing true, for location type "Billing" |
| 2091 | if ($locTypeId == $billingLocationTypeId) { |
| 2092 | $data[$blockName][$loc]['is_billing'] = 1; |
| 2093 | } |
| 2094 | |
| 2095 | if ($contactID) { |
| 2096 | //get the primary location type |
| 2097 | if ($locTypeId == $primaryLocationType) { |
| 2098 | $data[$blockName][$loc]['is_primary'] = 1; |
| 2099 | } |
| 2100 | } |
| 2101 | elseif ($locTypeId == $defaultLocationId) { |
| 2102 | $data[$blockName][$loc]['is_primary'] = 1; |
| 2103 | } |
| 2104 | |
| 2105 | if (in_array($fieldName, array('phone'))) { |
| 2106 | if ($typeId) { |
| 2107 | $data['phone'][$loc]['phone_type_id'] = $typeId; |
| 2108 | } |
| 2109 | else { |
| 2110 | $data['phone'][$loc]['phone_type_id'] = ''; |
| 2111 | } |
| 2112 | $data['phone'][$loc]['phone'] = $value; |
| 2113 | |
| 2114 | //special case to handle primary phone with different phone types |
| 2115 | // in this case we make first phone type as primary |
| 2116 | if (isset($data['phone'][$loc]['is_primary']) && !$primaryPhoneLoc) { |
| 2117 | $primaryPhoneLoc = $loc; |
| 2118 | } |
| 2119 | |
| 2120 | if ($loc != $primaryPhoneLoc) { |
| 2121 | unset($data['phone'][$loc]['is_primary']); |
| 2122 | } |
| 2123 | } |
| 2124 | elseif ($fieldName == 'phone_ext') { |
| 2125 | $data['phone'][$loc]['phone_ext'] = $value; |
| 2126 | } |
| 2127 | elseif ($fieldName == 'email') { |
| 2128 | $data['email'][$loc]['email'] = $value; |
| 2129 | if (empty($contactID)) { |
| 2130 | $data['email'][$loc]['is_primary'] = 1; |
| 2131 | } |
| 2132 | } |
| 2133 | elseif ($fieldName == 'im') { |
| 2134 | if (isset($params[$key . '-provider_id'])) { |
| 2135 | $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id']; |
| 2136 | } |
| 2137 | if (strpos($key, '-provider_id') !== FALSE) { |
| 2138 | $data['im'][$loc]['provider_id'] = $params[$key]; |
| 2139 | } |
| 2140 | else { |
| 2141 | $data['im'][$loc]['name'] = $value; |
| 2142 | } |
| 2143 | } |
| 2144 | elseif ($fieldName == 'openid') { |
| 2145 | $data['openid'][$loc]['openid'] = $value; |
| 2146 | } |
| 2147 | else { |
| 2148 | if ($fieldName === 'state_province') { |
| 2149 | // CRM-3393 |
| 2150 | if (is_numeric($value) && ((int ) $value) >= 1000) { |
| 2151 | $data['address'][$loc]['state_province_id'] = $value; |
| 2152 | } |
| 2153 | elseif (empty($value)) { |
| 2154 | $data['address'][$loc]['state_province_id'] = ''; |
| 2155 | } |
| 2156 | else { |
| 2157 | $data['address'][$loc]['state_province'] = $value; |
| 2158 | } |
| 2159 | } |
| 2160 | elseif ($fieldName === 'country') { |
| 2161 | // CRM-3393 |
| 2162 | if (is_numeric($value) && ((int ) $value) >= 1000 |
| 2163 | ) { |
| 2164 | $data['address'][$loc]['country_id'] = $value; |
| 2165 | } |
| 2166 | elseif (empty($value)) { |
| 2167 | $data['address'][$loc]['country_id'] = ''; |
| 2168 | } |
| 2169 | else { |
| 2170 | $data['address'][$loc]['country'] = $value; |
| 2171 | } |
| 2172 | } |
| 2173 | elseif ($fieldName === 'county') { |
| 2174 | $data['address'][$loc]['county_id'] = $value; |
| 2175 | } |
| 2176 | elseif ($fieldName == 'address_name') { |
| 2177 | $data['address'][$loc]['name'] = $value; |
| 2178 | } |
| 2179 | elseif (substr($fieldName, 0, 14) === 'address_custom') { |
| 2180 | $data['address'][$loc][substr($fieldName, 8)] = $value; |
| 2181 | } |
| 2182 | else { |
| 2183 | $data['address'][$loc][$fieldName] = $value; |
| 2184 | } |
| 2185 | } |
| 2186 | } |
| 2187 | else { |
| 2188 | if (substr($key, 0, 4) === 'url-') { |
| 2189 | $websiteField = explode('-', $key); |
| 2190 | $data['website'][$websiteField[1]]['website_type_id'] = $websiteField[1]; |
| 2191 | $data['website'][$websiteField[1]]['url'] = $value; |
| 2192 | } |
| 2193 | elseif (in_array($key, self::$_greetingTypes, TRUE)) { |
| 2194 | //save email/postal greeting and addressee values if any, CRM-4575 |
| 2195 | $data[$key . '_id'] = $value; |
| 2196 | } |
| 2197 | elseif (!$skipCustom && ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key))) { |
| 2198 | // for autocomplete transfer hidden value instead of label |
| 2199 | if ($params[$key] && isset($params[$key . '_id'])) { |
| 2200 | $value = $params[$key . '_id']; |
| 2201 | } |
| 2202 | |
| 2203 | // we need to append time with date |
| 2204 | if ($params[$key] && isset($params[$key . '_time'])) { |
| 2205 | $value .= ' ' . $params[$key . '_time']; |
| 2206 | } |
| 2207 | |
| 2208 | // if auth source is not checksum / login && $value is blank, do not proceed - CRM-10128 |
| 2209 | if (($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 && |
| 2210 | ($value == '' || !isset($value)) |
| 2211 | ) { |
| 2212 | continue; |
| 2213 | } |
| 2214 | |
| 2215 | $valueId = NULL; |
| 2216 | if (!empty($params['customRecordValues'])) { |
| 2217 | if (is_array($params['customRecordValues']) && !empty($params['customRecordValues'])) { |
| 2218 | foreach ($params['customRecordValues'] as $recId => $customFields) { |
| 2219 | if (is_array($customFields) && !empty($customFields)) { |
| 2220 | foreach ($customFields as $customFieldName) { |
| 2221 | if ($customFieldName == $key) { |
| 2222 | $valueId = $recId; |
| 2223 | break; |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | $type = $data['contact_type']; |
| 2232 | if (!empty($data['contact_sub_type'])) { |
| 2233 | $type = $data['contact_sub_type']; |
| 2234 | $type = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($type, CRM_Core_DAO::VALUE_SEPARATOR)); |
| 2235 | // generally a contact even if, has multiple subtypes the parent-type is going to be one only |
| 2236 | // and since formatCustomField() would be interested in parent type, lets consider only one subtype |
| 2237 | // as the results going to be same. |
| 2238 | $type = $type[0]; |
| 2239 | } |
| 2240 | |
| 2241 | CRM_Core_BAO_CustomField::formatCustomField($customFieldId, |
| 2242 | $data['custom'], |
| 2243 | $value, |
| 2244 | $type, |
| 2245 | $valueId, |
| 2246 | $contactID |
| 2247 | ); |
| 2248 | } |
| 2249 | elseif ($key == 'edit') { |
| 2250 | continue; |
| 2251 | } |
| 2252 | else { |
| 2253 | if ($key == 'location') { |
| 2254 | foreach ($value as $locationTypeId => $field) { |
| 2255 | foreach ($field as $block => $val) { |
| 2256 | if ($block == 'address' && array_key_exists('address_name', $val)) { |
| 2257 | $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name']; |
| 2258 | } |
| 2259 | } |
| 2260 | } |
| 2261 | } |
| 2262 | if ($key == 'phone' && isset($params['phone_ext'])) { |
| 2263 | $data[$key] = $value; |
| 2264 | foreach ($value as $cnt => $phoneBlock) { |
| 2265 | if ($params[$key][$cnt]['location_type_id'] == $params['phone_ext'][$cnt]['location_type_id']) { |
| 2266 | $data[$key][$cnt]['phone_ext'] = CRM_Utils_Array::retrieveValueRecursive($params['phone_ext'][$cnt], 'phone_ext'); |
| 2267 | } |
| 2268 | } |
| 2269 | } |
| 2270 | elseif (in_array($key, |
| 2271 | array( |
| 2272 | 'nick_name', |
| 2273 | 'job_title', |
| 2274 | 'middle_name', |
| 2275 | 'birth_date', |
| 2276 | 'gender_id', |
| 2277 | 'current_employer', |
| 2278 | 'prefix_id', |
| 2279 | 'suffix_id', |
| 2280 | )) && |
| 2281 | ($value == '' || !isset($value)) && |
| 2282 | ($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0 |
| 2283 | ) { |
| 2284 | // CRM-10128: if auth source is not checksum / login && $value is blank, do not fill $data with empty value |
| 2285 | // to avoid update with empty values |
| 2286 | continue; |
| 2287 | } |
| 2288 | else { |
| 2289 | $data[$key] = $value; |
| 2290 | } |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | if (!isset($data['contact_type'])) { |
| 2296 | $data['contact_type'] = 'Individual'; |
| 2297 | } |
| 2298 | |
| 2299 | //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone) |
| 2300 | $privacy = CRM_Core_SelectValues::privacy(); |
| 2301 | foreach ($privacy as $key => $value) { |
| 2302 | if (array_key_exists($key, $fields)) { |
| 2303 | // do not reset values for existing contacts, if fields are added to a profile |
| 2304 | if (array_key_exists($key, $params)) { |
| 2305 | $data[$key] = $params[$key]; |
| 2306 | if (empty($params[$key])) { |
| 2307 | $data[$key] = 0; |
| 2308 | } |
| 2309 | } |
| 2310 | elseif (!$contactID) { |
| 2311 | $data[$key] = 0; |
| 2312 | } |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | return array($data, $contactDetails); |
| 2317 | } |
| 2318 | |
| 2319 | /** |
| 2320 | * Find the get contact details. |
| 2321 | * |
| 2322 | * This function does not respect ACLs for now, which might need to be rectified at some |
| 2323 | * stage based on how its used. |
| 2324 | * |
| 2325 | * @param string $mail |
| 2326 | * Primary email address of the contact. |
| 2327 | * @param string $ctype |
| 2328 | * Contact type. |
| 2329 | * |
| 2330 | * @return object |
| 2331 | * $dao contact details |
| 2332 | */ |
| 2333 | public static function &matchContactOnEmail($mail, $ctype = NULL) { |
| 2334 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
| 2335 | $mail = $strtolower(trim($mail)); |
| 2336 | $query = " |
| 2337 | SELECT civicrm_contact.id as contact_id, |
| 2338 | civicrm_contact.hash as hash, |
| 2339 | civicrm_contact.contact_type as contact_type, |
| 2340 | civicrm_contact.contact_sub_type as contact_sub_type |
| 2341 | FROM civicrm_contact |
| 2342 | INNER JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id )"; |
| 2343 | |
| 2344 | if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, 'uniq_email_per_site')) { |
| 2345 | // try to find a match within a site (multisite). |
| 2346 | $groups = CRM_Core_BAO_Domain::getChildGroupIds(); |
| 2347 | if (!empty($groups)) { |
| 2348 | $query .= " |
| 2349 | INNER JOIN civicrm_group_contact gc ON |
| 2350 | (civicrm_contact.id = gc.contact_id AND gc.status = 'Added' AND gc.group_id IN (" . implode(',', $groups) . "))"; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | $query .= " |
| 2355 | WHERE civicrm_email.email = %1 AND civicrm_contact.is_deleted=0"; |
| 2356 | $p = array(1 => array($mail, 'String')); |
| 2357 | |
| 2358 | if ($ctype) { |
| 2359 | $query .= " AND civicrm_contact.contact_type = %3"; |
| 2360 | $p[3] = array($ctype, 'String'); |
| 2361 | } |
| 2362 | |
| 2363 | $query .= " ORDER BY civicrm_email.is_primary DESC"; |
| 2364 | |
| 2365 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
| 2366 | |
| 2367 | if ($dao->fetch()) { |
| 2368 | return $dao; |
| 2369 | } |
| 2370 | return CRM_Core_DAO::$_nullObject; |
| 2371 | } |
| 2372 | |
| 2373 | /** |
| 2374 | * Find the contact details associated with an OpenID. |
| 2375 | * |
| 2376 | * @param string $openId |
| 2377 | * OpenId of the contact. |
| 2378 | * @param string $ctype |
| 2379 | * Contact type. |
| 2380 | * |
| 2381 | * @return object |
| 2382 | * $dao contact details |
| 2383 | */ |
| 2384 | public static function &matchContactOnOpenId($openId, $ctype = NULL) { |
| 2385 | $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; |
| 2386 | $openId = $strtolower(trim($openId)); |
| 2387 | $query = " |
| 2388 | SELECT civicrm_contact.id as contact_id, |
| 2389 | civicrm_contact.hash as hash, |
| 2390 | civicrm_contact.contact_type as contact_type, |
| 2391 | civicrm_contact.contact_sub_type as contact_sub_type |
| 2392 | FROM civicrm_contact |
| 2393 | INNER JOIN civicrm_openid ON ( civicrm_contact.id = civicrm_openid.contact_id ) |
| 2394 | WHERE civicrm_openid.openid = %1"; |
| 2395 | $p = array(1 => array($openId, 'String')); |
| 2396 | |
| 2397 | if ($ctype) { |
| 2398 | $query .= " AND civicrm_contact.contact_type = %3"; |
| 2399 | $p[3] = array($ctype, 'String'); |
| 2400 | } |
| 2401 | |
| 2402 | $query .= " ORDER BY civicrm_openid.is_primary DESC"; |
| 2403 | |
| 2404 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
| 2405 | |
| 2406 | if ($dao->fetch()) { |
| 2407 | return $dao; |
| 2408 | } |
| 2409 | return CRM_Core_DAO::$_nullObject; |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * Get primary email of the contact. |
| 2414 | * |
| 2415 | * @param int $contactID |
| 2416 | * Contact id. |
| 2417 | * |
| 2418 | * @return string |
| 2419 | * Email address if present else null |
| 2420 | */ |
| 2421 | public static function getPrimaryEmail($contactID) { |
| 2422 | // fetch the primary email |
| 2423 | $query = " |
| 2424 | SELECT civicrm_email.email as email |
| 2425 | FROM civicrm_contact |
| 2426 | LEFT JOIN civicrm_email ON ( civicrm_contact.id = civicrm_email.contact_id ) |
| 2427 | WHERE civicrm_email.is_primary = 1 |
| 2428 | AND civicrm_contact.id = %1"; |
| 2429 | $p = array(1 => array($contactID, 'Integer')); |
| 2430 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
| 2431 | |
| 2432 | $email = NULL; |
| 2433 | if ($dao->fetch()) { |
| 2434 | $email = $dao->email; |
| 2435 | } |
| 2436 | $dao->free(); |
| 2437 | return $email; |
| 2438 | } |
| 2439 | |
| 2440 | /** |
| 2441 | * Function to get primary OpenID of the contact. |
| 2442 | * |
| 2443 | * @param int $contactID |
| 2444 | * Contact id. |
| 2445 | * |
| 2446 | * @return string |
| 2447 | * >openid OpenID if present else null |
| 2448 | */ |
| 2449 | public static function getPrimaryOpenId($contactID) { |
| 2450 | // fetch the primary OpenID |
| 2451 | $query = " |
| 2452 | SELECT civicrm_openid.openid as openid |
| 2453 | FROM civicrm_contact |
| 2454 | LEFT JOIN civicrm_openid ON ( civicrm_contact.id = civicrm_openid.contact_id ) |
| 2455 | WHERE civicrm_contact.id = %1 |
| 2456 | AND civicrm_openid.is_primary = 1"; |
| 2457 | $p = array(1 => array($contactID, 'Integer')); |
| 2458 | $dao = CRM_Core_DAO::executeQuery($query, $p); |
| 2459 | |
| 2460 | $openId = NULL; |
| 2461 | if ($dao->fetch()) { |
| 2462 | $openId = $dao->openid; |
| 2463 | } |
| 2464 | $dao->free(); |
| 2465 | return $openId; |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * Fetch the object and store the values in the values array. |
| 2470 | * |
| 2471 | * @param array $params |
| 2472 | * Input parameters to find object. |
| 2473 | * @param array $values |
| 2474 | * Output values of the object. |
| 2475 | * |
| 2476 | * @return CRM_Contact_BAO_Contact|null |
| 2477 | * The found object or null |
| 2478 | */ |
| 2479 | public static function getValues(&$params, &$values) { |
| 2480 | $contact = new CRM_Contact_BAO_Contact(); |
| 2481 | |
| 2482 | $contact->copyValues($params); |
| 2483 | |
| 2484 | if ($contact->find(TRUE)) { |
| 2485 | |
| 2486 | CRM_Core_DAO::storeValues($contact, $values); |
| 2487 | |
| 2488 | $privacy = array(); |
| 2489 | foreach (self::$_commPrefs as $name) { |
| 2490 | if (isset($contact->$name)) { |
| 2491 | $privacy[$name] = $contact->$name; |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | if (!empty($privacy)) { |
| 2496 | $values['privacy'] = $privacy; |
| 2497 | } |
| 2498 | |
| 2499 | // communication Prefferance |
| 2500 | $preffComm = $comm = array(); |
| 2501 | $comm = explode(CRM_Core_DAO::VALUE_SEPARATOR, |
| 2502 | $contact->preferred_communication_method |
| 2503 | ); |
| 2504 | foreach ($comm as $value) { |
| 2505 | $preffComm[$value] = 1; |
| 2506 | } |
| 2507 | $temp = array('preferred_communication_method' => $contact->preferred_communication_method); |
| 2508 | |
| 2509 | $names = array( |
| 2510 | 'preferred_communication_method' => array( |
| 2511 | 'newName' => 'preferred_communication_method_display', |
| 2512 | 'groupName' => 'preferred_communication_method', |
| 2513 | ), |
| 2514 | ); |
| 2515 | |
| 2516 | CRM_Core_OptionGroup::lookupValues($temp, $names, FALSE); |
| 2517 | |
| 2518 | $values['preferred_communication_method'] = $preffComm; |
| 2519 | $values['preferred_communication_method_display'] = CRM_Utils_Array::value('preferred_communication_method_display', $temp); |
| 2520 | |
| 2521 | $preferredMailingFormat = CRM_Core_SelectValues::pmf(); |
| 2522 | $values['preferred_mail_format'] = $preferredMailingFormat[$contact->preferred_mail_format]; |
| 2523 | |
| 2524 | // get preferred languages |
| 2525 | if (!empty($contact->preferred_language)) { |
| 2526 | $values['preferred_language'] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'preferred_language', $contact->preferred_language); |
| 2527 | } |
| 2528 | |
| 2529 | // Calculating Year difference |
| 2530 | if ($contact->birth_date) { |
| 2531 | $birthDate = CRM_Utils_Date::customFormat($contact->birth_date, '%Y%m%d'); |
| 2532 | if ($birthDate < date('Ymd')) { |
| 2533 | $age = CRM_Utils_Date::calculateAge($birthDate); |
| 2534 | $values['age']['y'] = CRM_Utils_Array::value('years', $age); |
| 2535 | $values['age']['m'] = CRM_Utils_Array::value('months', $age); |
| 2536 | } |
| 2537 | |
| 2538 | list($values['birth_date']) = CRM_Utils_Date::setDateDefaults($contact->birth_date, 'birth'); |
| 2539 | $values['birth_date_display'] = $contact->birth_date; |
| 2540 | } |
| 2541 | |
| 2542 | if ($contact->deceased_date) { |
| 2543 | list($values['deceased_date']) = CRM_Utils_Date::setDateDefaults($contact->deceased_date, 'birth'); |
| 2544 | $values['deceased_date_display'] = $contact->deceased_date; |
| 2545 | } |
| 2546 | |
| 2547 | $contact->contact_id = $contact->id; |
| 2548 | |
| 2549 | return $contact; |
| 2550 | } |
| 2551 | return NULL; |
| 2552 | } |
| 2553 | |
| 2554 | /** |
| 2555 | * Given the component name and returns the count of participation of contact. |
| 2556 | * |
| 2557 | * @param string $component |
| 2558 | * Input component name. |
| 2559 | * @param int $contactId |
| 2560 | * Input contact id. |
| 2561 | * @param string $tableName |
| 2562 | * Optional tableName if component is custom group. |
| 2563 | * |
| 2564 | * @return int |
| 2565 | * total number in database |
| 2566 | */ |
| 2567 | public static function getCountComponent($component, $contactId, $tableName = NULL) { |
| 2568 | $object = NULL; |
| 2569 | switch ($component) { |
| 2570 | case 'tag': |
| 2571 | return CRM_Core_BAO_EntityTag::getContactTags($contactId, TRUE); |
| 2572 | |
| 2573 | case 'rel': |
| 2574 | $result = CRM_Contact_BAO_Relationship::getRelationship($contactId, |
| 2575 | CRM_Contact_BAO_Relationship::CURRENT |
| 2576 | ); |
| 2577 | return count($result); |
| 2578 | |
| 2579 | case 'group': |
| 2580 | return CRM_Contact_BAO_GroupContact::getContactGroup($contactId, "Added", NULL, TRUE); |
| 2581 | |
| 2582 | case 'log': |
| 2583 | if (CRM_Core_BAO_Log::useLoggingReport()) { |
| 2584 | return FALSE; |
| 2585 | } |
| 2586 | return CRM_Core_BAO_Log::getContactLogCount($contactId); |
| 2587 | |
| 2588 | case 'note': |
| 2589 | return CRM_Core_BAO_Note::getContactNoteCount($contactId); |
| 2590 | |
| 2591 | case 'contribution': |
| 2592 | return CRM_Contribute_BAO_Contribution::contributionCount($contactId); |
| 2593 | |
| 2594 | case 'membership': |
| 2595 | return CRM_Member_BAO_Membership::getContactMembershipCount($contactId, TRUE); |
| 2596 | |
| 2597 | case 'participant': |
| 2598 | return CRM_Event_BAO_Participant::getContactParticipantCount($contactId); |
| 2599 | |
| 2600 | case 'pledge': |
| 2601 | return CRM_Pledge_BAO_Pledge::getContactPledgeCount($contactId); |
| 2602 | |
| 2603 | case 'case': |
| 2604 | return CRM_Case_BAO_Case::caseCount($contactId); |
| 2605 | |
| 2606 | case 'grant': |
| 2607 | return CRM_Grant_BAO_Grant::getContactGrantCount($contactId); |
| 2608 | |
| 2609 | case 'activity': |
| 2610 | $input = array( |
| 2611 | 'contact_id' => $contactId, |
| 2612 | 'admin' => FALSE, |
| 2613 | 'caseId' => NULL, |
| 2614 | 'context' => 'activity', |
| 2615 | ); |
| 2616 | return CRM_Activity_BAO_Activity::getActivitiesCount($input); |
| 2617 | |
| 2618 | case 'mailing': |
| 2619 | $params = array('contact_id' => $contactId); |
| 2620 | return CRM_Mailing_BAO_Mailing::getContactMailingsCount($params); |
| 2621 | |
| 2622 | default: |
| 2623 | $custom = explode('_', $component); |
| 2624 | if ($custom['0'] = 'custom') { |
| 2625 | if (!$tableName) { |
| 2626 | $tableName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $custom['1'], 'table_name'); |
| 2627 | } |
| 2628 | $queryString = "SELECT count(id) FROM {$tableName} WHERE entity_id = {$contactId}"; |
| 2629 | return CRM_Core_DAO::singleValueQuery($queryString); |
| 2630 | } |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | /** |
| 2635 | * Process greetings and cache. |
| 2636 | * |
| 2637 | * @param object $contact |
| 2638 | * Contact object after save. |
| 2639 | * @param bool $useDefaults |
| 2640 | * Use default greeting values. |
| 2641 | */ |
| 2642 | public static function processGreetings(&$contact, $useDefaults = FALSE) { |
| 2643 | if ($useDefaults) { |
| 2644 | //retrieve default greetings |
| 2645 | $defaultGreetings = CRM_Core_PseudoConstant::greetingDefaults(); |
| 2646 | $contactDefaults = $defaultGreetings[$contact->contact_type]; |
| 2647 | } |
| 2648 | |
| 2649 | // note that contact object not always has required greeting related |
| 2650 | // fields that are required to calculate greeting and |
| 2651 | // also other fields used in tokens etc, |
| 2652 | // hence we need to retrieve it again. |
| 2653 | if ($contact->_query !== FALSE) { |
| 2654 | $contact->find(TRUE); |
| 2655 | } |
| 2656 | |
| 2657 | // store object values to an array |
| 2658 | $contactDetails = array(); |
| 2659 | CRM_Core_DAO::storeValues($contact, $contactDetails); |
| 2660 | $contactDetails = array(array($contact->id => $contactDetails)); |
| 2661 | |
| 2662 | $emailGreetingString = $postalGreetingString = $addresseeString = NULL; |
| 2663 | $updateQueryString = array(); |
| 2664 | |
| 2665 | //cache email and postal greeting to greeting display |
| 2666 | if ($contact->email_greeting_custom != 'null' && $contact->email_greeting_custom) { |
| 2667 | $emailGreetingString = $contact->email_greeting_custom; |
| 2668 | } |
| 2669 | elseif ($contact->email_greeting_id != 'null' && $contact->email_greeting_id) { |
| 2670 | // the filter value for Individual contact type is set to 1 |
| 2671 | $filter = array( |
| 2672 | 'contact_type' => $contact->contact_type, |
| 2673 | 'greeting_type' => 'email_greeting', |
| 2674 | ); |
| 2675 | |
| 2676 | $emailGreeting = CRM_Core_PseudoConstant::greeting($filter); |
| 2677 | $emailGreetingString = $emailGreeting[$contact->email_greeting_id]; |
| 2678 | $updateQueryString[] = " email_greeting_custom = NULL "; |
| 2679 | } |
| 2680 | else { |
| 2681 | if ($useDefaults) { |
| 2682 | reset($contactDefaults['email_greeting']); |
| 2683 | $emailGreetingID = key($contactDefaults['email_greeting']); |
| 2684 | $emailGreetingString = $contactDefaults['email_greeting'][$emailGreetingID]; |
| 2685 | $updateQueryString[] = " email_greeting_id = $emailGreetingID "; |
| 2686 | $updateQueryString[] = " email_greeting_custom = NULL "; |
| 2687 | } |
| 2688 | elseif ($contact->email_greeting_custom) { |
| 2689 | $updateQueryString[] = " email_greeting_display = NULL "; |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | if ($emailGreetingString) { |
| 2694 | CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($emailGreetingString, |
| 2695 | $contactDetails, |
| 2696 | $contact->id, |
| 2697 | 'CRM_Contact_BAO_Contact' |
| 2698 | ); |
| 2699 | $emailGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($emailGreetingString)); |
| 2700 | $updateQueryString[] = " email_greeting_display = '{$emailGreetingString}'"; |
| 2701 | } |
| 2702 | |
| 2703 | //postal greetings |
| 2704 | if ($contact->postal_greeting_custom != 'null' && $contact->postal_greeting_custom) { |
| 2705 | $postalGreetingString = $contact->postal_greeting_custom; |
| 2706 | } |
| 2707 | elseif ($contact->postal_greeting_id != 'null' && $contact->postal_greeting_id) { |
| 2708 | $filter = array( |
| 2709 | 'contact_type' => $contact->contact_type, |
| 2710 | 'greeting_type' => 'postal_greeting', |
| 2711 | ); |
| 2712 | $postalGreeting = CRM_Core_PseudoConstant::greeting($filter); |
| 2713 | $postalGreetingString = $postalGreeting[$contact->postal_greeting_id]; |
| 2714 | $updateQueryString[] = " postal_greeting_custom = NULL "; |
| 2715 | } |
| 2716 | else { |
| 2717 | if ($useDefaults) { |
| 2718 | reset($contactDefaults['postal_greeting']); |
| 2719 | $postalGreetingID = key($contactDefaults['postal_greeting']); |
| 2720 | $postalGreetingString = $contactDefaults['postal_greeting'][$postalGreetingID]; |
| 2721 | $updateQueryString[] = " postal_greeting_id = $postalGreetingID "; |
| 2722 | $updateQueryString[] = " postal_greeting_custom = NULL "; |
| 2723 | } |
| 2724 | elseif ($contact->postal_greeting_custom) { |
| 2725 | $updateQueryString[] = " postal_greeting_display = NULL "; |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | if ($postalGreetingString) { |
| 2730 | CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($postalGreetingString, |
| 2731 | $contactDetails, |
| 2732 | $contact->id, |
| 2733 | 'CRM_Contact_BAO_Contact' |
| 2734 | ); |
| 2735 | $postalGreetingString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($postalGreetingString)); |
| 2736 | $updateQueryString[] = " postal_greeting_display = '{$postalGreetingString}'"; |
| 2737 | } |
| 2738 | |
| 2739 | // addressee |
| 2740 | if ($contact->addressee_custom != 'null' && $contact->addressee_custom) { |
| 2741 | $addresseeString = $contact->addressee_custom; |
| 2742 | } |
| 2743 | elseif ($contact->addressee_id != 'null' && $contact->addressee_id) { |
| 2744 | $filter = array( |
| 2745 | 'contact_type' => $contact->contact_type, |
| 2746 | 'greeting_type' => 'addressee', |
| 2747 | ); |
| 2748 | |
| 2749 | $addressee = CRM_Core_PseudoConstant::greeting($filter); |
| 2750 | $addresseeString = $addressee[$contact->addressee_id]; |
| 2751 | $updateQueryString[] = " addressee_custom = NULL "; |
| 2752 | } |
| 2753 | else { |
| 2754 | if ($useDefaults) { |
| 2755 | reset($contactDefaults['addressee']); |
| 2756 | $addresseeID = key($contactDefaults['addressee']); |
| 2757 | $addresseeString = $contactDefaults['addressee'][$addresseeID]; |
| 2758 | $updateQueryString[] = " addressee_id = $addresseeID "; |
| 2759 | $updateQueryString[] = " addressee_custom = NULL "; |
| 2760 | } |
| 2761 | elseif ($contact->addressee_custom) { |
| 2762 | $updateQueryString[] = " addressee_display = NULL "; |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | if ($addresseeString) { |
| 2767 | CRM_Contact_BAO_Contact_Utils::processGreetingTemplate($addresseeString, |
| 2768 | $contactDetails, |
| 2769 | $contact->id, |
| 2770 | 'CRM_Contact_BAO_Contact' |
| 2771 | ); |
| 2772 | $addresseeString = CRM_Core_DAO::escapeString(CRM_Utils_String::stripSpaces($addresseeString)); |
| 2773 | $updateQueryString[] = " addressee_display = '{$addresseeString}'"; |
| 2774 | } |
| 2775 | |
| 2776 | if (!empty($updateQueryString)) { |
| 2777 | $updateQueryString = implode(',', $updateQueryString); |
| 2778 | $queryString = "UPDATE civicrm_contact SET {$updateQueryString} WHERE id = {$contact->id}"; |
| 2779 | CRM_Core_DAO::executeQuery($queryString); |
| 2780 | } |
| 2781 | } |
| 2782 | |
| 2783 | /** |
| 2784 | * Retrieve loc block ids w/ given condition. |
| 2785 | * |
| 2786 | * @param int $contactId |
| 2787 | * Contact id. |
| 2788 | * @param array $criteria |
| 2789 | * Key => value pair which should be. |
| 2790 | * fulfill by return record ids. |
| 2791 | * @param string $condOperator |
| 2792 | * Operator use for grouping multiple conditions. |
| 2793 | * |
| 2794 | * @return array |
| 2795 | * loc block ids which fulfill condition. |
| 2796 | */ |
| 2797 | public static function getLocBlockIds($contactId, $criteria = array(), $condOperator = 'AND') { |
| 2798 | $locBlockIds = array(); |
| 2799 | if (!$contactId) { |
| 2800 | return $locBlockIds; |
| 2801 | } |
| 2802 | |
| 2803 | foreach (array('Email', 'OpenID', 'Phone', 'Address', 'IM') as $block) { |
| 2804 | $name = strtolower($block); |
| 2805 | $className = "CRM_Core_DAO_$block"; |
| 2806 | $blockDAO = new $className(); |
| 2807 | |
| 2808 | // build the condition. |
| 2809 | if (is_array($criteria)) { |
| 2810 | $fields = $blockDAO->fields(); |
| 2811 | $conditions = array(); |
| 2812 | foreach ($criteria as $field => $value) { |
| 2813 | if (array_key_exists($field, $fields)) { |
| 2814 | $cond = "( $field = $value )"; |
| 2815 | // value might be zero or null. |
| 2816 | if (!$value || strtolower($value) == 'null') { |
| 2817 | $cond = "( $field = 0 OR $field IS NULL )"; |
| 2818 | } |
| 2819 | $conditions[] = $cond; |
| 2820 | } |
| 2821 | } |
| 2822 | if (!empty($conditions)) { |
| 2823 | $blockDAO->whereAdd(implode(" $condOperator ", $conditions)); |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | $blockDAO->contact_id = $contactId; |
| 2828 | $blockDAO->find(); |
| 2829 | while ($blockDAO->fetch()) { |
| 2830 | $locBlockIds[$name][] = $blockDAO->id; |
| 2831 | } |
| 2832 | $blockDAO->free(); |
| 2833 | } |
| 2834 | |
| 2835 | return $locBlockIds; |
| 2836 | } |
| 2837 | |
| 2838 | /** |
| 2839 | * Build context menu items. |
| 2840 | * |
| 2841 | * @param int $contactId |
| 2842 | * |
| 2843 | * @return array |
| 2844 | * Array of context menu for logged in user. |
| 2845 | */ |
| 2846 | public static function contextMenu($contactId = NULL) { |
| 2847 | $menu = array( |
| 2848 | 'view' => array( |
| 2849 | 'title' => ts('View Contact'), |
| 2850 | 'weight' => 0, |
| 2851 | 'ref' => 'view-contact', |
| 2852 | 'class' => 'no-popup', |
| 2853 | 'key' => 'view', |
| 2854 | 'permissions' => array('view all contacts'), |
| 2855 | ), |
| 2856 | 'add' => array( |
| 2857 | 'title' => ts('Edit Contact'), |
| 2858 | 'weight' => 0, |
| 2859 | 'ref' => 'edit-contact', |
| 2860 | 'class' => 'no-popup', |
| 2861 | 'key' => 'add', |
| 2862 | 'permissions' => array('edit all contacts'), |
| 2863 | ), |
| 2864 | 'delete' => array( |
| 2865 | 'title' => ts('Delete Contact'), |
| 2866 | 'weight' => 0, |
| 2867 | 'ref' => 'delete-contact', |
| 2868 | 'key' => 'delete', |
| 2869 | 'permissions' => array('access deleted contacts', 'delete contacts'), |
| 2870 | ), |
| 2871 | 'contribution' => array( |
| 2872 | 'title' => ts('Add Contribution'), |
| 2873 | 'weight' => 5, |
| 2874 | 'ref' => 'new-contribution', |
| 2875 | 'key' => 'contribution', |
| 2876 | 'tab' => 'contribute', |
| 2877 | 'component' => 'CiviContribute', |
| 2878 | 'href' => CRM_Utils_System::url('civicrm/contact/view/contribution', |
| 2879 | 'reset=1&action=add&context=contribution' |
| 2880 | ), |
| 2881 | 'permissions' => array( |
| 2882 | 'access CiviContribute', |
| 2883 | 'edit contributions', |
| 2884 | ), |
| 2885 | ), |
| 2886 | 'participant' => array( |
| 2887 | 'title' => ts('Register for Event'), |
| 2888 | 'weight' => 10, |
| 2889 | 'ref' => 'new-participant', |
| 2890 | 'key' => 'participant', |
| 2891 | 'tab' => 'participant', |
| 2892 | 'component' => 'CiviEvent', |
| 2893 | 'href' => CRM_Utils_System::url('civicrm/contact/view/participant', 'reset=1&action=add&context=participant'), |
| 2894 | 'permissions' => array( |
| 2895 | 'access CiviEvent', |
| 2896 | 'edit event participants', |
| 2897 | ), |
| 2898 | ), |
| 2899 | 'activity' => array( |
| 2900 | 'title' => ts('Record Activity'), |
| 2901 | 'weight' => 35, |
| 2902 | 'ref' => 'new-activity', |
| 2903 | 'key' => 'activity', |
| 2904 | 'permissions' => array('edit all contacts'), |
| 2905 | ), |
| 2906 | 'pledge' => array( |
| 2907 | 'title' => ts('Add Pledge'), |
| 2908 | 'weight' => 15, |
| 2909 | 'ref' => 'new-pledge', |
| 2910 | 'key' => 'pledge', |
| 2911 | 'tab' => 'pledge', |
| 2912 | 'href' => CRM_Utils_System::url('civicrm/contact/view/pledge', |
| 2913 | 'reset=1&action=add&context=pledge' |
| 2914 | ), |
| 2915 | 'component' => 'CiviPledge', |
| 2916 | 'permissions' => array( |
| 2917 | 'access CiviPledge', |
| 2918 | 'edit pledges', |
| 2919 | ), |
| 2920 | ), |
| 2921 | 'membership' => array( |
| 2922 | 'title' => ts('Add Membership'), |
| 2923 | 'weight' => 20, |
| 2924 | 'ref' => 'new-membership', |
| 2925 | 'key' => 'membership', |
| 2926 | 'tab' => 'member', |
| 2927 | 'component' => 'CiviMember', |
| 2928 | 'href' => CRM_Utils_System::url('civicrm/contact/view/membership', |
| 2929 | 'reset=1&action=add&context=membership' |
| 2930 | ), |
| 2931 | 'permissions' => array( |
| 2932 | 'access CiviMember', |
| 2933 | 'edit memberships', |
| 2934 | ), |
| 2935 | ), |
| 2936 | 'case' => array( |
| 2937 | 'title' => ts('Add Case'), |
| 2938 | 'weight' => 25, |
| 2939 | 'ref' => 'new-case', |
| 2940 | 'key' => 'case', |
| 2941 | 'tab' => 'case', |
| 2942 | 'component' => 'CiviCase', |
| 2943 | 'href' => CRM_Utils_System::url('civicrm/case/add', 'reset=1&action=add&context=case'), |
| 2944 | 'permissions' => array('add cases'), |
| 2945 | ), |
| 2946 | 'grant' => array( |
| 2947 | 'title' => ts('Add Grant'), |
| 2948 | 'weight' => 26, |
| 2949 | 'ref' => 'new-grant', |
| 2950 | 'key' => 'grant', |
| 2951 | 'tab' => 'grant', |
| 2952 | 'component' => 'CiviGrant', |
| 2953 | 'href' => CRM_Utils_System::url('civicrm/contact/view/grant', |
| 2954 | 'reset=1&action=add&context=grant' |
| 2955 | ), |
| 2956 | 'permissions' => array('edit grants'), |
| 2957 | ), |
| 2958 | 'rel' => array( |
| 2959 | 'title' => ts('Add Relationship'), |
| 2960 | 'weight' => 30, |
| 2961 | 'ref' => 'new-relationship', |
| 2962 | 'key' => 'rel', |
| 2963 | 'tab' => 'rel', |
| 2964 | 'href' => CRM_Utils_System::url('civicrm/contact/view/rel', |
| 2965 | 'reset=1&action=add' |
| 2966 | ), |
| 2967 | 'permissions' => array('edit all contacts'), |
| 2968 | ), |
| 2969 | 'note' => array( |
| 2970 | 'title' => ts('Add Note'), |
| 2971 | 'weight' => 40, |
| 2972 | 'ref' => 'new-note', |
| 2973 | 'key' => 'note', |
| 2974 | 'tab' => 'note', |
| 2975 | 'class' => 'medium-popup', |
| 2976 | 'href' => CRM_Utils_System::url('civicrm/contact/view/note', |
| 2977 | 'reset=1&action=add' |
| 2978 | ), |
| 2979 | 'permissions' => array('edit all contacts'), |
| 2980 | ), |
| 2981 | 'email' => array( |
| 2982 | 'title' => ts('Send an Email'), |
| 2983 | 'weight' => 45, |
| 2984 | 'ref' => 'new-email', |
| 2985 | 'key' => 'email', |
| 2986 | 'permissions' => array('view all contacts'), |
| 2987 | ), |
| 2988 | 'group' => array( |
| 2989 | 'title' => ts('Add to Group'), |
| 2990 | 'weight' => 50, |
| 2991 | 'ref' => 'group-add-contact', |
| 2992 | 'key' => 'group', |
| 2993 | 'tab' => 'group', |
| 2994 | 'permissions' => array('edit groups'), |
| 2995 | ), |
| 2996 | 'tag' => array( |
| 2997 | 'title' => ts('Tag Contact'), |
| 2998 | 'weight' => 55, |
| 2999 | 'ref' => 'tag-contact', |
| 3000 | 'key' => 'tag', |
| 3001 | 'tab' => 'tag', |
| 3002 | 'permissions' => array('edit all contacts'), |
| 3003 | ), |
| 3004 | ); |
| 3005 | |
| 3006 | CRM_Utils_Hook::summaryActions($menu, $contactId); |
| 3007 | //1. check for component is active. |
| 3008 | //2. check for user permissions. |
| 3009 | //3. check for acls. |
| 3010 | //3. edit and view contact are directly accessible to user. |
| 3011 | |
| 3012 | $aclPermissionedTasks = array( |
| 3013 | 'view-contact', |
| 3014 | 'edit-contact', |
| 3015 | 'new-activity', |
| 3016 | 'new-email', |
| 3017 | 'group-add-contact', |
| 3018 | 'tag-contact', |
| 3019 | 'delete-contact', |
| 3020 | ); |
| 3021 | $corePermission = CRM_Core_Permission::getPermission(); |
| 3022 | |
| 3023 | $config = CRM_Core_Config::singleton(); |
| 3024 | |
| 3025 | $contextMenu = array(); |
| 3026 | foreach ($menu as $key => $values) { |
| 3027 | $componentName = CRM_Utils_Array::value('component', $values); |
| 3028 | |
| 3029 | // if component action - make sure component is enable. |
| 3030 | if ($componentName && !in_array($componentName, $config->enableComponents)) { |
| 3031 | continue; |
| 3032 | } |
| 3033 | |
| 3034 | // make sure user has all required permissions. |
| 3035 | $hasAllPermissions = FALSE; |
| 3036 | |
| 3037 | $permissions = CRM_Utils_Array::value('permissions', $values); |
| 3038 | if (!is_array($permissions) || empty($permissions)) { |
| 3039 | $hasAllPermissions = TRUE; |
| 3040 | } |
| 3041 | |
| 3042 | // iterate for required permissions in given permissions array. |
| 3043 | if (!$hasAllPermissions) { |
| 3044 | $hasPermissions = 0; |
| 3045 | foreach ($permissions as $permission) { |
| 3046 | if (CRM_Core_Permission::check($permission)) { |
| 3047 | $hasPermissions++; |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | if (count($permissions) == $hasPermissions) { |
| 3052 | $hasAllPermissions = TRUE; |
| 3053 | } |
| 3054 | |
| 3055 | // if still user does not have required permissions, check acl. |
| 3056 | if (!$hasAllPermissions && $values['ref'] != 'delete-contact') { |
| 3057 | if (in_array($values['ref'], $aclPermissionedTasks) && |
| 3058 | $corePermission == CRM_Core_Permission::EDIT |
| 3059 | ) { |
| 3060 | $hasAllPermissions = TRUE; |
| 3061 | } |
| 3062 | elseif (in_array($values['ref'], array( |
| 3063 | 'new-email', |
| 3064 | ))) { |
| 3065 | // grant permissions for these tasks. |
| 3066 | $hasAllPermissions = TRUE; |
| 3067 | } |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | // user does not have necessary permissions. |
| 3072 | if (!$hasAllPermissions) { |
| 3073 | continue; |
| 3074 | } |
| 3075 | |
| 3076 | // build directly accessible action menu. |
| 3077 | if (in_array($values['ref'], array( |
| 3078 | 'view-contact', |
| 3079 | 'edit-contact', |
| 3080 | ))) { |
| 3081 | $contextMenu['primaryActions'][$key] = array( |
| 3082 | 'title' => $values['title'], |
| 3083 | 'ref' => $values['ref'], |
| 3084 | 'class' => CRM_Utils_Array::value('class', $values), |
| 3085 | 'key' => $values['key'], |
| 3086 | ); |
| 3087 | continue; |
| 3088 | } |
| 3089 | |
| 3090 | // finally get menu item for -more- action widget. |
| 3091 | $contextMenu['moreActions'][$values['weight']] = array( |
| 3092 | 'title' => $values['title'], |
| 3093 | 'ref' => $values['ref'], |
| 3094 | 'href' => CRM_Utils_Array::value('href', $values), |
| 3095 | 'tab' => CRM_Utils_Array::value('tab', $values), |
| 3096 | 'class' => CRM_Utils_Array::value('class', $values), |
| 3097 | 'key' => $values['key'], |
| 3098 | ); |
| 3099 | } |
| 3100 | |
| 3101 | ksort($contextMenu['moreActions']); |
| 3102 | |
| 3103 | return $contextMenu; |
| 3104 | } |
| 3105 | |
| 3106 | /** |
| 3107 | * Retrieve display name of contact that address is shared. |
| 3108 | * |
| 3109 | * This is based on $masterAddressId or $contactId . |
| 3110 | * |
| 3111 | * @param int $masterAddressId |
| 3112 | * Master id. |
| 3113 | * @param int $contactId |
| 3114 | * Contact id. |
| 3115 | * |
| 3116 | * @return string|null |
| 3117 | * the found display name or null. |
| 3118 | */ |
| 3119 | public static function getMasterDisplayName($masterAddressId = NULL, $contactId = NULL) { |
| 3120 | $masterDisplayName = NULL; |
| 3121 | $sql = NULL; |
| 3122 | if (!$masterAddressId && !$contactId) { |
| 3123 | return $masterDisplayName; |
| 3124 | } |
| 3125 | |
| 3126 | if ($masterAddressId) { |
| 3127 | $sql = " |
| 3128 | SELECT display_name from civicrm_contact |
| 3129 | LEFT JOIN civicrm_address ON ( civicrm_address.contact_id = civicrm_contact.id ) |
| 3130 | WHERE civicrm_address.id = " . $masterAddressId; |
| 3131 | } |
| 3132 | elseif ($contactId) { |
| 3133 | $sql = " |
| 3134 | SELECT display_name from civicrm_contact cc, civicrm_address add1 |
| 3135 | LEFT JOIN civicrm_address add2 ON ( add1.master_id = add2.id ) |
| 3136 | WHERE cc.id = add2.contact_id AND add1.contact_id = " . $contactId; |
| 3137 | } |
| 3138 | |
| 3139 | $masterDisplayName = CRM_Core_DAO::singleValueQuery($sql); |
| 3140 | return $masterDisplayName; |
| 3141 | } |
| 3142 | |
| 3143 | /** |
| 3144 | * Get the creation/modification times for a contact. |
| 3145 | * |
| 3146 | * @param int $contactId |
| 3147 | * |
| 3148 | * @return array |
| 3149 | * Dates - ('created_date' => $, 'modified_date' => $) |
| 3150 | */ |
| 3151 | public static function getTimestamps($contactId) { |
| 3152 | $timestamps = CRM_Core_DAO::executeQuery( |
| 3153 | 'SELECT created_date, modified_date |
| 3154 | FROM civicrm_contact |
| 3155 | WHERE id = %1', |
| 3156 | array( |
| 3157 | 1 => array($contactId, 'Integer'), |
| 3158 | ) |
| 3159 | ); |
| 3160 | if ($timestamps->fetch()) { |
| 3161 | return array( |
| 3162 | 'created_date' => $timestamps->created_date, |
| 3163 | 'modified_date' => $timestamps->modified_date, |
| 3164 | ); |
| 3165 | } |
| 3166 | else { |
| 3167 | return NULL; |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | /** |
| 3172 | * Generate triggers to update the timestamp. |
| 3173 | * |
| 3174 | * The corresponding civicrm_contact row is updated on insert/update/delete |
| 3175 | * to a table that extends civicrm_contact. |
| 3176 | * Don't regenerate triggers for all such tables if only asked for one table. |
| 3177 | * |
| 3178 | * @param array $info |
| 3179 | * Reference to the array where generated trigger information is being stored |
| 3180 | * @param string|null $reqTableName |
| 3181 | * Name of the table for which triggers are being generated, or NULL if all tables |
| 3182 | * @param array $relatedTableNames |
| 3183 | * Array of all core or all custom table names extending civicrm_contact |
| 3184 | * @param string $contactRefColumn |
| 3185 | * 'contact_id' if processing core tables, 'entity_id' if processing custom tables |
| 3186 | * |
| 3187 | * @link https://issues.civicrm.org/jira/browse/CRM-15602 |
| 3188 | * @see triggerInfo |
| 3189 | */ |
| 3190 | public static function generateTimestampTriggers(&$info, $reqTableName, $relatedTableNames, $contactRefColumn) { |
| 3191 | // Safety |
| 3192 | $contactRefColumn = CRM_Core_DAO::escapeString($contactRefColumn); |
| 3193 | // If specific related table requested, just process that one |
| 3194 | if (in_array($reqTableName, $relatedTableNames)) { |
| 3195 | $relatedTableNames = array($reqTableName); |
| 3196 | } |
| 3197 | |
| 3198 | // If no specific table requested (include all related tables), |
| 3199 | // or a specific related table requested (as matched above) |
| 3200 | if (empty($reqTableName) || in_array($reqTableName, $relatedTableNames)) { |
| 3201 | $info[] = array( |
| 3202 | 'table' => $relatedTableNames, |
| 3203 | 'when' => 'AFTER', |
| 3204 | 'event' => array('INSERT', 'UPDATE'), |
| 3205 | 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.$contactRefColumn;\n", |
| 3206 | ); |
| 3207 | $info[] = array( |
| 3208 | 'table' => $relatedTableNames, |
| 3209 | 'when' => 'AFTER', |
| 3210 | 'event' => array('DELETE'), |
| 3211 | 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.$contactRefColumn;\n", |
| 3212 | ); |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | /** |
| 3217 | * Get a list of triggers for the contact table. |
| 3218 | * |
| 3219 | * @see hook_civicrm_triggerInfo |
| 3220 | * @see CRM_Core_DAO::triggerRebuild |
| 3221 | * @see http://issues.civicrm.org/jira/browse/CRM-10554 |
| 3222 | * |
| 3223 | * @param $info |
| 3224 | * @param null $tableName |
| 3225 | */ |
| 3226 | public static function triggerInfo(&$info, $tableName = NULL) { |
| 3227 | //during upgrade, first check for valid version and then create triggers |
| 3228 | //i.e the columns created_date and modified_date are introduced in 4.3.alpha1 so dont create triggers for older version |
| 3229 | if (CRM_Core_Config::isUpgradeMode()) { |
| 3230 | $currentVer = CRM_Core_BAO_Domain::version(TRUE); |
| 3231 | //if current version is less than 4.3.alpha1 dont create below triggers |
| 3232 | if (version_compare($currentVer, '4.3.alpha1') < 0) { |
| 3233 | return; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | // Update timestamp for civicrm_contact itself |
| 3238 | if ($tableName == NULL || $tableName == self::getTableName()) { |
| 3239 | $info[] = array( |
| 3240 | 'table' => array(self::getTableName()), |
| 3241 | 'when' => 'BEFORE', |
| 3242 | 'event' => array('INSERT'), |
| 3243 | 'sql' => "\nSET NEW.created_date = CURRENT_TIMESTAMP;\n", |
| 3244 | ); |
| 3245 | } |
| 3246 | |
| 3247 | // Update timestamp when modifying closely related core tables |
| 3248 | $relatedTables = array( |
| 3249 | 'civicrm_address', |
| 3250 | 'civicrm_email', |
| 3251 | 'civicrm_im', |
| 3252 | 'civicrm_phone', |
| 3253 | 'civicrm_website', |
| 3254 | ); |
| 3255 | self::generateTimestampTriggers($info, $tableName, $relatedTables, 'contact_id'); |
| 3256 | |
| 3257 | // Update timestamp when modifying related custom-data tables |
| 3258 | $customGroupTables = array(); |
| 3259 | $customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity('Contact'); |
| 3260 | $customGroupDAO->is_multiple = 0; |
| 3261 | $customGroupDAO->find(); |
| 3262 | while ($customGroupDAO->fetch()) { |
| 3263 | $customGroupTables[] = $customGroupDAO->table_name; |
| 3264 | } |
| 3265 | if (!empty($customGroupTables)) { |
| 3266 | self::generateTimestampTriggers($info, $tableName, $customGroupTables, 'entity_id'); |
| 3267 | } |
| 3268 | |
| 3269 | // Update phone table to populate phone_numeric field |
| 3270 | if (!$tableName || $tableName == 'civicrm_phone') { |
| 3271 | // Define stored sql function needed for phones |
| 3272 | CRM_Core_DAO::executeQuery(self::DROP_STRIP_FUNCTION_43); |
| 3273 | CRM_Core_DAO::executeQuery(self::CREATE_STRIP_FUNCTION_43); |
| 3274 | $info[] = array( |
| 3275 | 'table' => array('civicrm_phone'), |
| 3276 | 'when' => 'BEFORE', |
| 3277 | 'event' => array('INSERT', 'UPDATE'), |
| 3278 | 'sql' => "\nSET NEW.phone_numeric = civicrm_strip_non_numeric(NEW.phone);\n", |
| 3279 | ); |
| 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | /** |
| 3284 | * Check if contact is being used in civicrm_domain based on $contactId. |
| 3285 | * |
| 3286 | * @param int $contactId |
| 3287 | * Contact id. |
| 3288 | * |
| 3289 | * @return bool |
| 3290 | * true if present else false. |
| 3291 | */ |
| 3292 | public static function checkDomainContact($contactId) { |
| 3293 | if (!$contactId) { |
| 3294 | return FALSE; |
| 3295 | } |
| 3296 | $domainId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $contactId, 'id', 'contact_id'); |
| 3297 | |
| 3298 | if ($domainId) { |
| 3299 | return TRUE; |
| 3300 | } |
| 3301 | else { |
| 3302 | return FALSE; |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | /** |
| 3307 | * Get options for a given contact field. |
| 3308 | * |
| 3309 | * @see CRM_Core_DAO::buildOptions |
| 3310 | * |
| 3311 | * TODO: Should we always assume chainselect? What fn should be responsible for controlling that flow? |
| 3312 | * TODO: In context of chainselect, what to return if e.g. a country has no states? |
| 3313 | * |
| 3314 | * @param string $fieldName |
| 3315 | * @param string $context |
| 3316 | * @see CRM_Core_DAO::buildOptionsContext |
| 3317 | * @param array $props |
| 3318 | * whatever is known about this dao object. |
| 3319 | * |
| 3320 | * @return array|bool |
| 3321 | */ |
| 3322 | public static function buildOptions($fieldName, $context = NULL, $props = array()) { |
| 3323 | $params = array(); |
| 3324 | // Special logic for fields whose options depend on context or properties |
| 3325 | switch ($fieldName) { |
| 3326 | case 'contact_sub_type': |
| 3327 | if (!empty($props['contact_type'])) { |
| 3328 | $params['condition'] = "parent_id = (SELECT id FROM civicrm_contact_type WHERE name='{$props['contact_type']}')"; |
| 3329 | } |
| 3330 | break; |
| 3331 | |
| 3332 | case 'contact_type': |
| 3333 | if ($context == 'search') { |
| 3334 | // CRM-15495 - EntityRef filters and basic search forms expect this format |
| 3335 | // FIXME: Search builder does not |
| 3336 | return CRM_Contact_BAO_ContactType::getSelectElements(); |
| 3337 | } |
| 3338 | break; |
| 3339 | |
| 3340 | // The contact api supports some related entities so we'll honor that by fetching their options |
| 3341 | case 'group_id': |
| 3342 | case 'group': |
| 3343 | return CRM_Contact_BAO_GroupContact::buildOptions('group_id', $context, $props); |
| 3344 | |
| 3345 | case 'tag_id': |
| 3346 | case 'tag': |
| 3347 | $props['entity_table'] = 'civicrm_contact'; |
| 3348 | return CRM_Core_BAO_EntityTag::buildOptions('tag_id', $context, $props); |
| 3349 | |
| 3350 | } |
| 3351 | return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); |
| 3352 | } |
| 3353 | |
| 3354 | /** |
| 3355 | * Delete a contact-related object that has an 'is_primary' field. |
| 3356 | * |
| 3357 | * Ensures that is_primary gets assigned to another object if available |
| 3358 | * Also calls pre/post hooks |
| 3359 | * |
| 3360 | * @var object $type |
| 3361 | * @var int $id |
| 3362 | * @return bool |
| 3363 | */ |
| 3364 | public static function deleteObjectWithPrimary($type, $id) { |
| 3365 | if (!$id || !is_numeric($id)) { |
| 3366 | return FALSE; |
| 3367 | } |
| 3368 | $daoName = "CRM_Core_DAO_$type"; |
| 3369 | $obj = new $daoName(); |
| 3370 | $obj->id = $id; |
| 3371 | $obj->find(); |
| 3372 | if ($obj->fetch()) { |
| 3373 | CRM_Utils_Hook::pre('delete', $type, $id, CRM_Core_DAO::$_nullArray); |
| 3374 | $contactId = $obj->contact_id; |
| 3375 | $obj->delete(); |
| 3376 | } |
| 3377 | else { |
| 3378 | return FALSE; |
| 3379 | } |
| 3380 | // is_primary is only relavent if this field belongs to a contact |
| 3381 | if ($contactId) { |
| 3382 | $dao = new $daoName(); |
| 3383 | $dao->contact_id = $contactId; |
| 3384 | $dao->is_primary = 1; |
| 3385 | // Pick another record to be primary (if one isn't already) |
| 3386 | if (!$dao->find(TRUE)) { |
| 3387 | $dao->is_primary = 0; |
| 3388 | $dao->find(); |
| 3389 | if ($dao->fetch()) { |
| 3390 | $dao->is_primary = 1; |
| 3391 | $dao->save(); |
| 3392 | } |
| 3393 | } |
| 3394 | $dao->free(); |
| 3395 | } |
| 3396 | CRM_Utils_Hook::post('delete', $type, $id, $obj); |
| 3397 | $obj->free(); |
| 3398 | return TRUE; |
| 3399 | } |
| 3400 | |
| 3401 | } |