0214254bef118b4a275bb291bd83c29468ab084f
[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 */
36 class CRM_Case_XMLRepository {
37 private static $singleton;
38
39 /**
40 * @var array<String,SimpleXMLElement>
41 */
42 protected $xml = array();
43
44 /**
45 * @var array|NULL
46 */
47 protected $hookCache = NULL;
48
49 /**
50 * @var array|NULL symbolic names of case-types
51 */
52 protected $allCaseTypes = NULL;
53
54 /**
55 * @param bool $fresh
56 * @return CRM_Case_XMLRepository
57 */
58 public static function singleton($fresh = FALSE) {
59 if (!self::$singleton || $fresh) {
60 self::$singleton = new static();
61 }
62 return self::$singleton;
63 }
64
65 /**
66 * @param array<String,SimpleXMLElement> $xml
67 */
68 public function __construct($allCaseTypes = NULL, $xml = array()) {
69 $this->allCaseTypes = $allCaseTypes;
70 $this->xml = $xml;
71 }
72
73 /**
74 * @param string $caseType
75 * @return SimpleXMLElement|FALSE
76 */
77 public function retrieve($caseType) {
78 $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType);
79
80 if (!CRM_Utils_Array::value($caseType, $this->xml)) {
81 // first check custom templates directory
82 $fileName = NULL;
83 $config = CRM_Core_Config::singleton();
84 if (isset($config->customTemplateDir) &&
85 $config->customTemplateDir
86 ) {
87 // check if the file exists in the custom templates directory
88 $fileName = implode(DIRECTORY_SEPARATOR,
89 array(
90 $config->customTemplateDir,
91 'CRM',
92 'Case',
93 'xml',
94 'configuration',
95 "$caseType.xml",
96 )
97 );
98 }
99
100 if (!$fileName ||
101 !file_exists($fileName)
102 ) {
103 // check if file exists locally
104 $fileName = implode(DIRECTORY_SEPARATOR,
105 array(
106 dirname(__FILE__),
107 'xml',
108 'configuration',
109 "$caseType.xml",
110 )
111 );
112
113 if (!file_exists($fileName)) {
114 // check if file exists locally
115 $fileName = implode(DIRECTORY_SEPARATOR,
116 array(
117 dirname(__FILE__),
118 'xml',
119 'configuration.sample',
120 "$caseType.xml",
121 )
122 );
123 }
124
125 if (!file_exists($fileName)) {
126 $caseTypesViaHook = $this->getCaseTypesViaHook();
127 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
128 $fileName = $caseTypesViaHook[$caseType]['file'];
129 }
130 }
131
132 if (!file_exists($fileName)) {
133 return FALSE;
134 }
135 }
136
137 // read xml file
138 $dom = new DomDocument();
139 $dom->load($fileName);
140 $dom->xinclude();
141 $this->xml[$caseType] = simplexml_import_dom($dom);
142 }
143 return $this->xml[$caseType];
144 }
145
146 /**
147 * @return array
148 * @see CRM_Utils_Hook::caseTypes
149 */
150 public function getCaseTypesViaHook() {
151 if ($this->hookCache === NULL) {
152 $this->hookCache = array();
153 CRM_Utils_Hook::caseTypes($this->hookCache);
154 }
155 return $this->hookCache;
156 }
157
158 /**
159 * @return array<string> symbolic names of case-types
160 */
161 public function getAllCaseTypes() {
162 if ($this->allCaseTypes === NULL) {
163 $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name");
164 }
165 return $this->allCaseTypes;
166 }
167
168 /**
169 * Determine the number of times a particular activity-type is
170 * referenced in CiviCase XML.
171 *
172 * @param string $activityType symbolic-name of an activity type
173 * @return int
174 */
175 function getActivityReferenceCount($activityType) {
176 $p = new CRM_Case_XMLProcessor_Process();
177 $count = 0;
178 foreach ($this->getAllCaseTypes() as $caseTypeName) {
179 $caseTypeXML = $this->retrieve($caseTypeName);
180 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
181 $count++;
182 }
183 }
184 return $count;
185 }
186
187 /**
188 * Determine the number of times a particular activity-type is
189 * referenced in CiviCase XML.
190 *
191 * @param string $relationshipTypeName symbolic-name of a relationship-type
192 * @return int
193 */
194 function getRelationshipReferenceCount($relationshipTypeName) {
195 $p = new CRM_Case_XMLProcessor_Process();
196 $count = 0;
197 foreach ($this->getAllCaseTypes() as $caseTypeName) {
198 $caseTypeXML = $this->retrieve($caseTypeName);
199 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {
200 $count++;
201 }
202 }
203 return $count;
204 }
205
206 }