Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-02-16-17-51-20
[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 * @return array
61 * @see CRM_Utils_Hook::managed
62 * @throws CRM_Core_Exception
63 */
64 public static function createManagedActivityTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
65 $result = array();
66 $validActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
67
68 $actTypes = $xmlRepo->getAllDeclaredActivityTypes();
69 foreach ($actTypes as $actType) {
70 $managed = array(
71 'module' => 'civicrm',
72 'name' => "civicase:act:$actType",
73 'entity' => 'OptionValue',
74 'update' => 'never',
75 'cleanup' => 'unused',
76 'params' => array(
77 'version' => 3,
78 'option_group_id' => 'activity_type',
79 'label' => $actType,
80 'name' => $actType,
81 'description' => $actType,
82 'component_id' => 'CiviCase',
83 ),
84 );
85
86 // We'll create managed-entity if this record doesn't exist yet
87 // or if we previously decided to manage this record.
88 if (!in_array($actType, $validActTypes)) {
89 $result[] = $managed;
90 }
91 elseif ($me->get($managed['module'], $managed['name'])) {
92 $result[] = $managed;
93 }
94 }
95
96 return $result;
97 }
98
99 /**
100 * Get a list of managed relationship-types by searching CiviCase XML files
101 *
102 * @return array
103 * @see CRM_Utils_Hook::managed
104 * @throws CRM_Core_Exception
105 */
106 public static function createManagedRelationshipTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) {
107 $result = array();
108
109 $p = new CRM_Case_XMLProcessor();
110 $validRelTypes = $p->allRelationshipTypes();
111
112 $relTypes = $xmlRepo->getAllDeclaredRelationshipTypes();
113 foreach ($relTypes as $relType) {
114 $managed = array(
115 'module' => 'civicrm',
116 'name' => "civicase:rel:$relType",
117 'entity' => 'RelationshipType',
118 'update' => 'never',
119 'cleanup' => 'unused',
120 'params' => array(
121 'version' => 3,
122 'name_a_b' => "$relType is",
123 'name_b_a' => $relType,
124 'label_a_b' => "$relType is",
125 'label_b_a' => $relType,
126 'description' => $relType,
127 'contact_type_a' => 'Individual',
128 'contact_type_b' => 'Individual',
129 'contact_sub_type_a' => NULL,
130 'contact_sub_type_b' => NULL,
131 ),
132 );
133
134 // We'll create managed-entity if this record doesn't exist yet
135 // or if we previously decided to manage this record.
136 if (!in_array($relType, $validRelTypes)) {
137 $result[] = $managed;
138 }
139 elseif ($me->get($managed['module'], $managed['name'])) {
140 $result[] = $managed;
141 }
142 }
143
144 return $result;
145 }
146
147 }