CRM-14478 - Add generic API, "getrefcount"
[civicrm-core.git] / CRM / Case / XMLRepository.php
CommitLineData
fa1c763d
TO
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 */
36class 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_XMLProcessor
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($xml = array()) {
69 $this->xml = $xml;
70 }
71
72 /**
73 * @param string $caseType
74 * @return SimpleXMLElement|FALSE
75 */
76 public function retrieve($caseType) {
77 $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType);
78
79 if (!CRM_Utils_Array::value($caseType, $this->xml)) {
80 // first check custom templates directory
81 $fileName = NULL;
82 $config = CRM_Core_Config::singleton();
83 if (isset($config->customTemplateDir) &&
84 $config->customTemplateDir
85 ) {
86 // check if the file exists in the custom templates directory
87 $fileName = implode(DIRECTORY_SEPARATOR,
88 array(
89 $config->customTemplateDir,
90 'CRM',
91 'Case',
92 'xml',
93 'configuration',
94 "$caseType.xml",
95 )
96 );
97 }
98
99 if (!$fileName ||
100 !file_exists($fileName)
101 ) {
102 // check if file exists locally
103 $fileName = implode(DIRECTORY_SEPARATOR,
104 array(
105 dirname(__FILE__),
106 'xml',
107 'configuration',
108 "$caseType.xml",
109 )
110 );
111
112 if (!file_exists($fileName)) {
113 // check if file exists locally
114 $fileName = implode(DIRECTORY_SEPARATOR,
115 array(
116 dirname(__FILE__),
117 'xml',
118 'configuration.sample',
119 "$caseType.xml",
120 )
121 );
122 }
123
124 if (!file_exists($fileName)) {
125 $caseTypesViaHook = $this->getCaseTypesViaHook();
126 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
127 $fileName = $caseTypesViaHook[$caseType]['file'];
128 }
129 }
130
131 if (!file_exists($fileName)) {
132 return FALSE;
133 }
134 }
135
136 // read xml file
137 $dom = new DomDocument();
138 $dom->load($fileName);
139 $dom->xinclude();
140 $this->xml[$caseType] = simplexml_import_dom($dom);
141 }
142 return $this->xml[$caseType];
143 }
144
145 /**
146 * @return array
147 * @see CRM_Utils_Hook::caseTypes
148 */
149 public function getCaseTypesViaHook() {
150 if ($this->hookCache === NULL) {
151 $this->hookCache = array();
152 CRM_Utils_Hook::caseTypes($this->hookCache);
153 }
154 return $this->hookCache;
155 }
156}