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