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