Merge pull request #3739 from rohankatkar/testCaseTypeDelete_InUse
[civicrm-core.git] / CRM / Case / XMLRepository.php
index 61a504b062ea0546c971a168e2fc95a4ab47bae5..5193f468c6ea024a317c58006e90ceec38333478 100644 (file)
@@ -84,64 +84,23 @@ class CRM_Case_XMLRepository {
       return simplexml_load_string($definition);
     }
 
-    $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType);
+    if (!CRM_Case_BAO_CaseType::isValidName($caseType)) {
+      // perhaps caller provider a the label instead of the name?
+      throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]");
+    }
 
     if (!CRM_Utils_Array::value($caseType, $this->xml)) {
-      // first check custom templates directory
-      $fileName = NULL;
+      // Search for a file based directly on the $caseType name
+      $fileName = $this->findXmlFile($caseType);
 
+      // For backward compatibility, also search for double-mungd file names
+      // TODO In 4.6 or 5.0, remove support for loading double-munged file names
       if (!$fileName || !file_exists($fileName)) {
-        $caseTypesViaHook = $this->getCaseTypesViaHook();
-        if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
-          $fileName = $caseTypesViaHook[$caseType]['file'];
-        }
+        $fileName = $this->findXmlFile(CRM_Case_XMLProcessor::mungeCaseType($caseType));
       }
 
       if (!$fileName || !file_exists($fileName)) {
-        $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)) {
-        if (!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)) {
-          return FALSE;
-        }
+        return FALSE;
       }
 
       // read xml file
@@ -153,6 +112,65 @@ class CRM_Case_XMLRepository {
     return $this->xml[$caseType];
   }
 
+  /**
+   * @param string $caseType
+   * @return null|string file path
+   */
+  public function findXmlFile($caseType) { // first check custom templates directory
+    $fileName = NULL;
+
+    if (!$fileName || !file_exists($fileName)) {
+      $caseTypesViaHook = $this->getCaseTypesViaHook();
+      if (isset($caseTypesViaHook[$caseType], $caseTypesViaHook[$caseType]['file'])) {
+        $fileName = $caseTypesViaHook[$caseType]['file'];
+      }
+    }
+
+    if (!$fileName || !file_exists($fileName)) {
+      $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)) {
+      if (!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",
+          )
+        );
+      }
+    }
+    return file_exists($fileName) ? $fileName : NULL;
+  }
+
   /**
    * @return array
    * @see CRM_Utils_Hook::caseTypes