Merge pull request #10227 from WeMoveEU/CRM-20309
[civicrm-core.git] / CRM / Case / XMLRepository.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 /**
67 * Class constructor.
68 *
69 * @param array $allCaseTypes
70 * @param array $xml
71 */
72 public function __construct($allCaseTypes = NULL, $xml = array()) {
73 $this->allCaseTypes = $allCaseTypes;
74 $this->xml = $xml;
75 }
76
77 /**
78 * Retrieve case.
79 *
80 * @param string $caseType
81 *
82 * @return FALSE|\SimpleXMLElement
83 * @throws \CRM_Core_Exception
84 */
85 public function retrieve($caseType) {
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)) {
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;
95 }
96
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 //}
102
103 if (!CRM_Utils_Array::value($caseType, $this->xml)) {
104 $fileXml = $this->retrieveFile($caseType);
105 if ($fileXml) {
106 $this->xml[$caseType] = $fileXml;
107 }
108 else {
109 return FALSE;
110 }
111 }
112 return $this->xml[$caseType];
113 }
114
115 /**
116 * Retrieve file.
117 *
118 * @param string $caseType
119 * @return SimpleXMLElement|FALSE
120 */
121 public function retrieveFile($caseType) {
122 $fileName = NULL;
123 $fileXml = NULL;
124
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 }
129
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)) {
137 // read xml file
138 $dom = new DomDocument();
139 $xmlString = file_get_contents($fileName);
140 $dom->loadXML($xmlString);
141 $dom->documentURI = $fileName;
142 $dom->xinclude();
143 $fileXml = simplexml_import_dom($dom);
144 }
145
146 return $fileXml;
147 }
148
149 /**
150 * Find xml file.
151 *
152 * @param string $caseType
153 * @return null|string
154 * file path
155 */
156 public function findXmlFile($caseType) {
157 // first check custom templates directory
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
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 }
223
224 /**
225 * @return array<string> symbolic names of case-types
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
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
268 /**
269 * Determine the number of times a particular activity-type is
270 * referenced in CiviCase XML.
271 *
272 * @param string $activityType
273 * Symbolic-name of an activity type.
274 * @return int
275 */
276 public function getActivityReferenceCount($activityType) {
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 *
292 * @param string $relationshipTypeName
293 * Symbolic-name of a relationship-type.
294 * @return int
295 */
296 public function getRelationshipReferenceCount($relationshipTypeName) {
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
308 }