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;
66 public function flush() {
68 $this->hookCache
= NULL;
69 $this->allCaseTypes
= NULL;
70 CRM_Core_DAO
::$_dbColumnValueCache = array();
76 * @param array $allCaseTypes
79 public function __construct($allCaseTypes = NULL, $xml = array()) {
80 $this->allCaseTypes
= $allCaseTypes;
87 * @param string $caseType
89 * @return FALSE|\SimpleXMLElement
90 * @throws \CRM_Core_Exception
92 public function retrieve($caseType) {
93 // check if xml definition is defined in db
94 $definition = CRM_Core_DAO
::getFieldValue('CRM_Case_DAO_CaseType', $caseType, 'definition', 'name');
96 if (!empty($definition)) {
97 list ($xml, $error) = CRM_Utils_XML
::parseString($definition);
99 throw new CRM_Core_Exception("Failed to parse CaseType XML: $error");
104 // TODO In 4.6 or 5.0, remove support for weird machine-names
105 //if (!CRM_Case_BAO_CaseType::isValidName($caseType)) {
106 // // perhaps caller provider a the label instead of the name?
107 // throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]");
110 if (!CRM_Utils_Array
::value($caseType, $this->xml
)) {
111 $fileXml = $this->retrieveFile($caseType);
113 $this->xml
[$caseType] = $fileXml;
119 return $this->xml
[$caseType];
125 * @param string $caseType
126 * @return SimpleXMLElement|FALSE
128 public function retrieveFile($caseType) {
132 if (CRM_Case_BAO_CaseType
::isValidName($caseType)) {
133 // Search for a file based directly on the $caseType name
134 $fileName = $this->findXmlFile($caseType);
137 // For backward compatibility, also search for double-munged file names
138 // TODO In 4.6 or 5.0, remove support for loading double-munged file names
139 if (!$fileName ||
!file_exists($fileName)) {
140 $fileName = $this->findXmlFile(CRM_Case_XMLProcessor
::mungeCaseType($caseType));
143 if ($fileName && file_exists($fileName)) {
145 $dom = new DomDocument();
146 $xmlString = file_get_contents($fileName);
147 $dom->loadXML($xmlString);
148 $dom->documentURI
= $fileName;
150 $fileXml = simplexml_import_dom($dom);
159 * @param string $caseType
160 * @return null|string
163 public function findXmlFile($caseType) {
164 // first check custom templates directory
167 if (!$fileName ||
!file_exists($fileName)) {
168 $caseTypesViaHook = $this->getCaseTypesViaHook();
169 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
170 $fileName = $caseTypesViaHook[$caseType]['file'];
174 if (!$fileName ||
!file_exists($fileName)) {
175 $config = CRM_Core_Config
::singleton();
176 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
177 // check if the file exists in the custom templates directory
178 $fileName = implode(DIRECTORY_SEPARATOR
,
180 $config->customTemplateDir
,
191 if (!$fileName ||
!file_exists($fileName)) {
192 if (!file_exists($fileName)) {
193 // check if file exists locally
194 $fileName = implode(DIRECTORY_SEPARATOR
,
204 if (!file_exists($fileName)) {
205 // check if file exists locally
206 $fileName = implode(DIRECTORY_SEPARATOR
,
210 'configuration.sample',
216 return file_exists($fileName) ?
$fileName : NULL;
221 * @see CRM_Utils_Hook::caseTypes
223 public function getCaseTypesViaHook() {
224 if ($this->hookCache
=== NULL) {
225 $this->hookCache
= array();
226 CRM_Utils_Hook
::caseTypes($this->hookCache
);
228 return $this->hookCache
;
232 * @return array<string> symbolic names of case-types
234 public function getAllCaseTypes() {
235 if ($this->allCaseTypes
=== NULL) {
236 $this->allCaseTypes
= CRM_Case_PseudoConstant
::caseType("name");
238 return $this->allCaseTypes
;
242 * @return array<string> symbolic-names of activity-types
244 public function getAllDeclaredActivityTypes() {
247 $p = new CRM_Case_XMLProcessor_Process();
248 foreach ($this->getAllCaseTypes() as $caseTypeName) {
249 $caseTypeXML = $this->retrieve($caseTypeName);
250 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
253 $result = array_unique($result);
259 * @return array<string> symbolic-names of relationship-types
261 public function getAllDeclaredRelationshipTypes() {
264 $p = new CRM_Case_XMLProcessor_Process();
265 foreach ($this->getAllCaseTypes() as $caseTypeName) {
266 $caseTypeXML = $this->retrieve($caseTypeName);
267 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
270 $result = array_unique($result);
276 * Determine the number of times a particular activity-type is
277 * referenced in CiviCase XML.
279 * @param string $activityType
280 * Symbolic-name of an activity type.
283 public function getActivityReferenceCount($activityType) {
284 $p = new CRM_Case_XMLProcessor_Process();
286 foreach ($this->getAllCaseTypes() as $caseTypeName) {
287 $caseTypeXML = $this->retrieve($caseTypeName);
288 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
296 * Determine the number of times a particular activity-type is
297 * referenced in CiviCase XML.
299 * @param string $relationshipTypeName
300 * Symbolic-name of a relationship-type.
303 public function getRelationshipReferenceCount($relationshipTypeName) {
304 $p = new CRM_Case_XMLProcessor_Process();
306 foreach ($this->getAllCaseTypes() as $caseTypeName) {
307 $caseTypeXML = $this->retrieve($caseTypeName);
308 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {