3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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 $dom->load($fileName);
141 $fileXml = simplexml_import_dom($dom);
150 * @param string $caseType
151 * @return null|string
154 public function findXmlFile($caseType) {
155 // first check custom templates directory
158 if (!$fileName ||
!file_exists($fileName)) {
159 $caseTypesViaHook = $this->getCaseTypesViaHook();
160 if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
161 $fileName = $caseTypesViaHook[$caseType]['file'];
165 if (!$fileName ||
!file_exists($fileName)) {
166 $config = CRM_Core_Config
::singleton();
167 if (isset($config->customTemplateDir
) && $config->customTemplateDir
) {
168 // check if the file exists in the custom templates directory
169 $fileName = implode(DIRECTORY_SEPARATOR
,
171 $config->customTemplateDir
,
182 if (!$fileName ||
!file_exists($fileName)) {
183 if (!file_exists($fileName)) {
184 // check if file exists locally
185 $fileName = implode(DIRECTORY_SEPARATOR
,
195 if (!file_exists($fileName)) {
196 // check if file exists locally
197 $fileName = implode(DIRECTORY_SEPARATOR
,
201 'configuration.sample',
207 return file_exists($fileName) ?
$fileName : NULL;
212 * @see CRM_Utils_Hook::caseTypes
214 public function getCaseTypesViaHook() {
215 if ($this->hookCache
=== NULL) {
216 $this->hookCache
= array();
217 CRM_Utils_Hook
::caseTypes($this->hookCache
);
219 return $this->hookCache
;
223 * @return array<string> symbolic names of case-types
225 public function getAllCaseTypes() {
226 if ($this->allCaseTypes
=== NULL) {
227 $this->allCaseTypes
= CRM_Case_PseudoConstant
::caseType("name");
229 return $this->allCaseTypes
;
233 * @return array<string> symbolic-names of activity-types
235 public function getAllDeclaredActivityTypes() {
238 $p = new CRM_Case_XMLProcessor_Process();
239 foreach ($this->getAllCaseTypes() as $caseTypeName) {
240 $caseTypeXML = $this->retrieve($caseTypeName);
241 $result = array_merge($result, $p->getDeclaredActivityTypes($caseTypeXML));
244 $result = array_unique($result);
250 * @return array<string> symbolic-names of relationship-types
252 public function getAllDeclaredRelationshipTypes() {
255 $p = new CRM_Case_XMLProcessor_Process();
256 foreach ($this->getAllCaseTypes() as $caseTypeName) {
257 $caseTypeXML = $this->retrieve($caseTypeName);
258 $result = array_merge($result, $p->getDeclaredRelationshipTypes($caseTypeXML));
261 $result = array_unique($result);
267 * Determine the number of times a particular activity-type is
268 * referenced in CiviCase XML.
270 * @param string $activityType
271 * Symbolic-name of an activity type.
274 public function getActivityReferenceCount($activityType) {
275 $p = new CRM_Case_XMLProcessor_Process();
277 foreach ($this->getAllCaseTypes() as $caseTypeName) {
278 $caseTypeXML = $this->retrieve($caseTypeName);
279 if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) {
287 * Determine the number of times a particular activity-type is
288 * referenced in CiviCase XML.
290 * @param string $relationshipTypeName
291 * Symbolic-name of a relationship-type.
294 public function getRelationshipReferenceCount($relationshipTypeName) {
295 $p = new CRM_Case_XMLProcessor_Process();
297 foreach ($this->getAllCaseTypes() as $caseTypeName) {
298 $caseTypeXML = $this->retrieve($caseTypeName);
299 if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) {