Search Builder - Enhance UI with Select2 and EntityRef
[civicrm-core.git] / CRM / Case / ManagedEntities.php
CommitLineData
32e74b94
TO
1<?php
2
cd5823ae
EM
3/**
4 * Class CRM_Case_ManagedEntities
5 */
32e74b94
TO
6class CRM_Case_ManagedEntities {
7
8 /**
9 * Get a list of managed-entities representing auto-generated case-types
10 * using hook_civicrm_caseTypes.
11 *
12 * @return array
13 * @see CRM_Utils_Hook::managed
14 * @throws CRM_Core_Exception
15 */
16 public static function createManagedCaseTypes() {
be2fb01f 17 $entities = [];
32e74b94
TO
18
19 // Use hook_civicrm_caseTypes to build a list of OptionValues
20 // In the long run, we may want more specialized logic for this, but
21 // this design is fairly convenient and will allow us to replace it
22 // without changing the hook_civicrm_caseTypes interface.
23
be2fb01f 24 $caseTypes = [];
32e74b94
TO
25 CRM_Utils_Hook::caseTypes($caseTypes);
26
27 $proc = new CRM_Case_XMLProcessor();
28 foreach ($caseTypes as $name => $caseType) {
29 $xml = $proc->retrieve($name);
30 if (!$xml) {
31 throw new CRM_Core_Exception("Failed to load XML for case type (" . $name . ")");
32 }
33
34 if (isset($caseType['module'], $caseType['name'], $caseType['file'])) {
be2fb01f 35 $entities[] = [
32e74b94
TO
36 'module' => $caseType['module'],
37 'name' => $caseType['name'],
38 'entity' => 'CaseType',
be2fb01f 39 'params' => [
32e74b94
TO
40 'version' => 3,
41 'name' => $caseType['name'],
42 'title' => (string) $xml->name,
43 'description' => (string) $xml->description,
44 'is_reserved' => 1,
45 'is_active' => 1,
46 'weight' => $xml->weight ? $xml->weight : 1,
be2fb01f
CW
47 ],
48 ];
32e74b94
TO
49 }
50 else {
51 throw new CRM_Core_Exception("Invalid case type");
52 }
53 }
54 return $entities;
55 }
56
57 /**
3bdf1f3a 58 * Get a list of managed activity-types by searching CiviCase XML files.
59 *
60 * @param \CRM_Case_XMLRepository $xmlRepo
61 * @param \CRM_Core_ManagedEntities $me
32e74b94
TO
62 *
63 * @return array
64 * @see CRM_Utils_Hook::managed
32e74b94
TO
65 */
66 public static function createManagedActivityTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
be2fb01f 67 $result = [];
32e74b94
TO
68 $validActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
69
70 $actTypes = $xmlRepo->getAllDeclaredActivityTypes();
71 foreach ($actTypes as $actType) {
be2fb01f 72 $managed = [
32e74b94
TO
73 'module' => 'civicrm',
74 'name' => "civicase:act:$actType",
75 'entity' => 'OptionValue',
76 'update' => 'never',
77 'cleanup' => 'unused',
be2fb01f 78 'params' => [
32e74b94
TO
79 'version' => 3,
80 'option_group_id' => 'activity_type',
81 'label' => $actType,
82 'name' => $actType,
83 'description' => $actType,
84 'component_id' => 'CiviCase',
be2fb01f
CW
85 ],
86 ];
32e74b94
TO
87
88 // We'll create managed-entity if this record doesn't exist yet
89 // or if we previously decided to manage this record.
90 if (!in_array($actType, $validActTypes)) {
91 $result[] = $managed;
92 }
93 elseif ($me->get($managed['module'], $managed['name'])) {
94 $result[] = $managed;
95 }
96 }
97
98 return $result;
99 }
100
101 /**
ad37ac8e 102 * Get a list of managed relationship-types by searching CiviCase XML files.
103 *
104 * @param \CRM_Case_XMLRepository $xmlRepo
105 * @param \CRM_Core_ManagedEntities $me
32e74b94
TO
106 *
107 * @return array
108 * @see CRM_Utils_Hook::managed
32e74b94
TO
109 */
110 public static function createManagedRelationshipTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
be2fb01f 111 $result = [];
32e74b94 112
fa8e67b8 113 if (!isset(Civi::$statics[__CLASS__]['reltypes'])) {
2058bf54 114 $relationshipInfo = CRM_Core_PseudoConstant::relationshipType('name', TRUE, NULL);
41cf58d3 115 foreach ($relationshipInfo as $id => $relTypeDetails) {
2058bf54
D
116 Civi::$statics[__CLASS__]['reltypes']["{$id}_a_b"] = $relTypeDetails['name_a_b'];
117 if ($relTypeDetails['name_a_b'] != $relTypeDetails['name_b_a']) {
118 Civi::$statics[__CLASS__]['reltypes']["{$id}_b_a"] = $relTypeDetails['name_b_a'];
41cf58d3
AF
119 }
120 }
fa8e67b8
TO
121 }
122 $validRelTypes = Civi::$statics[__CLASS__]['reltypes'];
32e74b94
TO
123
124 $relTypes = $xmlRepo->getAllDeclaredRelationshipTypes();
125 foreach ($relTypes as $relType) {
d0878639
AF
126 // Making assumption that client is the A side of the relationship.
127 // Relationship label coming from XML, meaning from perspective of
128 // non-client.
129
130 // These assumptions only apply if a case type is introduced without the
131 // relationship types already existing.
be2fb01f 132 $managed = [
32e74b94
TO
133 'module' => 'civicrm',
134 'name' => "civicase:rel:$relType",
135 'entity' => 'RelationshipType',
136 'update' => 'never',
137 'cleanup' => 'unused',
be2fb01f 138 'params' => [
32e74b94
TO
139 'version' => 3,
140 'name_a_b' => "$relType is",
141 'name_b_a' => $relType,
142 'label_a_b' => "$relType is",
143 'label_b_a' => $relType,
144 'description' => $relType,
d0878639
AF
145 'contact_type_a' => NULL,
146 'contact_type_b' => NULL,
32e74b94
TO
147 'contact_sub_type_a' => NULL,
148 'contact_sub_type_b' => NULL,
be2fb01f
CW
149 ],
150 ];
32e74b94
TO
151
152 // We'll create managed-entity if this record doesn't exist yet
153 // or if we previously decided to manage this record.
154 if (!in_array($relType, $validRelTypes)) {
155 $result[] = $managed;
156 }
157 elseif ($me->get($managed['module'], $managed['name'])) {
158 $result[] = $managed;
159 }
160 }
161
162 return $result;
163 }
96025800 164
ef10e0b5 165}