3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
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.
38 class CRM_Case_XMLRepository
{
39 private static $singleton;
42 * @var array<String,SimpleXMLElement>
44 protected $xml = array();
49 protected $hookCache = NULL;
52 * @var array|NULL symbolic names of case-types
54 protected $allCaseTypes = NULL;
58 * @return CRM_Case_XMLRepository
60 public static function singleton($fresh = FALSE) {
61 if (!self
::$singleton ||
$fresh) {
62 self
::$singleton = new static();
64 return self
::$singleton;
68 * @param array<String,SimpleXMLElement> $xml
70 public function __construct($allCaseTypes = NULL, $xml = array()) {
71 $this->allCaseTypes
= $allCaseTypes;
76 * @param string $caseType
77 * @return SimpleXMLElement|FALSE
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');
83 if (!empty($definition)) {
84 return simplexml_load_string($definition);
87 // TODO In 4.6 or 5.0, remove support for weird machine-names
88 //if (!CRM_Case_BAO_CaseType::isValidName($caseType)) {
89 // // perhaps caller provider a the label instead of the name?
90 // throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]");
93 if (!CRM_Utils_Array
::value($caseType, $this->xml
)) {
94 $fileXml = $this->retrieveFile($caseType);
96 $this->xml
[$caseType] = $fileXml;
101 return $this->xml
[$caseType];
105 * @param string $caseType
106 * @return SimpleXMLElement|FALSE
108 public function retrieveFile($caseType) {
112 if (CRM_Case_BAO_CaseType
::isValidName($caseType)) {
113 // Search for a file based directly on the $caseType name
114 $fileName = $this->findXmlFile($caseType);
117 // For backward compatibility, also search for double-munged file names
118 // TODO In 4.6 or 5.0, remove support for loading double-munged file names
119 if (!$fileName ||
!file_exists($fileName)) {
120 $fileName = $this->findXmlFile(CRM_Case_XMLProcessor
::mungeCaseType($caseType));
123 if ($fileName && file_exists($fileName)) {
125 $dom = new DomDocument();
126 $dom->load($fileName);
128 $fileXml = simplexml_import_dom($dom);
135 * @param string $caseType
136 * @return null|string file path
138 public function findXmlFile($caseType) { // first check custom templates directory
141 if (!$fileName ||
!file_exists($fileName)) {
142 $caseTypesViaHook = $this->getCaseTypesViaHook();
143 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
144 $fileName = $caseTypesViaHook[$caseType]['file'];
148 if (!$fileName ||
!file_exists($fileName)) {
149 $config = CRM_Core_Config
::singleton();
150 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
151 // check if the file exists in the custom templates directory
152 $fileName = implode(DIRECTORY_SEPARATOR
,
154 $config->customTemplateDir
,
165 if (!$fileName ||
!file_exists($fileName)) {
166 if (!file_exists($fileName)) {
167 // check if file exists locally
168 $fileName = implode(DIRECTORY_SEPARATOR
,
178 if (!file_exists($fileName)) {
179 // check if file exists locally
180 $fileName = implode(DIRECTORY_SEPARATOR
,
184 'configuration.sample',
190 return file_exists($fileName) ?
$fileName : NULL;
195 * @see CRM_Utils_Hook::caseTypes
197 public function getCaseTypesViaHook() {
198 if ($this->hookCache
=== NULL) {
199 $this->hookCache
= array();
200 CRM_Utils_Hook
::caseTypes($this->hookCache
);
202 return $this->hookCache
;
206 * @return array<string> symbolic names of case-types
208 public function getAllCaseTypes() {
209 if ($this->allCaseTypes
=== NULL) {
210 $this->allCaseTypes
= CRM_Case_PseudoConstant
::caseType("name");
212 return $this->allCaseTypes
;
216 * @return array<string> symbolic-names of activity-types
218 public function getAllDeclaredActivityTypes() {
221 $p = new CRM_Case_XMLProcessor_Process();
222 foreach ($this->getAllCaseTypes() as $caseTypeName) {
223 $caseTypeXML = $this->retrieve($caseTypeName);
224 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
227 $result = array_unique($result);
233 * @return array<string> symbolic-names of relationship-types
235 public function getAllDeclaredRelationshipTypes() {
238 $p = new CRM_Case_XMLProcessor_Process();
239 foreach ($this->getAllCaseTypes() as $caseTypeName) {
240 $caseTypeXML = $this->retrieve($caseTypeName);
241 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
244 $result = array_unique($result);
250 * Determine the number of times a particular activity-type is
251 * referenced in CiviCase XML.
253 * @param string $activityType symbolic-name of an activity type
256 function getActivityReferenceCount($activityType) {
257 $p = new CRM_Case_XMLProcessor_Process();
259 foreach ($this->getAllCaseTypes() as $caseTypeName) {
260 $caseTypeXML = $this->retrieve($caseTypeName);
261 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
269 * Determine the number of times a particular activity-type is
270 * referenced in CiviCase XML.
272 * @param string $relationshipTypeName symbolic-name of a relationship-type
275 function getRelationshipReferenceCount($relationshipTypeName) {
276 $p = new CRM_Case_XMLProcessor_Process();
278 foreach ($this->getAllCaseTypes() as $caseTypeName) {
279 $caseTypeXML = $this->retrieve($caseTypeName);
280 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {