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