CRM-14478 - Create managed-entities for CiviCase's activity-types and relationship...
authorTim Otten <totten@civicrm.org>
Sat, 24 May 2014 03:38:38 +0000 (20:38 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 26 May 2014 20:14:45 +0000 (13:14 -0700)
CRM/Case/Info.php
CRM/Case/ManagedEntities.php [new file with mode: 0644]

index 6af83a35ba0e8fcb5034ff35fa008553abcfe41c..7067b2b7cb32820fbdcee96b266a0fcdcef1073f 100644 (file)
@@ -84,43 +84,11 @@ class CRM_Case_Info extends CRM_Core_Component_Info {
    * @throws CRM_Core_Exception
    */
   public function getManagedEntities() {
-    // Use hook_civicrm_caseTypes to build a list of OptionValues
-    // In the long run, we may want more specialized logic for this, but
-    // this design is fairly convenient and will allow us to replace it
-    // without changing the hook_civicrm_caseTypes interface.
-    $entities = array();
-
-    $caseTypes = array();
-    CRM_Utils_Hook::caseTypes($caseTypes);
-
-    $proc = new CRM_Case_XMLProcessor();
-    foreach ($caseTypes as $name => $caseType) {
-      $xml = $proc->retrieve($name);
-      if (!$xml) {
-        throw new CRM_Core_Exception("Failed to load XML for case type (" . $name . ")");
-      }
-
-      if (isset($caseType['module'], $caseType['name'], $caseType['file'])) {
-        $entities[] = array(
-          'module' => $caseType['module'],
-          'name' => $caseType['name'],
-          'entity' => 'CaseType',
-          'params' => array(
-            'version' => 3,
-            'name' => $caseType['name'],
-            'title' => (string) $xml->name,
-            'description' => (string) $xml->description,
-            'is_reserved' => 1,
-            'is_active' => 1,
-            'weight' => $xml->weight ? $xml->weight : 1,
-          ),
-        );
-      }
-      else {
-        throw new CRM_Core_Exception("Invalid case type");
-      }
-    }
-
+    $entities = array_merge(
+      CRM_Case_ManagedEntities::createManagedCaseTypes(),
+      CRM_Case_ManagedEntities::createManagedActivityTypes(CRM_Case_XMLRepository::singleton(), CRM_Core_ManagedEntities::singleton()),
+      CRM_Case_ManagedEntities::createManagedRelationshipTypes(CRM_Case_XMLRepository::singleton(), CRM_Core_ManagedEntities::singleton())
+    );
     return $entities;
   }
 
diff --git a/CRM/Case/ManagedEntities.php b/CRM/Case/ManagedEntities.php
new file mode 100644 (file)
index 0000000..6604e9e
--- /dev/null
@@ -0,0 +1,143 @@
+<?php
+
+class CRM_Case_ManagedEntities {
+
+  /**
+   * Get a list of managed-entities representing auto-generated case-types
+   * using hook_civicrm_caseTypes.
+   *
+   * @return array
+   * @see CRM_Utils_Hook::managed
+   * @throws CRM_Core_Exception
+   */
+  public static function createManagedCaseTypes() {
+    $entities = array();
+
+    // Use hook_civicrm_caseTypes to build a list of OptionValues
+    // In the long run, we may want more specialized logic for this, but
+    // this design is fairly convenient and will allow us to replace it
+    // without changing the hook_civicrm_caseTypes interface.
+
+    $caseTypes = array();
+    CRM_Utils_Hook::caseTypes($caseTypes);
+
+    $proc = new CRM_Case_XMLProcessor();
+    foreach ($caseTypes as $name => $caseType) {
+      $xml = $proc->retrieve($name);
+      if (!$xml) {
+        throw new CRM_Core_Exception("Failed to load XML for case type (" . $name . ")");
+      }
+
+      if (isset($caseType['module'], $caseType['name'], $caseType['file'])) {
+        $entities[] = array(
+          'module' => $caseType['module'],
+          'name' => $caseType['name'],
+          'entity' => 'CaseType',
+          'params' => array(
+            'version' => 3,
+            'name' => $caseType['name'],
+            'title' => (string) $xml->name,
+            'description' => (string) $xml->description,
+            'is_reserved' => 1,
+            'is_active' => 1,
+            'weight' => $xml->weight ? $xml->weight : 1,
+          ),
+        );
+      }
+      else {
+        throw new CRM_Core_Exception("Invalid case type");
+      }
+    }
+    return $entities;
+  }
+
+  /**
+   * Get a list of managed activity-types by searching CiviCase XML files
+   *
+   * @return array
+   * @see CRM_Utils_Hook::managed
+   * @throws CRM_Core_Exception
+   */
+  public static function createManagedActivityTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
+    $result = array();
+    $validActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
+
+    $actTypes = $xmlRepo->getAllDeclaredActivityTypes();
+    foreach ($actTypes as $actType) {
+      $managed = array(
+        'module' => 'civicrm',
+        'name' => "civicase:act:$actType",
+        'entity' => 'OptionValue',
+        'update' => 'never',
+        'cleanup' => 'unused',
+        'params' => array(
+          'version' => 3,
+          'option_group_id' => 'activity_type',
+          'label' => $actType,
+          'name' => $actType,
+          'description' => $actType,
+          'component_id' => 'CiviCase',
+        )
+      );
+
+      // We'll create managed-entity if this record doesn't exist yet
+      // or if we previously decided to manage this record.
+      if (!in_array($actType, $validActTypes)) {
+        $result[] = $managed;
+      }
+      elseif ($me->get($managed['module'], $managed['name'])) {
+        $result[] = $managed;
+      }
+    }
+
+    return $result;
+  }
+
+  /**
+   * Get a list of managed relationship-types by searching CiviCase XML files
+   *
+   * @return array
+   * @see CRM_Utils_Hook::managed
+   * @throws CRM_Core_Exception
+   */
+  public static function createManagedRelationshipTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
+    $result = array();
+
+    $p = new CRM_Case_XMLProcessor();
+    $validRelTypes = $p->allRelationshipTypes();
+
+    $relTypes = $xmlRepo->getAllDeclaredRelationshipTypes();
+    foreach ($relTypes as $relType) {
+      $managed = array(
+        'module' => 'civicrm',
+        'name' => "civicase:rel:$relType",
+        'entity' => 'RelationshipType',
+        'update' => 'never',
+        'cleanup' => 'unused',
+        'params' => array(
+          'version' => 3,
+          'name_a_b' => "$relType is",
+          'name_b_a' => $relType,
+          'label_a_b' => "$relType is",
+          'label_b_a' => $relType,
+          'description' => $relType,
+          'contact_type_a' => 'Individual',
+          'contact_type_b' => 'Individual',
+          'contact_sub_type_a' => NULL,
+          'contact_sub_type_b' => NULL,
+        )
+      );
+
+      // We'll create managed-entity if this record doesn't exist yet
+      // or if we previously decided to manage this record.
+      if (!in_array($relType, $validRelTypes)) {
+        $result[] = $managed;
+      }
+      elseif ($me->get($managed['module'], $managed['name'])) {
+        $result[] = $managed;
+      }
+    }
+
+    return $result;
+  }
+}
\ No newline at end of file