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