CRM-15368
[civicrm-core.git] / CRM / Case / XMLProcessor.php
index c2bbccc044859bd8b8744d6ff621d0c98237c707..a174a6a8ea472bf5a0d53b13ce2d191bd45cf49f 100644 (file)
  */
 class CRM_Case_XMLProcessor {
 
-  static protected $_xml;
-
-  static protected $_hookCache = NULL;
-
-  function retrieve($caseType) {
-    $caseType = self::mungeCaseType($caseType);
-
-    if (!CRM_Utils_Array::value($caseType, self::$_xml)) {
-      if (!self::$_xml) {
-        self::$_xml = array();
-      }
-
-      // first check custom templates directory
-      $fileName = NULL;
-      $config = CRM_Core_Config::singleton();
-      if (isset($config->customTemplateDir) &&
-        $config->customTemplateDir
-      ) {
-        // check if the file exists in the custom templates directory
-        $fileName = implode(DIRECTORY_SEPARATOR,
-          array(
-            $config->customTemplateDir,
-            'CRM',
-            'Case',
-            'xml',
-            'configuration',
-            "$caseType.xml",
-          )
-        );
-      }
-
-      if (!$fileName ||
-        !file_exists($fileName)
-      ) {
-        // check if file exists locally
-        $fileName = implode(DIRECTORY_SEPARATOR,
-          array(dirname(__FILE__),
-            'xml',
-            'configuration',
-            "$caseType.xml",
-          )
-        );
-
-        if (!file_exists($fileName)) {
-          // check if file exists locally
-          $fileName = implode(DIRECTORY_SEPARATOR,
-            array(dirname(__FILE__),
-              'xml',
-              'configuration.sample',
-              "$caseType.xml",
-            )
-          );
-        }
-
-        if (!file_exists($fileName)) {
-          if (self::$_hookCache === NULL) {
-            self::$_hookCache = array();
-            CRM_Utils_Hook::caseTypes(self::$_hookCache);
-          }
-          if (isset(self::$_hookCache[$caseType], self::$_hookCache[$caseType]['file'])) {
-            $fileName = self::$_hookCache[$caseType]['file'];
-          }
-        }
-
-        if (!file_exists($fileName)) {
-          return FALSE;
-        }
-      }
-
-      // read xml file
-      $dom = new DomDocument();
-      $dom->load($fileName);
-      $dom->xinclude();
-      self::$_xml[$caseType] = simplexml_import_dom($dom);
-    }
-    return self::$_xml[$caseType];
+  /**
+   * FIXME: This does *NOT* belong in a static property, but we're too late in
+   * the 4.5-cycle to do the necessary cleanup.
+   *
+   * @var array|null array(int $id => string $relTypeCname)
+   */
+  public static $activityTypes = NULL;
+
+  /**
+   * FIXME: This does *NOT* belong in a static property, but we're too late in
+   * the 4.5-cycle to do the necessary cleanup.
+   *
+   * @var array|null array(int $id => string $relTypeCname)
+   */
+  public static $relationshipTypes = NULL;
+
+  /**
+   * Relationship-types have four name fields (name_a_b, name_b_a, label_a_b,
+   * label_b_a), but CiviCase XML refers to reltypes by a single name.
+   * REL_TYPE_CNAME identifies the canonical name field as used by CiviCase XML.
+   *
+   * This appears to be "label_b_a", but IMHO "name_b_a" would be more
+   * sensible.
+   */
+  const REL_TYPE_CNAME = 'label_b_a';
+
+  /**
+   * @param $caseType
+   *
+   * @return FALSE|SimpleXMLElement
+   */
+  public function retrieve($caseType) {
+    return CRM_Case_XMLRepository::singleton()->retrieve($caseType);
   }
 
+  /**
+   * This function was previously used to convert a case-type's
+   * machine-name to a file-name. However, it's mind-boggling
+   * that the file-name might be a munged version of the
+   * machine-name (which is itself a munged version of the
+   * display-name), and naming is now a more visible issue (since
+   * the overhaul of CaseType admin UI).
+   *
+   * Usage note: This is called externally by civix stubs as a
+   * sort of side-ways validation of the case-type's name
+   * (validation which was needed because of the unintuitive
+   * double-munge). We should update civix templates and then
+   * remove this function in Civi 4.6 or 5.0.
+   *
+   * @param string $caseType
+   * @return string
+   * @deprecated
+   * @see CRM_Case_BAO_CaseType::isValidName
+   */
   public static function mungeCaseType($caseType) {
     // trim all spaces from $caseType
     $caseType = str_replace('_', ' ', $caseType);
@@ -119,27 +95,41 @@ class CRM_Case_XMLProcessor {
     return $caseType;
   }
 
+  /**
+   * @param bool $indexName
+   * @param bool $all
+   *
+   * @return array
+   */
   function &allActivityTypes($indexName = TRUE, $all = FALSE) {
-    static $activityTypes = NULL;
-    if (!$activityTypes) {
-      $activityTypes = CRM_Case_PseudoConstant::caseActivityType($indexName, $all);
+    if (self::$activityTypes === NULL) {
+      self::$activityTypes = CRM_Case_PseudoConstant::caseActivityType($indexName, $all);
     }
-    return $activityTypes;
+    return self::$activityTypes;
   }
 
+  /**
+   * @return array
+   */
   function &allRelationshipTypes() {
-    static $relationshipTypes = array();
-
-    if (!$relationshipTypes) {
-      $relationshipInfo = CRM_Core_PseudoConstant::relationshipType();
+    if (self::$relationshipTypes === NULL) {
+      $relationshipInfo = CRM_Core_PseudoConstant::relationshipType('label', TRUE);
 
-      $relationshipTypes = array();
+      self::$relationshipTypes = array();
       foreach ($relationshipInfo as $id => $info) {
-        $relationshipTypes[$id] = $info['label_b_a'];
+        self::$relationshipTypes[$id] = $info[CRM_Case_XMLProcessor::REL_TYPE_CNAME];
       }
     }
 
-    return $relationshipTypes;
+    return self::$relationshipTypes;
+  }
+
+  /**
+   * FIXME: This should not exist
+   */
+  public static function flushStaticCaches() {
+    self::$activityTypes = NULL;
+    self::$relationshipTypes = NULL;
   }
 }