Commit | Line | Data |
---|---|---|
fa1c763d TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
fa1c763d | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
fa1c763d TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
fa1c763d TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
fa1c763d TO |
32 | * |
33 | * The XMLRepository is responsible for loading XML for case-types. | |
7458ddd3 TO |
34 | * It includes any bulk operations that apply across the list of all XML |
35 | * documents of all case-types. | |
fa1c763d TO |
36 | */ |
37 | class CRM_Case_XMLRepository { | |
38 | private static $singleton; | |
39 | ||
40 | /** | |
41 | * @var array<String,SimpleXMLElement> | |
42 | */ | |
be2fb01f | 43 | protected $xml = []; |
fa1c763d TO |
44 | |
45 | /** | |
cc101011 | 46 | * @var array|null |
fa1c763d TO |
47 | */ |
48 | protected $hookCache = NULL; | |
49 | ||
50 | /** | |
cc101011 | 51 | * Symbolic names of case-types. |
52 | * | |
53 | * @var array|null | |
fa1c763d TO |
54 | */ |
55 | protected $allCaseTypes = NULL; | |
56 | ||
57 | /** | |
58 | * @param bool $fresh | |
e19323c9 | 59 | * @return CRM_Case_XMLRepository |
fa1c763d TO |
60 | */ |
61 | public static function singleton($fresh = FALSE) { | |
62 | if (!self::$singleton || $fresh) { | |
63 | self::$singleton = new static(); | |
64 | } | |
65 | return self::$singleton; | |
66 | } | |
67 | ||
e492e278 | 68 | public function flush() { |
be2fb01f | 69 | $this->xml = []; |
e492e278 TO |
70 | $this->hookCache = NULL; |
71 | $this->allCaseTypes = NULL; | |
be2fb01f | 72 | CRM_Core_DAO::$_dbColumnValueCache = []; |
e492e278 TO |
73 | } |
74 | ||
fa1c763d | 75 | /** |
54957108 | 76 | * Class constructor. |
77 | * | |
78 | * @param array $allCaseTypes | |
79 | * @param array $xml | |
fa1c763d | 80 | */ |
be2fb01f | 81 | public function __construct($allCaseTypes = NULL, $xml = []) { |
e19323c9 | 82 | $this->allCaseTypes = $allCaseTypes; |
fa1c763d TO |
83 | $this->xml = $xml; |
84 | } | |
85 | ||
86 | /** | |
54957108 | 87 | * Retrieve case. |
88 | * | |
fa1c763d | 89 | * @param string $caseType |
54957108 | 90 | * |
91 | * @return FALSE|\SimpleXMLElement | |
92 | * @throws \CRM_Core_Exception | |
fa1c763d TO |
93 | */ |
94 | public function retrieve($caseType) { | |
ff7838ab | 95 | // check if xml definition is defined in db |
96 | $definition = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseType', $caseType, 'definition', 'name'); | |
97 | ||
98 | if (!empty($definition)) { | |
edcc7f99 TO |
99 | list ($xml, $error) = CRM_Utils_XML::parseString($definition); |
100 | if (!$xml) { | |
101 | throw new CRM_Core_Exception("Failed to parse CaseType XML: $error"); | |
102 | } | |
103 | return $xml; | |
ff7838ab | 104 | } |
105 | ||
46ec593d TO |
106 | // TODO In 4.6 or 5.0, remove support for weird machine-names |
107 | //if (!CRM_Case_BAO_CaseType::isValidName($caseType)) { | |
108 | // // perhaps caller provider a the label instead of the name? | |
109 | // throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]"); | |
110 | //} | |
fa1c763d TO |
111 | |
112 | if (!CRM_Utils_Array::value($caseType, $this->xml)) { | |
32f1c917 TO |
113 | $fileXml = $this->retrieveFile($caseType); |
114 | if ($fileXml) { | |
115 | $this->xml[$caseType] = $fileXml; | |
0db6c3e1 TO |
116 | } |
117 | else { | |
32f1c917 | 118 | return FALSE; |
46ec593d | 119 | } |
32f1c917 TO |
120 | } |
121 | return $this->xml[$caseType]; | |
122 | } | |
bb68492c | 123 | |
32f1c917 | 124 | /** |
54957108 | 125 | * Retrieve file. |
126 | * | |
32f1c917 TO |
127 | * @param string $caseType |
128 | * @return SimpleXMLElement|FALSE | |
129 | */ | |
130 | public function retrieveFile($caseType) { | |
131 | $fileName = NULL; | |
132 | $fileXml = NULL; | |
fa1c763d | 133 | |
32f1c917 TO |
134 | if (CRM_Case_BAO_CaseType::isValidName($caseType)) { |
135 | // Search for a file based directly on the $caseType name | |
136 | $fileName = $this->findXmlFile($caseType); | |
137 | } | |
fa1c763d | 138 | |
32f1c917 TO |
139 | // For backward compatibility, also search for double-munged file names |
140 | // TODO In 4.6 or 5.0, remove support for loading double-munged file names | |
141 | if (!$fileName || !file_exists($fileName)) { | |
142 | $fileName = $this->findXmlFile(CRM_Case_XMLProcessor::mungeCaseType($caseType)); | |
143 | } | |
144 | ||
145 | if ($fileName && file_exists($fileName)) { | |
fa1c763d TO |
146 | // read xml file |
147 | $dom = new DomDocument(); | |
f9857c59 JP |
148 | $xmlString = file_get_contents($fileName); |
149 | $dom->loadXML($xmlString); | |
150 | $dom->documentURI = $fileName; | |
fa1c763d | 151 | $dom->xinclude(); |
32f1c917 | 152 | $fileXml = simplexml_import_dom($dom); |
fa1c763d | 153 | } |
32f1c917 TO |
154 | |
155 | return $fileXml; | |
fa1c763d TO |
156 | } |
157 | ||
a8354614 | 158 | /** |
54957108 | 159 | * Find xml file. |
160 | * | |
a8354614 | 161 | * @param string $caseType |
72b3a70c CW |
162 | * @return null|string |
163 | * file path | |
a8354614 | 164 | */ |
e547f744 TO |
165 | public function findXmlFile($caseType) { |
166 | // first check custom templates directory | |
a8354614 TO |
167 | $fileName = NULL; |
168 | ||
169 | if (!$fileName || !file_exists($fileName)) { | |
170 | $caseTypesViaHook = $this->getCaseTypesViaHook(); | |
171 | if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) { | |
172 | $fileName = $caseTypesViaHook[$caseType]['file']; | |
173 | } | |
174 | } | |
175 | ||
176 | if (!$fileName || !file_exists($fileName)) { | |
177 | $config = CRM_Core_Config::singleton(); | |
178 | if (isset($config->customTemplateDir) && $config->customTemplateDir) { | |
179 | // check if the file exists in the custom templates directory | |
180 | $fileName = implode(DIRECTORY_SEPARATOR, | |
be2fb01f | 181 | [ |
a8354614 TO |
182 | $config->customTemplateDir, |
183 | 'CRM', | |
184 | 'Case', | |
185 | 'xml', | |
186 | 'configuration', | |
187 | "$caseType.xml", | |
be2fb01f | 188 | ] |
a8354614 TO |
189 | ); |
190 | } | |
191 | } | |
192 | ||
193 | if (!$fileName || !file_exists($fileName)) { | |
194 | if (!file_exists($fileName)) { | |
195 | // check if file exists locally | |
196 | $fileName = implode(DIRECTORY_SEPARATOR, | |
be2fb01f | 197 | [ |
a8354614 TO |
198 | dirname(__FILE__), |
199 | 'xml', | |
200 | 'configuration', | |
201 | "$caseType.xml", | |
be2fb01f | 202 | ] |
a8354614 TO |
203 | ); |
204 | } | |
205 | ||
206 | if (!file_exists($fileName)) { | |
207 | // check if file exists locally | |
208 | $fileName = implode(DIRECTORY_SEPARATOR, | |
be2fb01f | 209 | [ |
a8354614 TO |
210 | dirname(__FILE__), |
211 | 'xml', | |
212 | 'configuration.sample', | |
213 | "$caseType.xml", | |
be2fb01f | 214 | ] |
a8354614 TO |
215 | ); |
216 | } | |
217 | } | |
218 | return file_exists($fileName) ? $fileName : NULL; | |
219 | } | |
220 | ||
fa1c763d TO |
221 | /** |
222 | * @return array | |
223 | * @see CRM_Utils_Hook::caseTypes | |
224 | */ | |
225 | public function getCaseTypesViaHook() { | |
226 | if ($this->hookCache === NULL) { | |
be2fb01f | 227 | $this->hookCache = []; |
fa1c763d TO |
228 | CRM_Utils_Hook::caseTypes($this->hookCache); |
229 | } | |
230 | return $this->hookCache; | |
231 | } | |
e19323c9 TO |
232 | |
233 | /** | |
bb68492c | 234 | * @return array<string> symbolic names of case-types |
e19323c9 TO |
235 | */ |
236 | public function getAllCaseTypes() { | |
237 | if ($this->allCaseTypes === NULL) { | |
238 | $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name"); | |
239 | } | |
240 | return $this->allCaseTypes; | |
241 | } | |
242 | ||
7458ddd3 TO |
243 | /** |
244 | * @return array<string> symbolic-names of activity-types | |
245 | */ | |
246 | public function getAllDeclaredActivityTypes() { | |
be2fb01f | 247 | $result = []; |
7458ddd3 TO |
248 | |
249 | $p = new CRM_Case_XMLProcessor_Process(); | |
250 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
251 | $caseTypeXML = $this->retrieve($caseTypeName); | |
252 | $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML)); | |
253 | } | |
254 | ||
255 | $result = array_unique($result); | |
256 | sort($result); | |
257 | return $result; | |
258 | } | |
259 | ||
260 | /** | |
d0a94888 AF |
261 | * Relationships are straight from XML, described from perspective of non-client |
262 | * | |
7458ddd3 TO |
263 | * @return array<string> symbolic-names of relationship-types |
264 | */ | |
265 | public function getAllDeclaredRelationshipTypes() { | |
be2fb01f | 266 | $result = []; |
7458ddd3 TO |
267 | |
268 | $p = new CRM_Case_XMLProcessor_Process(); | |
269 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
270 | $caseTypeXML = $this->retrieve($caseTypeName); | |
271 | $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML)); | |
272 | } | |
273 | ||
274 | $result = array_unique($result); | |
275 | sort($result); | |
276 | return $result; | |
277 | } | |
278 | ||
e19323c9 TO |
279 | /** |
280 | * Determine the number of times a particular activity-type is | |
281 | * referenced in CiviCase XML. | |
282 | * | |
64bd5a0e TO |
283 | * @param string $activityType |
284 | * Symbolic-name of an activity type. | |
e19323c9 TO |
285 | * @return int |
286 | */ | |
00be9182 | 287 | public function getActivityReferenceCount($activityType) { |
e19323c9 TO |
288 | $p = new CRM_Case_XMLProcessor_Process(); |
289 | $count = 0; | |
290 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
291 | $caseTypeXML = $this->retrieve($caseTypeName); | |
292 | if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) { | |
293 | $count++; | |
294 | } | |
295 | } | |
296 | return $count; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Determine the number of times a particular activity-type is | |
301 | * referenced in CiviCase XML. | |
302 | * | |
64bd5a0e TO |
303 | * @param string $relationshipTypeName |
304 | * Symbolic-name of a relationship-type. | |
e19323c9 TO |
305 | * @return int |
306 | */ | |
00be9182 | 307 | public function getRelationshipReferenceCount($relationshipTypeName) { |
e19323c9 TO |
308 | $p = new CRM_Case_XMLProcessor_Process(); |
309 | $count = 0; | |
310 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
311 | $caseTypeXML = $this->retrieve($caseTypeName); | |
312 | if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) { | |
313 | $count++; | |
314 | } | |
315 | } | |
316 | return $count; | |
317 | } | |
318 | ||
fa1c763d | 319 | } |