Commit | Line | Data |
---|---|---|
fa1c763d TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
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 | * |
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(); | |
139 | $dom->load($fileName); | |
140 | $dom->xinclude(); | |
32f1c917 | 141 | $fileXml = simplexml_import_dom($dom); |
fa1c763d | 142 | } |
32f1c917 TO |
143 | |
144 | return $fileXml; | |
fa1c763d TO |
145 | } |
146 | ||
a8354614 | 147 | /** |
54957108 | 148 | * Find xml file. |
149 | * | |
a8354614 | 150 | * @param string $caseType |
72b3a70c CW |
151 | * @return null|string |
152 | * file path | |
a8354614 | 153 | */ |
e547f744 TO |
154 | public function findXmlFile($caseType) { |
155 | // first check custom templates directory | |
a8354614 TO |
156 | $fileName = NULL; |
157 | ||
158 | if (!$fileName || !file_exists($fileName)) { | |
159 | $caseTypesViaHook = $this->getCaseTypesViaHook(); | |
160 | if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) { | |
161 | $fileName = $caseTypesViaHook[$caseType]['file']; | |
162 | } | |
163 | } | |
164 | ||
165 | if (!$fileName || !file_exists($fileName)) { | |
166 | $config = CRM_Core_Config::singleton(); | |
167 | if (isset($config->customTemplateDir) && $config->customTemplateDir) { | |
168 | // check if the file exists in the custom templates directory | |
169 | $fileName = implode(DIRECTORY_SEPARATOR, | |
170 | array( | |
171 | $config->customTemplateDir, | |
172 | 'CRM', | |
173 | 'Case', | |
174 | 'xml', | |
175 | 'configuration', | |
176 | "$caseType.xml", | |
177 | ) | |
178 | ); | |
179 | } | |
180 | } | |
181 | ||
182 | if (!$fileName || !file_exists($fileName)) { | |
183 | if (!file_exists($fileName)) { | |
184 | // check if file exists locally | |
185 | $fileName = implode(DIRECTORY_SEPARATOR, | |
186 | array( | |
187 | dirname(__FILE__), | |
188 | 'xml', | |
189 | 'configuration', | |
190 | "$caseType.xml", | |
191 | ) | |
192 | ); | |
193 | } | |
194 | ||
195 | if (!file_exists($fileName)) { | |
196 | // check if file exists locally | |
197 | $fileName = implode(DIRECTORY_SEPARATOR, | |
198 | array( | |
199 | dirname(__FILE__), | |
200 | 'xml', | |
201 | 'configuration.sample', | |
202 | "$caseType.xml", | |
203 | ) | |
204 | ); | |
205 | } | |
206 | } | |
207 | return file_exists($fileName) ? $fileName : NULL; | |
208 | } | |
209 | ||
fa1c763d TO |
210 | /** |
211 | * @return array | |
212 | * @see CRM_Utils_Hook::caseTypes | |
213 | */ | |
214 | public function getCaseTypesViaHook() { | |
215 | if ($this->hookCache === NULL) { | |
216 | $this->hookCache = array(); | |
217 | CRM_Utils_Hook::caseTypes($this->hookCache); | |
218 | } | |
219 | return $this->hookCache; | |
220 | } | |
e19323c9 TO |
221 | |
222 | /** | |
bb68492c | 223 | * @return array<string> symbolic names of case-types |
e19323c9 TO |
224 | */ |
225 | public function getAllCaseTypes() { | |
226 | if ($this->allCaseTypes === NULL) { | |
227 | $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name"); | |
228 | } | |
229 | return $this->allCaseTypes; | |
230 | } | |
231 | ||
7458ddd3 TO |
232 | /** |
233 | * @return array<string> symbolic-names of activity-types | |
234 | */ | |
235 | public function getAllDeclaredActivityTypes() { | |
236 | $result = array(); | |
237 | ||
238 | $p = new CRM_Case_XMLProcessor_Process(); | |
239 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
240 | $caseTypeXML = $this->retrieve($caseTypeName); | |
241 | $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML)); | |
242 | } | |
243 | ||
244 | $result = array_unique($result); | |
245 | sort($result); | |
246 | return $result; | |
247 | } | |
248 | ||
249 | /** | |
250 | * @return array<string> symbolic-names of relationship-types | |
251 | */ | |
252 | public function getAllDeclaredRelationshipTypes() { | |
253 | $result = array(); | |
254 | ||
255 | $p = new CRM_Case_XMLProcessor_Process(); | |
256 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
257 | $caseTypeXML = $this->retrieve($caseTypeName); | |
258 | $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML)); | |
259 | } | |
260 | ||
261 | $result = array_unique($result); | |
262 | sort($result); | |
263 | return $result; | |
264 | } | |
265 | ||
e19323c9 TO |
266 | /** |
267 | * Determine the number of times a particular activity-type is | |
268 | * referenced in CiviCase XML. | |
269 | * | |
64bd5a0e TO |
270 | * @param string $activityType |
271 | * Symbolic-name of an activity type. | |
e19323c9 TO |
272 | * @return int |
273 | */ | |
00be9182 | 274 | public function getActivityReferenceCount($activityType) { |
e19323c9 TO |
275 | $p = new CRM_Case_XMLProcessor_Process(); |
276 | $count = 0; | |
277 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
278 | $caseTypeXML = $this->retrieve($caseTypeName); | |
279 | if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) { | |
280 | $count++; | |
281 | } | |
282 | } | |
283 | return $count; | |
284 | } | |
285 | ||
286 | /** | |
287 | * Determine the number of times a particular activity-type is | |
288 | * referenced in CiviCase XML. | |
289 | * | |
64bd5a0e TO |
290 | * @param string $relationshipTypeName |
291 | * Symbolic-name of a relationship-type. | |
e19323c9 TO |
292 | * @return int |
293 | */ | |
00be9182 | 294 | public function getRelationshipReferenceCount($relationshipTypeName) { |
e19323c9 TO |
295 | $p = new CRM_Case_XMLProcessor_Process(); |
296 | $count = 0; | |
297 | foreach ($this->getAllCaseTypes() as $caseTypeName) { | |
298 | $caseTypeXML = $this->retrieve($caseTypeName); | |
299 | if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) { | |
300 | $count++; | |
301 | } | |
302 | } | |
303 | return $count; | |
304 | } | |
305 | ||
fa1c763d | 306 | } |