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