Merge pull request #12683 from civicrm/5.5
[civicrm-core.git] / CRM / Case / XMLRepository.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 *
33 * The XMLRepository is responsible for loading XML for case-types.
34 * It includes any bulk operations that apply across the list of all XML
35 * documents of all case-types.
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
57 * @return CRM_Case_XMLRepository
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 public function flush() {
67 $this->xml = array();
68 $this->hookCache = NULL;
69 $this->allCaseTypes = NULL;
70 CRM_Core_DAO::$_dbColumnValueCache = array();
71 }
72
73 /**
74 * Class constructor.
75 *
76 * @param array $allCaseTypes
77 * @param array $xml
78 */
79 public function __construct($allCaseTypes = NULL, $xml = array()) {
80 $this->allCaseTypes = $allCaseTypes;
81 $this->xml = $xml;
82 }
83
84 /**
85 * Retrieve case.
86 *
87 * @param string $caseType
88 *
89 * @return FALSE|\SimpleXMLElement
90 * @throws \CRM_Core_Exception
91 */
92 public function retrieve($caseType) {
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)) {
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;
102 }
103
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 //}
109
110 if (!CRM_Utils_Array::value($caseType, $this->xml)) {
111 $fileXml = $this->retrieveFile($caseType);
112 if ($fileXml) {
113 $this->xml[$caseType] = $fileXml;
114 }
115 else {
116 return FALSE;
117 }
118 }
119 return $this->xml[$caseType];
120 }
121
122 /**
123 * Retrieve file.
124 *
125 * @param string $caseType
126 * @return SimpleXMLElement|FALSE
127 */
128 public function retrieveFile($caseType) {
129 $fileName = NULL;
130 $fileXml = NULL;
131
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 }
136
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)) {
144 // read xml file
145 $dom = new DomDocument();
146 $xmlString = file_get_contents($fileName);
147 $dom->loadXML($xmlString);
148 $dom->documentURI = $fileName;
149 $dom->xinclude();
150 $fileXml = simplexml_import_dom($dom);
151 }
152
153 return $fileXml;
154 }
155
156 /**
157 * Find xml file.
158 *
159 * @param string $caseType
160 * @return null|string
161 * file path
162 */
163 public function findXmlFile($caseType) {
164 // first check custom templates directory
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
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 }
230
231 /**
232 * @return array<string> symbolic names of case-types
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
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
275 /**
276 * Determine the number of times a particular activity-type is
277 * referenced in CiviCase XML.
278 *
279 * @param string $activityType
280 * Symbolic-name of an activity type.
281 * @return int
282 */
283 public function getActivityReferenceCount($activityType) {
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 *
299 * @param string $relationshipTypeName
300 * Symbolic-name of a relationship-type.
301 * @return int
302 */
303 public function getRelationshipReferenceCount($relationshipTypeName) {
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
315 }