dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxUser_groups.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\dbxUser_admin;
3
4Class dbxUser_groups extends \dbxObj {
5
6 private $dd = 'dbxUser_groups';
7
8 private function base_url($run2, $params = array()) {
9 $url = '?dbx_modul=dbxUser_admin&dbx_run1=user&dbx_run2=' . rawurlencode((string) $run2);
10 foreach ($params as $key => $value) {
11 $url .= '&' . rawurlencode((string) $key) . '=' . rawurlencode((string) $value);
12 }
13 return $url;
14 }
15
16 private function request_json() {
17 $raw = file_get_contents('php://input');
18 $data = $raw ? json_decode($raw, true) : array();
19 return is_array($data) ? $data : array();
20 }
21
22 private function normalize_row($row, $is_new = false) {
23 $out = array();
24 foreach (array('name', 'description', 'active') as $key) {
25 if (array_key_exists($key, (array) $row)) {
26 $out[$key] = is_array($row[$key]) ? implode(',', $row[$key]) : trim((string) $row[$key]);
27 }
28 }
29 if (isset($out['active'])) {
30 $out['active'] = (int) $out['active'];
31 }
32 if ($is_new) {
33 $out['name'] = $out['name'] ?? 'neue_gruppe_' . date('YmdHis');
34 $out['description'] = $out['description'] ?? 'Neue Gruppe';
35 $out['active'] = 1;
36 }
37 return $out;
38 }
39
40 private function grid_cols() {
41 return implode(',', array(
42 'id[ID]:number:p:width=72;hozAlign=center;headerHozAlign=center',
43 'name[Gruppe]:text::width=180',
44 'description[Beschreibung]:text::editor=textarea;width=360',
45 'active[Aktiv]:text::editor=list;values=0=Nein~1=Ja;width=100',
46 'update_date[Aktualisiert]:text:p:width=170'
47 ));
48 }
49
50 private function grid_read() {
51 $oDB = dbx()->get_system_obj('dbxDB');
52 $search = dbx()->get_request_var('dbx_search', '', 'sqlsearch|max=128');
53 $where = $oDB->build_search_where($this->dd, $search, array('name', 'description'), array('id'), 'contains');
54 $rows = $oDB->select($this->dd, $where, array('id', 'name', 'description', 'active', 'update_date'), 'name', 'ASC', '', 500, 0);
55 if (!is_array($rows)) {
56 $rows = array();
57 }
58 dbx()->json_response(array('ok' => 1, 'count' => count($rows), 'rows' => array_values($rows), 'server_time' => date('Y-m-d H:i:s')));
59 }
60
61 private function grid_save() {
62 $oDB = dbx()->get_system_obj('dbxDB');
63 $payload = $this->request_json();
64 $rows = is_array($payload['rows'] ?? null) ? $payload['rows'] : array();
65 $saved = array();
66
67 foreach ($rows as $row) {
68 $id = (int)($row['id'] ?? 0);
69 if ($id <= 0) {
70 continue;
71 }
72 $data = $this->normalize_row($row);
73 if (!$data) {
74 continue;
75 }
76 $ok = $oDB->update($this->dd, $data, $id);
77 if ($ok >= 0) {
78 $saved[] = $oDB->select1($this->dd, $id);
79 }
80 }
81
82 dbx()->json_response(array('ok' => 1, 'success' => true, 'rows' => $saved));
83 }
84
85 private function grid_insert() {
86 $oDB = dbx()->get_system_obj('dbxDB');
87 $data = $this->normalize_row(array(), true);
88 $id = ($oDB->insert($this->dd, $data) === 1) ? $oDB->get_insert_id() : 0;
89 if ($id > 0) {
90 dbx()->json_response(array('ok' => 1, 'success' => true, 'row' => $oDB->select1($this->dd, $id)));
91 }
92 dbx()->json_response(array('ok' => 0, 'success' => false, 'msg' => 'Gruppe konnte nicht angelegt werden.'));
93 }
94
95 private function grid_delete() {
96 $payload = $this->request_json();
97 $id = (int)($payload['id'] ?? 0);
98 if ($id <= 0) {
99 dbx()->json_response(array('ok' => 0, 'success' => false, 'msg' => 'ID fehlt.'));
100 }
101 $oDB = dbx()->get_system_obj('dbxDB');
102 $ok = $oDB->delete($this->dd, $id);
103 dbx()->json_response(array('ok' => $ok ? 1 : 0, 'success' => $ok ? true : false));
104 }
105
106 private function report_user_groups() {
107 $oReport = dbx()->get_system_obj('dbxReport');
108 $oDB = dbx()->get_system_obj('dbxDB');
109 $all = $oDB->count($this->dd);
110 $active = $oDB->count($this->dd, 'active = 1');
111
112 $oReport->init('report-groups-grid', 'group-admin-grid');
113 $oReport->add_rep('shell_panel_class', 'dbx-grid dbx-user-groups dbx-ajax-root');
114 $oReport->add_rep('bar_title', 'Benutzergruppen');
115 $oReport->add_rep('bar_subtitle', 'Rollen und Berechtigungsgruppen pflegen');
116 $oReport->_mode = 'tabulurator';
117 $oReport->_rrows = 520;
118 $oReport->_grid_id = 'dbxUser_groups_grid';
119 $oReport->_grid_cols = $this->grid_cols();
120 $oReport->_grid_layout = 'fitDataStretch';
121 $oReport->_grid_read_url = $this->base_url('group_grid_read');
122 $oReport->_grid_save_url = $this->base_url('group_grid_save');
123 $oReport->_grid_insert_url = $this->base_url('group_grid_insert');
124 $oReport->_grid_delete_url = $this->base_url('group_grid_delete');
125 $oReport->add_grid_stats(array(
126 array('label' => 'Gruppen', 'value' => (string)$all),
127 array('label' => 'Aktiv', 'value' => (string)$active, 'tone' => 'ok'),
128 ), 'Gruppen Kennzahlen');
129 $oReport->add_obj('users_url', 'obj-value', $this->base_url('list_user'));
130 $oReport->add_obj('bar_actions', 'obj-value',
131 '<a class="btn btn-outline-secondary btn-sm" href="' . dbx()->esc($this->base_url('list_user')) . '" title="Zur Benutzerliste"><i class="bi bi-person-lines-fill"></i></a>'
132 );
133
134 return $oReport->run();
135 }
136
137 private function delete_user_group($rid = 0) {
138 $rid = (int)$rid;
139 if ($rid <= 0) {
140 $rid = (int)dbx()->get_modul_var('rid', 0);
141 }
142 if ($rid <= 0) {
143 return dbx()->redirect($this->base_url('list_groups'), 1);
144 }
145
146 $db = dbx()->get_system_obj('dbxDB');
147 $db->delete($this->dd, $rid);
148 return dbx()->redirect($this->base_url('list_groups'), 1);
149 }
150
151 private function edit_user_group($rid = 0) {
152 $content = '';
153 $rid = (int)$rid;
154 if ($rid <= 0) {
155 $rid = (int)dbx()->get_modul_var('rid', 0);
156 }
157 $isNew = ($rid <= 0);
158
159 $db = dbx()->get_system_obj('dbxDB');
160 $data = array('active' => 1);
161 if (!$isNew) {
162 $record = $db->select1($this->dd, $rid, '*', 0);
163 if (!is_array($record)) {
164 return '<div class="alert alert-warning">Benutzergruppe nicht gefunden.</div>';
165 }
166 $data = $record;
167 }
168
169 $actionUrl = $isNew
170 ? $this->base_url('new_group')
171 : $this->base_url('edit_group', array('rid' => $rid));
172
173 $oForm = dbx()->get_system_obj('dbxForm');
174 $oForm->init('form-group');
175 $oForm->prepare_form_shell(array('class' => 'dbx-user-group-form'));
176 $oForm->_dd = $this->dd;
177 $oForm->_fld_id = 'id';
178 $oForm->_data = $data;
179 $oForm->_msg_info = $isNew ? 'Neue Benutzergruppe anlegen.' : 'Benutzergruppe bearbeiten.';
180 $oForm->_action = $actionUrl;
181 if (!$isNew) {
182 $oForm->set_activ_id($rid);
183 $oForm->_rid = $rid;
184 }
185
186 $oForm->add_module_bar(
187 $isNew ? 'Neue Benutzergruppe' : 'Gruppe bearbeiten',
188 'bi-people',
189 'Rollenname, Beschreibung und Aktivstatus'
190 );
191
192 $formActions = array(
193 'save' => true,
194 'reload' => true,
195 'reload_url' => $actionUrl,
196 'delete' => !$isNew,
197 );
198 if (!$isNew) {
199 $formActions['delete_url'] = $this->base_url('delete_group', array('rid' => $rid));
200 $formActions['delete_title'] = 'Gruppe loeschen';
201 }
202 $oForm->add_module_bar_form_actions($formActions);
203
204 $oForm->add_obj('bar_actions', 'obj-value',
205 '<a class="btn btn-outline-secondary btn-sm" href="' . dbx()->esc($this->base_url('list_groups')) . '" title="Zur Gruppenliste"><i class="bi bi-list-ul"></i></a>'
206 );
207
208 $oForm->add_fld('name', 'text-label', 'Gruppe', 'parameter|min=2|max=255');
209 $oForm->add_fld('description', 'textarea-label', 'Beschreibung', 'parameter|max=1000', data: array('rows' => 4));
210 $oForm->add_fld('active', 'checkbox-label', 'Aktiv', 'int');
211
212 if ($oForm->submit()) {
213 if (!$oForm->errors() && !$oForm->warnings()) {
214 if ($isNew) {
215 $values = array(
216 'name' => $oForm->get_post('name', '', 'parameter|max=255'),
217 'description' => $oForm->get_post('description', '', 'parameter|max=1000'),
218 'active' => $oForm->get_post('active', 1, 'int'),
219 );
220 $ok = $db->save($this->dd, $values, 0);
221 if ($ok) {
222 $newId = (int)$db->get_insert_id();
223 if ($newId > 0) {
224 return dbx()->redirect($this->base_url('edit_group', array('rid' => $newId)), 1);
225 }
226 $oForm->_msg_success = 'Gruppe angelegt.';
227 } else {
228 $oForm->_msg_error = 'Gruppe konnte nicht angelegt werden.';
229 }
230 } else {
231 $change = $oForm->changed();
232 if ($change) {
233 $ok = $oForm->save_post($this->dd, $rid);
234 $oForm->_msg_success = $ok ? 'Gruppe gespeichert.' : 'Gruppe konnte nicht gespeichert werden.';
235 } else {
236 $oForm->_msg_success = 'Keine Aenderung.';
237 }
238 }
239 } else {
240 $oForm->_msg_error = 'Pruefen Sie bitte Ihre Eingaben.';
241 }
242 }
243
244 return $oForm->run();
245 }
246
247 public function run($action='list_groups') {
248 $work = dbx()->get_request_var('dbx_run2');
249 $gid = dbx()->get_request_var('id',0,'int');
250 if (!$gid) {
251 $gid = dbx()->get_request_var('rid',0,'int');
252 }
253
254 switch ($action) {
255 case 'group_grid_read':
256 $this->grid_read();
257 break;
258 case 'group_grid_save':
259 $this->grid_save();
260 break;
261 case 'group_grid_insert':
262 $this->grid_insert();
263 break;
264 case 'group_grid_delete':
265 $this->grid_delete();
266 break;
267 case 'list_groups':
268 return $this->report_user_groups();
269 case 'new_group':
270 return $this->edit_user_group(0);
271 case 'edit_group':
272 return $this->edit_user_group($gid);
273 case 'delete_group':
274 return $this->delete_user_group($gid);
275 }
276
277 return 'Modul dbxUser_admin action('.dbx()->html($action).') not defined';
278 }
279}
280
281?>
DBX schema administration.