3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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>
43 protected $xml = array();
48 protected $hookCache = NULL;
51 * @var array|NULL symbolic names of case-types
53 protected $allCaseTypes = NULL;
57 * @return CRM_Case_XMLRepository
59 public static function singleton($fresh = FALSE) {
60 if (!self
::$singleton ||
$fresh) {
61 self
::$singleton = new static();
63 return self
::$singleton;
69 * @param array $allCaseTypes
72 public function __construct($allCaseTypes = NULL, $xml = array()) {
73 $this->allCaseTypes
= $allCaseTypes;
80 * @param string $caseType
82 * @return FALSE|\SimpleXMLElement
83 * @throws \CRM_Core_Exception
85 public function retrieve($caseType) {
86 // check if xml definition is defined in db
87 $definition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseType, 'definition', 'name');
89 if (!empty($definition)) {
90 list ($xml, $error) = CRM_Utils_XML
::parseString($definition);
92 throw new CRM_Core_Exception("Failed to parse CaseType XML: $error");
97 // TODO In 4.6 or 5.0, remove support for weird machine-names
98 //if (!CRM_Case_BAO_CaseType::isValidName($caseType)) {
99 // // perhaps caller provider a the label instead of the name?
100 // throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]");
103 if (!CRM_Utils_Array
::value($caseType, $this->xml
)) {
104 $fileXml = $this->retrieveFile($caseType);
106 $this->xml
[$caseType] = $fileXml;
112 return $this->xml
[$caseType];
118 * @param string $caseType
119 * @return SimpleXMLElement|FALSE
121 public function retrieveFile($caseType) {
125 if (CRM_Case_BAO_CaseType
::isValidName($caseType)) {
126 // Search for a file based directly on the $caseType name
127 $fileName = $this->findXmlFile($caseType);
130 // For backward compatibility, also search for double-munged file names
131 // TODO In 4.6 or 5.0, remove support for loading double-munged file names
132 if (!$fileName ||
!file_exists($fileName)) {
133 $fileName = $this->findXmlFile(CRM_Case_XMLProcessor
::mungeCaseType($caseType));
136 if ($fileName && file_exists($fileName)) {
138 $dom = new DomDocument();
139 $xmlString = file_get_contents($fileName);
140 $dom->loadXML($xmlString);
141 $dom->documentURI
= $fileName;
143 $fileXml = simplexml_import_dom($dom);
152 * @param string $caseType
153 * @return null|string
156 public function findXmlFile($caseType) {
157 // first check custom templates directory
160 if (!$fileName ||
!file_exists($fileName)) {
161 $caseTypesViaHook = $this->getCaseTypesViaHook();
162 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
163 $fileName = $caseTypesViaHook[$caseType]['file'];
167 if (!$fileName ||
!file_exists($fileName)) {
168 $config = CRM_Core_Config
::singleton();
169 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
170 // check if the file exists in the custom templates directory
171 $fileName = implode(DIRECTORY_SEPARATOR
,
173 $config->customTemplateDir
,
184 if (!$fileName ||
!file_exists($fileName)) {
185 if (!file_exists($fileName)) {
186 // check if file exists locally
187 $fileName = implode(DIRECTORY_SEPARATOR
,
197 if (!file_exists($fileName)) {
198 // check if file exists locally
199 $fileName = implode(DIRECTORY_SEPARATOR
,
203 'configuration.sample',
209 return file_exists($fileName) ?
$fileName : NULL;
214 * @see CRM_Utils_Hook::caseTypes
216 public function getCaseTypesViaHook() {
217 if ($this->hookCache
=== NULL) {
218 $this->hookCache
= array();
219 CRM_Utils_Hook
::caseTypes($this->hookCache
);
221 return $this->hookCache
;
225 * @return array<string> symbolic names of case-types
227 public function getAllCaseTypes() {
228 if ($this->allCaseTypes
=== NULL) {
229 $this->allCaseTypes
= CRM_Case_PseudoConstant
::caseType("name");
231 return $this->allCaseTypes
;
235 * @return array<string> symbolic-names of activity-types
237 public function getAllDeclaredActivityTypes() {
240 $p = new CRM_Case_XMLProcessor_Process();
241 foreach ($this->getAllCaseTypes() as $caseTypeName) {
242 $caseTypeXML = $this->retrieve($caseTypeName);
243 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
246 $result = array_unique($result);
252 * @return array<string> symbolic-names of relationship-types
254 public function getAllDeclaredRelationshipTypes() {
257 $p = new CRM_Case_XMLProcessor_Process();
258 foreach ($this->getAllCaseTypes() as $caseTypeName) {
259 $caseTypeXML = $this->retrieve($caseTypeName);
260 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
263 $result = array_unique($result);
269 * Determine the number of times a particular activity-type is
270 * referenced in CiviCase XML.
272 * @param string $activityType
273 * Symbolic-name of an activity type.
276 public function getActivityReferenceCount($activityType) {
277 $p = new CRM_Case_XMLProcessor_Process();
279 foreach ($this->getAllCaseTypes() as $caseTypeName) {
280 $caseTypeXML = $this->retrieve($caseTypeName);
281 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
289 * Determine the number of times a particular activity-type is
290 * referenced in CiviCase XML.
292 * @param string $relationshipTypeName
293 * Symbolic-name of a relationship-type.
296 public function getRelationshipReferenceCount($relationshipTypeName) {
297 $p = new CRM_Case_XMLProcessor_Process();
299 foreach ($this->getAllCaseTypes() as $caseTypeName) {
300 $caseTypeXML = $this->retrieve($caseTypeName);
301 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {