CRM-14478 - Create managed-entities for CiviCase's activity-types and relationship...
[civicrm-core.git] / CRM / Case / ManagedEntities.php
CommitLineData
32e74b94
TO
1<?php
2
3class CRM_Case_ManagedEntities {
4
5 /**
6 * Get a list of managed-entities representing auto-generated case-types
7 * using hook_civicrm_caseTypes.
8 *
9 * @return array
10 * @see CRM_Utils_Hook::managed
11 * @throws CRM_Core_Exception
12 */
13 public static function createManagedCaseTypes() {
14 $entities = array();
15
16 // Use hook_civicrm_caseTypes to build a list of OptionValues
17 // In the long run, we may want more specialized logic for this, but
18 // this design is fairly convenient and will allow us to replace it
19 // without changing the hook_civicrm_caseTypes interface.
20
21 $caseTypes = array();
22 CRM_Utils_Hook::caseTypes($caseTypes);
23
24 $proc = new CRM_Case_XMLProcessor();
25 foreach ($caseTypes as $name => $caseType) {
26 $xml = $proc->retrieve($name);
27 if (!$xml) {
28 throw new CRM_Core_Exception("Failed to load XML for case type (" . $name . ")");
29 }
30
31 if (isset($caseType['module'], $caseType['name'], $caseType['file'])) {
32 $entities[] = array(
33 'module' => $caseType['module'],
34 'name' => $caseType['name'],
35 'entity' => 'CaseType',
36 'params' => array(
37 'version' => 3,
38 'name' => $caseType['name'],
39 'title' => (string) $xml->name,
40 'description' => (string) $xml->description,
41 'is_reserved' => 1,
42 'is_active' => 1,
43 'weight' => $xml->weight ? $xml->weight : 1,
44 ),
45 );
46 }
47 else {
48 throw new CRM_Core_Exception("Invalid case type");
49 }
50 }
51 return $entities;
52 }
53
54 /**
55 * Get a list of managed activity-types by searching CiviCase XML files
56 *
57 * @return array
58 * @see CRM_Utils_Hook::managed
59 * @throws CRM_Core_Exception
60 */
61 public static function createManagedActivityTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
62 $result = array();
63 $validActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
64
65 $actTypes = $xmlRepo->getAllDeclaredActivityTypes();
66 foreach ($actTypes as $actType) {
67 $managed = array(
68 'module' => 'civicrm',
69 'name' => "civicase:act:$actType",
70 'entity' => 'OptionValue',
71 'update' => 'never',
72 'cleanup' => 'unused',
73 'params' => array(
74 'version' => 3,
75 'option_group_id' => 'activity_type',
76 'label' => $actType,
77 'name' => $actType,
78 'description' => $actType,
79 'component_id' => 'CiviCase',
80 )
81 );
82
83 // We'll create managed-entity if this record doesn't exist yet
84 // or if we previously decided to manage this record.
85 if (!in_array($actType, $validActTypes)) {
86 $result[] = $managed;
87 }
88 elseif ($me->get($managed['module'], $managed['name'])) {
89 $result[] = $managed;
90 }
91 }
92
93 return $result;
94 }
95
96 /**
97 * Get a list of managed relationship-types by searching CiviCase XML files
98 *
99 * @return array
100 * @see CRM_Utils_Hook::managed
101 * @throws CRM_Core_Exception
102 */
103 public static function createManagedRelationshipTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
104 $result = array();
105
106 $p = new CRM_Case_XMLProcessor();
107 $validRelTypes = $p->allRelationshipTypes();
108
109 $relTypes = $xmlRepo->getAllDeclaredRelationshipTypes();
110 foreach ($relTypes as $relType) {
111 $managed = array(
112 'module' => 'civicrm',
113 'name' => "civicase:rel:$relType",
114 'entity' => 'RelationshipType',
115 'update' => 'never',
116 'cleanup' => 'unused',
117 'params' => array(
118 'version' => 3,
119 'name_a_b' => "$relType is",
120 'name_b_a' => $relType,
121 'label_a_b' => "$relType is",
122 'label_b_a' => $relType,
123 'description' => $relType,
124 'contact_type_a' => 'Individual',
125 'contact_type_b' => 'Individual',
126 'contact_sub_type_a' => NULL,
127 'contact_sub_type_b' => NULL,
128 )
129 );
130
131 // We'll create managed-entity if this record doesn't exist yet
132 // or if we previously decided to manage this record.
133 if (!in_array($relType, $validRelTypes)) {
134 $result[] = $managed;
135 }
136 elseif ($me->get($managed['module'], $managed['name'])) {
137 $result[] = $managed;
138 }
139 }
140
141 return $result;
142 }
143}