Merge pull request #5785 from pratikshad/16336
[civicrm-core.git] / CRM / Core / PseudoConstant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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
e7112fa7 46 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
47 * $Id$
48 *
49 */
50class CRM_Core_PseudoConstant {
51
887a4028 52 /**
d09edf64 53 * Static cache for pseudoconstant arrays.
887a4028 54 * @var array
887a4028
A
55 */
56 private static $cache;
57
6a488035 58 /**
c490a46a 59 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 60 *
6a488035
TO
61 * activity type
62 * @var array
6a488035
TO
63 */
64 private static $activityType;
65
6a488035 66 /**
100fef9d 67 * States, provinces
6a488035 68 * @var array
6a488035
TO
69 */
70 private static $stateProvince;
71
72 /**
d09edf64 73 * Counties.
6a488035 74 * @var array
6a488035
TO
75 */
76 private static $county;
77
78 /**
100fef9d 79 * States/provinces abbreviations
6a488035 80 * @var array
6a488035
TO
81 */
82 private static $stateProvinceAbbreviation;
83
84 /**
d09edf64 85 * Country.
6a488035 86 * @var array
6a488035
TO
87 */
88 private static $country;
89
90 /**
d09edf64 91 * CountryIsoCode.
6a488035 92 * @var array
6a488035
TO
93 */
94 private static $countryIsoCode;
95
6a488035 96 /**
c490a46a 97 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 98 *
6a488035
TO
99 * group
100 * @var array
6a488035
TO
101 */
102 private static $group;
103
104 /**
100fef9d 105 * GroupIterator
6a488035 106 * @var mixed
6a488035
TO
107 */
108 private static $groupIterator;
109
110 /**
100fef9d 111 * RelationshipType
6a488035 112 * @var array
6a488035
TO
113 */
114 private static $relationshipType;
115
116 /**
100fef9d 117 * Civicrm groups that are not smart groups
6a488035 118 * @var array
6a488035
TO
119 */
120 private static $staticGroup;
121
6a488035 122 /**
100fef9d 123 * Currency codes
6a488035 124 * @var array
6a488035
TO
125 */
126 private static $currencyCode;
127
6a488035 128 /**
100fef9d 129 * Payment processor
6a488035 130 * @var array
6a488035
TO
131 */
132 private static $paymentProcessor;
133
134 /**
100fef9d 135 * Payment processor types
6a488035 136 * @var array
6a488035
TO
137 */
138 private static $paymentProcessorType;
139
140 /**
141 * World Region
142 * @var array
6a488035
TO
143 */
144 private static $worldRegions;
145
6a488035 146 /**
c490a46a 147 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 148 *
6a488035
TO
149 * activity status
150 * @var array
6a488035
TO
151 */
152 private static $activityStatus;
153
6a488035
TO
154 /**
155 * Visibility
156 * @var array
6a488035
TO
157 */
158 private static $visibility;
159
6a488035
TO
160 /**
161 * Greetings
162 * @var array
6a488035
TO
163 */
164 private static $greeting;
165
166 /**
167 * Default Greetings
168 * @var array
6a488035
TO
169 */
170 private static $greetingDefaults;
171
172 /**
173 * Extensions of type module
174 * @var array
6a488035
TO
175 */
176 private static $extensions;
177
f743a6eb
CW
178 /**
179 * Financial Account Type
180 * @var array
f743a6eb
CW
181 */
182 private static $accountOptionValues;
183
dc428161 184 /**
185 * Tax Rates
186 * @var array
dc428161 187 */
188 private static $taxRates;
189
887a4028 190 /**
6d68a4cb
CW
191 * Low-level option getter, rarely accessed directly.
192 * NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
07c365da 193 * @see http://wiki.civicrm.org/confluence/display/CRMDOC/Pseudoconstant+%28option+list%29+Reference
6d68a4cb 194 *
6a0b768e
TO
195 * @param string $daoName
196 * @param string $fieldName
197 * @param array $params
a42ef93c
CW
198 * - name string name of the option group
199 * - flip boolean results are return in id => label format if false
200 * if true, the results are reversed
201 * - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
202 * - localize boolean if true, localize the results before returning
f743a6eb 203 * - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
091fe2a8
CW
204 * - keyColumn string the column to use for 'id'
205 * - labelColumn string the column to use for 'label'
6d68a4cb 206 * - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
a42ef93c
CW
207 * - onlyActive boolean return only the action option values
208 * - fresh boolean ignore cache entries and go back to DB
353ffa53 209 * @param string $context : Context string
887a4028 210 *
8d7a9d07 211 * @return array|bool
72b3a70c 212 * array on success, FALSE on error.
a42ef93c 213 *
887a4028 214 */
786ad6e1
CW
215 public static function get($daoName, $fieldName, $params = array(), $context = NULL) {
216 CRM_Core_DAO::buildOptionsContext($context);
f2b53f26 217 $flip = !empty($params['flip']);
786ad6e1
CW
218 // Merge params with defaults
219 $params += array(
220 'grouping' => FALSE,
221 'localize' => FALSE,
c203a3d6 222 'onlyActive' => ($context == 'validate' || $context == 'get') ? FALSE : TRUE,
786ad6e1
CW
223 'fresh' => FALSE,
224 );
f2b53f26
CW
225
226 // Custom fields are not in the schema
786ad6e1 227 if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
a3d8b390
CW
228 $customField = new CRM_Core_DAO_CustomField();
229 $customField->id = (int) substr($fieldName, 7);
230 $customField->find(TRUE);
231 $options = FALSE;
8ef12e64 232
a3d8b390
CW
233 if (!empty($customField->option_group_id)) {
234 $options = CRM_Core_OptionGroup::valuesByID($customField->option_group_id,
2fea9ed9 235 FALSE,
a3d8b390
CW
236 $params['grouping'],
237 $params['localize'],
238 // Note: for custom fields the 'name' column is NULL
239 CRM_Utils_Array::value('labelColumn', $params, 'label'),
240 $params['onlyActive'],
241 $params['fresh']
242 );
243 }
244 else {
245 if ($customField->data_type === 'StateProvince') {
246 $options = self::stateProvince();
247 }
248 elseif ($customField->data_type === 'Country') {
249 $options = $context == 'validate' ? self::countryIsoCode() : self::country();
250 }
251 elseif ($customField->data_type === 'Boolean') {
14f42e31 252 $options = $context == 'validate' ? array(0, 1) : CRM_Core_SelectValues::boolean();
a3d8b390 253 }
a3d8b390 254 }
2fea9ed9
CW
255 CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE);
256 if ($options && $flip) {
257 $options = array_flip($options);
a3d8b390
CW
258 }
259 $customField->free();
786ad6e1 260 return $options;
f2b53f26
CW
261 }
262
263 // Core field: load schema
8d7a9d07 264 $dao = new $daoName();
5fafc9b0 265 $fieldSpec = $dao->getFieldSpec($fieldName);
091fe2a8 266 $dao->free();
000652dc 267 // If neither worked then this field doesn't exist. Return false.
5fafc9b0 268 if (empty($fieldSpec)) {
ecc63dd6
A
269 return FALSE;
270 }
887a4028 271
887a4028
A
272 elseif (!empty($fieldSpec['pseudoconstant'])) {
273 $pseudoconstant = $fieldSpec['pseudoconstant'];
cc6443c4 274
275 // if callback is specified..
22e263ad 276 if (!empty($pseudoconstant['callback'])) {
c8074a93 277 return call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']));
cc6443c4 278 }
279
786ad6e1 280 // Merge params with schema defaults
091fe2a8 281 $params += array(
786ad6e1 282 'condition' => CRM_Utils_Array::value('condition', $pseudoconstant, array()),
091fe2a8
CW
283 'keyColumn' => CRM_Utils_Array::value('keyColumn', $pseudoconstant),
284 'labelColumn' => CRM_Utils_Array::value('labelColumn', $pseudoconstant),
091fe2a8
CW
285 );
286
f6635f33
AH
287 if ($context == 'abbreviate') {
288 switch ($fieldName) {
289 case 'state_province_id':
290 $params['labelColumn'] = 'abbreviation';
291 break;
2e1050f4 292
f6635f33
AH
293 case 'country_id':
294 $params['labelColumn'] = 'iso_code';
295 break;
2e1050f4 296
f6635f33
AH
297 default:
298 }
299 }
300
091fe2a8 301 // Fetch option group from option_value table
22e263ad 302 if (!empty($pseudoconstant['optionGroupName'])) {
786ad6e1
CW
303 if ($context == 'validate') {
304 $params['labelColumn'] = 'name';
305 }
b432ddaa
CW
306 if ($context == 'match') {
307 $params['keyColumn'] = 'name';
308 }
a42ef93c
CW
309 // Call our generic fn for retrieving from the option_value table
310 return CRM_Core_OptionGroup::values(
887a4028 311 $pseudoconstant['optionGroupName'],
a42ef93c
CW
312 $flip,
313 $params['grouping'],
314 $params['localize'],
f743a6eb 315 $params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
091fe2a8 316 $params['labelColumn'] ? $params['labelColumn'] : 'label',
a42ef93c 317 $params['onlyActive'],
c0c9cd82
CW
318 $params['fresh'],
319 $params['keyColumn'] ? $params['keyColumn'] : 'value'
887a4028 320 );
887a4028 321 }
091fe2a8
CW
322
323 // Fetch options from other tables
887a4028 324 if (!empty($pseudoconstant['table'])) {
a42ef93c
CW
325 // Normalize params so the serialized cache string will be consistent.
326 CRM_Utils_Array::remove($params, 'flip', 'fresh');
887a4028 327 ksort($params);
a1ef51e2 328 $cacheKey = $daoName . $fieldName . serialize($params);
887a4028 329
a1ef51e2 330 // Retrieve cached options
a42ef93c 331 if (isset(self::$cache[$cacheKey]) && empty($params['fresh'])) {
a1ef51e2 332 $output = self::$cache[$cacheKey];
887a4028 333 }
a1ef51e2 334 else {
4539198e 335 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($pseudoconstant['table']);
2158332a
CW
336 if (!class_exists($daoName)) {
337 return FALSE;
338 }
339 // Get list of fields for the option table
8d7a9d07 340 $dao = new $daoName();
7ae0389f
CW
341 $availableFields = array_keys($dao->fieldKeys());
342 $dao->free();
887a4028 343
a1ef51e2
CW
344 $select = "SELECT %1 AS id, %2 AS label";
345 $from = "FROM %3";
346 $wheres = array();
347 $order = "ORDER BY %2";
786ad6e1 348
b432ddaa
CW
349 // Use machine name in certain contexts
350 if ($context == 'validate' || $context == 'match') {
351 $nameField = $context == 'validate' ? 'labelColumn' : 'keyColumn';
a38a89fc 352 if (!empty($pseudoconstant['nameColumn'])) {
b432ddaa 353 $params[$nameField] = $pseudoconstant['nameColumn'];
a38a89fc
CW
354 }
355 elseif (in_array('name', $availableFields)) {
b432ddaa 356 $params[$nameField] = 'name';
a38a89fc 357 }
786ad6e1 358 }
6d68a4cb 359 // Condition param can be passed as an sql clause string or an array of clauses
a1ef51e2 360 if (!empty($params['condition'])) {
6d68a4cb 361 $wheres[] = implode(' AND ', (array) $params['condition']);
a1ef51e2 362 }
6d68a4cb 363 // onlyActive param will automatically filter on common flags
a1ef51e2 364 if (!empty($params['onlyActive'])) {
21d4b9c1 365 foreach (array('is_active' => 1, 'is_deleted' => 0, 'is_test' => 0, 'is_hidden' => 0) as $flag => $val) {
6d68a4cb
CW
366 if (in_array($flag, $availableFields)) {
367 $wheres[] = "$flag = $val";
368 }
a1ef51e2
CW
369 }
370 }
6d68a4cb
CW
371 // Filter domain specific options
372 if (in_array('domain_id', $availableFields)) {
373 $wheres[] = 'domain_id = ' . CRM_Core_Config::domainID();
374 }
a1ef51e2 375 $queryParams = array(
353ffa53
TO
376 1 => array($params['keyColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
377 2 => array($params['labelColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
378 3 => array($pseudoconstant['table'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES),
a1ef51e2
CW
379 );
380 // Add orderColumn param
381 if (!empty($params['orderColumn'])) {
382 $queryParams[4] = array($params['orderColumn'], 'String', CRM_Core_DAO::QUERY_FORMAT_NO_QUOTES);
383 $order = "ORDER BY %4";
384 }
385 // Support no sorting if $params[orderColumn] is FALSE
386 elseif (isset($params['orderColumn']) && $params['orderColumn'] === FALSE) {
387 $order = '';
388 }
389 // Default to 'weight' if that column exists
390 elseif (in_array('weight', $availableFields)) {
391 $order = "ORDER BY weight";
392 }
a42ef93c 393
a1ef51e2
CW
394 $output = array();
395 $query = "$select $from";
396 if ($wheres) {
397 $query .= " WHERE " . implode($wheres, ' AND ');
398 }
399 $query .= ' ' . $order;
400 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
401 while ($dao->fetch()) {
402 $output[$dao->id] = $dao->label;
403 }
404 $dao->free();
a3d8b390
CW
405 // Localize results
406 if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') {
407 $I18nParams = array();
408 if ($pseudoconstant['table'] == 'civicrm_country') {
409 $I18nParams['context'] = 'country';
410 }
411 if ($pseudoconstant['table'] == 'civicrm_state_province') {
412 $I18nParams['context'] = 'province';
413 }
a1ef51e2 414 $i18n = CRM_Core_I18n::singleton();
a3d8b390 415 $i18n->localizeArray($output, $I18nParams);
a1ef51e2
CW
416 // Maintain sort by label
417 if ($order == "ORDER BY %2") {
418 CRM_Utils_Array::asort($output);
419 }
420 }
421 self::$cache[$cacheKey] = $output;
422 }
423 return $flip ? array_flip($output) : $output;
887a4028
A
424 }
425 }
a3d8b390
CW
426
427 // Return "Yes" and "No" for boolean fields
428 elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
429 $output = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No'));
430 return $flip ? array_flip($output) : $output;
431 }
887a4028
A
432 // If we're still here, it's an error. Return FALSE.
433 return FALSE;
434 }
435
6a488035 436 /**
d09edf64 437 * Fetch the translated label for a field given its key.
dcda1cd5 438 *
6a0b768e
TO
439 * @param string $baoName
440 * @param string $fieldName
441 * @param string|Int $key
a8c23526
CW
442 *
443 * TODO: Accept multivalued input?
dcda1cd5 444 *
2a3f958d
CW
445 * @return bool|null|string
446 * FALSE if the given field has no associated option list
447 * NULL if the given key has no corresponding option
448 * String if label is found
dcda1cd5 449 */
00be9182 450 public static function getLabel($baoName, $fieldName, $key) {
a8c23526
CW
451 $values = $baoName::buildOptions($fieldName, 'get');
452 if ($values === FALSE) {
453 return FALSE;
454 }
455 return CRM_Utils_Array::value($key, $values);
456 }
457
458 /**
d09edf64 459 * Fetch the machine name for a field given its key.
a8c23526 460 *
6a0b768e
TO
461 * @param string $baoName
462 * @param string $fieldName
463 * @param string|Int $key
a8c23526
CW
464 *
465 * @return bool|null|string
466 * FALSE if the given field has no associated option list
467 * NULL if the given key has no corresponding option
468 * String if label is found
469 */
00be9182 470 public static function getName($baoName, $fieldName, $key) {
a8c23526 471 $values = $baoName::buildOptions($fieldName, 'validate');
2a3f958d
CW
472 if ($values === FALSE) {
473 return FALSE;
474 }
475 return CRM_Utils_Array::value($key, $values);
476 }
dcda1cd5
CW
477
478 /**
d09edf64 479 * Fetch the key for a field option given its name.
dcda1cd5 480 *
6a0b768e
TO
481 * @param string $baoName
482 * @param string $fieldName
483 * @param string|Int $value
dcda1cd5 484 *
8d7a9d07 485 * @return bool|null|string|int
2a3f958d
CW
486 * FALSE if the given field has no associated option list
487 * NULL if the given key has no corresponding option
488 * String|Number if key is found
dcda1cd5 489 */
00be9182 490 public static function getKey($baoName, $fieldName, $value) {
a8c23526 491 $values = $baoName::buildOptions($fieldName, 'validate');
2a3f958d
CW
492 if ($values === FALSE) {
493 return FALSE;
494 }
495 return CRM_Utils_Array::key($value, $values);
496 }
dcda1cd5 497
e869b07d
CW
498 /**
499 * Lookup the admin page at which a field's option list can be edited
500 * @param $fieldSpec
501 * @return string|null
502 */
00be9182 503 public static function getOptionEditUrl($fieldSpec) {
e869b07d
CW
504 // If it's an option group, that's easy
505 if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
506 return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
507 }
508 // For everything else...
509 elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
510 $daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']);
511 if (!$daoName) {
512 return NULL;
513 }
514 // We don't have good mapping so have to do a bit of guesswork from the menu
e1462487 515 list(, $parent, , $child) = explode('_', $daoName);
e869b07d 516 $sql = "SELECT path FROM civicrm_menu
e1462487
CW
517 WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
518 ORDER BY page_callback
e869b07d
CW
519 LIMIT 1";
520 return CRM_Core_Dao::singleValueQuery($sql);
521 }
522 return NULL;
523 }
524
dcda1cd5 525 /**
d09edf64 526 * DEPRECATED generic populate method.
c490a46a 527 * All pseudoconstant functions that use this method are also @deprecated
6a488035
TO
528 *
529 * The static array $var is populated from the db
530 * using the <b>$name DAO</b>.
531 *
532 * Note: any database errors will be trapped by the DAO.
533 *
6a0b768e
TO
534 * @param array $var
535 * The associative array we will fill.
536 * @param string $name
537 * The name of the DAO.
538 * @param bool $all
539 * Get all objects. default is to get only active ones.
540 * @param string $retrieve
541 * The field that we are interested in (normally name, differs in some objects).
542 * @param string $filter
543 * The field that we want to filter the result set with.
544 * @param string $condition
545 * The condition that gets passed to the final query as the WHERE clause.
745b795a
EM
546 *
547 * @param null $orderby
548 * @param string $key
549 * @param null $force
6a488035
TO
550 *
551 * @return void
6a488035 552 */
8360b701
DL
553 public static function populate(
554 &$var,
555 $name,
556 $all = FALSE,
557 $retrieve = 'name',
558 $filter = 'is_active',
559 $condition = NULL,
560 $orderby = NULL,
561 $key = 'id',
562 $force = NULL
563 ) {
6a488035 564 $cacheKey = "CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}";
353ffa53
TO
565 $cache = CRM_Utils_Cache::singleton();
566 $var = $cache->get($cacheKey);
6a488035
TO
567 if ($var && empty($force)) {
568 return $var;
569 }
570
8d7a9d07 571 $object = new $name();
6a488035
TO
572
573 $object->selectAdd();
574 $object->selectAdd("$key, $retrieve");
575 if ($condition) {
576 $object->whereAdd($condition);
577 }
578
579 if (!$orderby) {
580 $object->orderBy($retrieve);
581 }
582 else {
583 $object->orderBy($orderby);
584 }
585
586 if (!$all) {
587 $object->$filter = 1;
588 }
589
590 $object->find();
591 $var = array();
592 while ($object->fetch()) {
593 $var[$object->$key] = $object->$retrieve;
594 }
595
596 $cache->set($cacheKey, $var);
597 }
598
599 /**
d09edf64 600 * Flush given pseudoconstant so it can be reread from db.
6a488035
TO
601 * nex time it's requested.
602 *
6a488035 603 *
2a6da8d7 604 * @param bool|string $name pseudoconstant to be flushed
6a488035 605 */
2683ce94 606 public static function flush($name = 'cache') {
80085473 607 if (isset(self::$$name)) {
fa56270d
CW
608 self::$$name = NULL;
609 }
80085473
CW
610 if ($name == 'cache') {
611 CRM_Core_OptionGroup::flushAll();
612 }
6a488035
TO
613 }
614
6a488035 615 /**
c490a46a 616 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
a19392e7 617 *
6a488035
TO
618 * Get all Activty types.
619 *
620 * The static array activityType is returned
621 *
6a488035 622 *
a6c01b45
CW
623 * @return array
624 * array reference of all activity types.
6a488035
TO
625 */
626 public static function &activityType() {
627 $args = func_get_args();
628 $all = CRM_Utils_Array::value(0, $args, TRUE);
629 $includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE);
630 $reset = CRM_Utils_Array::value(2, $args, FALSE);
631 $returnColumn = CRM_Utils_Array::value(3, $args, 'label');
632 $includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE);
633 $onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE);
634 $index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
635 $index .= '_' . (int) $includeCampaignActivities;
636 $index .= '_' . (int) $onlyComponentActivities;
637
638 if (NULL === self::$activityType) {
639 self::$activityType = array();
640 }
641
642 if (!isset(self::$activityType[$index]) || $reset) {
643 $condition = NULL;
644 if (!$all) {
645 $condition = 'AND filter = 0';
646 }
647 $componentClause = " v.component_id IS NULL";
648 if ($onlyComponentActivities) {
649 $componentClause = " v.component_id IS NOT NULL";
650 }
651
652 $componentIds = array();
653 $compInfo = CRM_Core_Component::getEnabledComponents();
654
655 // build filter for listing activity types only if their
656 // respective components are enabled
657 foreach ($compInfo as $compName => $compObj) {
658 if ($compName == 'CiviCase') {
659 if ($includeCaseActivities) {
660 $componentIds[] = $compObj->componentID;
661 }
662 }
663 elseif ($compName == 'CiviCampaign') {
664 if ($includeCampaignActivities) {
665 $componentIds[] = $compObj->componentID;
666 }
667 }
668 else {
669 $componentIds[] = $compObj->componentID;
670 }
671 }
672
673 if (count($componentIds)) {
674 $componentIds = implode(',', $componentIds);
675 $componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
676 if ($onlyComponentActivities) {
677 $componentClause = " ( v.component_id IN ($componentIds ) )";
678 }
679 }
bdd7b275 680 $condition = $condition . ' AND ' . $componentClause;
6a488035
TO
681
682 self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
683 }
684 return self::$activityType[$index];
685 }
0e6e8724 686
6a488035
TO
687 /**
688 * Get all the State/Province from database.
689 *
690 * The static array stateProvince is returned, and if it's
691 * called the first time, the <b>State Province DAO</b> is used
692 * to get all the States.
693 *
694 * Note: any database errors will be trapped by the DAO.
695 *
6a488035 696 *
6a0b768e 697 * @param bool|int $id - Optional id to return
6a488035 698 *
2a6da8d7 699 * @param bool $limit
6a488035 700 *
a6c01b45
CW
701 * @return array
702 * array reference of all State/Provinces.
6a488035
TO
703 */
704 public static function &stateProvince($id = FALSE, $limit = TRUE) {
705 if (($id && !CRM_Utils_Array::value($id, self::$stateProvince)) || !self::$stateProvince || !$id) {
706 $whereClause = FALSE;
707 $config = CRM_Core_Config::singleton();
708 if ($limit) {
709 $countryIsoCodes = self::countryIsoCode();
353ffa53
TO
710 $limitCodes = $config->provinceLimit();
711 $limitIds = array();
6a488035
TO
712 foreach ($limitCodes as $code) {
713 $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
714 }
715 if (!empty($limitIds)) {
716 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
717 }
718 else {
719 $whereClause = FALSE;
720 }
721 }
722 self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
723
724 // localise the province names if in an non-en_US locale
725 global $tsLocale;
726 if ($tsLocale != '' and $tsLocale != 'en_US') {
727 $i18n = CRM_Core_I18n::singleton();
728 $i18n->localizeArray(self::$stateProvince, array(
353ffa53
TO
729 'context' => 'province',
730 ));
6a488035
TO
731 self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince);
732 }
733 }
734 if ($id) {
735 if (array_key_exists($id, self::$stateProvince)) {
736 return self::$stateProvince[$id];
737 }
738 else {
739 $result = NULL;
740 return $result;
741 }
742 }
743 return self::$stateProvince;
744 }
745
746 /**
747 * Get all the State/Province abbreviations from the database.
748 *
749 * Same as above, except gets the abbreviations instead of the names.
750 *
6a488035 751 *
6a0b768e 752 * @param bool|int $id - Optional id to return
2a6da8d7
EM
753 *
754 * @param bool $limit
6a488035 755 *
a6c01b45
CW
756 * @return array
757 * array reference of all State/Province abbreviations.
6a488035
TO
758 */
759 public static function &stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
760 if ($id > 1) {
761 $query = "
762SELECT abbreviation
763FROM civicrm_state_province
764WHERE id = %1";
765 $params = array(
766 1 => array(
767 $id,
768 'Integer',
769 ),
770 );
771 return CRM_Core_DAO::singleValueQuery($query, $params);
772 }
773
774 if (!self::$stateProvinceAbbreviation || !$id) {
775
776 $whereClause = FALSE;
777
778 if ($limit) {
353ffa53 779 $config = CRM_Core_Config::singleton();
6a488035 780 $countryIsoCodes = self::countryIsoCode();
353ffa53
TO
781 $limitCodes = $config->provinceLimit();
782 $limitIds = array();
6a488035
TO
783 foreach ($limitCodes as $code) {
784 $tmpArray = array_keys($countryIsoCodes, $code);
785
786 if (!empty($tmpArray)) {
787 $limitIds[] = array_shift($tmpArray);
788 }
789 }
790 if (!empty($limitIds)) {
791 $whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
792 }
793 }
794 self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
795 }
796
797 if ($id) {
798 if (array_key_exists($id, self::$stateProvinceAbbreviation)) {
799 return self::$stateProvinceAbbreviation[$id];
800 }
801 else {
802 $result = NULL;
803 return $result;
804 }
805 }
806 return self::$stateProvinceAbbreviation;
807 }
808
809 /**
810 * Get all the countries from database.
811 *
812 * The static array country is returned, and if it's
813 * called the first time, the <b>Country DAO</b> is used
814 * to get all the countries.
815 *
816 * Note: any database errors will be trapped by the DAO.
817 *
6a488035 818 *
dd244018 819 * @param bool|int $id - Optional id to return
6a488035 820 *
dd244018 821 * @param bool $applyLimit
6a488035 822 *
a6c01b45
CW
823 * @return array
824 * array reference of all countries.
6a488035
TO
825 */
826 public static function country($id = FALSE, $applyLimit = TRUE) {
827 if (($id && !CRM_Utils_Array::value($id, self::$country)) || !self::$country || !$id) {
828
829 $config = CRM_Core_Config::singleton();
830 $limitCodes = array();
831
832 if ($applyLimit) {
833 // limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
834 // (ensuring it's a subset of the legal values)
835 // K/P: We need to fix this, i dont think it works with new setting files
836 $limitCodes = $config->countryLimit();
837 if (!is_array($limitCodes)) {
838 $limitCodes = array(
839 $config->countryLimit => 1,
840 );
841 }
842
843 $limitCodes = array_intersect(self::countryIsoCode(), $limitCodes);
844 }
845
846 if (count($limitCodes)) {
847 $whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
848 }
849 else {
850 $whereClause = NULL;
851 }
852
853 self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
854
855 // if default country is set, percolate it to the top
856 if ($config->defaultContactCountry()) {
857 $countryIsoCodes = self::countryIsoCode();
858 $defaultID = array_search($config->defaultContactCountry(), $countryIsoCodes);
859 if ($defaultID !== FALSE) {
860 $default[$defaultID] = CRM_Utils_Array::value($defaultID, self::$country);
861 self::$country = $default + self::$country;
862 }
863 }
864
865 // localise the country names if in an non-en_US locale
866 global $tsLocale;
867 if ($tsLocale != '' and $tsLocale != 'en_US') {
868 $i18n = CRM_Core_I18n::singleton();
869 $i18n->localizeArray(self::$country, array(
353ffa53
TO
870 'context' => 'country',
871 ));
6a488035
TO
872 self::$country = CRM_Utils_Array::asort(self::$country);
873 }
874 }
875 if ($id) {
876 if (array_key_exists($id, self::$country)) {
877 return self::$country[$id];
878 }
879 else {
7cdc9e9f 880 return CRM_Core_DAO::$_nullObject;
6a488035
TO
881 }
882 }
883 return self::$country;
884 }
885
886 /**
887 * Get all the country ISO Code abbreviations from the database.
888 *
889 * The static array countryIsoCode is returned, and if it's
890 * called the first time, the <b>Country DAO</b> is used
891 * to get all the countries' ISO codes.
892 *
893 * Note: any database errors will be trapped by the DAO.
894 *
6a488035 895 *
fd31fa4c 896 * @param bool $id
6a488035 897 *
a6c01b45
CW
898 * @return array
899 * array reference of all country ISO codes.
6a488035
TO
900 */
901 public static function &countryIsoCode($id = FALSE) {
902 if (!self::$countryIsoCode) {
903 self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
904 }
905 if ($id) {
906 if (array_key_exists($id, self::$countryIsoCode)) {
907 return self::$countryIsoCode[$id];
908 }
909 else {
7cdc9e9f 910 return CRM_Core_DAO::$_nullObject;
6a488035
TO
911 }
912 }
913 return self::$countryIsoCode;
914 }
915
6a488035 916 /**
c490a46a 917 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 918 *
6a488035
TO
919 * Get all groups from database
920 *
921 * The static array group is returned, and if it's
922 * called the first time, the <b>Group DAO</b> is used
923 * to get all the groups.
924 *
925 * Note: any database errors will be trapped by the DAO.
926 *
6a0b768e
TO
927 * @param string $groupType
928 * Type of group(Access/Mailing).
929 * @param bool $excludeHidden
930 * Exclude hidden groups.
6a488035 931 *
6a488035 932 *
a6c01b45
CW
933 * @return array
934 * array reference of all groups.
6a488035
TO
935 */
936 public static function &allGroup($groupType = NULL, $excludeHidden = TRUE) {
937 $condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
938
939 if (!self::$group) {
940 self::$group = array();
941 }
942
943 $groupKey = $groupType ? $groupType : 'null';
944
945 if (!isset(self::$group[$groupKey])) {
946 self::$group[$groupKey] = NULL;
947 self::populate(self::$group[$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
948 }
949 return self::$group[$groupKey];
950 }
951
952 /**
953 * Create or get groups iterator (iterates over nested groups in a
954 * logical fashion)
955 *
956 * The GroupNesting instance is returned; it's created if this is being
957 * called for the first time
958 *
959 *
6a488035 960 *
dd244018 961 * @param bool $styledLabels
6a488035 962 *
72b3a70c 963 * @return CRM_Contact_BAO_GroupNesting
6a488035
TO
964 */
965 public static function &groupIterator($styledLabels = FALSE) {
966 if (!self::$groupIterator) {
d424ffde
CW
967 // When used as an object, GroupNesting implements Iterator
968 // and iterates nested groups in a logical manner for us
6a488035
TO
969 self::$groupIterator = new CRM_Contact_BAO_GroupNesting($styledLabels);
970 }
971 return self::$groupIterator;
972 }
973
974 /**
d09edf64 975 * Get all permissioned groups from database.
6a488035
TO
976 *
977 * The static array group is returned, and if it's
978 * called the first time, the <b>Group DAO</b> is used
979 * to get all the groups.
980 *
981 * Note: any database errors will be trapped by the DAO.
982 *
6a0b768e
TO
983 * @param string $groupType
984 * Type of group(Access/Mailing).
985 * @param bool $excludeHidden
986 * Exclude hidden groups.
dd244018 987 *
6a488035 988 *
a6c01b45
CW
989 * @return array
990 * array reference of all groups.
6a488035
TO
991 */
992 public static function group($groupType = NULL, $excludeHidden = TRUE) {
993 return CRM_Core_Permission::group($groupType, $excludeHidden);
994 }
995
24431f7b 996 /**
d09edf64 997 * Fetch groups in a nested format suitable for use in select form element.
24431f7b
CW
998 * @param bool $checkPermissions
999 * @param string|null $groupType
1000 * @param bool $excludeHidden
1001 * @return array
1002 */
1003 public static function nestedGroup($checkPermissions = TRUE, $groupType = NULL, $excludeHidden = TRUE) {
1004 $groups = $checkPermissions ? self::group($groupType, $excludeHidden) : self::allGroup($groupType, $excludeHidden);
1005 return CRM_Contact_BAO_Group::getGroupsHierarchy($groups, NULL, '&nbsp;&nbsp;', TRUE);
1006 }
1007
6a488035 1008 /**
d09edf64 1009 * Get all permissioned groups from database.
6a488035
TO
1010 *
1011 * The static array group is returned, and if it's
1012 * called the first time, the <b>Group DAO</b> is used
1013 * to get all the groups.
1014 *
1015 * Note: any database errors will be trapped by the DAO.
1016 *
6a488035 1017 *
dd244018
EM
1018 * @param bool $onlyPublic
1019 * @param null $groupType
1020 * @param bool $excludeHidden
6a488035 1021 *
a6c01b45
CW
1022 * @return array
1023 * array reference of all groups.
6a488035
TO
1024 */
1025 public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
1026 if (!self::$staticGroup) {
1027 $condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
1028 if ($onlyPublic) {
1029 $condition .= " AND visibility != 'User and User Admin Only'";
1030 }
1031
1032 if ($groupType) {
1033 $condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType);
1034 }
1035
1036 if ($excludeHidden) {
1037 $condition .= ' AND is_hidden != 1 ';
1038 }
1039
1040 self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
1041 }
1042
1043 return self::$staticGroup;
1044 }
1045
6a488035
TO
1046 /**
1047 * Get all Relationship Types from database.
1048 *
1049 * The static array group is returned, and if it's
1050 * called the first time, the <b>RelationshipType DAO</b> is used
1051 * to get all the relationship types.
1052 *
1053 * Note: any database errors will be trapped by the DAO.
1054 *
6a0b768e
TO
1055 * @param string $valueColumnName
1056 * Db column name/label.
1057 * @param bool $reset
1058 * Reset relationship types if true.
6a488035 1059 *
6a488035 1060 *
a6c01b45
CW
1061 * @return array
1062 * array reference of all relationship types.
6a488035
TO
1063 */
1064 public static function &relationshipType($valueColumnName = 'label', $reset = FALSE) {
1065 if (!CRM_Utils_Array::value($valueColumnName, self::$relationshipType) || $reset) {
1066 self::$relationshipType[$valueColumnName] = array();
1067
1068 //now we have name/label columns CRM-3336
1069 $column_a_b = "{$valueColumnName}_a_b";
1070 $column_b_a = "{$valueColumnName}_b_a";
1071
1072 $relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
1073 $relationshipTypeDAO->selectAdd();
1074 $relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
1075 $relationshipTypeDAO->is_active = 1;
1076 $relationshipTypeDAO->find();
1077 while ($relationshipTypeDAO->fetch()) {
1078
1079 self::$relationshipType[$valueColumnName][$relationshipTypeDAO->id] = array(
05802b8e 1080 'id' => $relationshipTypeDAO->id,
6a488035
TO
1081 $column_a_b => $relationshipTypeDAO->$column_a_b,
1082 $column_b_a => $relationshipTypeDAO->$column_b_a,
1083 'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
1084 'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
1085 'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
1086 'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
1087 );
1088 }
1089 }
1090
1091 return self::$relationshipType[$valueColumnName];
1092 }
1093
6a488035 1094 /**
100fef9d 1095 * Get all the ISO 4217 currency codes
6a488035
TO
1096 *
1097 * so far, we use this for validation only, so there's no point of putting this into the database
1098 *
6a488035 1099 *
a6c01b45
CW
1100 * @return array
1101 * array reference of all currency codes
6a488035
TO
1102 */
1103 public static function &currencyCode() {
1104 if (!self::$currencyCode) {
1105 self::$currencyCode = array(
1106 'AFN',
1107 'ALL',
1108 'DZD',
1109 'USD',
1110 'EUR',
1111 'AOA',
1112 'XCD',
1113 'XCD',
1114 'ARS',
1115 'AMD',
1116 'AWG',
1117 'AUD',
1118 'EUR',
1119 'AZM',
1120 'BSD',
1121 'BHD',
1122 'BDT',
1123 'BBD',
1124 'BYR',
1125 'EUR',
1126 'BZD',
1127 'XOF',
1128 'BMD',
1129 'INR',
1130 'BTN',
1131 'BOB',
1132 'BOV',
1133 'BAM',
1134 'BWP',
1135 'NOK',
1136 'BRL',
1137 'USD',
1138 'BND',
1139 'BGN',
1140 'XOF',
1141 'BIF',
1142 'KHR',
1143 'XAF',
1144 'CAD',
1145 'CVE',
1146 'KYD',
1147 'XAF',
1148 'XAF',
1149 'CLP',
1150 'CLF',
1151 'CNY',
1152 'AUD',
1153 'AUD',
1154 'COP',
1155 'COU',
1156 'KMF',
1157 'XAF',
1158 'CDF',
1159 'NZD',
1160 'CRC',
1161 'XOF',
1162 'HRK',
1163 'CUP',
1164 'CYP',
1165 'CZK',
1166 'DKK',
1167 'DJF',
1168 'XCD',
1169 'DOP',
1170 'USD',
1171 'EGP',
1172 'SVC',
1173 'USD',
1174 'XAF',
1175 'ERN',
1176 'EEK',
1177 'ETB',
1178 'FKP',
1179 'DKK',
1180 'FJD',
1181 'EUR',
1182 'EUR',
1183 'EUR',
1184 'XPF',
1185 'EUR',
1186 'XAF',
1187 'GMD',
1188 'GEL',
1189 'EUR',
1190 'GHC',
1191 'GIP',
1192 'EUR',
1193 'DKK',
1194 'XCD',
1195 'EUR',
1196 'USD',
1197 'GTQ',
1198 'GNF',
1199 'GWP',
1200 'XOF',
1201 'GYD',
1202 'HTG',
1203 'USD',
1204 'AUD',
1205 'EUR',
1206 'HNL',
1207 'HKD',
1208 'HUF',
1209 'ISK',
1210 'INR',
1211 'IDR',
1212 'XDR',
1213 'IRR',
1214 'IQD',
1215 'EUR',
1216 'ILS',
1217 'EUR',
1218 'JMD',
1219 'JPY',
1220 'JOD',
1221 'KZT',
1222 'KES',
1223 'AUD',
1224 'KPW',
1225 'KRW',
1226 'KWD',
1227 'KGS',
1228 'LAK',
1229 'LVL',
1230 'LBP',
1231 'ZAR',
1232 'LSL',
1233 'LRD',
1234 'LYD',
1235 'CHF',
1236 'LTL',
1237 'EUR',
1238 'MOP',
1239 'MKD',
1240 'MGA',
1241 'MWK',
1242 'MYR',
1243 'MVR',
1244 'XOF',
1245 'MTL',
1246 'USD',
1247 'EUR',
1248 'MRO',
1249 'MUR',
1250 'EUR',
1251 'MXN',
1252 'MXV',
1253 'USD',
1254 'MDL',
1255 'EUR',
1256 'MNT',
1257 'XCD',
1258 'MAD',
1259 'MZM',
1260 'MMK',
1261 'ZAR',
1262 'NAD',
1263 'AUD',
1264 'NPR',
1265 'EUR',
1266 'ANG',
1267 'XPF',
1268 'NZD',
1269 'NIO',
1270 'XOF',
1271 'NGN',
1272 'NZD',
1273 'AUD',
1274 'USD',
1275 'NOK',
1276 'OMR',
1277 'PKR',
1278 'USD',
1279 'PAB',
1280 'USD',
1281 'PGK',
1282 'PYG',
1283 'PEN',
1284 'PHP',
1285 'NZD',
1286 'PLN',
1287 'EUR',
1288 'USD',
1289 'QAR',
1290 'EUR',
1291 'ROL',
1292 'RON',
1293 'RUB',
1294 'RWF',
1295 'SHP',
1296 'XCD',
1297 'XCD',
1298 'EUR',
1299 'XCD',
1300 'WST',
1301 'EUR',
1302 'STD',
1303 'SAR',
1304 'XOF',
1305 'CSD',
1306 'EUR',
1307 'SCR',
1308 'SLL',
1309 'SGD',
1310 'SKK',
1311 'SIT',
1312 'SBD',
1313 'SOS',
1314 'ZAR',
1315 'EUR',
1316 'LKR',
1317 'SDD',
1318 'SRD',
1319 'NOK',
1320 'SZL',
1321 'SEK',
1322 'CHF',
1323 'CHW',
1324 'CHE',
1325 'SYP',
1326 'TWD',
1327 'TJS',
1328 'TZS',
1329 'THB',
1330 'USD',
1331 'XOF',
1332 'NZD',
1333 'TOP',
1334 'TTD',
1335 'TND',
1336 'TRY',
1337 'TRL',
1338 'TMM',
1339 'USD',
1340 'AUD',
1341 'UGX',
1342 'UAH',
1343 'AED',
1344 'GBP',
1345 'USD',
1346 'USS',
1347 'USN',
1348 'USD',
1349 'UYU',
1350 'UZS',
1351 'VUV',
1352 'VEB',
1353 'VND',
1354 'USD',
1355 'USD',
1356 'XPF',
1357 'MAD',
1358 'YER',
1359 'ZMK',
1360 'ZWD',
1361 'XAU',
1362 'XBA',
1363 'XBB',
1364 'XBC',
1365 'XBD',
1366 'XPD',
1367 'XPT',
1368 'XAG',
1369 'XFU',
1370 'XFO',
1371 'XTS',
1372 'XXX',
1373 );
1374 }
1375 return self::$currencyCode;
1376 }
1377
1378 /**
1379 * Get all the County from database.
1380 *
1381 * The static array county is returned, and if it's
1382 * called the first time, the <b>County DAO</b> is used
1383 * to get all the Counties.
1384 *
1385 * Note: any database errors will be trapped by the DAO.
1386 *
6a488035 1387 *
6a0b768e 1388 * @param bool|int $id - Optional id to return
6a488035 1389 *
a6c01b45
CW
1390 * @return array
1391 * array reference of all Counties
6a488035
TO
1392 */
1393 public static function &county($id = FALSE) {
1394 if (!self::$county) {
1395
1396 $config = CRM_Core_Config::singleton();
1397 // order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
1398 self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
1399 }
1400 if ($id) {
1401 if (array_key_exists($id, self::$county)) {
1402 return self::$county[$id];
1403 }
1404 else {
7cdc9e9f 1405 return CRM_Core_DAO::$_nullObject;
6a488035
TO
1406 }
1407 }
1408 return self::$county;
1409 }
1410
6a488035 1411 /**
c490a46a 1412 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
1413 * Get all active payment processors
1414 *
1415 * The static array paymentProcessor is returned
1416 *
6a488035 1417 *
6a0b768e
TO
1418 * @param bool $all
1419 * Get payment processors - default is to get only active ones.
1420 * @param bool $test
1421 * Get test payment processors.
6a488035 1422 *
77b97be7 1423 * @param null $additionalCond
6a488035 1424 *
a6c01b45
CW
1425 * @return array
1426 * array of all payment processors
6a488035
TO
1427 */
1428 public static function &paymentProcessor($all = FALSE, $test = FALSE, $additionalCond = NULL) {
1429 $condition = "is_test = ";
1430 $condition .= ($test) ? '1' : '0';
1431
1432 if ($additionalCond) {
1433 $condition .= " AND ( $additionalCond ) ";
1434 }
1435
1436 // CRM-7178. Make sure we only include payment processors valid in ths
1437 // domain
1438 $condition .= " AND domain_id = " . CRM_Core_Config::domainID();
1439
1440 $cacheKey = $condition . '_' . (int) $all;
1441 if (!isset(self::$paymentProcessor[$cacheKey])) {
1442 self::populate(self::$paymentProcessor[$cacheKey], 'CRM_Financial_DAO_PaymentProcessor', $all, 'name', 'is_active', $condition, 'is_default desc, name');
1443 }
1444
1445 return self::$paymentProcessor[$cacheKey];
1446 }
1447
1448 /**
c490a46a 1449 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
6a488035
TO
1450 *
1451 * The static array paymentProcessorType is returned
1452 *
6a488035 1453 *
6a0b768e
TO
1454 * @param bool $all
1455 * Get payment processors - default is to get only active ones.
6a488035 1456 *
100fef9d 1457 * @param int $id
77b97be7 1458 * @param string $return
6a488035 1459 *
a6c01b45
CW
1460 * @return array
1461 * array of all payment processor types
6a488035
TO
1462 */
1463 public static function &paymentProcessorType($all = FALSE, $id = NULL, $return = 'title') {
86bfa4f6 1464 $cacheKey = $id . '_' . $return;
6a488035
TO
1465 if (empty(self::$paymentProcessorType[$cacheKey])) {
1466 self::populate(self::$paymentProcessorType[$cacheKey], 'CRM_Financial_DAO_PaymentProcessorType', $all, $return, 'is_active', NULL, "is_default, $return", 'id');
1467 }
1468 if ($id && CRM_Utils_Array::value($id, self::$paymentProcessorType[$cacheKey])) {
1469 return self::$paymentProcessorType[$cacheKey][$id];
1470 }
1471 return self::$paymentProcessorType[$cacheKey];
1472 }
1473
1474 /**
d09edf64 1475 * Get all the World Regions from Database.
6a488035 1476 *
6a488035 1477 *
77b97be7
EM
1478 * @param bool $id
1479 *
a6c01b45
CW
1480 * @return array
1481 * array reference of all World Regions
6a488035
TO
1482 */
1483 public static function &worldRegion($id = FALSE) {
1484 if (!self::$worldRegions) {
1485 self::populate(self::$worldRegions, 'CRM_Core_DAO_Worldregion', TRUE, 'name', NULL, NULL, 'id');
1486 }
1487
1488 if ($id) {
1489 if (array_key_exists($id, self::$worldRegions)) {
1490 return self::$worldRegions[$id];
1491 }
1492 else {
7cdc9e9f 1493 return CRM_Core_DAO::$_nullObject;
6a488035
TO
1494 }
1495 }
1496
1497 return self::$worldRegions;
1498 }
1499
6a488035 1500 /**
c490a46a 1501 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
29494eef 1502 *
6a488035
TO
1503 * Get all Activity Statuses.
1504 *
1505 * The static array activityStatus is returned
1506 *
6a488035 1507 *
77b97be7
EM
1508 * @param string $column
1509 *
a6c01b45
CW
1510 * @return array
1511 * array reference of all activity statuses
6a488035
TO
1512 */
1513 public static function &activityStatus($column = 'label') {
1514 if (NULL === self::$activityStatus) {
1515 self::$activityStatus = array();
1516 }
1517 if (!array_key_exists($column, self::$activityStatus)) {
1518 self::$activityStatus[$column] = array();
1519
1520 self::$activityStatus[$column] = CRM_Core_OptionGroup::values('activity_status', FALSE, FALSE, FALSE, NULL, $column);
1521 }
1522
1523 return self::$activityStatus[$column];
1524 }
1525
6a488035 1526 /**
c490a46a 1527 * @deprecated Please use the buildOptions() method in the appropriate BAO object.
a19392e7 1528 *
6a488035
TO
1529 * Get all Visibility levels.
1530 *
1531 * The static array visibility is returned
1532 *
6a488035 1533 *
77b97be7 1534 * @param string $column
6a488035 1535 *
a6c01b45
CW
1536 * @return array
1537 * array reference of all Visibility levels.
6a488035
TO
1538 */
1539 public static function &visibility($column = 'label') {
1540 if (!isset(self::$visibility)) {
2aa397bc 1541 self::$visibility = array();
6a488035
TO
1542 }
1543
1544 if (!isset(self::$visibility[$column])) {
1545 self::$visibility[$column] = CRM_Core_OptionGroup::values('visibility', FALSE, FALSE, FALSE, NULL, $column);
2aa397bc 1546 }
6a488035
TO
1547
1548 return self::$visibility[$column];
1549 }
1550
a0ee3941 1551 /**
100fef9d 1552 * @param int $countryID
a0ee3941
EM
1553 * @param string $field
1554 *
1555 * @return array
1556 */
6a488035
TO
1557 public static function &stateProvinceForCountry($countryID, $field = 'name') {
1558 static $_cache = NULL;
1559
1560 $cacheKey = "{$countryID}_{$field}";
1561 if (!$_cache) {
1562 $_cache = array();
1563 }
1564
1565 if (!empty($_cache[$cacheKey])) {
1566 return $_cache[$cacheKey];
1567 }
1568
1569 $query = "
1570SELECT civicrm_state_province.{$field} name, civicrm_state_province.id id
1571 FROM civicrm_state_province
1572WHERE country_id = %1
1573ORDER BY name";
1574 $params = array(
1575 1 => array(
1576 $countryID,
1577 'Integer',
1578 ),
1579 );
1580
1581 $dao = CRM_Core_DAO::executeQuery($query, $params);
1582
1583 $result = array();
1584 while ($dao->fetch()) {
1585 $result[$dao->id] = $dao->name;
1586 }
1587
1588 // localise the stateProvince names if in an non-en_US locale
1589 $config = CRM_Core_Config::singleton();
1590 global $tsLocale;
1591 if ($tsLocale != '' and $tsLocale != 'en_US') {
1592 $i18n = CRM_Core_I18n::singleton();
1593 $i18n->localizeArray($result, array(
1594 'context' => 'province',
1595 ));
1596 $result = CRM_Utils_Array::asort($result);
1597 }
1598
1599 $_cache[$cacheKey] = $result;
1600
1601 CRM_Utils_Hook::buildStateProvinceForCountry($countryID, $result);
1602
1603 return $result;
1604 }
1605
a0ee3941 1606 /**
100fef9d 1607 * @param int $stateID
a0ee3941
EM
1608 *
1609 * @return array
1610 */
6a488035
TO
1611 public static function &countyForState($stateID) {
1612 if (is_array($stateID)) {
1613 $states = implode(", ", $stateID);
1614 $query = "
1615 SELECT civicrm_county.name name, civicrm_county.id id, civicrm_state_province.abbreviation abbreviation
1616 FROM civicrm_county
1617 LEFT JOIN civicrm_state_province ON civicrm_county.state_province_id = civicrm_state_province.id
1618 WHERE civicrm_county.state_province_id in ( $states )
1619 ORDER BY civicrm_state_province.abbreviation, civicrm_county.name";
1620
1621 $dao = CRM_Core_DAO::executeQuery($query);
1622
1623 $result = array();
1624 while ($dao->fetch()) {
1625 $result[$dao->id] = $dao->abbreviation . ': ' . $dao->name;
1626 }
1627 }
1628 else {
1629
1630 static $_cache = NULL;
1631
1632 $cacheKey = "{$stateID}_name";
1633 if (!$_cache) {
1634 $_cache = array();
1635 }
1636
1637 if (!empty($_cache[$cacheKey])) {
1638 return $_cache[$cacheKey];
1639 }
1640
1641 $query = "
1642 SELECT civicrm_county.name name, civicrm_county.id id
1643 FROM civicrm_county
1644 WHERE state_province_id = %1
1645 ORDER BY name";
1646 $params = array(
1647 1 => array(
1648 $stateID,
1649 'Integer',
1650 ),
1651 );
1652
1653 $dao = CRM_Core_DAO::executeQuery($query, $params);
1654
1655 $result = array();
1656 while ($dao->fetch()) {
1657 $result[$dao->id] = $dao->name;
1658 }
1659 }
1660
1661 return $result;
1662 }
1663
b05e28de
DL
1664 /**
1665 * Given a state ID return the country ID, this allows
1666 * us to populate forms and values for downstream code
1667 *
5a4f6742 1668 * @param int $stateID
b05e28de 1669 *
a6c01b45
CW
1670 * @return int
1671 * the country id that the state belongs to
b05e28de 1672 */
00be9182 1673 public static function countryIDForStateID($stateID) {
b05e28de 1674 if (empty($stateID)) {
7cdc9e9f 1675 return CRM_Core_DAO::$_nullObject;
b05e28de
DL
1676 }
1677
1678 $query = "
1679SELECT country_id
1680FROM civicrm_state_province
1681WHERE id = %1
1682";
1683 $params = array(1 => array($stateID, 'Integer'));
1684
1685 return CRM_Core_DAO::singleValueQuery($query, $params);
1686 }
1687
6a488035
TO
1688 /**
1689 * Get all types of Greetings.
1690 *
1691 * The static array of greeting is returned
1692 *
6a488035 1693 *
6a0b768e
TO
1694 * @param $filter
1695 * Get All Email Greetings - default is to get only active ones.
6a488035 1696 *
77b97be7 1697 * @param string $columnName
6a488035 1698 *
a6c01b45
CW
1699 * @return array
1700 * array reference of all greetings.
6a488035
TO
1701 */
1702 public static function greeting($filter, $columnName = 'label') {
1703 $index = $filter['greeting_type'] . '_' . $columnName;
1704
1705 // also add contactType to the array
1706 $contactType = CRM_Utils_Array::value('contact_type', $filter);
1707 if ($contactType) {
1708 $index .= '_' . $contactType;
1709 }
1710
1711 if (NULL === self::$greeting) {
1712 self::$greeting = array();
1713 }
1714
1715 if (!CRM_Utils_Array::value($index, self::$greeting)) {
1716 $filterCondition = NULL;
1717 if ($contactType) {
1718 $filterVal = 'v.filter =';
1719 switch ($contactType) {
1720 case 'Individual':
1721 $filterVal .= "1";
1722 break;
1723
1724 case 'Household':
1725 $filterVal .= "2";
1726 break;
1727
1728 case 'Organization':
1729 $filterVal .= "3";
1730 break;
1731 }
1732 $filterCondition .= "AND (v.filter = 0 OR {$filterVal}) ";
1733 }
1734
1735 self::$greeting[$index] = CRM_Core_OptionGroup::values($filter['greeting_type'], NULL, NULL, NULL, $filterCondition, $columnName);
1736 }
1737
1738 return self::$greeting[$index];
1739 }
1740
1741 /**
d09edf64 1742 * Construct array of default greeting values for contact type.
6a488035 1743 *
6a488035 1744 *
a6c01b45
CW
1745 * @return array
1746 * array reference of default greetings.
6a488035
TO
1747 */
1748 public static function &greetingDefaults() {
1749 if (!self::$greetingDefaults) {
1750 $defaultGreetings = array();
353ffa53
TO
1751 $contactTypes = self::get('CRM_Contact_DAO_Contact', 'contact_type', array(
1752 'keyColumn' => 'id',
8d7a9d07 1753 'labelColumn' => 'name',
353ffa53 1754 ));
6a488035 1755
091fe2a8 1756 foreach ($contactTypes as $filter => $contactType) {
6a488035
TO
1757 $filterCondition = " AND (v.filter = 0 OR v.filter = $filter) AND v.is_default = 1 ";
1758
1759 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
1760 $tokenVal = CRM_Core_OptionGroup::values($greeting, NULL, NULL, NULL, $filterCondition, 'label');
1761 $defaultGreetings[$contactType][$greeting] = $tokenVal;
1762 }
1763 }
1764
1765 self::$greetingDefaults = $defaultGreetings;
1766 }
1767
1768 return self::$greetingDefaults;
1769 }
1770
6a488035 1771 /**
d09edf64 1772 * Get all extensions.
6a488035
TO
1773 *
1774 * The static array extensions
1775 *
1776 * FIXME: This is called by civix but not by any core code. We
1777 * should provide an API call which civix can use instead.
1778 *
6a488035 1779 *
a6c01b45
CW
1780 * @return array
1781 * array($fullyQualifiedName => $label) list of extensions
6a488035
TO
1782 */
1783 public static function &getExtensions() {
1784 if (!self::$extensions) {
1785 self::$extensions = array();
1786 $sql = '
1787 SELECT full_name, label
1788 FROM civicrm_extension
1789 WHERE is_active = 1
1790 ';
1791 $dao = CRM_Core_DAO::executeQuery($sql);
1792 while ($dao->fetch()) {
1793 self::$extensions[$dao->full_name] = $dao->label;
1794 }
1795 }
1796
1797 return self::$extensions;
1798 }
1799
f743a6eb 1800 /**
d09edf64 1801 * Get all options values.
f743a6eb
CW
1802 *
1803 * The static array option values is returned
1804 *
f743a6eb 1805 *
6a0b768e
TO
1806 * @param bool $optionGroupName
1807 * Get All Option Group values- default is to get only active ones.
f743a6eb 1808 *
100fef9d 1809 * @param int $id
77b97be7 1810 * @param null $condition
f743a6eb 1811 *
a6c01b45
CW
1812 * @return array
1813 * array reference of all Option Group Name
f743a6eb 1814 */
2aa397bc 1815 public static function accountOptionValues($optionGroupName, $id = NULL, $condition = NULL) {
f743a6eb
CW
1816 $cacheKey = $optionGroupName . '_' . $condition;
1817 if (empty(self::$accountOptionValues[$cacheKey])) {
2aa397bc 1818 self::$accountOptionValues[$cacheKey] = CRM_Core_OptionGroup::values($optionGroupName, FALSE, FALSE, FALSE, $condition);
f743a6eb
CW
1819 }
1820 if ($id) {
1821 return CRM_Utils_Array::value($id, self::$accountOptionValues[$cacheKey]);
1822 }
1823
1824 return self::$accountOptionValues[$cacheKey];
1825 }
1826
6a488035
TO
1827 /**
1828 * Fetch the list of active extensions of type 'module'
1829 *
5a4f6742
CW
1830 * @param bool $fresh
1831 * Whether to forcibly reload extensions list from canonical store.
6a488035 1832 *
a6c01b45
CW
1833 * @return array
1834 * array(array('prefix' => $, 'file' => $))
6a488035
TO
1835 */
1836 public static function getModuleExtensions($fresh = FALSE) {
1837 return CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles($fresh);
1838 }
dc428161 1839
1840
1841 /**
d09edf64 1842 * Get all tax rates.
dc428161 1843 *
1844 * The static array tax rates is returned
1845 *
a6c01b45
CW
1846 * @return array
1847 * array list of tax rates with the financial type
dc428161 1848 */
1849 public static function getTaxRates() {
1850 if (!self::$taxRates) {
1851 self::$taxRates = array();
1852 $sql = "
1853 SELECT fa.tax_rate, efa.entity_id
1854 FROM civicrm_entity_financial_account efa
1855 INNER JOIN civicrm_financial_account fa ON fa.id = efa.financial_account_id
1856 INNER JOIN civicrm_option_value cov ON cov.value = efa.account_relationship
1857 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
1858 WHERE efa.entity_table = 'civicrm_financial_type'
1859 AND cov.name = 'Sales Tax Account is'
1860 AND cog.name = 'account_relationship'
1861 AND fa.is_active = 1";
1862 $dao = CRM_Core_DAO::executeQuery($sql);
1863 while ($dao->fetch()) {
1864 self::$taxRates[$dao->entity_id] = $dao->tax_rate;
1865 }
1866 }
1867
1868 return self::$taxRates;
1869 }
96025800 1870
6a488035 1871}