Commit | Line | Data |
---|---|---|
fa1c763d TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.5 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2014 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2014 | |
32 | * $Id$ | |
33 | * | |
34 | * The XMLRepository is responsible for loading XML for case-types. | |
7458ddd3 TO |
35 | * It includes any bulk operations that apply across the list of all XML |
36 | * documents of all case-types. | |
fa1c763d TO |
37 | */ |
38 | class CRM_Case_XMLRepository { | |
39 | private static $singleton; | |
40 | ||
41 | /** | |
42 | * @var array<String,SimpleXMLElement> | |
43 | */ | |
44 | protected $xml = array(); | |
45 | ||
46 | /** | |
47 | * @var array|NULL | |
48 | */ | |
49 | protected $hookCache = NULL; | |
50 | ||
51 | /** | |
52 | * @var array|NULL symbolic names of case-types | |
53 | */ | |
54 | protected $allCaseTypes = NULL; | |
55 | ||
56 | /** | |
57 | * @param bool $fresh | |
e19323c9 | 58 | * @return CRM_Case_XMLRepository |
fa1c763d TO |
59 | */ |
60 | public static function singleton($fresh = FALSE) { | |
61 | if (!self::$singleton || $fresh) { | |
62 | self::$singleton = new static(); | |
63 | } | |
64 | return self::$singleton; | |
65 | } | |
66 | ||
67 | /** | |
68 | * @param array<String,SimpleXMLElement> $xml | |
69 | */ | |
e19323c9 TO |
70 | public function __construct($allCaseTypes = NULL, $xml = array()) { |
71 | $this->allCaseTypes = $allCaseTypes; | |
fa1c763d TO |
72 | $this->xml = $xml; |
73 | } | |
74 | ||
75 | /** | |
76 | * @param string $caseType | |
77 | * @return SimpleXMLElement|FALSE | |
78 | */ | |
79 | public function retrieve($caseType) { | |
ff7838ab | 80 | // check if xml definition is defined in db |
81 | $definition = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseType, 'definition', 'name'); | |
82 | ||
83 | if (!empty($definition)) { | |
84 | return simplexml_load_string($definition); | |
85 | } | |
86 | ||
fa1c763d TO |
87 | $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType); |
88 | ||
89 | if (!CRM_Utils_Array::value($caseType, $this->xml)) { | |
90 | // first check custom templates directory | |
91 | $fileName = NULL; | |
bb68492c TO |
92 | |
93 | if (!$fileName || !file_exists($fileName)) { | |
94 | $caseTypesViaHook = $this->getCaseTypesViaHook(); | |
95 | if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) { | |
96 | $fileName = $caseTypesViaHook[$caseType]['file']; | |
97 | } | |
fa1c763d TO |
98 | } |
99 | ||
bb68492c TO |
100 | if (!$fileName || !file_exists($fileName)) { |
101 | $config = CRM_Core_Config::singleton(); | |
102 | if (isset($config->customTemplateDir) && $config->customTemplateDir) { | |
103 | // check if the file exists in the custom templates directory | |
104 | $fileName = implode(DIRECTORY_SEPARATOR, | |
105 | array( | |
106 | $config->customTemplateDir, | |
107 | 'CRM', | |
108 | 'Case', | |
109 | 'xml', | |
110 | 'configuration', | |
111 | "$caseType.xml", | |
112 | ) | |
113 | ); | |
114 | } | |
115 | } | |
fa1c763d | 116 | |
bb68492c | 117 | if (!$fileName || !file_exists($fileName)) { |
fa1c763d TO |
118 | if (!file_exists($fileName)) { |
119 | // check if file exists locally | |
120 | $fileName = implode(DIRECTORY_SEPARATOR, | |
121 | array( | |
122 | dirname(__FILE__), | |
123 | 'xml', | |
bb68492c | 124 | 'configuration', |
fa1c763d TO |
125 | "$caseType.xml", |
126 | ) | |
127 | ); | |
128 | } | |
129 | ||
130 | if (!file_exists($fileName)) { | |
bb68492c TO |
131 | // check if file exists locally |
132 | $fileName = implode(DIRECTORY_SEPARATOR, | |
133 | array( | |
134 | dirname(__FILE__), | |
135 | 'xml', | |
136 | 'configuration.sample', | |
137 | "$caseType.xml", | |
138 | ) | |
139 | ); | |
fa1c763d TO |
140 | } |
141 | ||
142 | if (!file_exists($fileName)) { | |
143 | return FALSE; | |
144 | } | |
145 | } | |
146 | ||
147 | // read xml file | |
148 | $dom = new DomDocument(); | |
149 | $dom->load($fileName); | |
150 | $dom->xinclude(); | |
151 | $this->xml[$caseType] = simplexml_import_dom($dom); | |
152 | } | |
153 | return $this->xml[$caseType]; | |
154 | } | |
155 | ||
156 | /** | |
157 | * @return array | |
158 | * @see CRM_Utils_Hook::caseTypes | |
159 | */ | |
160 | public function getCaseTypesViaHook() { | |
161 | if ($this->hookCache === NULL) { | |
162 | $this->hookCache = array(); | |
163 | CRM_Utils_Hook::caseTypes($this->hookCache); | |
164 | } | |
165 | return $this->hookCache; | |
166 | } | |
e19323c9 TO |
167 | |
168 | /** | |
bb68492c | 169 | * @return array<string> symbolic names of case-types |
e19323c9 TO |
170 | */ |
171 | public function getAllCaseTypes() { | |
172 | if ($this->allCaseTypes === NULL) { | |
173 | $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name"); | |
174 | } | |
175 | return $this->allCaseTypes; | |
176 | } | |
177 | ||
7458ddd3 TO |
178 | /** |
179 | * @return array<string> symbolic-names of activity-types | |
180 | */ | |
181 | public function getAllDeclaredActivityTypes() { | |
182 | $result = array(); | |
183 | ||
184 | $p = new CRM_Case_XMLProcessor_Process(); | |
185 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
186 | $caseTypeXML = $this->retrieve($caseTypeName); | |
187 | $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML)); | |
188 | } | |
189 | ||
190 | $result = array_unique($result); | |
191 | sort($result); | |
192 | return $result; | |
193 | } | |
194 | ||
195 | /** | |
196 | * @return array<string> symbolic-names of relationship-types | |
197 | */ | |
198 | public function getAllDeclaredRelationshipTypes() { | |
199 | $result = array(); | |
200 | ||
201 | $p = new CRM_Case_XMLProcessor_Process(); | |
202 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
203 | $caseTypeXML = $this->retrieve($caseTypeName); | |
204 | $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML)); | |
205 | } | |
206 | ||
207 | $result = array_unique($result); | |
208 | sort($result); | |
209 | return $result; | |
210 | } | |
211 | ||
e19323c9 TO |
212 | /** |
213 | * Determine the number of times a particular activity-type is | |
214 | * referenced in CiviCase XML. | |
215 | * | |
216 | * @param string $activityType symbolic-name of an activity type | |
217 | * @return int | |
218 | */ | |
219 | function getActivityReferenceCount($activityType) { | |
220 | $p = new CRM_Case_XMLProcessor_Process(); | |
221 | $count = 0; | |
222 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
223 | $caseTypeXML = $this->retrieve($caseTypeName); | |
224 | if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) { | |
225 | $count++; | |
226 | } | |
227 | } | |
228 | return $count; | |
229 | } | |
230 | ||
231 | /** | |
232 | * Determine the number of times a particular activity-type is | |
233 | * referenced in CiviCase XML. | |
234 | * | |
235 | * @param string $relationshipTypeName symbolic-name of a relationship-type | |
236 | * @return int | |
237 | */ | |
238 | function getRelationshipReferenceCount($relationshipTypeName) { | |
239 | $p = new CRM_Case_XMLProcessor_Process(); | |
240 | $count = 0; | |
241 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
242 | $caseTypeXML = $this->retrieve($caseTypeName); | |
243 | if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) { | |
244 | $count++; | |
245 | } | |
246 | } | |
247 | return $count; | |
248 | } | |
249 | ||
fa1c763d | 250 | } |