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