| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | * Stores all constants and pseudo constants for CRM application. |
| 31 | * |
| 32 | * examples of constants are "Contact Type" which will always be either |
| 33 | * 'Individual', 'Household', 'Organization'. |
| 34 | * |
| 35 | * pseudo constants are entities from the database whose values rarely |
| 36 | * change. examples are list of countries, states, location types, |
| 37 | * relationship types. |
| 38 | * |
| 39 | * currently we're getting the data from the underlying database. this |
| 40 | * will be reworked to use caching. |
| 41 | * |
| 42 | * Note: All pseudoconstants should be uninitialized or default to NULL. |
| 43 | * This provides greater consistency/predictability after flushing. |
| 44 | * |
| 45 | * @package CRM |
| 46 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 47 | */ |
| 48 | class CRM_Core_PseudoConstant { |
| 49 | |
| 50 | /** |
| 51 | * Static cache for pseudoconstant arrays. |
| 52 | * @var array |
| 53 | */ |
| 54 | private static $cache; |
| 55 | |
| 56 | /** |
| 57 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 58 | * |
| 59 | * activity type |
| 60 | * @var array |
| 61 | */ |
| 62 | private static $activityType; |
| 63 | |
| 64 | /** |
| 65 | * States, provinces |
| 66 | * @var array |
| 67 | */ |
| 68 | private static $stateProvince; |
| 69 | |
| 70 | /** |
| 71 | * Counties. |
| 72 | * @var array |
| 73 | */ |
| 74 | private static $county; |
| 75 | |
| 76 | /** |
| 77 | * States/provinces abbreviations |
| 78 | * @var array |
| 79 | */ |
| 80 | private static $stateProvinceAbbreviation = array(); |
| 81 | |
| 82 | /** |
| 83 | * Country. |
| 84 | * @var array |
| 85 | */ |
| 86 | private static $country; |
| 87 | |
| 88 | /** |
| 89 | * CountryIsoCode. |
| 90 | * @var array |
| 91 | */ |
| 92 | private static $countryIsoCode; |
| 93 | |
| 94 | /** |
| 95 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 96 | * |
| 97 | * group |
| 98 | * @var array |
| 99 | */ |
| 100 | private static $group; |
| 101 | |
| 102 | /** |
| 103 | * RelationshipType |
| 104 | * @var array |
| 105 | */ |
| 106 | private static $relationshipType; |
| 107 | |
| 108 | /** |
| 109 | * Civicrm groups that are not smart groups |
| 110 | * @var array |
| 111 | */ |
| 112 | private static $staticGroup; |
| 113 | |
| 114 | /** |
| 115 | * Currency codes |
| 116 | * @var array |
| 117 | */ |
| 118 | private static $currencyCode; |
| 119 | |
| 120 | /** |
| 121 | * Payment processor |
| 122 | * @var array |
| 123 | */ |
| 124 | private static $paymentProcessor; |
| 125 | |
| 126 | /** |
| 127 | * Payment processor types |
| 128 | * @var array |
| 129 | */ |
| 130 | private static $paymentProcessorType; |
| 131 | |
| 132 | /** |
| 133 | * World Region |
| 134 | * @var array |
| 135 | */ |
| 136 | private static $worldRegions; |
| 137 | |
| 138 | /** |
| 139 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 140 | * |
| 141 | * activity status |
| 142 | * @var array |
| 143 | */ |
| 144 | private static $activityStatus; |
| 145 | |
| 146 | /** |
| 147 | * Visibility |
| 148 | * @var array |
| 149 | */ |
| 150 | private static $visibility; |
| 151 | |
| 152 | /** |
| 153 | * Greetings |
| 154 | * @var array |
| 155 | */ |
| 156 | private static $greeting; |
| 157 | |
| 158 | /** |
| 159 | * Extensions of type module |
| 160 | * @var array |
| 161 | */ |
| 162 | private static $extensions; |
| 163 | |
| 164 | /** |
| 165 | * Financial Account Type |
| 166 | * @var array |
| 167 | */ |
| 168 | private static $accountOptionValues; |
| 169 | |
| 170 | /** |
| 171 | * Low-level option getter, rarely accessed directly. |
| 172 | * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions() |
| 173 | * @see https://docs.civicrm.org/dev/en/latest/framework/pseudoconstant/ |
| 174 | * |
| 175 | * NOTE: If someone undertakes a refactoring of this, please consider the use-case of |
| 176 | * the Setting.getoptions API. There is no DAO/field, but it would be nice to use the |
| 177 | * same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling |
| 178 | * between $field lookup and the $pseudoconstant evaluation. |
| 179 | * |
| 180 | * @param string $daoName |
| 181 | * @param string $fieldName |
| 182 | * @param array $params |
| 183 | * - name string name of the option group |
| 184 | * - flip boolean results are return in id => label format if false |
| 185 | * if true, the results are reversed |
| 186 | * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value) |
| 187 | * - localize boolean if true, localize the results before returning |
| 188 | * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND' |
| 189 | * - keyColumn string the column to use for 'id' |
| 190 | * - labelColumn string the column to use for 'label' |
| 191 | * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn |
| 192 | * - onlyActive boolean return only the action option values |
| 193 | * - fresh boolean ignore cache entries and go back to DB |
| 194 | * @param string $context : Context string |
| 195 | * @see CRM_Core_DAO::buildOptionsContext |
| 196 | * |
| 197 | * @return array|bool |
| 198 | * array on success, FALSE on error. |
| 199 | * |
| 200 | */ |
| 201 | public static function get($daoName, $fieldName, $params = array(), $context = NULL) { |
| 202 | CRM_Core_DAO::buildOptionsContext($context); |
| 203 | $flip = !empty($params['flip']); |
| 204 | // Merge params with defaults |
| 205 | $params += array( |
| 206 | 'grouping' => FALSE, |
| 207 | 'localize' => FALSE, |
| 208 | 'onlyActive' => ($context == 'validate' || $context == 'get') ? FALSE : TRUE, |
| 209 | 'fresh' => FALSE, |
| 210 | 'context' => $context, |
| 211 | ); |
| 212 | $entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getCanonicalClassName($daoName)); |
| 213 | |
| 214 | // Custom fields are not in the schema |
| 215 | if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) { |
| 216 | $customField = new CRM_Core_BAO_CustomField(); |
| 217 | $customField->id = (int) substr($fieldName, 7); |
| 218 | $options = $customField->getOptions($context); |
| 219 | if ($options && $flip) { |
| 220 | $options = array_flip($options); |
| 221 | } |
| 222 | $customField->free(); |
| 223 | return $options; |
| 224 | } |
| 225 | |
| 226 | // Core field: load schema |
| 227 | $dao = new $daoName(); |
| 228 | $fieldSpec = $dao->getFieldSpec($fieldName); |
| 229 | $dao->free(); |
| 230 | |
| 231 | // Ensure we have the canonical name for this field |
| 232 | $fieldName = CRM_Utils_Array::value('name', $fieldSpec, $fieldName); |
| 233 | |
| 234 | // Return false if field doesn't exist. |
| 235 | if (empty($fieldSpec)) { |
| 236 | return FALSE; |
| 237 | } |
| 238 | |
| 239 | elseif (!empty($fieldSpec['pseudoconstant'])) { |
| 240 | $pseudoconstant = $fieldSpec['pseudoconstant']; |
| 241 | |
| 242 | // if callback is specified.. |
| 243 | if (!empty($pseudoconstant['callback'])) { |
| 244 | $fieldOptions = call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']), $context); |
| 245 | //CRM-18223: Allow additions to field options via hook. |
| 246 | CRM_Utils_Hook::fieldOptions($entity, $fieldName, $fieldOptions, $params); |
| 247 | return $fieldOptions; |
| 248 | } |
| 249 | |
| 250 | // Merge params with schema defaults |
| 251 | $params += array( |
| 252 | 'condition' => CRM_Utils_Array::value('condition', $pseudoconstant, array()), |
| 253 | 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant), |
| 254 | 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant), |
| 255 | ); |
| 256 | |
| 257 | if ($context == 'abbreviate') { |
| 258 | switch ($fieldName) { |
| 259 | case 'state_province_id': |
| 260 | $params['labelColumn'] = 'abbreviation'; |
| 261 | break; |
| 262 | |
| 263 | case 'country_id': |
| 264 | $params['labelColumn'] = 'iso_code'; |
| 265 | break; |
| 266 | |
| 267 | default: |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Fetch option group from option_value table |
| 272 | if (!empty($pseudoconstant['optionGroupName'])) { |
| 273 | if ($context == 'validate') { |
| 274 | $params['labelColumn'] = 'name'; |
| 275 | } |
| 276 | if ($context == 'match') { |
| 277 | $params['keyColumn'] = 'name'; |
| 278 | } |
| 279 | // Call our generic fn for retrieving from the option_value table |
| 280 | $options = CRM_Core_OptionGroup::values( |
| 281 | $pseudoconstant['optionGroupName'], |
| 282 | $flip, |
| 283 | $params['grouping'], |
| 284 | $params['localize'], |
| 285 | $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL, |
| 286 | $params['labelColumn'] ? $params['labelColumn'] : 'label', |
| 287 | $params['onlyActive'], |
| 288 | $params['fresh'], |
| 289 | $params['keyColumn'] ? $params['keyColumn'] : 'value', |
| 290 | !empty($params['orderColumn']) ? $params['orderColumn'] : 'weight' |
| 291 | ); |
| 292 | CRM_Utils_Hook::fieldOptions($entity, $fieldName, $options, $params); |
| 293 | return $options; |
| 294 | } |
| 295 | |
| 296 | // Fetch options from other tables |
| 297 | if (!empty($pseudoconstant['table'])) { |
| 298 | // Normalize params so the serialized cache string will be consistent. |
| 299 | CRM_Utils_Array::remove($params, 'flip', 'fresh'); |
| 300 | ksort($params); |
| 301 | $cacheKey = $daoName . $fieldName . serialize($params); |
| 302 | |
| 303 | // Retrieve cached options |
| 304 | if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) { |
| 305 | $output = self::$cache[$cacheKey]; |
| 306 | } |
| 307 | else { |
| 308 | $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']); |
| 309 | if (!class_exists($daoName)) { |
| 310 | return FALSE; |
| 311 | } |
| 312 | // Get list of fields for the option table |
| 313 | $dao = new $daoName(); |
| 314 | $availableFields = array_keys($dao->fieldKeys()); |
| 315 | $dao->free(); |
| 316 | |
| 317 | $select = "SELECT %1 AS id, %2 AS label"; |
| 318 | $from = "FROM %3"; |
| 319 | $wheres = array(); |
| 320 | $order = "ORDER BY %2"; |
| 321 | |
| 322 | // Use machine name in certain contexts |
| 323 | if ($context == 'validate' || $context == 'match') { |
| 324 | $nameField = $context == 'validate' ? 'labelColumn' : 'keyColumn'; |
| 325 | if (!empty($pseudoconstant['nameColumn'])) { |
| 326 | $params[$nameField] = $pseudoconstant['nameColumn']; |
| 327 | } |
| 328 | elseif (in_array('name', $availableFields)) { |
| 329 | $params[$nameField] = 'name'; |
| 330 | } |
| 331 | } |
| 332 | // Condition param can be passed as an sql clause string or an array of clauses |
| 333 | if (!empty($params['condition'])) { |
| 334 | $wheres[] = implode(' AND ', (array) $params['condition']); |
| 335 | } |
| 336 | // onlyActive param will automatically filter on common flags |
| 337 | if (!empty($params['onlyActive'])) { |
| 338 | foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0) as $flag => $val) { |
| 339 | if (in_array($flag, $availableFields)) { |
| 340 | $wheres[] = "$flag = $val"; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | // Filter domain specific options |
| 345 | if (in_array('domain_id', $availableFields)) { |
| 346 | $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID(); |
| 347 | } |
| 348 | $queryParams = array( |
| 349 | 1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), |
| 350 | 2 => array($params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), |
| 351 | 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES), |
| 352 | ); |
| 353 | // Add orderColumn param |
| 354 | if (!empty($params['orderColumn'])) { |
| 355 | $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES); |
| 356 | $order = "ORDER BY %4"; |
| 357 | } |
| 358 | // Support no sorting if $params[orderColumn] is FALSE |
| 359 | elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) { |
| 360 | $order = ''; |
| 361 | } |
| 362 | // Default to 'weight' if that column exists |
| 363 | elseif (in_array('weight', $availableFields)) { |
| 364 | $order = "ORDER BY weight"; |
| 365 | } |
| 366 | |
| 367 | $output = array(); |
| 368 | $query = "$select $from"; |
| 369 | if ($wheres) { |
| 370 | $query .= " WHERE " . implode($wheres, ' AND '); |
| 371 | } |
| 372 | $query .= ' ' . $order; |
| 373 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
| 374 | while ($dao->fetch()) { |
| 375 | $output[$dao->id] = $dao->label; |
| 376 | } |
| 377 | $dao->free(); |
| 378 | // Localize results |
| 379 | if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') { |
| 380 | $I18nParams = array(); |
| 381 | if ($pseudoconstant['table'] == 'civicrm_country') { |
| 382 | $I18nParams['context'] = 'country'; |
| 383 | } |
| 384 | if ($pseudoconstant['table'] == 'civicrm_state_province') { |
| 385 | $I18nParams['context'] = 'province'; |
| 386 | } |
| 387 | $i18n = CRM_Core_I18n::singleton(); |
| 388 | $i18n->localizeArray($output, $I18nParams); |
| 389 | // Maintain sort by label |
| 390 | if ($order == "ORDER BY %2") { |
| 391 | CRM_Utils_Array::asort($output); |
| 392 | } |
| 393 | } |
| 394 | CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params); |
| 395 | self::$cache[$cacheKey] = $output; |
| 396 | } |
| 397 | return $flip ? array_flip($output) : $output; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Return "Yes" and "No" for boolean fields |
| 402 | elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) { |
| 403 | $output = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean(); |
| 404 | CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params); |
| 405 | return $flip ? array_flip($output) : $output; |
| 406 | } |
| 407 | // If we're still here, it's an error. Return FALSE. |
| 408 | return FALSE; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Fetch the translated label for a field given its key. |
| 413 | * |
| 414 | * @param string $baoName |
| 415 | * @param string $fieldName |
| 416 | * @param string|Int $key |
| 417 | * |
| 418 | * TODO: Accept multivalued input? |
| 419 | * |
| 420 | * @return bool|null|string |
| 421 | * FALSE if the given field has no associated option list |
| 422 | * NULL if the given key has no corresponding option |
| 423 | * String if label is found |
| 424 | */ |
| 425 | public static function getLabel($baoName, $fieldName, $key) { |
| 426 | $values = $baoName::buildOptions($fieldName, 'get'); |
| 427 | if ($values === FALSE) { |
| 428 | return FALSE; |
| 429 | } |
| 430 | return CRM_Utils_Array::value($key, $values); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Fetch the machine name for a field given its key. |
| 435 | * |
| 436 | * @param string $baoName |
| 437 | * @param string $fieldName |
| 438 | * @param string|Int $key |
| 439 | * |
| 440 | * @return bool|null|string |
| 441 | * FALSE if the given field has no associated option list |
| 442 | * NULL if the given key has no corresponding option |
| 443 | * String if label is found |
| 444 | */ |
| 445 | public static function getName($baoName, $fieldName, $key) { |
| 446 | $values = $baoName::buildOptions($fieldName, 'validate'); |
| 447 | if ($values === FALSE) { |
| 448 | return FALSE; |
| 449 | } |
| 450 | return CRM_Utils_Array::value($key, $values); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Fetch the key for a field option given its name. |
| 455 | * |
| 456 | * @param string $baoName |
| 457 | * @param string $fieldName |
| 458 | * @param string|Int $value |
| 459 | * |
| 460 | * @return bool|null|string|int |
| 461 | * FALSE if the given field has no associated option list |
| 462 | * NULL if the given key has no corresponding option |
| 463 | * String|Number if key is found |
| 464 | */ |
| 465 | public static function getKey($baoName, $fieldName, $value) { |
| 466 | $values = $baoName::buildOptions($fieldName, 'validate'); |
| 467 | if ($values === FALSE) { |
| 468 | return FALSE; |
| 469 | } |
| 470 | return CRM_Utils_Array::key($value, $values); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Lookup the admin page at which a field's option list can be edited |
| 475 | * @param $fieldSpec |
| 476 | * @return string|null |
| 477 | */ |
| 478 | public static function getOptionEditUrl($fieldSpec) { |
| 479 | // If it's an option group, that's easy |
| 480 | if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) { |
| 481 | return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName']; |
| 482 | } |
| 483 | // For everything else... |
| 484 | elseif (!empty($fieldSpec['pseudoconstant']['table'])) { |
| 485 | $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']); |
| 486 | if (!$daoName) { |
| 487 | return NULL; |
| 488 | } |
| 489 | // We don't have good mapping so have to do a bit of guesswork from the menu |
| 490 | list(, $parent, , $child) = explode('_', $daoName); |
| 491 | $sql = "SELECT path FROM civicrm_menu |
| 492 | WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%' |
| 493 | ORDER BY page_callback |
| 494 | LIMIT 1"; |
| 495 | return CRM_Core_Dao::singleValueQuery($sql); |
| 496 | } |
| 497 | return NULL; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * DEPRECATED generic populate method. |
| 502 | * All pseudoconstant functions that use this method are also @deprecated |
| 503 | * |
| 504 | * The static array $var is populated from the db |
| 505 | * using the <b>$name DAO</b>. |
| 506 | * |
| 507 | * Note: any database errors will be trapped by the DAO. |
| 508 | * |
| 509 | * @param array $var |
| 510 | * The associative array we will fill. |
| 511 | * @param string $name |
| 512 | * The name of the DAO. |
| 513 | * @param bool $all |
| 514 | * Get all objects. default is to get only active ones. |
| 515 | * @param string $retrieve |
| 516 | * The field that we are interested in (normally name, differs in some objects). |
| 517 | * @param string $filter |
| 518 | * The field that we want to filter the result set with. |
| 519 | * @param string $condition |
| 520 | * The condition that gets passed to the final query as the WHERE clause. |
| 521 | * |
| 522 | * @param bool $orderby |
| 523 | * @param string $key |
| 524 | * @param bool $force |
| 525 | * |
| 526 | * @return array |
| 527 | */ |
| 528 | public static function populate( |
| 529 | &$var, |
| 530 | $name, |
| 531 | $all = FALSE, |
| 532 | $retrieve = 'name', |
| 533 | $filter = 'is_active', |
| 534 | $condition = NULL, |
| 535 | $orderby = NULL, |
| 536 | $key = 'id', |
| 537 | $force = NULL |
| 538 | ) { |
| 539 | $cacheKey = CRM_Core_BAO_Cache::cleanKey("CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}"); |
| 540 | $cache = CRM_Utils_Cache::singleton(); |
| 541 | $var = $cache->get($cacheKey); |
| 542 | if ($var !== NULL && empty($force)) { |
| 543 | return $var; |
| 544 | } |
| 545 | |
| 546 | /* @var CRM_Core_DAO $object */ |
| 547 | $object = new $name(); |
| 548 | |
| 549 | $object->selectAdd(); |
| 550 | $object->selectAdd("$key, $retrieve"); |
| 551 | if ($condition) { |
| 552 | $object->whereAdd($condition); |
| 553 | } |
| 554 | |
| 555 | if (!$orderby) { |
| 556 | $object->orderBy($retrieve); |
| 557 | } |
| 558 | else { |
| 559 | $object->orderBy($orderby); |
| 560 | } |
| 561 | |
| 562 | if (!$all) { |
| 563 | $object->$filter = 1; |
| 564 | $aclClauses = array_filter($name::getSelectWhereClause()); |
| 565 | foreach ($aclClauses as $clause) { |
| 566 | $object->whereAdd($clause); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | $object->find(); |
| 571 | $var = array(); |
| 572 | while ($object->fetch()) { |
| 573 | $var[$object->$key] = $object->$retrieve; |
| 574 | } |
| 575 | |
| 576 | $cache->set($cacheKey, $var); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Flush given pseudoconstant so it can be reread from db. |
| 581 | * nex time it's requested. |
| 582 | * |
| 583 | * @param bool|string $name pseudoconstant to be flushed |
| 584 | */ |
| 585 | public static function flush($name = 'cache') { |
| 586 | if (isset(self::$$name)) { |
| 587 | self::$$name = NULL; |
| 588 | } |
| 589 | if ($name == 'cache') { |
| 590 | CRM_Core_OptionGroup::flushAll(); |
| 591 | if (isset(\Civi::$statics[__CLASS__])) { |
| 592 | unset(\Civi::$statics[__CLASS__]); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 599 | * |
| 600 | * Get all Activty types. |
| 601 | * |
| 602 | * The static array activityType is returned |
| 603 | * |
| 604 | * |
| 605 | * @return array |
| 606 | * array reference of all activity types. |
| 607 | */ |
| 608 | public static function &activityType() { |
| 609 | $args = func_get_args(); |
| 610 | $all = CRM_Utils_Array::value(0, $args, TRUE); |
| 611 | $includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE); |
| 612 | $reset = CRM_Utils_Array::value(2, $args, FALSE); |
| 613 | $returnColumn = CRM_Utils_Array::value(3, $args, 'label'); |
| 614 | $includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE); |
| 615 | $onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE); |
| 616 | $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities; |
| 617 | $index .= '_' . (int) $includeCampaignActivities; |
| 618 | $index .= '_' . (int) $onlyComponentActivities; |
| 619 | |
| 620 | if (NULL === self::$activityType) { |
| 621 | self::$activityType = array(); |
| 622 | } |
| 623 | |
| 624 | if (!isset(self::$activityType[$index]) || $reset) { |
| 625 | $condition = NULL; |
| 626 | if (!$all) { |
| 627 | $condition = 'AND filter = 0'; |
| 628 | } |
| 629 | $componentClause = " v.component_id IS NULL"; |
| 630 | if ($onlyComponentActivities) { |
| 631 | $componentClause = " v.component_id IS NOT NULL"; |
| 632 | } |
| 633 | |
| 634 | $componentIds = array(); |
| 635 | $compInfo = CRM_Core_Component::getEnabledComponents(); |
| 636 | |
| 637 | // build filter for listing activity types only if their |
| 638 | // respective components are enabled |
| 639 | foreach ($compInfo as $compName => $compObj) { |
| 640 | if ($compName == 'CiviCase') { |
| 641 | if ($includeCaseActivities) { |
| 642 | $componentIds[] = $compObj->componentID; |
| 643 | } |
| 644 | } |
| 645 | elseif ($compName == 'CiviCampaign') { |
| 646 | if ($includeCampaignActivities) { |
| 647 | $componentIds[] = $compObj->componentID; |
| 648 | } |
| 649 | } |
| 650 | else { |
| 651 | $componentIds[] = $compObj->componentID; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if (count($componentIds)) { |
| 656 | $componentIds = implode(',', $componentIds); |
| 657 | $componentClause = " ($componentClause OR v.component_id IN ($componentIds))"; |
| 658 | if ($onlyComponentActivities) { |
| 659 | $componentClause = " ( v.component_id IN ($componentIds ) )"; |
| 660 | } |
| 661 | } |
| 662 | $condition = $condition . ' AND ' . $componentClause; |
| 663 | |
| 664 | self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn); |
| 665 | } |
| 666 | return self::$activityType[$index]; |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Get all the State/Province from database. |
| 671 | * |
| 672 | * The static array stateProvince is returned, and if it's |
| 673 | * called the first time, the <b>State Province DAO</b> is used |
| 674 | * to get all the States. |
| 675 | * |
| 676 | * Note: any database errors will be trapped by the DAO. |
| 677 | * |
| 678 | * |
| 679 | * @param bool|int $id - Optional id to return |
| 680 | * |
| 681 | * @param bool $limit |
| 682 | * |
| 683 | * @return array |
| 684 | * array reference of all State/Provinces. |
| 685 | */ |
| 686 | public static function &stateProvince($id = FALSE, $limit = TRUE) { |
| 687 | if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) { |
| 688 | $whereClause = FALSE; |
| 689 | if ($limit) { |
| 690 | $countryIsoCodes = self::countryIsoCode(); |
| 691 | $limitCodes = CRM_Core_BAO_Country::provinceLimit(); |
| 692 | $limitIds = array(); |
| 693 | foreach ($limitCodes as $code) { |
| 694 | $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code)); |
| 695 | } |
| 696 | if (!empty($limitIds)) { |
| 697 | $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')'; |
| 698 | } |
| 699 | else { |
| 700 | $whereClause = FALSE; |
| 701 | } |
| 702 | } |
| 703 | self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause); |
| 704 | |
| 705 | // localise the province names if in an non-en_US locale |
| 706 | $tsLocale = CRM_Core_I18n::getLocale(); |
| 707 | if ($tsLocale != '' and $tsLocale != 'en_US') { |
| 708 | $i18n = CRM_Core_I18n::singleton(); |
| 709 | $i18n->localizeArray(self::$stateProvince, array( |
| 710 | 'context' => 'province', |
| 711 | )); |
| 712 | self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince); |
| 713 | } |
| 714 | } |
| 715 | if ($id) { |
| 716 | if (array_key_exists($id, self::$stateProvince)) { |
| 717 | return self::$stateProvince[$id]; |
| 718 | } |
| 719 | else { |
| 720 | $result = NULL; |
| 721 | return $result; |
| 722 | } |
| 723 | } |
| 724 | return self::$stateProvince; |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Get all the State/Province abbreviations from the database. |
| 729 | * |
| 730 | * Same as above, except gets the abbreviations instead of the names. |
| 731 | * |
| 732 | * |
| 733 | * @param bool|int $id - Optional id to return |
| 734 | * |
| 735 | * @param bool $limit |
| 736 | * |
| 737 | * @return array |
| 738 | * array reference of all State/Province abbreviations. |
| 739 | */ |
| 740 | public static function stateProvinceAbbreviation($id = FALSE, $limit = TRUE) { |
| 741 | if ($id && is_numeric($id)) { |
| 742 | if (!array_key_exists($id, (array) self::$stateProvinceAbbreviation)) { |
| 743 | $query = "SELECT abbreviation |
| 744 | FROM civicrm_state_province |
| 745 | WHERE id = %1"; |
| 746 | $params = array( |
| 747 | 1 => array( |
| 748 | $id, |
| 749 | 'Integer', |
| 750 | ), |
| 751 | ); |
| 752 | self::$stateProvinceAbbreviation[$id] = CRM_Core_DAO::singleValueQuery($query, $params); |
| 753 | } |
| 754 | return self::$stateProvinceAbbreviation[$id]; |
| 755 | } |
| 756 | else { |
| 757 | $whereClause = FALSE; |
| 758 | |
| 759 | if ($limit) { |
| 760 | $countryIsoCodes = self::countryIsoCode(); |
| 761 | $limitCodes = CRM_Core_BAO_Country::provinceLimit(); |
| 762 | $limitIds = array(); |
| 763 | foreach ($limitCodes as $code) { |
| 764 | $tmpArray = array_keys($countryIsoCodes, $code); |
| 765 | |
| 766 | if (!empty($tmpArray)) { |
| 767 | $limitIds[] = array_shift($tmpArray); |
| 768 | } |
| 769 | } |
| 770 | if (!empty($limitIds)) { |
| 771 | $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')'; |
| 772 | } |
| 773 | } |
| 774 | self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause); |
| 775 | } |
| 776 | |
| 777 | return self::$stateProvinceAbbreviation; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Get all the State/Province abbreviations from the database for the specified country. |
| 782 | * |
| 783 | * @param int $countryID |
| 784 | * |
| 785 | * @return array |
| 786 | * array of all State/Province abbreviations for the given country. |
| 787 | */ |
| 788 | public static function stateProvinceAbbreviationForCountry($countryID) { |
| 789 | if (!isset(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID])) { |
| 790 | \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID] = array(); |
| 791 | } |
| 792 | self::populate(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID], 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation'); |
| 793 | return \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID]; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Get all the State/Province abbreviations from the database for the default country. |
| 798 | * |
| 799 | * @return array |
| 800 | * array of all State/Province abbreviations for the given country. |
| 801 | */ |
| 802 | public static function stateProvinceAbbreviationForDefaultCountry() { |
| 803 | return CRM_Core_PseudoConstant::stateProvinceAbbreviationForCountry(Civi::settings()->get('defaultContactCountry')); |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * Get all the countries from database. |
| 808 | * |
| 809 | * The static array country is returned, and if it's |
| 810 | * called the first time, the <b>Country DAO</b> is used |
| 811 | * to get all the countries. |
| 812 | * |
| 813 | * Note: any database errors will be trapped by the DAO. |
| 814 | * |
| 815 | * |
| 816 | * @param bool|int $id - Optional id to return |
| 817 | * |
| 818 | * @param bool $applyLimit |
| 819 | * |
| 820 | * @return array|null |
| 821 | * array reference of all countries. |
| 822 | */ |
| 823 | public static function country($id = FALSE, $applyLimit = TRUE) { |
| 824 | if (($id && !CRM_Utils_Array::value($id, self::$country)) || !self::$country || !$id) { |
| 825 | |
| 826 | $config = CRM_Core_Config::singleton(); |
| 827 | $limitCodes = array(); |
| 828 | |
| 829 | if ($applyLimit) { |
| 830 | // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT |
| 831 | // (ensuring it's a subset of the legal values) |
| 832 | // K/P: We need to fix this, i dont think it works with new setting files |
| 833 | $limitCodes = CRM_Core_BAO_Country::countryLimit(); |
| 834 | if (!is_array($limitCodes)) { |
| 835 | $limitCodes = array( |
| 836 | $config->countryLimit => 1, |
| 837 | ); |
| 838 | } |
| 839 | |
| 840 | $limitCodes = array_intersect(self::countryIsoCode(), $limitCodes); |
| 841 | } |
| 842 | |
| 843 | if (count($limitCodes)) { |
| 844 | $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')"; |
| 845 | } |
| 846 | else { |
| 847 | $whereClause = NULL; |
| 848 | } |
| 849 | |
| 850 | self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause); |
| 851 | |
| 852 | // if default country is set, percolate it to the top |
| 853 | if ($config->defaultContactCountry()) { |
| 854 | $countryIsoCodes = self::countryIsoCode(); |
| 855 | $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes); |
| 856 | if ($defaultID !== FALSE) { |
| 857 | $default[$defaultID] = CRM_Utils_Array::value($defaultID, self::$country); |
| 858 | self::$country = $default + self::$country; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | // localise the country names if in an non-en_US locale |
| 863 | $tsLocale = CRM_Core_I18n::getLocale(); |
| 864 | if ($tsLocale != '' and $tsLocale != 'en_US') { |
| 865 | $i18n = CRM_Core_I18n::singleton(); |
| 866 | $i18n->localizeArray(self::$country, array( |
| 867 | 'context' => 'country', |
| 868 | )); |
| 869 | self::$country = CRM_Utils_Array::asort(self::$country); |
| 870 | } |
| 871 | } |
| 872 | if ($id) { |
| 873 | if (array_key_exists($id, self::$country)) { |
| 874 | return self::$country[$id]; |
| 875 | } |
| 876 | else { |
| 877 | return NULL; |
| 878 | } |
| 879 | } |
| 880 | return self::$country; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * Get all the country ISO Code abbreviations from the database. |
| 885 | * |
| 886 | * The static array countryIsoCode is returned, and if it's |
| 887 | * called the first time, the <b>Country DAO</b> is used |
| 888 | * to get all the countries' ISO codes. |
| 889 | * |
| 890 | * Note: any database errors will be trapped by the DAO. |
| 891 | * |
| 892 | * |
| 893 | * @param bool $id |
| 894 | * |
| 895 | * @return array |
| 896 | * array reference of all country ISO codes. |
| 897 | */ |
| 898 | public static function &countryIsoCode($id = FALSE) { |
| 899 | if (!self::$countryIsoCode) { |
| 900 | self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); |
| 901 | } |
| 902 | if ($id) { |
| 903 | if (array_key_exists($id, self::$countryIsoCode)) { |
| 904 | return self::$countryIsoCode[$id]; |
| 905 | } |
| 906 | else { |
| 907 | return CRM_Core_DAO::$_nullObject; |
| 908 | } |
| 909 | } |
| 910 | return self::$countryIsoCode; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 915 | * |
| 916 | * Get all groups from database |
| 917 | * |
| 918 | * The static array group is returned, and if it's |
| 919 | * called the first time, the <b>Group DAO</b> is used |
| 920 | * to get all the groups. |
| 921 | * |
| 922 | * Note: any database errors will be trapped by the DAO. |
| 923 | * |
| 924 | * @param string $groupType |
| 925 | * Type of group(Access/Mailing). |
| 926 | * @param bool $excludeHidden |
| 927 | * Exclude hidden groups. |
| 928 | * |
| 929 | * |
| 930 | * @return array |
| 931 | * array reference of all groups. |
| 932 | */ |
| 933 | public static function allGroup($groupType = NULL, $excludeHidden = TRUE) { |
| 934 | if ($groupType === 'validate') { |
| 935 | // validate gets passed through from getoptions. Handle in the deprecated |
| 936 | // fn rather than change the new pattern. |
| 937 | $groupType = NULL; |
| 938 | } |
| 939 | $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden); |
| 940 | $groupKey = ($groupType ? $groupType : 'null') . !empty($excludeHidden); |
| 941 | |
| 942 | if (!isset(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey])) { |
| 943 | self::populate(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition); |
| 944 | } |
| 945 | return Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey]; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Get all permissioned groups from database. |
| 950 | * |
| 951 | * The static array group is returned, and if it's |
| 952 | * called the first time, the <b>Group DAO</b> is used |
| 953 | * to get all the groups. |
| 954 | * |
| 955 | * Note: any database errors will be trapped by the DAO. |
| 956 | * |
| 957 | * @param string $groupType |
| 958 | * Type of group(Access/Mailing). |
| 959 | * @param bool $excludeHidden |
| 960 | * Exclude hidden groups. |
| 961 | * |
| 962 | * |
| 963 | * @return array |
| 964 | * array reference of all groups. |
| 965 | */ |
| 966 | public static function group($groupType = NULL, $excludeHidden = TRUE) { |
| 967 | return CRM_Core_Permission::group($groupType, $excludeHidden); |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * Fetch groups in a nested format suitable for use in select form element. |
| 972 | * @param bool $checkPermissions |
| 973 | * @param string|null $groupType |
| 974 | * @param bool $excludeHidden |
| 975 | * @return array |
| 976 | */ |
| 977 | public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) { |
| 978 | $groups = $checkPermissions ? self::group($groupType, $excludeHidden) : self::allGroup($groupType, $excludeHidden); |
| 979 | return CRM_Contact_BAO_Group::getGroupsHierarchy($groups, NULL, ' ', TRUE); |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * Get all permissioned groups from database. |
| 984 | * |
| 985 | * The static array group is returned, and if it's |
| 986 | * called the first time, the <b>Group DAO</b> is used |
| 987 | * to get all the groups. |
| 988 | * |
| 989 | * Note: any database errors will be trapped by the DAO. |
| 990 | * |
| 991 | * |
| 992 | * @param bool $onlyPublic |
| 993 | * @param null $groupType |
| 994 | * @param bool $excludeHidden |
| 995 | * |
| 996 | * @return array |
| 997 | * array reference of all groups. |
| 998 | */ |
| 999 | public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) { |
| 1000 | if (!self::$staticGroup) { |
| 1001 | $condition = 'saved_search_id = 0 OR saved_search_id IS NULL'; |
| 1002 | if ($onlyPublic) { |
| 1003 | $condition .= " AND visibility != 'User and User Admin Only'"; |
| 1004 | } |
| 1005 | |
| 1006 | if ($groupType) { |
| 1007 | $condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType); |
| 1008 | } |
| 1009 | |
| 1010 | if ($excludeHidden) { |
| 1011 | $condition .= ' AND is_hidden != 1 '; |
| 1012 | } |
| 1013 | |
| 1014 | self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title'); |
| 1015 | } |
| 1016 | |
| 1017 | return self::$staticGroup; |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Get all Relationship Types from database. |
| 1022 | * |
| 1023 | * The static array group is returned, and if it's |
| 1024 | * called the first time, the <b>RelationshipType DAO</b> is used |
| 1025 | * to get all the relationship types. |
| 1026 | * |
| 1027 | * Note: any database errors will be trapped by the DAO. |
| 1028 | * |
| 1029 | * @param string $valueColumnName |
| 1030 | * Db column name/label. |
| 1031 | * @param bool $reset |
| 1032 | * Reset relationship types if true. |
| 1033 | * @param bool $isActive |
| 1034 | * Filter by is_active. NULL to disable. |
| 1035 | * |
| 1036 | * @return array |
| 1037 | * array reference of all relationship types. |
| 1038 | */ |
| 1039 | public static function &relationshipType($valueColumnName = 'label', $reset = FALSE, $isActive = 1) { |
| 1040 | $cacheKey = $valueColumnName . '::' . $isActive; |
| 1041 | if (!CRM_Utils_Array::value($cacheKey, self::$relationshipType) || $reset) { |
| 1042 | self::$relationshipType[$cacheKey] = array(); |
| 1043 | |
| 1044 | //now we have name/label columns CRM-3336 |
| 1045 | $column_a_b = "{$valueColumnName}_a_b"; |
| 1046 | $column_b_a = "{$valueColumnName}_b_a"; |
| 1047 | |
| 1048 | $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType(); |
| 1049 | $relationshipTypeDAO->selectAdd(); |
| 1050 | $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b"); |
| 1051 | if ($isActive !== NULL) { |
| 1052 | $relationshipTypeDAO->is_active = $isActive; |
| 1053 | } |
| 1054 | $relationshipTypeDAO->find(); |
| 1055 | while ($relationshipTypeDAO->fetch()) { |
| 1056 | |
| 1057 | self::$relationshipType[$cacheKey][$relationshipTypeDAO->id] = array( |
| 1058 | 'id' => $relationshipTypeDAO->id, |
| 1059 | $column_a_b => $relationshipTypeDAO->$column_a_b, |
| 1060 | $column_b_a => $relationshipTypeDAO->$column_b_a, |
| 1061 | 'contact_type_a' => "$relationshipTypeDAO->contact_type_a", |
| 1062 | 'contact_type_b' => "$relationshipTypeDAO->contact_type_b", |
| 1063 | 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a", |
| 1064 | 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b", |
| 1065 | ); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return self::$relationshipType[$cacheKey]; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * Get all the ISO 4217 currency codes |
| 1074 | * |
| 1075 | * so far, we use this for validation only, so there's no point of putting this into the database |
| 1076 | * |
| 1077 | * |
| 1078 | * @return array |
| 1079 | * array reference of all currency codes |
| 1080 | */ |
| 1081 | public static function ¤cyCode() { |
| 1082 | if (!self::$currencyCode) { |
| 1083 | |
| 1084 | $query = "SELECT name FROM civicrm_currency"; |
| 1085 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1086 | $currencyCode = array(); |
| 1087 | while ($dao->fetch()) { |
| 1088 | self::$currencyCode[] = $dao->name; |
| 1089 | } |
| 1090 | } |
| 1091 | return self::$currencyCode; |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * Get all the County from database. |
| 1096 | * |
| 1097 | * The static array county is returned, and if it's |
| 1098 | * called the first time, the <b>County DAO</b> is used |
| 1099 | * to get all the Counties. |
| 1100 | * |
| 1101 | * Note: any database errors will be trapped by the DAO. |
| 1102 | * |
| 1103 | * |
| 1104 | * @param bool|int $id - Optional id to return |
| 1105 | * |
| 1106 | * @return array |
| 1107 | * array reference of all Counties |
| 1108 | */ |
| 1109 | public static function &county($id = FALSE) { |
| 1110 | if (!self::$county) { |
| 1111 | |
| 1112 | $config = CRM_Core_Config::singleton(); |
| 1113 | // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties |
| 1114 | self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id'); |
| 1115 | } |
| 1116 | if ($id) { |
| 1117 | if (array_key_exists($id, self::$county)) { |
| 1118 | return self::$county[$id]; |
| 1119 | } |
| 1120 | else { |
| 1121 | return CRM_Core_DAO::$_nullObject; |
| 1122 | } |
| 1123 | } |
| 1124 | return self::$county; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 1129 | * Get all active payment processors |
| 1130 | * |
| 1131 | * The static array paymentProcessor is returned |
| 1132 | * |
| 1133 | * |
| 1134 | * @param bool $all |
| 1135 | * Get payment processors - default is to get only active ones. |
| 1136 | * @param bool $test |
| 1137 | * Get test payment processors. |
| 1138 | * |
| 1139 | * @param null $additionalCond |
| 1140 | * |
| 1141 | * @return array |
| 1142 | * array of all payment processors |
| 1143 | */ |
| 1144 | public static function paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) { |
| 1145 | $condition = "is_test = "; |
| 1146 | $condition .= ($test) ? '1' : '0'; |
| 1147 | |
| 1148 | if ($additionalCond) { |
| 1149 | $condition .= " AND ( $additionalCond ) "; |
| 1150 | } |
| 1151 | |
| 1152 | // CRM-7178. Make sure we only include payment processors valid in this |
| 1153 | // domain |
| 1154 | $condition .= " AND domain_id = " . CRM_Core_Config::domainID(); |
| 1155 | |
| 1156 | $cacheKey = $condition . '_' . (int) $all; |
| 1157 | if (!isset(self::$paymentProcessor[$cacheKey])) { |
| 1158 | self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name'); |
| 1159 | } |
| 1160 | |
| 1161 | return self::$paymentProcessor[$cacheKey]; |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 1166 | * |
| 1167 | * The static array paymentProcessorType is returned |
| 1168 | * |
| 1169 | * |
| 1170 | * @param bool $all |
| 1171 | * Get payment processors - default is to get only active ones. |
| 1172 | * |
| 1173 | * @param int $id |
| 1174 | * @param string $return |
| 1175 | * |
| 1176 | * @return array |
| 1177 | * array of all payment processor types |
| 1178 | */ |
| 1179 | public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') { |
| 1180 | $cacheKey = $id . '_' . $return; |
| 1181 | if (empty(self::$paymentProcessorType[$cacheKey])) { |
| 1182 | self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id'); |
| 1183 | } |
| 1184 | if ($id && CRM_Utils_Array::value($id, self::$paymentProcessorType[$cacheKey])) { |
| 1185 | return self::$paymentProcessorType[$cacheKey][$id]; |
| 1186 | } |
| 1187 | return self::$paymentProcessorType[$cacheKey]; |
| 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * Get all the World Regions from Database. |
| 1192 | * |
| 1193 | * |
| 1194 | * @param bool $id |
| 1195 | * |
| 1196 | * @return array |
| 1197 | * array reference of all World Regions |
| 1198 | */ |
| 1199 | public static function &worldRegion($id = FALSE) { |
| 1200 | if (!self::$worldRegions) { |
| 1201 | self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id'); |
| 1202 | } |
| 1203 | |
| 1204 | if ($id) { |
| 1205 | if (array_key_exists($id, self::$worldRegions)) { |
| 1206 | return self::$worldRegions[$id]; |
| 1207 | } |
| 1208 | else { |
| 1209 | return CRM_Core_DAO::$_nullObject; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | return self::$worldRegions; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 1218 | * |
| 1219 | * Get all Activity Statuses. |
| 1220 | * |
| 1221 | * The static array activityStatus is returned |
| 1222 | * |
| 1223 | * |
| 1224 | * @param string $column |
| 1225 | * |
| 1226 | * @return array |
| 1227 | * array reference of all activity statuses |
| 1228 | */ |
| 1229 | public static function &activityStatus($column = 'label') { |
| 1230 | if (NULL === self::$activityStatus) { |
| 1231 | self::$activityStatus = array(); |
| 1232 | } |
| 1233 | if (!array_key_exists($column, self::$activityStatus)) { |
| 1234 | self::$activityStatus[$column] = array(); |
| 1235 | |
| 1236 | self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column); |
| 1237 | } |
| 1238 | |
| 1239 | return self::$activityStatus[$column]; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * @deprecated Please use the buildOptions() method in the appropriate BAO object. |
| 1244 | * |
| 1245 | * Get all Visibility levels. |
| 1246 | * |
| 1247 | * The static array visibility is returned |
| 1248 | * |
| 1249 | * |
| 1250 | * @param string $column |
| 1251 | * |
| 1252 | * @return array |
| 1253 | * array reference of all Visibility levels. |
| 1254 | */ |
| 1255 | public static function &visibility($column = 'label') { |
| 1256 | if (!isset(self::$visibility)) { |
| 1257 | self::$visibility = array(); |
| 1258 | } |
| 1259 | |
| 1260 | if (!isset(self::$visibility[$column])) { |
| 1261 | self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column); |
| 1262 | } |
| 1263 | |
| 1264 | return self::$visibility[$column]; |
| 1265 | } |
| 1266 | |
| 1267 | /** |
| 1268 | * @param int $countryID |
| 1269 | * @param string $field |
| 1270 | * |
| 1271 | * @return array |
| 1272 | */ |
| 1273 | public static function &stateProvinceForCountry($countryID, $field = 'name') { |
| 1274 | static $_cache = NULL; |
| 1275 | |
| 1276 | $cacheKey = "{$countryID}_{$field}"; |
| 1277 | if (!$_cache) { |
| 1278 | $_cache = array(); |
| 1279 | } |
| 1280 | |
| 1281 | if (!empty($_cache[$cacheKey])) { |
| 1282 | return $_cache[$cacheKey]; |
| 1283 | } |
| 1284 | |
| 1285 | $query = " |
| 1286 | SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id |
| 1287 | FROM civicrm_state_province |
| 1288 | WHERE country_id = %1 |
| 1289 | ORDER BY name"; |
| 1290 | $params = array( |
| 1291 | 1 => array( |
| 1292 | $countryID, |
| 1293 | 'Integer', |
| 1294 | ), |
| 1295 | ); |
| 1296 | |
| 1297 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1298 | |
| 1299 | $result = array(); |
| 1300 | while ($dao->fetch()) { |
| 1301 | $result[$dao->id] = $dao->name; |
| 1302 | } |
| 1303 | |
| 1304 | // localise the stateProvince names if in an non-en_US locale |
| 1305 | $config = CRM_Core_Config::singleton(); |
| 1306 | $tsLocale = CRM_Core_I18n::getLocale(); |
| 1307 | if ($tsLocale != '' and $tsLocale != 'en_US') { |
| 1308 | $i18n = CRM_Core_I18n::singleton(); |
| 1309 | $i18n->localizeArray($result, array( |
| 1310 | 'context' => 'province', |
| 1311 | )); |
| 1312 | $result = CRM_Utils_Array::asort($result); |
| 1313 | } |
| 1314 | |
| 1315 | $_cache[$cacheKey] = $result; |
| 1316 | |
| 1317 | CRM_Utils_Hook::buildStateProvinceForCountry($countryID, $result); |
| 1318 | |
| 1319 | return $result; |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * @param int $stateID |
| 1324 | * |
| 1325 | * @return array |
| 1326 | */ |
| 1327 | public static function &countyForState($stateID) { |
| 1328 | if (is_array($stateID)) { |
| 1329 | $states = implode(", ", $stateID); |
| 1330 | $query = " |
| 1331 | SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation |
| 1332 | FROM civicrm_county |
| 1333 | LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id |
| 1334 | WHERE civicrm_county.state_province_id in ( $states ) |
| 1335 | ORDER BY civicrm_state_province.abbreviation, civicrm_county.name"; |
| 1336 | |
| 1337 | $dao = CRM_Core_DAO::executeQuery($query); |
| 1338 | |
| 1339 | $result = array(); |
| 1340 | while ($dao->fetch()) { |
| 1341 | $result[$dao->id] = $dao->abbreviation . ': ' . $dao->name; |
| 1342 | } |
| 1343 | } |
| 1344 | else { |
| 1345 | |
| 1346 | static $_cache = NULL; |
| 1347 | |
| 1348 | $cacheKey = "{$stateID}_name"; |
| 1349 | if (!$_cache) { |
| 1350 | $_cache = array(); |
| 1351 | } |
| 1352 | |
| 1353 | if (!empty($_cache[$cacheKey])) { |
| 1354 | return $_cache[$cacheKey]; |
| 1355 | } |
| 1356 | |
| 1357 | $query = " |
| 1358 | SELECT civicrm_county.name name, civicrm_county.id id |
| 1359 | FROM civicrm_county |
| 1360 | WHERE state_province_id = %1 |
| 1361 | ORDER BY name"; |
| 1362 | $params = array( |
| 1363 | 1 => array( |
| 1364 | $stateID, |
| 1365 | 'Integer', |
| 1366 | ), |
| 1367 | ); |
| 1368 | |
| 1369 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
| 1370 | |
| 1371 | $result = array(); |
| 1372 | while ($dao->fetch()) { |
| 1373 | $result[$dao->id] = $dao->name; |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | return $result; |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * Given a state ID return the country ID, this allows |
| 1382 | * us to populate forms and values for downstream code |
| 1383 | * |
| 1384 | * @param int $stateID |
| 1385 | * |
| 1386 | * @return int|null |
| 1387 | * the country id that the state belongs to |
| 1388 | */ |
| 1389 | public static function countryIDForStateID($stateID) { |
| 1390 | if (empty($stateID)) { |
| 1391 | return NULL; |
| 1392 | } |
| 1393 | |
| 1394 | $query = " |
| 1395 | SELECT country_id |
| 1396 | FROM civicrm_state_province |
| 1397 | WHERE id = %1 |
| 1398 | "; |
| 1399 | $params = array(1 => array($stateID, 'Integer')); |
| 1400 | |
| 1401 | return CRM_Core_DAO::singleValueQuery($query, $params); |
| 1402 | } |
| 1403 | |
| 1404 | /** |
| 1405 | * Get all types of Greetings. |
| 1406 | * |
| 1407 | * The static array of greeting is returned |
| 1408 | * |
| 1409 | * |
| 1410 | * @param $filter |
| 1411 | * Get All Email Greetings - default is to get only active ones. |
| 1412 | * |
| 1413 | * @param string $columnName |
| 1414 | * |
| 1415 | * @return array |
| 1416 | * array reference of all greetings. |
| 1417 | */ |
| 1418 | public static function greeting($filter, $columnName = 'label') { |
| 1419 | if (!isset(Civi::$statics[__CLASS__]['greeting'])) { |
| 1420 | Civi::$statics[__CLASS__]['greeting'] = array(); |
| 1421 | } |
| 1422 | |
| 1423 | $index = $filter['greeting_type'] . '_' . $columnName; |
| 1424 | |
| 1425 | // also add contactType to the array |
| 1426 | $contactType = CRM_Utils_Array::value('contact_type', $filter); |
| 1427 | if ($contactType) { |
| 1428 | $index .= '_' . $contactType; |
| 1429 | } |
| 1430 | |
| 1431 | if (!CRM_Utils_Array::value($index, Civi::$statics[__CLASS__]['greeting'])) { |
| 1432 | $filterCondition = NULL; |
| 1433 | if ($contactType) { |
| 1434 | $filterVal = 'v.filter ='; |
| 1435 | switch ($contactType) { |
| 1436 | case 'Individual': |
| 1437 | $filterVal .= "1"; |
| 1438 | break; |
| 1439 | |
| 1440 | case 'Household': |
| 1441 | $filterVal .= "2"; |
| 1442 | break; |
| 1443 | |
| 1444 | case 'Organization': |
| 1445 | $filterVal .= "3"; |
| 1446 | break; |
| 1447 | } |
| 1448 | $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) "; |
| 1449 | } |
| 1450 | |
| 1451 | Civi::$statics[__CLASS__]['greeting'][$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName); |
| 1452 | } |
| 1453 | |
| 1454 | return Civi::$statics[__CLASS__]['greeting'][$index]; |
| 1455 | } |
| 1456 | |
| 1457 | /** |
| 1458 | * Get all extensions. |
| 1459 | * |
| 1460 | * The static array extensions |
| 1461 | * |
| 1462 | * FIXME: This is called by civix but not by any core code. We |
| 1463 | * should provide an API call which civix can use instead. |
| 1464 | * |
| 1465 | * |
| 1466 | * @return array |
| 1467 | * array($fullyQualifiedName => $label) list of extensions |
| 1468 | */ |
| 1469 | public static function &getExtensions() { |
| 1470 | if (!self::$extensions) { |
| 1471 | self::$extensions = array(); |
| 1472 | $sql = ' |
| 1473 | SELECT full_name, label |
| 1474 | FROM civicrm_extension |
| 1475 | WHERE is_active = 1 |
| 1476 | '; |
| 1477 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 1478 | while ($dao->fetch()) { |
| 1479 | self::$extensions[$dao->full_name] = $dao->label; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | return self::$extensions; |
| 1484 | } |
| 1485 | |
| 1486 | /** |
| 1487 | * Get all options values. |
| 1488 | * |
| 1489 | * The static array option values is returned |
| 1490 | * |
| 1491 | * |
| 1492 | * @param bool $optionGroupName |
| 1493 | * Get All Option Group values- default is to get only active ones. |
| 1494 | * |
| 1495 | * @param int $id |
| 1496 | * @param null $condition |
| 1497 | * |
| 1498 | * @return array |
| 1499 | * array reference of all Option Group Name |
| 1500 | */ |
| 1501 | public static function accountOptionValues($optionGroupName, $id = NULL, $condition = NULL) { |
| 1502 | $cacheKey = $optionGroupName . '_' . $condition; |
| 1503 | if (empty(self::$accountOptionValues[$cacheKey])) { |
| 1504 | self::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup::values($optionGroupName, FALSE, FALSE, FALSE, $condition); |
| 1505 | } |
| 1506 | if ($id) { |
| 1507 | return CRM_Utils_Array::value($id, self::$accountOptionValues[$cacheKey]); |
| 1508 | } |
| 1509 | |
| 1510 | return self::$accountOptionValues[$cacheKey]; |
| 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * Fetch the list of active extensions of type 'module' |
| 1515 | * |
| 1516 | * @param bool $fresh |
| 1517 | * Whether to forcibly reload extensions list from canonical store. |
| 1518 | * |
| 1519 | * @return array |
| 1520 | * array(array('prefix' => $, 'file' => $)) |
| 1521 | */ |
| 1522 | public static function getModuleExtensions($fresh = FALSE) { |
| 1523 | return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles($fresh); |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | /** |
| 1528 | * Get all tax rates. |
| 1529 | * |
| 1530 | * The static array tax rates is returned |
| 1531 | * |
| 1532 | * @return array |
| 1533 | * array list of tax rates with the financial type |
| 1534 | */ |
| 1535 | public static function getTaxRates() { |
| 1536 | if (!isset(Civi::$statics[__CLASS__]['taxRates'])) { |
| 1537 | Civi::$statics[__CLASS__]['taxRates'] = array(); |
| 1538 | $option = civicrm_api3('option_value', 'get', array( |
| 1539 | 'sequential' => 1, |
| 1540 | 'option_group_id' => 'account_relationship', |
| 1541 | 'name' => 'Sales Tax Account is', |
| 1542 | )); |
| 1543 | $value = array(); |
| 1544 | if ($option['count'] !== 0) { |
| 1545 | if ($option['count'] > 1) { |
| 1546 | foreach ($option['values'] as $opt) { |
| 1547 | $value[] = $opt['value']; |
| 1548 | } |
| 1549 | } |
| 1550 | else { |
| 1551 | $value[] = $option['values'][0]['value']; |
| 1552 | } |
| 1553 | $where = 'AND efa.account_relationship IN (' . implode(', ', $value) . ' )'; |
| 1554 | } |
| 1555 | else { |
| 1556 | $where = ''; |
| 1557 | } |
| 1558 | $sql = " |
| 1559 | SELECT fa.tax_rate, efa.entity_id |
| 1560 | FROM civicrm_entity_financial_account efa |
| 1561 | INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id |
| 1562 | WHERE efa.entity_table = 'civicrm_financial_type' |
| 1563 | {$where} |
| 1564 | AND fa.is_active = 1"; |
| 1565 | $dao = CRM_Core_DAO::executeQuery($sql); |
| 1566 | while ($dao->fetch()) { |
| 1567 | Civi::$statics[__CLASS__]['taxRates'][$dao->entity_id] = $dao->tax_rate; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | return Civi::$statics[__CLASS__]['taxRates']; |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * Get participant status class options. |
| 1576 | * |
| 1577 | * @return array |
| 1578 | */ |
| 1579 | public static function emailOnHoldOptions() { |
| 1580 | return array( |
| 1581 | '0' => ts('No'), |
| 1582 | '1' => ts('On Hold Bounce'), |
| 1583 | '2' => ts('On Hold Opt Out'), |
| 1584 | ); |
| 1585 | } |
| 1586 | |
| 1587 | } |