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