CRM_Case_XMLRepository -- Add getAllDeclaredActivityTypes() & getAllDeclaredRelations...
[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 $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType);
81
82 if (!CRM_Utils_Array::value($caseType, $this->xml)) {
83 // first check custom templates directory
84 $fileName = NULL;
85 $config = CRM_Core_Config::singleton();
86 if (isset($config->customTemplateDir) &&
87 $config->customTemplateDir
88 ) {
89 // check if the file exists in the custom templates directory
90 $fileName = implode(DIRECTORY_SEPARATOR,
91 array(
92 $config->customTemplateDir,
93 'CRM',
94 'Case',
95 'xml',
96 'configuration',
97 "$caseType.xml",
98 )
99 );
100 }
101
102 if (!$fileName ||
103 !file_exists($fileName)
104 ) {
105 // check if file exists locally
106 $fileName = implode(DIRECTORY_SEPARATOR,
107 array(
108 dirname(__FILE__),
109 'xml',
110 'configuration',
111 "$caseType.xml",
112 )
113 );
114
115 if (!file_exists($fileName)) {
116 // check if file exists locally
117 $fileName = implode(DIRECTORY_SEPARATOR,
118 array(
119 dirname(__FILE__),
120 'xml',
121 'configuration.sample',
122 "$caseType.xml",
123 )
124 );
125 }
126
127 if (!file_exists($fileName)) {
128 $caseTypesViaHook = $this->getCaseTypesViaHook();
129 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
130 $fileName = $caseTypesViaHook[$caseType]['file'];
131 }
132 }
133
134 if (!file_exists($fileName)) {
135 return FALSE;
136 }
137 }
138
139 // read xml file
140 $dom = new DomDocument();
141 $dom->load($fileName);
142 $dom->xinclude();
143 $this->xml[$caseType] = simplexml_import_dom($dom);
144 }
145 return $this->xml[$caseType];
146 }
147
148 /**
149 * @return array
150 * @see CRM_Utils_Hook::caseTypes
151 */
152 public function getCaseTypesViaHook() {
153 if ($this->hookCache === NULL) {
154 $this->hookCache = array();
155 CRM_Utils_Hook::caseTypes($this->hookCache);
156 }
157 return $this->hookCache;
158 }
159
160 /**
161 * @return array<string> symbolic names of case-types
162 */
163 public function getAllCaseTypes() {
164 if ($this->allCaseTypes === NULL) {
165 $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name");
166 }
167 return $this->allCaseTypes;
168 }
169
170 /**
171 * @return array<string> symbolic-names of activity-types
172 */
173 public function getAllDeclaredActivityTypes() {
174 $result = array();
175
176 $p = new CRM_Case_XMLProcessor_Process();
177 foreach ($this->getAllCaseTypes() as $caseTypeName) {
178 $caseTypeXML = $this->retrieve($caseTypeName);
179 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
180 }
181
182 $result = array_unique($result);
183 sort($result);
184 return $result;
185 }
186
187 /**
188 * @return array<string> symbolic-names of relationship-types
189 */
190 public function getAllDeclaredRelationshipTypes() {
191 $result = array();
192
193 $p = new CRM_Case_XMLProcessor_Process();
194 foreach ($this->getAllCaseTypes() as $caseTypeName) {
195 $caseTypeXML = $this->retrieve($caseTypeName);
196 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
197 }
198
199 $result = array_unique($result);
200 sort($result);
201 return $result;
202 }
203
204 /**
205 * Determine the number of times a particular activity-type is
206 * referenced in CiviCase XML.
207 *
208 * @param string $activityType symbolic-name of an activity type
209 * @return int
210 */
211 function getActivityReferenceCount($activityType) {
212 $p = new CRM_Case_XMLProcessor_Process();
213 $count = 0;
214 foreach ($this->getAllCaseTypes() as $caseTypeName) {
215 $caseTypeXML = $this->retrieve($caseTypeName);
216 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
217 $count++;
218 }
219 }
220 return $count;
221 }
222
223 /**
224 * Determine the number of times a particular activity-type is
225 * referenced in CiviCase XML.
226 *
227 * @param string $relationshipTypeName symbolic-name of a relationship-type
228 * @return int
229 */
230 function getRelationshipReferenceCount($relationshipTypeName) {
231 $p = new CRM_Case_XMLProcessor_Process();
232 $count = 0;
233 foreach ($this->getAllCaseTypes() as $caseTypeName) {
234 $caseTypeXML = $this->retrieve($caseTypeName);
235 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {
236 $count++;
237 }
238 }
239 return $count;
240 }
241
242 }