dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
myLKW_list.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\myLKW;
3
4use function dbx\myLKW\dbx_get_datum;
5
6class myLKW_list {
7
8 private string $dd = 'lkw';
9 private string $fd = 'lkw-grid';
10 private $oDB;
11
12 public function __construct(){
13 $this->oDB = dbx()->get_system_obj('dbxDB');
14 }
15
16 /* --- Fenster erzeugen --- */
17 private function dbx_shift_window(int $block): string {
18
19 // Basisdatum über zentrale Logik bestimmen:
20 // 0 Offset = aktueller Dispo-Tag (automatisch Workday korrekt)
21 $baseStr = dbx_get_datum('yyyy-mm-dd', 0);
22 $base = new \DateTime($baseStr);
23
24 // Offset-Logik identisch:
25 // d0=-2, d1=-1, d2=0, d3=+1, d4=+2, d5=+3
26 $offset = $block - 2;
27
28 return dbx_get_datum('dy dd.mm~~', $offset, $base);
29 }
30
32 private function create_tab() {
33 $oTPL = dbx()->get_system_obj('dbxTPL');
34
35 $data['dat0'] = $this->dbx_shift_window(0);
36 $data['dat1'] = $this->dbx_shift_window(1);
37 $data['dat2'] = $this->dbx_shift_window(2);
38 $data['dat3'] = $this->dbx_shift_window(3);
39 $data['dat4'] = $this->dbx_shift_window(4);
40 $data['dat5'] = $this->dbx_shift_window(5);
41
42 $cols = $this->get_fd_grid_cols();
43 $data['cols'] = $oTPL->replaces($cols, $data);
44 $data['dbx_search'] = $oTPL->get_tpl('dbx|search', dbx()->search_defaults(array(
45 'title' => 'Search',
46 'extra_attrs' => 'data-dbx="grid-search"',
47 'i' => dbx()->next_id(),
48 )));
49
50 dbx()->debug("grid_cols=($cols)");
51
52 $content=$oTPL->get_tpl('modul|report-lkw',$data);
53 return $content;
54 }
55
56 private function get_fd_fields(): array {
57 $dir = dbx()->get_base_dir() . 'dbx/modules/myLKW/fd/';
58 $file = function_exists('dbx_lng_resolve_file')
59 ? dbx_lng_resolve_file($dir, $this->fd, 'fd.php', '', true)
60 : $dir . $this->fd . '.fd.php';
61
62 if (!$file || !is_file($file)) {
63 return array();
64 }
65
66 $fields = array();
67 $field = array();
68 include $file;
69
70 return is_array($fields) ? array_values($fields) : array();
71 }
72
73 private function get_fd_cols(): string {
74 $cols = array();
75
76 foreach ($this->get_fd_fields() as $field) {
77 if (!empty($field['name']) && is_string($field['name'])) {
78 $cols[] = $field['name'];
79 }
80 }
81
82 return implode(',', $cols);
83 }
84
85 private function get_fd_grid_cols(): string {
86 $cols = array();
87
88 foreach ($this->get_fd_fields() as $field) {
89 if (empty($field['name'])) {
90 continue;
91 }
92
93 $name = (string) $field['name'];
94 if (str_starts_with($name, '_')) {
95 continue;
96 }
97
98 $type = $field['type'] ?? 'text';
99 $gridType = method_exists($this->oDB, 'map_dd_type_to_grid_type')
100 ? $this->oDB->map_dd_type_to_grid_type((string) $type)
101 : 'text';
102
103 $label = trim((string)($field['label'] ?? ''));
104 $label = str_replace(array(':', '[', ']'), '-', $label);
105 if ($label === '') {
106 $label = $name;
107 }
108
109 $group = '';
110 if (!empty($field['group']) && is_string($field['group'])) {
111 $group = '@' . trim($field['group']);
112 }
113
114 $protect = isset($field['protect']) ? (string) $field['protect'] : '0';
115 $suffix = '';
116 if ($protect === '2') {
117 $suffix = ':!v';
118 } elseif ($protect === '1') {
119 $suffix = ':p';
120 }
121
122 $cols[] = $name . '[' . $label . ']:' . $gridType . $suffix . $group;
123 }
124
125 return implode(',', $cols);
126 }
127
129 private function sort_lkw(){
130
131 $oDB = $this->oDB;
132 $dd = $this->dd;
133
134 $server_time = (new \DateTime())->format('Y-m-d H:i:s.v');
135
136 $rwhere = 'TRACTOR > " "';
137 $flds = $this->get_fd_cols();
138
139 $rrows = 300;
140 $rpos = 0;
141
142 $field = $_GET['field'] ?? '';
143 $dir = $_GET['dir'] ?? 'asc';
144 if ($dir=='none') $dir ='desc';
145
146 if(!preg_match('/^[a-zA-Z0-9_]+$/',$field)){
147 $field = 'TIPO';
148 }
149
150 $dir = strtolower($dir) === 'desc' ? 'DESC' : 'ASC';
151
152 if ($field === 'TIPO') {
153 $dir = ($dir === 'ASC') ? 'DESC' : 'ASC';
154 $rsort = "TIPO {$dir}";
155 } else {
156 $rsort = "TIPO DESC, {$field} {$dir}";
157 }
158
159 $rdesc = '';
160
161 $rows = $oDB->select(
162 $dd,
163 $rwhere,
164 $flds,
165 $rsort,
166 $rdesc,
167 '',
168 $rrows,
169 $rpos
170 );
171
172 if(!is_array($rows)){
173 $rows = [];
174 }
175
176 $count = count($rows);
177 dbx()->debug("count=($count) resort=($rsort)");
178
179 dbx()->json_response(array(
180 'ok' => 1,
181 'count' => $count,
182 'rows' => array_values($rows),
183 'server_time' => $server_time,
184 ), true);
185 }
186
187 /* ========================================================= */
188
189 private function read_lkw(){
190
191 $oDB = $this->oDB;
192 $dd = $this->dd;
193
194 $server_time = (new \DateTime())->format('Y-m-d H:i:s.v');
195
196 $rwhere = 'TRACTOR > " "';
197 $flds = $this->get_fd_cols();
198
199 $rrows = 300;
200 $rpos = 0;
201 $rsort = 'TIPO DESC, d2_carga_region ASC, TRACTOR ASC';
202 $rdesc = '';
203
204 $rows = $oDB->select(
205 $dd,
206 $rwhere,
207 $flds,
208 $rsort,
209 $rdesc,
210 '',
211 $rrows,
212 $rpos
213 );
214
215 if(!is_array($rows)){
216 $rows = [];
217 }
218
219 $count = count($rows);
220
221 dbx()->json_response(array(
222 'ok' => 1,
223 'count' => $count,
224 'rows' => array_values($rows),
225 'server_time' => $server_time,
226 ), true);
227 }
228
229 /* ========================================================= */
230
231 private function save_lkw(){
232 $ok=0;
234 if (isset($_POST['rows'])) {
235 $post = $_POST['rows'];
236 } else {
237 $this->json(false,"Keine rows übergeben");
238 }
239 //dbx_debug("##SAVE###",$post);
240 foreach ($post as $no => $record) {
241 dbx()->debug("record=",$record);
242 if(!empty($record['id'])){
243 $new_rec="ERROR";
244 $id = intval($record['id']);
245 $ok = $this->oDB->update($this->dd,$record,$id);
246 if ($ok) $new_rec=$this->oDB->select1($dd,$id);
247 dbx()->debug("WRITE-LKW ok=($ok)= id=($id) New rec=",$new_rec);
248 }
249 }
250 $this->json($ok);
251
252 /*
253 if(empty($post['id'])){
254 $this->json(false,"Keine ID übergeben");
255 }
256
257 $id = intval($post['id']);
258 unset($post['id']);
259
260 foreach($post as $k=>$v){
261 if(is_string($v)) $post[$k] = trim($v);
262 }
263 dbx()->debug("Save LKW Id=($id) Post=",$post);
264 $ok = $this->oDB->update($this->dd, $post,$id,0,1,0,1);
265 dbx()->debug("WRITE-LKW ok($ok)= id=($id) data=",$post);
266
267 $this->json($ok);
268 */
269 }
270
271 /* ========================================================= */
272
273 private function delete_lkw(){
274
275 $id = intval($_POST['id'] ?? 0);
276
277 if(!$id){
278 $this->json(false,"Keine ID übergeben");
279 }
280
281 $ok = $this->oDB->delete($this->dd,$id);
282
283 $this->json($ok);
284 }
285
286 /* ========================================================= */
287
288 private function json(bool $success, string $msg=""){
289 header('Content-Type: application/json; charset=utf-8');
290 echo json_encode([
291 'success'=>$success,
292 'msg'=>$msg
293 ],JSON_UNESCAPED_UNICODE);
294 exit;
295 }
296
297 /* ========================================================= */
298
299 public function run() {
300
301 $run = dbx()->get_modul_var('dbx_run2','create_tab','parameter');
302 dbx()->debug("myLKW_list run work=($run)");
303
304 switch($run){
305
306 case 'create_tab':
307 return $this->create_tab();
308 break;
309
310 case 'sort_lkw':
311 $this->sort_lkw();
312 break;
313
314 case 'read_lkw':
315 $this->read_lkw();
316 break;
317
318 case 'save_lkw':
319 $this->save_lkw();
320 break;
321
322 case 'delete_lkw':
323 $this->delete_lkw();
324 break;
325
326 default:
327 return "Unbekannte Aktion ($run)";
328 }
329 }
330
331}
get_base_dir(int $cutData=0)
Liefert das Basisverzeichnis der Installation.
Definition dbxApi.php:1507
search_defaults(array $overrides=array())
Standard-Optionen fuer dbx|search (wie Content-Grid).
Definition dbxApi.php:2326
next_id(int $add=1)
Erzeugt eine fortlaufende ID innerhalb des DBX-Kontexts.
Definition dbxApi.php:2181
dbx_lng_resolve_file(string $dir, string $name, string $ext, string $lng='', bool $fallback=true)
Datei mit Sprachsuffix aufloesen: name_lng.ext mit Fallback name.ext.
Definition dbxApi.php:3109
exit
Definition index.php:146
dbx_get_datum(string $format='day, dd.mm.yyyy', int $offset=0, ?DateTime $base=null)
DBX schema administration.