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