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