dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
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'] = dbx()->search_html(dbx()->search_defaults(array(
45 'title' => 'Search',
46 'extra_attrs' => 'data-dbx="grid-search"',
47 )));
48
49 dbx()->debug("grid_cols=($cols)");
50
51 $content=$oTPL->get_tpl('modul|report-lkw',$data);
52 return $content;
53 }
54
55 private function get_fd_fields(): array {
56 $dir = dbx_get_base_dir() . 'dbx/modules/myLKW/fd/';
57 $file = function_exists('dbx_lng_resolve_file')
58 ? dbx_lng_resolve_file($dir, $this->fd, 'fd.php', '', true)
59 : $dir . $this->fd . '.fd.php';
60
61 if (!$file || !is_file($file)) {
62 return array();
63 }
64
65 $fields = array();
66 $field = array();
67 include $file;
68
69 return is_array($fields) ? array_values($fields) : array();
70 }
71
72 private function get_fd_cols(): string {
73 $cols = array();
74
75 foreach ($this->get_fd_fields() as $field) {
76 if (!empty($field['name']) && is_string($field['name'])) {
77 $cols[] = $field['name'];
78 }
79 }
80
81 return implode(',', $cols);
82 }
83
84 private function get_fd_grid_cols(): string {
85 $cols = array();
86
87 foreach ($this->get_fd_fields() as $field) {
88 if (empty($field['name'])) {
89 continue;
90 }
91
92 $name = (string) $field['name'];
93 if (str_starts_with($name, '_')) {
94 continue;
95 }
96
97 $type = $field['type'] ?? 'text';
98 $gridType = method_exists($this->oDB, 'map_dd_type_to_grid_type')
99 ? $this->oDB->map_dd_type_to_grid_type((string) $type)
100 : 'text';
101
102 $label = trim((string)($field['label'] ?? ''));
103 $label = str_replace(array(':', '[', ']'), '-', $label);
104 if ($label === '') {
105 $label = $name;
106 }
107
108 $group = '';
109 if (!empty($field['group']) && is_string($field['group'])) {
110 $group = '@' . trim($field['group']);
111 }
112
113 $protect = isset($field['protect']) ? (string) $field['protect'] : '0';
114 $suffix = '';
115 if ($protect === '2') {
116 $suffix = ':!v';
117 } elseif ($protect === '1') {
118 $suffix = ':p';
119 }
120
121 $cols[] = $name . '[' . $label . ']:' . $gridType . $suffix . $group;
122 }
123
124 return implode(',', $cols);
125 }
126
128 private function sort_lkw(){
129
130 $oDB = $this->oDB;
131 $dd = $this->dd;
132
133 $server_time = (new \DateTime())->format('Y-m-d H:i:s.v');
134
135 $rwhere = 'TRACTOR > " "';
136 $flds = $this->get_fd_cols();
137
138 $rrows = 300;
139 $rpos = 0;
140
141 $field = $_GET['field'] ?? '';
142 $dir = $_GET['dir'] ?? 'asc';
143 if ($dir=='none') $dir ='desc';
144
145 if(!preg_match('/^[a-zA-Z0-9_]+$/',$field)){
146 $field = 'TIPO';
147 }
148
149 $dir = strtolower($dir) === 'desc' ? 'DESC' : 'ASC';
150
151 if ($field === 'TIPO') {
152 $dir = ($dir === 'ASC') ? 'DESC' : 'ASC';
153 $rsort = "TIPO {$dir}";
154 } else {
155 $rsort = "TIPO DESC, {$field} {$dir}";
156 }
157
158 $rdesc = '';
159
160 $rows = $oDB->select(
161 $dd,
162 $rwhere,
163 $flds,
164 $rsort,
165 $rdesc,
166 '',
167 $rrows,
168 $rpos
169 );
170
171 if(!is_array($rows)){
172 $rows = [];
173 }
174
175 $count = count($rows);
176 dbx()->debug("count=($count) resort=($rsort)");
177
178 dbx()->json_response(array(
179 'ok' => 1,
180 'count' => $count,
181 'rows' => array_values($rows),
182 'server_time' => $server_time,
183 ), true);
184 }
185
186 /* ========================================================= */
187
188 private function read_lkw(){
189
190 $oDB = $this->oDB;
191 $dd = $this->dd;
192
193 $server_time = (new \DateTime())->format('Y-m-d H:i:s.v');
194
195 $rwhere = 'TRACTOR > " "';
196 $flds = $this->get_fd_cols();
197
198 $rrows = 300;
199 $rpos = 0;
200 $rsort = 'TIPO DESC, d2_carga_region ASC, TRACTOR ASC';
201 $rdesc = '';
202
203 $rows = $oDB->select(
204 $dd,
205 $rwhere,
206 $flds,
207 $rsort,
208 $rdesc,
209 '',
210 $rrows,
211 $rpos
212 );
213
214 if(!is_array($rows)){
215 $rows = [];
216 }
217
218 $count = count($rows);
219
220 dbx()->json_response(array(
221 'ok' => 1,
222 'count' => $count,
223 'rows' => array_values($rows),
224 'server_time' => $server_time,
225 ), true);
226 }
227
228 /* ========================================================= */
229
230 private function save_lkw(){
231 $ok=0;
232 $dd=$this->dd;
233 if (isset($_POST['rows'])) {
234 $post = $_POST['rows'];
235 } else {
236 $this->json(false,"Keine rows übergeben");
237 }
238 //dbx_debug("##SAVE###",$post);
239 foreach ($post as $no => $record) {
240 dbx()->debug("record=",$record);
241 if(!empty($record['id'])){
242 $new_rec="ERROR";
243 $id = intval($record['id']);
244 $ok = $this->oDB->update($this->dd,$record,$id);
245 if ($ok) $new_rec=$this->oDB->select1($dd,$id);
246 dbx()->debug("WRITE-LKW ok=($ok)= id=($id) New rec=",$new_rec);
247 }
248 }
249 $this->json($ok);
250
251 /*
252 if(empty($post['id'])){
253 $this->json(false,"Keine ID übergeben");
254 }
255
256 $id = intval($post['id']);
257 unset($post['id']);
258
259 foreach($post as $k=>$v){
260 if(is_string($v)) $post[$k] = trim($v);
261 }
262 dbx()->debug("Save LKW Id=($id) Post=",$post);
263 $ok = $this->oDB->update($this->dd, $post,$id,0,1,0,1);
264 dbx()->debug("WRITE-LKW ok($ok)= id=($id) data=",$post);
265
266 $this->json($ok);
267 */
268 }
269
270 /* ========================================================= */
271
272 private function delete_lkw(){
273
274 $id = intval($_POST['id'] ?? 0);
275
276 if(!$id){
277 $this->json(false,"Keine ID übergeben");
278 }
279
280 $ok = $this->oDB->delete($this->dd,$id);
281
282 $this->json($ok);
283 }
284
285 /* ========================================================= */
286
287 private function json(bool $success, string $msg=""){
288 header('Content-Type: application/json; charset=utf-8');
289 echo json_encode([
290 'success'=>$success,
291 'msg'=>$msg
292 ],JSON_UNESCAPED_UNICODE);
293 exit;
294 }
295
296 /* ========================================================= */
297
298 public function run() {
299
300 $run = dbx()->get_modul_var('dbx_run2','create_tab','parameter');
301 dbx()->debug("myLKW_list run work=($run)");
302
303 switch($run){
304
305 case 'create_tab':
306 return $this->create_tab();
307 break;
308
309 case 'sort_lkw':
310 $this->sort_lkw();
311 break;
312
313 case 'read_lkw':
314 $this->read_lkw();
315 break;
316
317 case 'save_lkw':
318 $this->save_lkw();
319 break;
320
321 case 'delete_lkw':
322 $this->delete_lkw();
323 break;
324
325 default:
326 return "Unbekannte Aktion ($run)";
327 }
328 }
329
330}
$fields[]
Definition config.dd.php:16
$field
Definition config.dd.php:4
exit
Definition index.php:532
dbx_get_base_dir($cut_Data=0)
Definition index.php:157
dbx_get_datum(string $format='day, dd.mm.yyyy', int $offset=0, ?DateTime $base=null)
DBX schema administration.