| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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-2019 |
| 32 | * |
| 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. |
| 36 | */ |
| 37 | class CRM_Case_XMLRepository { |
| 38 | private static $singleton; |
| 39 | |
| 40 | /** |
| 41 | * @var array<String,SimpleXMLElement> |
| 42 | */ |
| 43 | protected $xml = []; |
| 44 | |
| 45 | /** |
| 46 | * @var array|null |
| 47 | */ |
| 48 | protected $hookCache = NULL; |
| 49 | |
| 50 | /** |
| 51 | * Symbolic names of case-types. |
| 52 | * |
| 53 | * @var array|null |
| 54 | */ |
| 55 | protected $allCaseTypes = NULL; |
| 56 | |
| 57 | /** |
| 58 | * @param bool $fresh |
| 59 | * @return CRM_Case_XMLRepository |
| 60 | */ |
| 61 | public static function singleton($fresh = FALSE) { |
| 62 | if (!self::$singleton || $fresh) { |
| 63 | self::$singleton = new static(); |
| 64 | } |
| 65 | return self::$singleton; |
| 66 | } |
| 67 | |
| 68 | public function flush() { |
| 69 | $this->xml = []; |
| 70 | $this->hookCache = NULL; |
| 71 | $this->allCaseTypes = NULL; |
| 72 | CRM_Core_DAO::$_dbColumnValueCache = []; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Class constructor. |
| 77 | * |
| 78 | * @param array $allCaseTypes |
| 79 | * @param array $xml |
| 80 | */ |
| 81 | public function __construct($allCaseTypes = NULL, $xml = []) { |
| 82 | $this->allCaseTypes = $allCaseTypes; |
| 83 | $this->xml = $xml; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Retrieve case. |
| 88 | * |
| 89 | * @param string $caseType |
| 90 | * |
| 91 | * @return FALSE|\SimpleXMLElement |
| 92 | * @throws \CRM_Core_Exception |
| 93 | */ |
| 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'); |
| 97 | |
| 98 | if (!empty($definition)) { |
| 99 | list ($xml, $error) = CRM_Utils_XML::parseString($definition); |
| 100 | if (!$xml) { |
| 101 | throw new CRM_Core_Exception("Failed to parse CaseType XML: $error"); |
| 102 | } |
| 103 | return $xml; |
| 104 | } |
| 105 | |
| 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]"); |
| 110 | //} |
| 111 | |
| 112 | if (!CRM_Utils_Array::value($caseType, $this->xml)) { |
| 113 | $fileXml = $this->retrieveFile($caseType); |
| 114 | if ($fileXml) { |
| 115 | $this->xml[$caseType] = $fileXml; |
| 116 | } |
| 117 | else { |
| 118 | return FALSE; |
| 119 | } |
| 120 | } |
| 121 | return $this->xml[$caseType]; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Retrieve file. |
| 126 | * |
| 127 | * @param string $caseType |
| 128 | * @return SimpleXMLElement|FALSE |
| 129 | */ |
| 130 | public function retrieveFile($caseType) { |
| 131 | $fileName = NULL; |
| 132 | $fileXml = NULL; |
| 133 | |
| 134 | if (CRM_Case_BAO_CaseType::isValidName($caseType)) { |
| 135 | // Search for a file based directly on the $caseType name |
| 136 | $fileName = $this->findXmlFile($caseType); |
| 137 | } |
| 138 | |
| 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)); |
| 143 | } |
| 144 | |
| 145 | if ($fileName && file_exists($fileName)) { |
| 146 | // read xml file |
| 147 | $dom = new DomDocument(); |
| 148 | $xmlString = file_get_contents($fileName); |
| 149 | $dom->loadXML($xmlString); |
| 150 | $dom->documentURI = $fileName; |
| 151 | $dom->xinclude(); |
| 152 | $fileXml = simplexml_import_dom($dom); |
| 153 | } |
| 154 | |
| 155 | return $fileXml; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Find xml file. |
| 160 | * |
| 161 | * @param string $caseType |
| 162 | * @return null|string |
| 163 | * file path |
| 164 | */ |
| 165 | public function findXmlFile($caseType) { |
| 166 | // first check custom templates directory |
| 167 | $fileName = NULL; |
| 168 | |
| 169 | if (!$fileName || !file_exists($fileName)) { |
| 170 | $caseTypesViaHook = $this->getCaseTypesViaHook(); |
| 171 | if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) { |
| 172 | $fileName = $caseTypesViaHook[$caseType]['file']; |
| 173 | } |
| 174 | } |
| 175 | |
| 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, |
| 181 | [ |
| 182 | $config->customTemplateDir, |
| 183 | 'CRM', |
| 184 | 'Case', |
| 185 | 'xml', |
| 186 | 'configuration', |
| 187 | "$caseType.xml", |
| 188 | ] |
| 189 | ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (!$fileName || !file_exists($fileName)) { |
| 194 | if (!file_exists($fileName)) { |
| 195 | // check if file exists locally |
| 196 | $fileName = implode(DIRECTORY_SEPARATOR, |
| 197 | [ |
| 198 | dirname(__FILE__), |
| 199 | 'xml', |
| 200 | 'configuration', |
| 201 | "$caseType.xml", |
| 202 | ] |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | if (!file_exists($fileName)) { |
| 207 | // check if file exists locally |
| 208 | $fileName = implode(DIRECTORY_SEPARATOR, |
| 209 | [ |
| 210 | dirname(__FILE__), |
| 211 | 'xml', |
| 212 | 'configuration.sample', |
| 213 | "$caseType.xml", |
| 214 | ] |
| 215 | ); |
| 216 | } |
| 217 | } |
| 218 | return file_exists($fileName) ? $fileName : NULL; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * @return array |
| 223 | * @see CRM_Utils_Hook::caseTypes |
| 224 | */ |
| 225 | public function getCaseTypesViaHook() { |
| 226 | if ($this->hookCache === NULL) { |
| 227 | $this->hookCache = []; |
| 228 | CRM_Utils_Hook::caseTypes($this->hookCache); |
| 229 | } |
| 230 | return $this->hookCache; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @return array<string> symbolic names of case-types |
| 235 | */ |
| 236 | public function getAllCaseTypes() { |
| 237 | if ($this->allCaseTypes === NULL) { |
| 238 | $this->allCaseTypes = CRM_Case_PseudoConstant::caseType("name"); |
| 239 | } |
| 240 | return $this->allCaseTypes; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * @return array<string> symbolic-names of activity-types |
| 245 | */ |
| 246 | public function getAllDeclaredActivityTypes() { |
| 247 | $result = []; |
| 248 | |
| 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)); |
| 253 | } |
| 254 | |
| 255 | $result = array_unique($result); |
| 256 | sort($result); |
| 257 | return $result; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Relationships are straight from XML, described from perspective of non-client |
| 262 | * |
| 263 | * @return array<string> symbolic-names of relationship-types |
| 264 | */ |
| 265 | public function getAllDeclaredRelationshipTypes() { |
| 266 | $result = []; |
| 267 | |
| 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)); |
| 272 | } |
| 273 | |
| 274 | $result = array_unique($result); |
| 275 | sort($result); |
| 276 | return $result; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Determine the number of times a particular activity-type is |
| 281 | * referenced in CiviCase XML. |
| 282 | * |
| 283 | * @param string $activityType |
| 284 | * Symbolic-name of an activity type. |
| 285 | * @return int |
| 286 | */ |
| 287 | public function getActivityReferenceCount($activityType) { |
| 288 | $p = new CRM_Case_XMLProcessor_Process(); |
| 289 | $count = 0; |
| 290 | foreach ($this->getAllCaseTypes() as $caseTypeName) { |
| 291 | $caseTypeXML = $this->retrieve($caseTypeName); |
| 292 | if (in_array($activityType, $p->getDeclaredActivityTypes($caseTypeXML))) { |
| 293 | $count++; |
| 294 | } |
| 295 | } |
| 296 | return $count; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Determine the number of times a particular activity-type is |
| 301 | * referenced in CiviCase XML. |
| 302 | * |
| 303 | * @param string $relationshipTypeName |
| 304 | * Symbolic-name of a relationship-type. |
| 305 | * @return int |
| 306 | */ |
| 307 | public function getRelationshipReferenceCount($relationshipTypeName) { |
| 308 | $p = new CRM_Case_XMLProcessor_Process(); |
| 309 | $count = 0; |
| 310 | foreach ($this->getAllCaseTypes() as $caseTypeName) { |
| 311 | $caseTypeXML = $this->retrieve($caseTypeName); |
| 312 | if (in_array($relationshipTypeName, $p->getDeclaredRelationshipTypes($caseTypeXML))) { |
| 313 | $count++; |
| 314 | } |
| 315 | } |
| 316 | return $count; |
| 317 | } |
| 318 | |
| 319 | } |