-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathPseudoConstant.php
1586 lines (1439 loc) · 47.6 KB
/
PseudoConstant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* Stores all constants and pseudo constants for CRM application.
*
* examples of constants are "Contact Type" which will always be either
* 'Individual', 'Household', 'Organization'.
*
* pseudo constants are entities from the database whose values rarely
* change. examples are list of countries, states, location types,
* relationship types.
*
* currently we're getting the data from the underlying database. this
* will be reworked to use caching.
*
* Note: All pseudoconstants should be uninitialized or default to NULL.
* This provides greater consistency/predictability after flushing.
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
class CRM_Core_PseudoConstant {
/**
* Static cache for pseudoconstant arrays.
* @var array
*/
private static $cache;
/**
* activity type
* @var array
* @deprecated Please use the buildOptions() method in the appropriate BAO object.
*/
private static $activityType;
/**
* States, provinces
* @var array
*/
private static $stateProvince;
/**
* Counties.
* @var array
*/
private static $county;
/**
* States/provinces abbreviations
* @var array
*/
private static $stateProvinceAbbreviation = [];
/**
* Country.
* @var array
*/
private static $country;
/**
* CountryIsoCode.
* @var array
*/
private static $countryIsoCode;
/**
* group
* @var array
* @deprecated Please use the buildOptions() method in the appropriate BAO object.
*/
private static $group;
/**
* RelationshipType
* @var array
*/
private static $relationshipType = [];
/**
* Civicrm groups that are not smart groups
* @var array
*/
private static $staticGroup;
/**
* Currency codes
* @var array
*/
private static $currencyCode;
/**
* Payment processor
* @var array
*/
private static $paymentProcessor;
/**
* Payment processor types
* @var array
*/
private static $paymentProcessorType;
/**
* World Region
* @var array
*/
private static $worldRegions;
/**
* activity status
* @var array
* @deprecated Please use the buildOptions() method in the appropriate BAO object.
*/
private static $activityStatus;
/**
* Visibility
* @var array
*/
private static $visibility;
/**
* Greetings
* @var array
*/
private static $greeting;
/**
* Extensions of type module
* @var array
*/
private static $extensions;
/**
* Financial Account Type
* @var array
*/
private static $accountOptionValues;
/**
* Low-level option getter, rarely accessed directly.
* NOTE: Rather than calling this function directly use CRM_*_BAO_*::buildOptions()
* @see https://docs.civicrm.org/dev/en/latest/framework/pseudoconstant/
*
* NOTE: If someone undertakes a refactoring of this, please consider the use-case of
* the Setting.getoptions API. There is no DAO/field, but it would be nice to use the
* same 'pseudoconstant' struct in *.settings.php. This means loosening the coupling
* between $field lookup and the $pseudoconstant evaluation.
*
* @param string $daoName
* @param string $fieldName
* @param array $params
* - name string name of the option group
* - flip boolean results are return in id => label format if false
* if true, the results are reversed
* - grouping boolean if true, return the value in 'grouping' column (currently unsupported for tables other than option_value)
* - localize boolean if true, localize the results before returning
* - condition string|array add condition(s) to the sql query - will be concatenated using 'AND'
* - keyColumn string the column to use for 'id'
* - labelColumn string the column to use for 'label'
* - orderColumn string the column to use for sorting, defaults to 'weight' column if one exists, else defaults to labelColumn
* - onlyActive boolean return only the action option values
* - fresh boolean ignore cache entries and go back to DB
* @param string $context : Context string
* @see CRM_Core_DAO::buildOptionsContext
*
* @return array|bool
* array on success, FALSE on error.
*
*/
public static function get($daoName, $fieldName, $params = [], $context = NULL) {
CRM_Core_DAO::buildOptionsContext($context);
$flip = !empty($params['flip']);
// Historically this was 'false' but according to the notes in
// CRM_Core_DAO::buildOptionsContext it should be context dependent.
// timidly changing for 'search' only to fix world_region in search options.
$localizeDefault = in_array($context, ['search']);
// Merge params with defaults
$params += [
'grouping' => FALSE,
'localize' => $localizeDefault,
'onlyActive' => !($context == 'validate' || $context == 'get'),
'fresh' => FALSE,
'context' => $context,
];
$entity = CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
// Custom fields are not in the schema
if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) {
$customField = new CRM_Core_BAO_CustomField();
$customField->id = (int) substr($fieldName, 7);
$options = $customField->getOptions($context);
if ($options && $flip) {
$options = array_flip($options);
}
return $options;
}
// Core field: load schema
$dao = new $daoName();
$fieldSpec = $dao->getFieldSpec($fieldName);
// Return false if field doesn't exist.
if (empty($fieldSpec)) {
return FALSE;
}
// Ensure we have the canonical name for this field
$fieldName = $fieldSpec['name'] ?? $fieldName;
if (!empty($fieldSpec['pseudoconstant'])) {
$pseudoconstant = $fieldSpec['pseudoconstant'];
// if callback is specified..
if (!empty($pseudoconstant['callback'])) {
$fieldOptions = call_user_func(Civi\Core\Resolver::singleton()->get($pseudoconstant['callback']), $context, $params);
//CRM-18223: Allow additions to field options via hook.
CRM_Utils_Hook::fieldOptions($entity, $fieldName, $fieldOptions, $params);
return $fieldOptions;
}
// Merge params with schema defaults
$params += [
'condition' => $pseudoconstant['condition'] ?? [],
'keyColumn' => $pseudoconstant['keyColumn'] ?? NULL,
'labelColumn' => $pseudoconstant['labelColumn'] ?? NULL,
];
// Fetch option group from option_value table
if (!empty($pseudoconstant['optionGroupName'])) {
if ($context == 'validate') {
$params['labelColumn'] = 'name';
}
if ($context == 'match') {
$params['keyColumn'] = 'name';
}
// Call our generic fn for retrieving from the option_value table
$options = CRM_Core_OptionGroup::values(
$pseudoconstant['optionGroupName'],
$flip,
$params['grouping'],
$params['localize'],
$params['condition'] ? ' AND ' . implode(' AND ', (array) $params['condition']) : NULL,
$params['labelColumn'] ? $params['labelColumn'] : 'label',
$params['onlyActive'],
$params['fresh'],
$params['keyColumn'] ? $params['keyColumn'] : 'value',
!empty($params['orderColumn']) ? $params['orderColumn'] : 'weight'
);
CRM_Utils_Hook::fieldOptions($entity, $fieldName, $options, $params);
return $options;
}
// Fetch options from other tables
if (!empty($pseudoconstant['table'])) {
CRM_Utils_Array::remove($params, 'flip', 'fresh');
// Normalize params so the serialized cache string will be consistent.
ksort($params);
$cacheKey = $daoName . $fieldName . serialize($params);
// Retrieve cached options
if (isset(\Civi::$statics[__CLASS__][$cacheKey]) && empty($params['fresh'])) {
$output = \Civi::$statics[__CLASS__][$cacheKey];
}
else {
$output = self::renderOptionsFromTablePseudoconstant($pseudoconstant, $params, ($fieldSpec['localize_context'] ?? NULL), $context);
CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
\Civi::$statics[__CLASS__][$cacheKey] = $output;
}
return $flip ? array_flip($output) : $output;
}
}
// Return "Yes" and "No" for boolean fields
elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) {
$output = $context == 'validate' ? [0, 1] : CRM_Core_SelectValues::boolean();
CRM_Utils_Hook::fieldOptions($entity, $fieldName, $output, $params);
return $flip ? array_flip($output) : $output;
}
// If we're still here, it's an error. Return FALSE.
return FALSE;
}
/**
* Fetch the translated label for a field given its key.
*
* @param string $baoName
* @param string $fieldName
* @param string|int $key
*
* TODO: Accept multivalued input?
*
* @return bool|null|string
* FALSE if the given field has no associated option list
* NULL if the given key has no corresponding option
* String if label is found
*/
public static function getLabel($baoName, $fieldName, $key) {
$values = $baoName::buildOptions($fieldName, 'get');
if ($values === FALSE) {
return FALSE;
}
return $values[$key] ?? NULL;
}
/**
* Fetch the machine name for a field given its key.
*
* @param string $baoName
* @param string $fieldName
* @param string|int $key
*
* @return bool|null|string
* FALSE if the given field has no associated option list
* NULL if the given key has no corresponding option
* String if label is found
*/
public static function getName($baoName, $fieldName, $key) {
$values = $baoName::buildOptions($fieldName, 'validate');
if ($values === FALSE) {
return FALSE;
}
return $values[$key] ?? NULL;
}
/**
* Fetch the key for a field option given its name.
*
* @param string $baoName
* @param string $fieldName
* @param string|int $value
*
* @return bool|null|string|int
* FALSE if the given field has no associated option list
* NULL if the given key has no corresponding option
* String|Number if key is found
*/
public static function getKey($baoName, $fieldName, $value) {
$values = $baoName::buildOptions($fieldName, 'validate');
if ($values === FALSE) {
return FALSE;
}
return CRM_Utils_Array::key($value, $values);
}
/**
* Lookup the admin page at which a field's option list can be edited
* @param $fieldSpec
* @return string|null
*/
public static function getOptionEditUrl($fieldSpec) {
// If it's an option group, that's easy
if (!empty($fieldSpec['pseudoconstant']['optionGroupName'])) {
return 'civicrm/admin/options/' . $fieldSpec['pseudoconstant']['optionGroupName'];
}
// For everything else...
elseif (!empty($fieldSpec['pseudoconstant']['table'])) {
$daoName = CRM_Core_DAO_AllCoreTables::getClassForTable($fieldSpec['pseudoconstant']['table']);
if (!$daoName) {
return NULL;
}
// We don't have good mapping so have to do a bit of guesswork from the menu
list(, $parent, , $child) = explode('_', $daoName);
$sql = "SELECT path FROM civicrm_menu
WHERE page_callback LIKE '%CRM_Admin_Page_$child%' OR page_callback LIKE '%CRM_{$parent}_Page_$child%'
ORDER BY page_callback
LIMIT 1";
return CRM_Core_DAO::singleValueQuery($sql);
}
return NULL;
}
/**
* @deprecated generic populate method.
* All pseudoconstant functions that use this method are also @deprecated
*
* The static array $var is populated from the db
* using the <b>$name DAO</b>.
*
* Note: any database errors will be trapped by the DAO.
*
* @param array $var
* The associative array we will fill.
* @param string $name
* The name of the DAO.
* @param bool $all
* Get all objects. default is to get only active ones.
* @param string $retrieve
* The field that we are interested in (normally name, differs in some objects).
* @param string $filter
* The field that we want to filter the result set with.
* @param string $condition
* The condition that gets passed to the final query as the WHERE clause.
*
* @param bool $orderby
* @param string $key
* @param bool $force
*
* @return array
*/
public static function populate(
&$var,
$name,
$all = FALSE,
$retrieve = 'name',
$filter = 'is_active',
$condition = NULL,
$orderby = NULL,
$key = 'id',
$force = NULL
) {
$cacheKey = CRM_Utils_Cache::cleanKey("CRM_PC_{$name}_{$all}_{$key}_{$retrieve}_{$filter}_{$condition}_{$orderby}");
$cache = CRM_Utils_Cache::singleton();
$var = $cache->get($cacheKey);
if ($var !== NULL && empty($force)) {
return $var;
}
/* @var CRM_Core_DAO $object */
$object = new $name();
$object->selectAdd();
$object->selectAdd("$key, $retrieve");
if ($condition) {
$object->whereAdd($condition);
}
if (!$orderby) {
$object->orderBy($retrieve);
}
else {
$object->orderBy($orderby);
}
if (!$all) {
$object->$filter = 1;
$aclClauses = array_filter($name::getSelectWhereClause());
foreach ($aclClauses as $clause) {
$object->whereAdd($clause);
}
}
$object->find();
$var = [];
while ($object->fetch()) {
$var[$object->$key] = $object->$retrieve;
}
$cache->set($cacheKey, $var);
}
/**
* Flush given pseudoconstant so it can be reread from db.
* nex time it's requested.
*
* @param bool|string $name pseudoconstant to be flushed
*/
public static function flush($name = 'cache') {
if (isset(self::$$name)) {
self::$$name = NULL;
}
if ($name == 'cache') {
CRM_Core_OptionGroup::flushAll();
if (isset(\Civi::$statics[__CLASS__])) {
unset(\Civi::$statics[__CLASS__]);
}
}
}
/**
* @deprecated Please use the buildOptions() method in the appropriate BAO object.
*
* Get all Activty types.
*
* The static array activityType is returned
*
*
* @return array
* array reference of all activity types.
*/
public static function &activityType() {
$args = func_get_args();
$all = CRM_Utils_Array::value(0, $args, TRUE);
$includeCaseActivities = CRM_Utils_Array::value(1, $args, FALSE);
$reset = CRM_Utils_Array::value(2, $args, FALSE);
$returnColumn = CRM_Utils_Array::value(3, $args, 'label');
$includeCampaignActivities = CRM_Utils_Array::value(4, $args, FALSE);
$onlyComponentActivities = CRM_Utils_Array::value(5, $args, FALSE);
$index = (int) $all . '_' . $returnColumn . '_' . (int) $includeCaseActivities;
$index .= '_' . (int) $includeCampaignActivities;
$index .= '_' . (int) $onlyComponentActivities;
if (NULL === self::$activityType) {
self::$activityType = [];
}
if (!isset(self::$activityType[$index]) || $reset) {
$condition = NULL;
if (!$all) {
$condition = 'AND filter = 0';
}
$componentClause = " v.component_id IS NULL";
if ($onlyComponentActivities) {
$componentClause = " v.component_id IS NOT NULL";
}
$componentIds = [];
$compInfo = CRM_Core_Component::getEnabledComponents();
// build filter for listing activity types only if their
// respective components are enabled
foreach ($compInfo as $compName => $compObj) {
if ($compName == 'CiviCase') {
if ($includeCaseActivities) {
$componentIds[] = $compObj->componentID;
}
}
elseif ($compName == 'CiviCampaign') {
if ($includeCampaignActivities) {
$componentIds[] = $compObj->componentID;
}
}
else {
$componentIds[] = $compObj->componentID;
}
}
if (count($componentIds)) {
$componentIds = implode(',', $componentIds);
$componentClause = " ($componentClause OR v.component_id IN ($componentIds))";
if ($onlyComponentActivities) {
$componentClause = " ( v.component_id IN ($componentIds ) )";
}
}
$condition = $condition . ' AND ' . $componentClause;
self::$activityType[$index] = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition, $returnColumn);
}
return self::$activityType[$index];
}
/**
* Get all the State/Province from database.
*
* The static array stateProvince is returned, and if it's
* called the first time, the <b>State Province DAO</b> is used
* to get all the States.
*
* Note: any database errors will be trapped by the DAO.
*
*
* @param bool|int $id - Optional id to return
*
* @param bool $limit
*
* @return array
* array reference of all State/Provinces.
*/
public static function &stateProvince($id = FALSE, $limit = TRUE) {
if (($id && empty(self::$stateProvince[$id])) || !self::$stateProvince || !$id) {
$whereClause = FALSE;
if ($limit) {
$countryIsoCodes = self::countryIsoCode();
$limitCodes = CRM_Core_BAO_Country::provinceLimit();
$limitIds = [];
foreach ($limitCodes as $code) {
$limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code));
}
if (!empty($limitIds)) {
$whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
}
else {
$whereClause = FALSE;
}
}
self::populate(self::$stateProvince, 'CRM_Core_DAO_StateProvince', TRUE, 'name', 'is_active', $whereClause);
// localise the province names if in an non-en_US locale
$tsLocale = CRM_Core_I18n::getLocale();
if ($tsLocale != '' and $tsLocale != 'en_US') {
$i18n = CRM_Core_I18n::singleton();
$i18n->localizeArray(self::$stateProvince, [
'context' => 'province',
]);
self::$stateProvince = CRM_Utils_Array::asort(self::$stateProvince);
}
}
if ($id) {
if (array_key_exists($id, self::$stateProvince)) {
return self::$stateProvince[$id];
}
else {
$result = NULL;
return $result;
}
}
return self::$stateProvince;
}
/**
* Get all the State/Province abbreviations from the database.
*
* Same as above, except gets the abbreviations instead of the names.
*
*
* @param bool|int $id - Optional id to return
*
* @param bool $limit
*
* @return array
* array reference of all State/Province abbreviations.
*/
public static function stateProvinceAbbreviation($id = FALSE, $limit = TRUE) {
if ($id && is_numeric($id)) {
if (!array_key_exists($id, (array) self::$stateProvinceAbbreviation)) {
$query = "SELECT abbreviation
FROM civicrm_state_province
WHERE id = %1";
$params = [
1 => [
$id,
'Integer',
],
];
self::$stateProvinceAbbreviation[$id] = CRM_Core_DAO::singleValueQuery($query, $params);
}
return self::$stateProvinceAbbreviation[$id];
}
else {
$whereClause = FALSE;
if ($limit) {
$countryIsoCodes = self::countryIsoCode();
$limitCodes = CRM_Core_BAO_Country::provinceLimit();
$limitIds = [];
foreach ($limitCodes as $code) {
$tmpArray = array_keys($countryIsoCodes, $code);
if (!empty($tmpArray)) {
$limitIds[] = array_shift($tmpArray);
}
}
if (!empty($limitIds)) {
$whereClause = 'country_id IN (' . implode(', ', $limitIds) . ')';
}
}
self::populate(self::$stateProvinceAbbreviation, 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', $whereClause);
}
return self::$stateProvinceAbbreviation;
}
/**
* Get all the State/Province abbreviations from the database for the specified country.
*
* @param int $countryID
*
* @return array
* array of all State/Province abbreviations for the given country.
*/
public static function stateProvinceAbbreviationForCountry($countryID) {
if (!isset(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID])) {
\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID] = [];
}
self::populate(\Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID], 'CRM_Core_DAO_StateProvince', TRUE, 'abbreviation', 'is_active', "country_id = " . (int) $countryID, 'abbreviation');
return \Civi::$statics[__CLASS__]['stateProvinceAbbreviationForCountry'][$countryID];
}
/**
* Get all the State/Province abbreviations from the database for the default country.
*
* @return array
* array of all State/Province abbreviations for the given country.
*/
public static function stateProvinceAbbreviationForDefaultCountry() {
return CRM_Core_PseudoConstant::stateProvinceAbbreviationForCountry(Civi::settings()->get('defaultContactCountry'));
}
/**
* Get all the countries from database.
*
* The static array country is returned, and if it's
* called the first time, the <b>Country DAO</b> is used
* to get all the countries.
*
* Note: any database errors will be trapped by the DAO.
*
*
* @param bool|int $id - Optional id to return
*
* @param bool $applyLimit
*
* @return array|null
* array reference of all countries.
*/
public static function country($id = FALSE, $applyLimit = TRUE) {
if (($id && empty(self::$country[$id])) || !self::$country || !$id) {
$config = CRM_Core_Config::singleton();
$limitCodes = [];
if ($applyLimit) {
// limit the country list to the countries specified in CIVICRM_COUNTRY_LIMIT
// (ensuring it's a subset of the legal values)
// K/P: We need to fix this, i dont think it works with new setting files
$limitCodes = CRM_Core_BAO_Country::countryLimit();
if (!is_array($limitCodes)) {
$limitCodes = [
$config->countryLimit => 1,
];
}
$limitCodes = array_intersect(self::countryIsoCode(), $limitCodes);
}
if (count($limitCodes)) {
$whereClause = "iso_code IN ('" . implode("', '", $limitCodes) . "')";
}
else {
$whereClause = NULL;
}
self::populate(self::$country, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active', $whereClause);
self::$country = CRM_Core_BAO_Country::_defaultContactCountries(self::$country);
}
if ($id) {
if (array_key_exists($id, self::$country)) {
return self::$country[$id];
}
else {
return NULL;
}
}
return self::$country;
}
/**
* Get all the country ISO Code abbreviations from the database.
*
* The static array countryIsoCode is returned, and if it's
* called the first time, the <b>Country DAO</b> is used
* to get all the countries' ISO codes.
*
* Note: any database errors will be trapped by the DAO.
*
*
* @param bool $id
*
* @return array
* array reference of all country ISO codes.
*/
public static function &countryIsoCode($id = FALSE) {
if (!self::$countryIsoCode) {
self::populate(self::$countryIsoCode, 'CRM_Core_DAO_Country', TRUE, 'iso_code');
}
if ($id) {
if (array_key_exists($id, self::$countryIsoCode)) {
return self::$countryIsoCode[$id];
}
else {
return CRM_Core_DAO::$_nullObject;
}
}
return self::$countryIsoCode;
}
/**
* @deprecated Please use the buildOptions() method in the appropriate BAO object.
*
* Get all groups from database
*
* The static array group is returned, and if it's
* called the first time, the <b>Group DAO</b> is used
* to get all the groups.
*
* Note: any database errors will be trapped by the DAO.
*
* @param string $groupType
* Type of group(Access/Mailing).
* @param bool $excludeHidden
* Exclude hidden groups.
*
*
* @return array
* array reference of all groups.
*/
public static function allGroup($groupType = NULL, $excludeHidden = TRUE) {
if ($groupType === 'validate') {
// validate gets passed through from getoptions. Handle in the deprecated
// fn rather than change the new pattern.
$groupType = NULL;
}
$condition = CRM_Contact_BAO_Group::groupTypeCondition($groupType, $excludeHidden);
$groupKey = ($groupType ? $groupType : 'null') . !empty($excludeHidden);
if (!isset(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey])) {
self::populate(Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey], 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition);
}
return Civi::$statics[__CLASS__]['groups']['allGroup'][$groupKey];
}
/**
* Get all permissioned groups from database.
*
* The static array group is returned, and if it's
* called the first time, the <b>Group DAO</b> is used
* to get all the groups.
*
* Note: any database errors will be trapped by the DAO.
*
* @param string $groupType
* Type of group(Access/Mailing).
* @param bool $excludeHidden
* Exclude hidden groups.
*
*
* @return array
* array reference of all groups.
*/
public static function group($groupType = NULL, $excludeHidden = TRUE) {
return CRM_Core_Permission::group($groupType, $excludeHidden);
}
/**
* Fetch groups in a nested format suitable for use in select form element.
* @param bool $checkPermissions
* @param string|null $groupType
* @param bool $excludeHidden
* @return array
*/
public static function nestedGroup(bool $checkPermissions = TRUE, $groupType = NULL, bool $excludeHidden = TRUE) {
$groups = $checkPermissions ? self::group($groupType, $excludeHidden) : self::allGroup($groupType, $excludeHidden);
return CRM_Contact_BAO_Group::getGroupsHierarchy($groups, NULL, ' ', TRUE);
}
/**
* Get all permissioned groups from database.
*
* The static array group is returned, and if it's
* called the first time, the <b>Group DAO</b> is used
* to get all the groups.
*
* Note: any database errors will be trapped by the DAO.
*
*
* @param bool $onlyPublic
* @param null $groupType
* @param bool $excludeHidden
*
* @return array
* array reference of all groups.
*/
public static function &staticGroup($onlyPublic = FALSE, $groupType = NULL, $excludeHidden = TRUE) {
if (!self::$staticGroup) {
$condition = 'saved_search_id = 0 OR saved_search_id IS NULL';
if ($onlyPublic) {
$condition .= " AND visibility != 'User and User Admin Only'";
}
if ($groupType) {
$condition .= ' AND ' . CRM_Contact_BAO_Group::groupTypeCondition($groupType);
}
if ($excludeHidden) {
$condition .= ' AND is_hidden != 1 ';
}
self::populate(self::$staticGroup, 'CRM_Contact_DAO_Group', FALSE, 'title', 'is_active', $condition, 'title');
}
return self::$staticGroup;
}
/**
* Get all Relationship Types from database.
*
* The static array group is returned, and if it's
* called the first time, the <b>RelationshipType DAO</b> is used
* to get all the relationship types.
*
* Note: any database errors will be trapped by the DAO.
*
* @param string $valueColumnName
* Db column name/label.
* @param bool $reset
* Reset relationship types if true.
* @param bool $isActive
* Filter by is_active. NULL to disable.
*
* @return array
* array reference of all relationship types.
*/
public static function relationshipType($valueColumnName = 'label', $reset = FALSE, $isActive = 1) {
$cacheKey = $valueColumnName . '::' . $isActive;
if (!isset(self::$relationshipType[$cacheKey]) || $reset) {
self::$relationshipType[$cacheKey] = [];
//now we have name/label columns CRM-3336
$column_a_b = "{$valueColumnName}_a_b";
$column_b_a = "{$valueColumnName}_b_a";
$relationshipTypeDAO = new CRM_Contact_DAO_RelationshipType();
$relationshipTypeDAO->selectAdd();
$relationshipTypeDAO->selectAdd("id, {$column_a_b}, {$column_b_a}, contact_type_a, contact_type_b, contact_sub_type_a, contact_sub_type_b");
if ($isActive !== NULL) {
$relationshipTypeDAO->is_active = $isActive;
}
$relationshipTypeDAO->find();
while ($relationshipTypeDAO->fetch()) {
self::$relationshipType[$cacheKey][$relationshipTypeDAO->id] = [
'id' => $relationshipTypeDAO->id,
$column_a_b => $relationshipTypeDAO->$column_a_b,
$column_b_a => $relationshipTypeDAO->$column_b_a,
'contact_type_a' => "$relationshipTypeDAO->contact_type_a",
'contact_type_b' => "$relationshipTypeDAO->contact_type_b",
'contact_sub_type_a' => "$relationshipTypeDAO->contact_sub_type_a",
'contact_sub_type_b' => "$relationshipTypeDAO->contact_sub_type_b",
];
}
}
return self::$relationshipType[$cacheKey];
}
/**
* Name => Label pairs for all relationship types
*
* @return array
*/
public static function relationshipTypeOptions() {
$relationshipTypes = [];
$relationshipLabels = self::relationshipType();
foreach (self::relationshipType('name') as $id => $type) {
$relationshipTypes[$type['name_a_b']] = $relationshipLabels[$id]['label_a_b'];
if ($type['name_b_a'] && $type['name_b_a'] != $type['name_a_b']) {
$relationshipTypes[$type['name_b_a']] = $relationshipLabels[$id]['label_b_a'];
}
}
return $relationshipTypes;
}
/**
* Get all the ISO 4217 currency codes
*
* so far, we use this for validation only, so there's no point of putting this into the database
*
*
* @return array
* array reference of all currency codes
*/
public static function ¤cyCode() {
if (!self::$currencyCode) {
$query = "SELECT name FROM civicrm_currency";
$dao = CRM_Core_DAO::executeQuery($query);
$currencyCode = [];
while ($dao->fetch()) {
self::$currencyCode[] = $dao->name;
}
}
return self::$currencyCode;
}
/**
* Get all the County from database.
*
* The static array county is returned, and if it's
* called the first time, the <b>County DAO</b> is used
* to get all the Counties.
*
* Note: any database errors will be trapped by the DAO.
*
*
* @param bool|int $id - Optional id to return
*
* @return array
* array reference of all Counties
*/
public static function &county($id = FALSE) {
if (!self::$county) {
$config = CRM_Core_Config::singleton();
// order by id so users who populate civicrm_county can have more control over sort by the order they load the counties
self::populate(self::$county, 'CRM_Core_DAO_County', TRUE, 'name', NULL, NULL, 'id');
}
if ($id) {
if (array_key_exists($id, self::$county)) {
return self::$county[$id];