comment fixes
[civicrm-core.git] / CRM / Case / ManagedEntities.php
1 <?php
2
3 /**
4 * Class CRM_Case_ManagedEntities
5 */
6 class 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() {
17 $entities = array();
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
24 $caseTypes = array();
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'])) {
35 $entities[] = array(
36 'module' => $caseType['module'],
37 'name' => $caseType['name'],
38 'entity' => 'CaseType',
39 'params' => array(
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,
47 ),
48 );
49 }
50 else {
51 throw new CRM_Core_Exception("Invalid case type");
52 }
53 }
54 return $entities;
55 }
56
57 /**
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
62 *
63 * @return array
64 * @see CRM_Utils_Hook::managed
65 */
66 public static function createManagedActivityTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
67 $result = array();
68 $validActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
69
70 $actTypes = $xmlRepo->getAllDeclaredActivityTypes();
71 foreach ($actTypes as $actType) {
72 $managed = array(
73 'module' => 'civicrm',
74 'name' => "civicase:act:$actType",
75 'entity' => 'OptionValue',
76 'update' => 'never',
77 'cleanup' => 'unused',
78 'params' => array(
79 'version' => 3,
80 'option_group_id' => 'activity_type',
81 'label' => $actType,
82 'name' => $actType,
83 'description' => $actType,
84 'component_id' => 'CiviCase',
85 ),
86 );
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 /**
102 * Get a list of managed relationship-types by searching CiviCase XML files
103 *
104 * @return array
105 * @see CRM_Utils_Hook::managed
106 * @throws CRM_Core_Exception
107 */
108 public static function createManagedRelationshipTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
109 $result = array();
110
111 $p = new CRM_Case_XMLProcessor();
112 $validRelTypes = $p->allRelationshipTypes();
113
114 $relTypes = $xmlRepo->getAllDeclaredRelationshipTypes();
115 foreach ($relTypes as $relType) {
116 $managed = array(
117 'module' => 'civicrm',
118 'name' => "civicase:rel:$relType",
119 'entity' => 'RelationshipType',
120 'update' => 'never',
121 'cleanup' => 'unused',
122 'params' => array(
123 'version' => 3,
124 'name_a_b' => "$relType is",
125 'name_b_a' => $relType,
126 'label_a_b' => "$relType is",
127 'label_b_a' => $relType,
128 'description' => $relType,
129 'contact_type_a' => 'Individual',
130 'contact_type_b' => 'Individual',
131 'contact_sub_type_a' => NULL,
132 'contact_sub_type_b' => NULL,
133 ),
134 );
135
136 // We'll create managed-entity if this record doesn't exist yet
137 // or if we previously decided to manage this record.
138 if (!in_array($relType, $validRelTypes)) {
139 $result[] = $managed;
140 }
141 elseif ($me->get($managed['module'], $managed['name'])) {
142 $result[] = $managed;
143 }
144 }
145
146 return $result;
147 }
148
149 }