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