Merge remote branch 'canonical/master' into merge-20140930
[civicrm-core.git] / CRM / Case / XMLRepository.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 * The XMLRepository is responsible for loading XML for case-types.
35 * It includes any bulk operations that apply across the list of all XML
36 * documents of all case-types.
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
58 * @return CRM_Case_XMLRepository
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 /**
68 * @param array<String,SimpleXMLElement> $xml
69 */
70 public function __construct($allCaseTypes = NULL, $xml = array()) {
71 $this->allCaseTypes = $allCaseTypes;
72 $this->xml = $xml;
73 }
74
75 /**
76 * @param string $caseType
77 * @return SimpleXMLElement|FALSE
78 */
79 public function retrieve($caseType) {
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)) {
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;
89 }
90
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 //}
96
97 if (!CRM_Utils_Array::value($caseType, $this->xml)) {
98 $fileXml = $this->retrieveFile($caseType);
99 if ($fileXml) {
100 $this->xml[$caseType] = $fileXml;
101 } else {
102 return FALSE;
103 }
104 }
105 return $this->xml[$caseType];
106 }
107
108 /**
109 * @param string $caseType
110 * @return SimpleXMLElement|FALSE
111 */
112 public function retrieveFile($caseType) {
113 $fileName = NULL;
114 $fileXml = NULL;
115
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 }
120
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)) {
128 // read xml file
129 $dom = new DomDocument();
130 $dom->load($fileName);
131 $dom->xinclude();
132 $fileXml = simplexml_import_dom($dom);
133 }
134
135 return $fileXml;
136 }
137
138 /**
139 * @param string $caseType
140 * @return null|string file path
141 */
142 public function findXmlFile($caseType) { // first check custom templates directory
143 $fileName = NULL;
144
145 if (!$fileName || !file_exists($fileName)) {
146 $caseTypesViaHook = $this->getCaseTypesViaHook();
147 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
148 $fileName = $caseTypesViaHook[$caseType]['file'];
149 }
150 }
151
152 if (!$fileName || !file_exists($fileName)) {
153 $config = CRM_Core_Config::singleton();
154 if (isset($config->customTemplateDir) && $config->customTemplateDir) {
155 // check if the file exists in the custom templates directory
156 $fileName = implode(DIRECTORY_SEPARATOR,
157 array(
158 $config->customTemplateDir,
159 'CRM',
160 'Case',
161 'xml',
162 'configuration',
163 "$caseType.xml",
164 )
165 );
166 }
167 }
168
169 if (!$fileName || !file_exists($fileName)) {
170 if (!file_exists($fileName)) {
171 // check if file exists locally
172 $fileName = implode(DIRECTORY_SEPARATOR,
173 array(
174 dirname(__FILE__),
175 'xml',
176 'configuration',
177 "$caseType.xml",
178 )
179 );
180 }
181
182 if (!file_exists($fileName)) {
183 // check if file exists locally
184 $fileName = implode(DIRECTORY_SEPARATOR,
185 array(
186 dirname(__FILE__),
187 'xml',
188 'configuration.sample',
189 "$caseType.xml",
190 )
191 );
192 }
193 }
194 return file_exists($fileName) ? $fileName : NULL;
195 }
196
197 /**
198 * @return array
199 * @see CRM_Utils_Hook::caseTypes
200 */
201 public function getCaseTypesViaHook() {
202 if ($this->hookCache === NULL) {
203 $this->hookCache = array();
204 CRM_Utils_Hook::caseTypes($this->hookCache);
205 }
206 return $this->hookCache;
207 }
208
209 /**
210 * @return array<string> symbolic names of case-types
211 */
212 public function getAllCaseTypes() {
213 if ($this->allCaseTypes === NULL) {
214 $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name");
215 }
216 return $this->allCaseTypes;
217 }
218
219 /**
220 * @return array<string> symbolic-names of activity-types
221 */
222 public function getAllDeclaredActivityTypes() {
223 $result = array();
224
225 $p = new CRM_Case_XMLProcessor_Process();
226 foreach ($this->getAllCaseTypes() as $caseTypeName) {
227 $caseTypeXML = $this->retrieve($caseTypeName);
228 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
229 }
230
231 $result = array_unique($result);
232 sort($result);
233 return $result;
234 }
235
236 /**
237 * @return array<string> symbolic-names of relationship-types
238 */
239 public function getAllDeclaredRelationshipTypes() {
240 $result = array();
241
242 $p = new CRM_Case_XMLProcessor_Process();
243 foreach ($this->getAllCaseTypes() as $caseTypeName) {
244 $caseTypeXML = $this->retrieve($caseTypeName);
245 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
246 }
247
248 $result = array_unique($result);
249 sort($result);
250 return $result;
251 }
252
253 /**
254 * Determine the number of times a particular activity-type is
255 * referenced in CiviCase XML.
256 *
257 * @param string $activityType symbolic-name of an activity type
258 * @return int
259 */
260 function getActivityReferenceCount($activityType) {
261 $p = new CRM_Case_XMLProcessor_Process();
262 $count = 0;
263 foreach ($this->getAllCaseTypes() as $caseTypeName) {
264 $caseTypeXML = $this->retrieve($caseTypeName);
265 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
266 $count++;
267 }
268 }
269 return $count;
270 }
271
272 /**
273 * Determine the number of times a particular activity-type is
274 * referenced in CiviCase XML.
275 *
276 * @param string $relationshipTypeName symbolic-name of a relationship-type
277 * @return int
278 */
279 function getRelationshipReferenceCount($relationshipTypeName) {
280 $p = new CRM_Case_XMLProcessor_Process();
281 $count = 0;
282 foreach ($this->getAllCaseTypes() as $caseTypeName) {
283 $caseTypeXML = $this->retrieve($caseTypeName);
284 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {
285 $count++;
286 }
287 }
288 return $count;
289 }
290
291 }