CRM-20312 regenerated DAOS with indexes
[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 $dom->load($fileName);
140 $dom->xinclude();
141 $fileXml = simplexml_import_dom($dom);
142 }
143
144 return $fileXml;
145 }
146
147 /**
148 * Find xml file.
149 *
150 * @param string $caseType
151 * @return null|string
152 * file path
153 */
154 public function findXmlFile($caseType) {
155 // first check custom templates directory
156 $fileName = NULL;
157
158 if (!$fileName || !file_exists($fileName)) {
159 $caseTypesViaHook = $this->getCaseTypesViaHook();
160 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
161 $fileName = $caseTypesViaHook[$caseType]['file'];
162 }
163 }
164
165 if (!$fileName || !file_exists($fileName)) {
166 $config = CRM_Core_Config::singleton();
167 if (isset($config->customTemplateDir) && $config->customTemplateDir) {
168 // check if the file exists in the custom templates directory
169 $fileName = implode(DIRECTORY_SEPARATOR,
170 array(
171 $config->customTemplateDir,
172 'CRM',
173 'Case',
174 'xml',
175 'configuration',
176 "$caseType.xml",
177 )
178 );
179 }
180 }
181
182 if (!$fileName || !file_exists($fileName)) {
183 if (!file_exists($fileName)) {
184 // check if file exists locally
185 $fileName = implode(DIRECTORY_SEPARATOR,
186 array(
187 dirname(__FILE__),
188 'xml',
189 'configuration',
190 "$caseType.xml",
191 )
192 );
193 }
194
195 if (!file_exists($fileName)) {
196 // check if file exists locally
197 $fileName = implode(DIRECTORY_SEPARATOR,
198 array(
199 dirname(__FILE__),
200 'xml',
201 'configuration.sample',
202 "$caseType.xml",
203 )
204 );
205 }
206 }
207 return file_exists($fileName) ? $fileName : NULL;
208 }
209
210 /**
211 * @return array
212 * @see CRM_Utils_Hook::caseTypes
213 */
214 public function getCaseTypesViaHook() {
215 if ($this->hookCache === NULL) {
216 $this->hookCache = array();
217 CRM_Utils_Hook::caseTypes($this->hookCache);
218 }
219 return $this->hookCache;
220 }
221
222 /**
223 * @return array<string> symbolic names of case-types
224 */
225 public function getAllCaseTypes() {
226 if ($this->allCaseTypes === NULL) {
227 $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name");
228 }
229 return $this->allCaseTypes;
230 }
231
232 /**
233 * @return array<string> symbolic-names of activity-types
234 */
235 public function getAllDeclaredActivityTypes() {
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->getDeclaredActivityTypes($caseTypeXML));
242 }
243
244 $result = array_unique($result);
245 sort($result);
246 return $result;
247 }
248
249 /**
250 * @return array<string> symbolic-names of relationship-types
251 */
252 public function getAllDeclaredRelationshipTypes() {
253 $result = array();
254
255 $p = new CRM_Case_XMLProcessor_Process();
256 foreach ($this->getAllCaseTypes() as $caseTypeName) {
257 $caseTypeXML = $this->retrieve($caseTypeName);
258 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
259 }
260
261 $result = array_unique($result);
262 sort($result);
263 return $result;
264 }
265
266 /**
267 * Determine the number of times a particular activity-type is
268 * referenced in CiviCase XML.
269 *
270 * @param string $activityType
271 * Symbolic-name of an activity type.
272 * @return int
273 */
274 public function getActivityReferenceCount($activityType) {
275 $p = new CRM_Case_XMLProcessor_Process();
276 $count = 0;
277 foreach ($this->getAllCaseTypes() as $caseTypeName) {
278 $caseTypeXML = $this->retrieve($caseTypeName);
279 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
280 $count++;
281 }
282 }
283 return $count;
284 }
285
286 /**
287 * Determine the number of times a particular activity-type is
288 * referenced in CiviCase XML.
289 *
290 * @param string $relationshipTypeName
291 * Symbolic-name of a relationship-type.
292 * @return int
293 */
294 public function getRelationshipReferenceCount($relationshipTypeName) {
295 $p = new CRM_Case_XMLProcessor_Process();
296 $count = 0;
297 foreach ($this->getAllCaseTypes() as $caseTypeName) {
298 $caseTypeXML = $this->retrieve($caseTypeName);
299 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {
300 $count++;
301 }
302 }
303 return $count;
304 }
305
306 }