Merge pull request #22496 from civicrm/5.46
[civicrm-core.git] / CRM / Custom / Page / Group.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Create a page for displaying Custom Sets.
20 *
21 * Heart of this class is the run method which checks
22 * for action type and then displays the appropriate
23 * page.
24 *
25 */
26 class CRM_Custom_Page_Group extends CRM_Core_Page {
27
28 /**
29 * The action links that we need to display for the browse screen
30 *
31 * @var array
32 */
33 private static $_actionLinks;
34
35 /**
36 * Get the action links for this page.
37 *
38 *
39 * @return array
40 * array of action links that we need to display for the browse screen
41 */
42 public static function &actionLinks() {
43 // check if variable _actionsLinks is populated
44 if (!isset(self::$_actionLinks)) {
45 self::$_actionLinks = [
46 CRM_Core_Action::BROWSE => [
47 'name' => ts('View and Edit Custom Fields'),
48 'url' => 'civicrm/admin/custom/group/field',
49 'qs' => 'reset=1&action=browse&gid=%%id%%',
50 'title' => ts('View and Edit Custom Fields'),
51 ],
52 CRM_Core_Action::PREVIEW => [
53 'name' => ts('Preview'),
54 'url' => 'civicrm/admin/custom/group',
55 'qs' => 'action=preview&reset=1&id=%%id%%',
56 'title' => ts('Preview Custom Data Set'),
57 ],
58 CRM_Core_Action::UPDATE => [
59 'name' => ts('Settings'),
60 'url' => 'civicrm/admin/custom/group',
61 'qs' => 'action=update&reset=1&id=%%id%%',
62 'title' => ts('Edit Custom Set'),
63 ],
64 CRM_Core_Action::DISABLE => [
65 'name' => ts('Disable'),
66 'ref' => 'crm-enable-disable',
67 'title' => ts('Disable Custom Set'),
68 ],
69 CRM_Core_Action::ENABLE => [
70 'name' => ts('Enable'),
71 'ref' => 'crm-enable-disable',
72 'title' => ts('Enable Custom Set'),
73 ],
74 CRM_Core_Action::DELETE => [
75 'name' => ts('Delete'),
76 'url' => 'civicrm/admin/custom/group',
77 'qs' => 'action=delete&reset=1&id=%%id%%',
78 'title' => ts('Delete Custom Set'),
79 ],
80 ];
81 }
82 return self::$_actionLinks;
83 }
84
85 /**
86 * Run the page.
87 *
88 * This method is called after the page is created. It checks for the
89 * type of action and executes that action.
90 * Finally it calls the parent's run method.
91 *
92 * @return void
93 */
94 public function run() {
95 // get the requested action
96 $action = CRM_Utils_Request::retrieve('action', 'String',
97 // default to 'browse'
98 $this, FALSE, 'browse'
99 );
100
101 if ($action & CRM_Core_Action::DELETE) {
102 $session = CRM_Core_Session::singleton();
103 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/', 'action=browse'));
104 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_DeleteGroup', "Delete Cutom Set", NULL);
105 $id = CRM_Utils_Request::retrieve('id', 'Positive',
106 $this, FALSE, 0
107 );
108 $controller->set('id', $id);
109 $controller->setEmbedded(TRUE);
110 $controller->process();
111 $controller->run();
112 }
113 // assign vars to templates
114 $this->assign('action', $action);
115 $id = CRM_Utils_Request::retrieve('id', 'Positive',
116 $this, FALSE, 0
117 );
118
119 // what action to take ?
120 if ($action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
121 $this->edit($id, $action);
122 }
123 elseif ($action & CRM_Core_Action::PREVIEW) {
124 $this->preview($id);
125 }
126 else {
127 // finally browse the custom groups
128 $this->browse();
129 }
130 // parent run
131 return parent::run();
132 }
133
134 /**
135 * Edit custom group.
136 *
137 * @param int $id
138 * Custom group id.
139 * @param string $action
140 * The action to be invoked.
141 *
142 * @return void
143 */
144 public function edit($id, $action) {
145 // create a simple controller for editing custom data
146 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Group', ts('Custom Set'), $action);
147
148 // set the userContext stack
149 $session = CRM_Core_Session::singleton();
150 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/', 'action=browse'));
151 $controller->set('id', $id);
152 $controller->setEmbedded(TRUE);
153 $controller->process();
154 $controller->run();
155 }
156
157 /**
158 * Preview custom group.
159 *
160 * @param int $id
161 * Custom group id.
162 *
163 * @return void
164 */
165 public function preview($id) {
166 $controller = new CRM_Core_Controller_Simple('CRM_Custom_Form_Preview', ts('Preview Custom Data'), NULL);
167 $session = CRM_Core_Session::singleton();
168 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group', 'action=browse'));
169 $controller->set('groupId', $id);
170 $controller->setEmbedded(TRUE);
171 $controller->process();
172 $controller->run();
173 }
174
175 /**
176 * Browse all custom data groups.
177 *
178 * @param string $action
179 * The action to be invoked.
180 *
181 * @return void
182 */
183 public function browse($action = NULL) {
184 // get all custom groups sorted by weight
185 $customGroup = [];
186 $dao = new CRM_Core_DAO_CustomGroup();
187 $dao->is_reserved = FALSE;
188 $dao->orderBy('weight, title');
189 $dao->find();
190
191 $customGroupExtends = CRM_Core_SelectValues::customGroupExtends();
192 $customGroupStyle = CRM_Core_SelectValues::customGroupStyle();
193 while ($dao->fetch()) {
194 $id = $dao->id;
195 $customGroup[$id] = ['class' => ''];
196 CRM_Core_DAO::storeValues($dao, $customGroup[$id]);
197 // form all action links
198 $action = array_sum(array_keys(self::actionLinks()));
199
200 // update enable/disable links depending on custom_group properties.
201 if ($dao->is_active) {
202 $action -= CRM_Core_Action::ENABLE;
203 }
204 else {
205 $action -= CRM_Core_Action::DISABLE;
206 }
207 $customGroup[$id]['order'] = $customGroup[$id]['weight'];
208 $customGroup[$id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action,
209 ['id' => $id],
210 ts('more'),
211 FALSE,
212 'customGroup.row.actions',
213 'CustomGroup',
214 $id
215 );
216 if (!empty($customGroup[$id]['style'])) {
217 $customGroup[$id]['style_display'] = $customGroupStyle[$customGroup[$id]['style']];
218 }
219 $customGroup[$id]['extends_display'] = $customGroupExtends[$customGroup[$id]['extends']];
220 }
221
222 //fix for Displaying subTypes
223 $subTypes = [];
224
225 $subTypes['Activity'] = CRM_Core_PseudoConstant::activityType(FALSE, TRUE, FALSE, 'label', TRUE);
226 $subTypes['Contribution'] = CRM_Contribute_PseudoConstant::financialType();
227 $subTypes['Membership'] = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
228 $subTypes['Event'] = CRM_Core_OptionGroup::values('event_type');
229 $subTypes['Grant'] = CRM_Core_OptionGroup::values('grant_type');
230 $subTypes['Campaign'] = CRM_Campaign_PseudoConstant::campaignType();
231 $subTypes['Participant'] = [];
232 $subTypes['ParticipantRole'] = CRM_Core_OptionGroup::values('participant_role');
233 $subTypes['ParticipantEventName'] = CRM_Event_PseudoConstant::event();
234 $subTypes['ParticipantEventType'] = CRM_Core_OptionGroup::values('event_type');
235 $subTypes['Individual'] = CRM_Contact_BAO_ContactType::subTypePairs('Individual', FALSE, NULL);
236 $subTypes['Household'] = CRM_Contact_BAO_ContactType::subTypePairs('Household', FALSE, NULL);
237 $subTypes['Organization'] = CRM_Contact_BAO_ContactType::subTypePairs('Organization', FALSE, NULL);
238
239 $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Individual');
240 $relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Organization');
241 $relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Household');
242
243 $allRelationshipType = [];
244 $allRelationshipType = array_merge($relTypeInd, $relTypeOrg);
245 $allRelationshipType = array_merge($allRelationshipType, $relTypeHou);
246
247 //adding subtype specific relationships CRM-5256
248 $relSubType = CRM_Contact_BAO_ContactType::subTypeInfo();
249 foreach ($relSubType as $subType => $val) {
250 $subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, NULL, NULL, $val['parent'],
251 FALSE, 'label', TRUE, $subType
252 );
253 $allRelationshipType = array_merge($allRelationshipType, $subTypeRelationshipTypes);
254 }
255
256 $subTypes['Relationship'] = $allRelationshipType;
257
258 $cSubTypes = CRM_Core_Component::contactSubTypes();
259 $contactSubTypes = [];
260 foreach ($cSubTypes as $key => $value) {
261 $contactSubTypes[$key] = $key;
262 }
263
264 $subTypes['Contact'] = $contactSubTypes;
265
266 CRM_Core_BAO_CustomGroup::getExtendedObjectTypes($subTypes);
267
268 foreach ($customGroup as $key => $values) {
269 $subValue = $customGroup[$key]['extends_entity_column_value'] ?? NULL;
270 $subName = $customGroup[$key]['extends_entity_column_id'] ?? NULL;
271 $type = $customGroup[$key]['extends'] ?? NULL;
272 if ($subValue) {
273 $subValue = explode(CRM_Core_DAO::VALUE_SEPARATOR,
274 substr($subValue, 1, -1)
275 );
276 $colValue = NULL;
277 foreach ($subValue as $sub) {
278 if ($sub) {
279 if ($type == 'Participant') {
280 if ($subName == 1) {
281 $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantRole'][$sub] : $subTypes['ParticipantRole'][$sub];
282 }
283 elseif ($subName == 2) {
284 $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantEventName'][$sub] : $subTypes['ParticipantEventName'][$sub];
285 }
286 elseif ($subName == 3) {
287 $colValue = $colValue ? $colValue . ', ' . $subTypes['ParticipantEventType'][$sub] : $subTypes['ParticipantEventType'][$sub];
288 }
289 }
290 elseif ($type == 'Relationship') {
291 $colValue = $colValue ? $colValue . ', ' . $subTypes[$type][$sub . '_a_b'] : $subTypes[$type][$sub . '_a_b'];
292 if (isset($subTypes[$type][$sub . '_b_a'])) {
293 $colValue = $colValue ? $colValue . ', ' . $subTypes[$type][$sub . '_b_a'] : $subTypes[$type][$sub . '_b_a'];
294 }
295 }
296 else {
297 $colValue = $colValue ? ($colValue . (isset($subTypes[$type][$sub]) ? ', ' . $subTypes[$type][$sub] : '')) : ($subTypes[$type][$sub] ?? '');
298 }
299 }
300 }
301 $customGroup[$key]["extends_entity_column_value"] = $colValue;
302 }
303 else {
304 if (isset($subTypes[$type]) && is_array($subTypes[$type])) {
305 $customGroup[$key]["extends_entity_column_value"] = ts("Any");
306 }
307 }
308 }
309
310 $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group', "reset=1&action=browse");
311 CRM_Utils_Weight::addOrder($customGroup, 'CRM_Core_DAO_CustomGroup',
312 'id', $returnURL
313 );
314
315 $this->assign('rows', $customGroup);
316 }
317
318 }