Merge pull request #15810 from eileenmcnaughton/mem_fix
[civicrm-core.git] / CRM / Case / XMLProcessor.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Case_XMLProcessor {
18
19 /**
20 * FIXME: This does *NOT* belong in a static property, but we're too late in
21 * the 4.5-cycle to do the necessary cleanup.
22 *
23 * Format is [int $id => string $relTypeCname].
24 *
25 * @var array|null
26 */
27 public static $activityTypes = NULL;
28
29 /**
30 * @param $caseType
31 *
32 * @return FALSE|SimpleXMLElement
33 */
34 public function retrieve($caseType) {
35 return CRM_Case_XMLRepository::singleton()->retrieve($caseType);
36 }
37
38 /**
39 * This function was previously used to convert a case-type's
40 * machine-name to a file-name. However, it's mind-boggling
41 * that the file-name might be a munged version of the
42 * machine-name (which is itself a munged version of the
43 * display-name), and naming is now a more visible issue (since
44 * the overhaul of CaseType admin UI).
45 *
46 * Usage note: This is called externally by civix stubs as a
47 * sort of side-ways validation of the case-type's name
48 * (validation which was needed because of the unintuitive
49 * double-munge). We should update civix templates and then
50 * remove this function in Civi 4.6 or 5.0.
51 *
52 * @param string $caseType
53 * @return string
54 * @deprecated
55 * @see CRM_Case_BAO_CaseType::isValidName
56 */
57 public static function mungeCaseType($caseType) {
58 // trim all spaces from $caseType
59 $caseType = str_replace('_', ' ', $caseType);
60 $caseType = CRM_Utils_String::munge(ucwords($caseType), '', 0);
61 return $caseType;
62 }
63
64 /**
65 * @param bool $indexName
66 * @param bool $all
67 *
68 * @return array
69 */
70 public static function &allActivityTypes($indexName = TRUE, $all = FALSE) {
71 if (self::$activityTypes === NULL) {
72 self::$activityTypes = CRM_Case_PseudoConstant::caseActivityType($indexName, $all);
73 }
74 return self::$activityTypes;
75 }
76
77 /**
78 * Get all relationship type display labels (not machine names)
79 *
80 * @param bool $fromXML
81 * TODO: This parameter is always FALSE now so no longer needed.
82 * Is this to be used for lookup of values from XML?
83 * Relationships are recorded in XML from the perspective of the non-client
84 * while relationships in the UI and everywhere else are from the
85 * perspective of the client. Since the XML can't be expected to be
86 * switched, the direction needs to be translated.
87 * @return array
88 */
89 public function &allRelationshipTypes($fromXML = FALSE) {
90 if (!isset(Civi::$statics[__CLASS__]['reltypes'][$fromXML])) {
91 // Note this now includes disabled types too. The only place this
92 // function is being used is for comparison against a list, not
93 // displaying a dropdown list or something like that, so we need
94 // to include disabled.
95 $relationshipInfo = civicrm_api3('RelationshipType', 'get', [
96 'options' => ['limit' => 0],
97 ]);
98
99 Civi::$statics[__CLASS__]['reltypes'][$fromXML] = [];
100 foreach ($relationshipInfo['values'] as $id => $info) {
101 Civi::$statics[__CLASS__]['reltypes'][$fromXML][$id . '_b_a'] = ($fromXML) ? $info['label_a_b'] : $info['label_b_a'];
102 /**
103 * Exclude if bidirectional
104 * (Why? I'm thinking this was for consistency with the dropdown
105 * in ang/crmCaseType.js where it would be needed to avoid seeing
106 * duplicates in the dropdown. Not sure if needed here but keeping
107 * as-is.)
108 */
109 if ($info['label_b_a'] !== $info['label_a_b']) {
110 Civi::$statics[__CLASS__]['reltypes'][$fromXML][$id . '_a_b'] = ($fromXML) ? $info['label_b_a'] : $info['label_a_b'];
111 }
112 }
113 }
114
115 return Civi::$statics[__CLASS__]['reltypes'][$fromXML];
116 }
117
118 /**
119 * FIXME: This should not exist
120 */
121 public static function flushStaticCaches() {
122 self::$activityTypes = NULL;
123 unset(Civi::$statics[__CLASS__]['reltypes']);
124 }
125
126 }