3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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-2020
33 * The XMLRepository is responsible for loading XML for case-types.
34 * It includes any bulk operations that apply across the list of all XML
35 * documents of all case-types.
37 class CRM_Case_XMLRepository
{
38 private static $singleton;
41 * @var array<String,SimpleXMLElement>
48 protected $hookCache = NULL;
51 * Symbolic names of case-types.
55 protected $allCaseTypes = NULL;
59 * @return CRM_Case_XMLRepository
61 public static function singleton($fresh = FALSE) {
62 if (!self
::$singleton ||
$fresh) {
63 self
::$singleton = new static();
65 return self
::$singleton;
68 public function flush() {
70 $this->hookCache
= NULL;
71 $this->allCaseTypes
= NULL;
72 CRM_Core_DAO
::$_dbColumnValueCache = [];
78 * @param array $allCaseTypes
81 public function __construct($allCaseTypes = NULL, $xml = []) {
82 $this->allCaseTypes
= $allCaseTypes;
89 * @param string $caseType
91 * @return FALSE|\SimpleXMLElement
92 * @throws \CRM_Core_Exception
94 public function retrieve($caseType) {
95 // check if xml definition is defined in db
96 $definition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseType, 'definition', 'name');
98 if (!empty($definition)) {
99 list ($xml, $error) = CRM_Utils_XML
::parseString($definition);
101 throw new CRM_Core_Exception("Failed to parse CaseType XML: $error");
106 // TODO In 4.6 or 5.0, remove support for weird machine-names
107 //if (!CRM_Case_BAO_CaseType::isValidName($caseType)) {
108 // // perhaps caller provider a the label instead of the name?
109 // throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]");
112 if (!CRM_Utils_Array
::value($caseType, $this->xml
)) {
113 $fileXml = $this->retrieveFile($caseType);
115 $this->xml
[$caseType] = $fileXml;
121 return $this->xml
[$caseType];
127 * @param string $caseType
128 * @return SimpleXMLElement|FALSE
130 public function retrieveFile($caseType) {
134 if (CRM_Case_BAO_CaseType
::isValidName($caseType)) {
135 // Search for a file based directly on the $caseType name
136 $fileName = $this->findXmlFile($caseType);
139 // For backward compatibility, also search for double-munged file names
140 // TODO In 4.6 or 5.0, remove support for loading double-munged file names
141 if (!$fileName ||
!file_exists($fileName)) {
142 $fileName = $this->findXmlFile(CRM_Case_XMLProcessor
::mungeCaseType($caseType));
145 if ($fileName && file_exists($fileName)) {
147 $dom = new DomDocument();
148 $xmlString = file_get_contents($fileName);
149 $dom->loadXML($xmlString);
150 $dom->documentURI
= $fileName;
152 $fileXml = simplexml_import_dom($dom);
161 * @param string $caseType
162 * @return null|string
165 public function findXmlFile($caseType) {
166 // first check custom templates directory
169 if (!$fileName ||
!file_exists($fileName)) {
170 $caseTypesViaHook = $this->getCaseTypesViaHook();
171 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
172 $fileName = $caseTypesViaHook[$caseType]['file'];
176 if (!$fileName ||
!file_exists($fileName)) {
177 $config = CRM_Core_Config
::singleton();
178 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
179 // check if the file exists in the custom templates directory
180 $fileName = implode(DIRECTORY_SEPARATOR
,
182 $config->customTemplateDir
,
193 if (!$fileName ||
!file_exists($fileName)) {
194 if (!file_exists($fileName)) {
195 // check if file exists locally
196 $fileName = implode(DIRECTORY_SEPARATOR
,
206 if (!file_exists($fileName)) {
207 // check if file exists locally
208 $fileName = implode(DIRECTORY_SEPARATOR
,
212 'configuration.sample',
218 return file_exists($fileName) ?
$fileName : NULL;
223 * @see CRM_Utils_Hook::caseTypes
225 public function getCaseTypesViaHook() {
226 if ($this->hookCache
=== NULL) {
227 $this->hookCache
= [];
228 CRM_Utils_Hook
::caseTypes($this->hookCache
);
230 return $this->hookCache
;
234 * @return array<string> symbolic names of case-types
236 public function getAllCaseTypes() {
237 if ($this->allCaseTypes
=== NULL) {
238 $this->allCaseTypes
= CRM_Case_PseudoConstant
::caseType("name");
240 return $this->allCaseTypes
;
244 * @return array<string> symbolic-names of activity-types
246 public function getAllDeclaredActivityTypes() {
249 $p = new CRM_Case_XMLProcessor_Process();
250 foreach ($this->getAllCaseTypes() as $caseTypeName) {
251 $caseTypeXML = $this->retrieve($caseTypeName);
252 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
255 $result = array_unique($result);
261 * Relationships are straight from XML, described from perspective of non-client
263 * @return array<string> symbolic-names of relationship-types
265 public function getAllDeclaredRelationshipTypes() {
268 $p = new CRM_Case_XMLProcessor_Process();
269 foreach ($this->getAllCaseTypes() as $caseTypeName) {
270 $caseTypeXML = $this->retrieve($caseTypeName);
271 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
274 $result = array_unique($result);
280 * Determine the number of times a particular activity-type is
281 * referenced in CiviCase XML.
283 * @param string $activityType
284 * Symbolic-name of an activity type.
287 public function getActivityReferenceCount($activityType) {
288 $p = new CRM_Case_XMLProcessor_Process();
290 foreach ($this->getAllCaseTypes() as $caseTypeName) {
291 $caseTypeXML = $this->retrieve($caseTypeName);
292 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
300 * Determine the number of times a particular activity-type is
301 * referenced in CiviCase XML.
303 * @param string $relationshipTypeName
304 * Symbolic-name of a relationship-type.
307 public function getRelationshipReferenceCount($relationshipTypeName) {
308 $p = new CRM_Case_XMLProcessor_Process();
310 foreach ($this->getAllCaseTypes() as $caseTypeName) {
311 $caseTypeXML = $this->retrieve($caseTypeName);
312 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {