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