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