Merge pull request #17863 from demeritcowboy/be-greener
[civicrm-core.git] / CRM / ACL / Page / ACL.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 class CRM_ACL_Page_ACL extends CRM_Core_Page_Basic {
18
19 public $useLivePageJS = TRUE;
20
21 /**
22 * The action links that we need to display for the browse screen.
23 *
24 * @var array
25 */
26 public static $_links = NULL;
27
28 /**
29 * Get BAO Name.
30 *
31 * @return string
32 * Classname of BAO.
33 */
34 public function getBAOName() {
35 return 'CRM_ACL_BAO_ACL';
36 }
37
38 /**
39 * Get action Links.
40 *
41 * @return array
42 * (reference) of action links
43 */
44 public function &links() {
45 if (!(self::$_links)) {
46 self::$_links = [
47 CRM_Core_Action::UPDATE => [
48 'name' => ts('Edit'),
49 'url' => 'civicrm/acl',
50 'qs' => 'reset=1&action=update&id=%%id%%',
51 'title' => ts('Edit ACL'),
52 ],
53 CRM_Core_Action::DISABLE => [
54 'name' => ts('Disable'),
55 'ref' => 'crm-enable-disable',
56 'title' => ts('Disable ACL'),
57 ],
58 CRM_Core_Action::ENABLE => [
59 'name' => ts('Enable'),
60 'ref' => 'crm-enable-disable',
61 'title' => ts('Enable ACL'),
62 ],
63 CRM_Core_Action::DELETE => [
64 'name' => ts('Delete'),
65 'url' => 'civicrm/acl',
66 'qs' => 'reset=1&action=delete&id=%%id%%',
67 'title' => ts('Delete ACL'),
68 ],
69 ];
70 }
71 return self::$_links;
72 }
73
74 /**
75 * Run the page.
76 *
77 * Set the breadcrumb before beginning the standard page run.
78 */
79 public function run() {
80 // set breadcrumb to append to admin/access
81 $breadCrumb = [
82 [
83 'title' => ts('Access Control'),
84 'url' => CRM_Utils_System::url('civicrm/admin/access', 'reset=1'),
85 ],
86 ];
87 CRM_Utils_System::appendBreadCrumb($breadCrumb);
88
89 // parent run
90 return parent::run();
91 }
92
93 /**
94 * Browse all acls.
95 */
96 public function browse() {
97 // get all acl's sorted by weight
98 $acl = [];
99 $query = "
100 SELECT *
101 FROM civicrm_acl
102 WHERE ( object_table IN ( 'civicrm_saved_search', 'civicrm_uf_group', 'civicrm_custom_group', 'civicrm_event' ) )
103 ORDER BY entity_id
104 ";
105 $dao = CRM_Core_DAO::executeQuery($query);
106
107 $roles = CRM_Core_OptionGroup::values('acl_role');
108
109 $group = [
110 '-1' => ts('- select -'),
111 '0' => ts('All Groups'),
112 ] + CRM_Core_PseudoConstant::group();
113 $customGroup = [
114 '-1' => ts('- select -'),
115 '0' => ts('All Custom Groups'),
116 ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
117 $ufGroup = [
118 '-1' => ts('- select -'),
119 '0' => ts('All Profiles'),
120 ] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
121
122 $event = [
123 '-1' => ts('- select -'),
124 '0' => ts('All Events'),
125 ] + CRM_Event_PseudoConstant::event();
126
127 while ($dao->fetch()) {
128 $acl[$dao->id] = [];
129 $acl[$dao->id]['name'] = $dao->name;
130 $acl[$dao->id]['operation'] = $dao->operation;
131 $acl[$dao->id]['entity_id'] = $dao->entity_id;
132 $acl[$dao->id]['entity_table'] = $dao->entity_table;
133 $acl[$dao->id]['object_table'] = $dao->object_table;
134 $acl[$dao->id]['object_id'] = $dao->object_id;
135 $acl[$dao->id]['is_active'] = $dao->is_active;
136
137 if ($acl[$dao->id]['entity_id']) {
138 $acl[$dao->id]['entity'] = $roles[$acl[$dao->id]['entity_id']] ?? NULL;
139 }
140 else {
141 $acl[$dao->id]['entity'] = ts('Everyone');
142 }
143
144 switch ($acl[$dao->id]['object_table']) {
145 case 'civicrm_saved_search':
146 $acl[$dao->id]['object'] = $group[$acl[$dao->id]['object_id']] ?? NULL;
147 $acl[$dao->id]['object_name'] = ts('Group');
148 break;
149
150 case 'civicrm_uf_group':
151 $acl[$dao->id]['object'] = $ufGroup[$acl[$dao->id]['object_id']] ?? NULL;
152 $acl[$dao->id]['object_name'] = ts('Profile');
153 break;
154
155 case 'civicrm_custom_group':
156 $acl[$dao->id]['object'] = $customGroup[$acl[$dao->id]['object_id']] ?? NULL;
157 $acl[$dao->id]['object_name'] = ts('Custom Group');
158 break;
159
160 case 'civicrm_event':
161 $acl[$dao->id]['object'] = $event[$acl[$dao->id]['object_id']] ?? NULL;
162 $acl[$dao->id]['object_name'] = ts('Event');
163 break;
164 }
165
166 // form all action links
167 $action = array_sum(array_keys($this->links()));
168
169 if ($dao->is_active) {
170 $action -= CRM_Core_Action::ENABLE;
171 }
172 else {
173 $action -= CRM_Core_Action::DISABLE;
174 }
175
176 $acl[$dao->id]['action'] = CRM_Core_Action::formLink(
177 self::links(),
178 $action,
179 ['id' => $dao->id],
180 ts('more'),
181 FALSE,
182 'ACL.manage.action',
183 'ACL',
184 $dao->id
185 );
186 }
187 $this->assign('rows', $acl);
188 }
189
190 /**
191 * Get name of edit form.
192 *
193 * @return string
194 * Classname of edit form.
195 */
196 public function editForm() {
197 return 'CRM_ACL_Form_ACL';
198 }
199
200 /**
201 * Get edit form name.
202 *
203 * @return string
204 * name of this page.
205 */
206 public function editName() {
207 return 'ACL';
208 }
209
210 /**
211 * Get user context.
212 *
213 * @param null $mode
214 *
215 * @return string
216 * user context.
217 */
218 public function userContext($mode = NULL) {
219 return 'civicrm/acl';
220 }
221
222 /**
223 * Edit an ACL.
224 *
225 * @param int $mode
226 * What mode for the form ?.
227 * @param int $id
228 * Id of the entity (for update, view operations).
229 * @param bool $imageUpload
230 * Not used in this case, but extended from CRM_Core_Page_Basic.
231 * @param bool $pushUserContext
232 * Not used in this case, but extended from CRM_Core_Page_Basic.
233 */
234 public function edit($mode, $id = NULL, $imageUpload = FALSE, $pushUserContext = TRUE) {
235 if ($mode & (CRM_Core_Action::UPDATE)) {
236 if (isset($id)) {
237 $aclName = CRM_Core_DAO::getFieldValue('CRM_ACL_DAO_ACL', $id);
238 CRM_Utils_System::setTitle(ts('Edit ACL &ndash; %1', [1 => $aclName]));
239 }
240 }
241 parent::edit($mode, $id, $imageUpload, $pushUserContext);
242 }
243
244 }