dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
myLKW_import.class.php
Go to the documentation of this file.
1<?php
2namespace dbx\myLKW;
3
5
6
7/* =========================================================
8 PUBLIC IMPORT
9 ========================================================= */
10
11public function import(){
12
13 $oTPL = dbx()->get_system_obj('dbxTPL');
14 $oDB = dbx()->get_system_obj('dbxDB');
15
16 $path_file = dbx_get_file_dir().'sys/csv/lkw.csv';
17
18 if (!file_exists($path_file)) {
19 return $oTPL->get_tpl('dbx|alert-danger',[
20 'msg'=>"CSV Datei nicht gefunden: $path_file"
21 ]);
22 }
23
24 $rows = $this->read_csv($path_file);
25
26 if (!$rows) {
27 return $oTPL->get_tpl('dbx|alert-warning',[
28 'msg'=>"CSV Datei leer oder nicht lesbar"
29 ]);
30 }
31
32 $ddFields = $oDB->get_dd_fields('lkw');
33
34 $ddNames=[];
35 foreach ($ddFields as $f) {
36 if (!empty($f['name'])) {
37 $ddNames[$f['name']] = 1;
38 }
39 }
40
41 $oDB->empty('lkw');
42
43 $count_insert=0;
44 $count_skip=0;
45
46 foreach ($rows as $row) {
47
48 $record = $this->map_csv_row_to_lkw($row);
49
50 /* LEERZEILEN ERKENNEN */
51 if ($this->is_empty_lkw_row($record)) {
52 $count_skip++;
53 continue;
54 }
55
56 /* nur gültige DD Felder */
57 $record = array_intersect_key($record,$ddNames);
58
59 if (!$record) {
60 $count_skip++;
61 continue;
62 }
63
64 $ok = $oDB->insert('lkw',$record);
65
66 if ($ok) $count_insert++;
67 else $count_skip++;
68 }
69
70 $msg['msg'] =
71 "Import abgeschlossen.<br>".
72 "Neu angelegt: $count_insert<br>".
73 "Übersprungen: $count_skip";
74
75 return $oTPL->get_tpl('dbx|alert-info',$msg);
76}
77
78
79/* =========================================================
80 CSV LESEN
81 ========================================================= */
82
83protected function read_csv(string $file): array {
84
85 $data=[];
86 $sep=',';
87
88 $fh=fopen($file,'rb');
89 if(!$fh) return [];
90
91 $bom=fread($fh,3);
92 if($bom!=="\xEF\xBB\xBF"){
93 rewind($fh);
94 }
95
96 $header=fgetcsv($fh,0,$sep);
97
98 if(!$header){
99 fclose($fh);
100 return [];
101 }
102
103 foreach ($header as &$h) {
104 $h = $this->normalize_str($h);
105 }
106
107 $fixed=[
108 'DOMICILIO'=>'DOMICILIO',
109 'TRACTOR'=>'TRACTOR',
110 'I.T.V.TRACT'=>'ITV_TRACT',
111 'TIPO'=>'TIPO',
112 'REMOLQUE'=>'REMOLQUE',
113 'I.T.V.REMOL.'=>'ITV_REMOL',
114 'CONDUCTOR'=>'CONDUCTOR',
115 'TELF.'=>'TELF',
116 'EMPRESA'=>'EMPRESA',
117 'EXT.'=>'EXT',
118 'MANT.'=>'MANT',
119 'EVENTOS'=>'EVENTOS',
120 'BUJES'=>'BUJES',
121 'VENCIMIENTO'=>'VENCIMIENTO',
122 'ANOTACIONES'=>'ANOTACIONES',
123 'ODOMETRO'=>'ODOMETRO',
124 ];
125
126 $fixedCols=[];
127
128 foreach($header as $i=>$h){
129 if(isset($fixed[$h])){
130 $fixedCols[$fixed[$h]]=$i;
131 }
132 }
133
134 if(!isset($fixedCols['TRACTOR'])){
135 fclose($fh);
136 return [];
137 }
138
139 $map=[
140 'd0'=>['X','Y','Z','AA','AB'],
141 'd1'=>['AD','AE','AF','AG','AH'],
142 'd2'=>['AJ','AK','AL','AM','AN'],
143 'd3'=>['AP','AQ','AR','AS','AT'],
144 'd4'=>['AV','AW','AX','AY','AZ']
145 ];
146
147 $colIndex=[];
148
149 foreach($map as $d=>$cols){
150 foreach($cols as $c){
151 $colIndex[$d][]=$this->excel_col_to_index($c);
152 }
153 }
154
155 while(($row=fgetcsv($fh,0,$sep))!==false){
156
157 if(!is_array($row)) continue;
158
159 /* komplett leere CSV Zeilen überspringen */
160 if(!array_filter($row)){
161 continue;
162 }
163
164 $row=array_pad($row,140,'');
165
166 foreach($row as &$v){
167 $v=$this->normalize_str($v);
168 }
169
170 $rec=[];
171
172 foreach($fixedCols as $f=>$idx){
173 $rec[$f]=$row[$idx] ?? '';
174 }
175
176 foreach($colIndex as $d=>$idxs){
177
178 $rec["__{$d}__"]=[
179 'origen_region'=>$row[$idxs[0]] ?? '',
180 'origen_lugar'=>$row[$idxs[1]] ?? '',
181 'carga_region'=>$row[$idxs[2]] ?? '',
182 'carga_lugar'=>$row[$idxs[3]] ?? '',
183 'obs'=>$row[$idxs[4]] ?? '',
184 ];
185 }
186
187 $data[]=$rec;
188 }
189
190 fclose($fh);
191
192 return $data;
193}
194
195
196/* =========================================================
197 UTF8 CLEAN
198 ========================================================= */
199
200protected function dbx_clean_utf8(string $s): string {
201
202 if ($s === '') return '';
203
204 $s = preg_replace('/^\xEF\xBB\xBF/', '', $s);
205
206 for ($i = 0; $i < 5; $i++) {
207
208 $prev = $s;
209
210 $s = @iconv('UTF-8', 'Windows-1252//IGNORE', $s);
211 $s = @iconv('Windows-1252', 'UTF-8//IGNORE', $s);
212
213 if ($s === $prev) break;
214 }
215
216 $s = mb_convert_encoding($s, 'UTF-8', 'UTF-8');
217
218 $s = preg_replace('/[\x00-\x1F\x7F]/u', ' ', $s);
219
220 $s = str_replace("\xC2\xA0", ' ', $s);
221
222 $s = preg_replace('/\s+/u', ' ', $s);
223
224 return trim($s);
225}
226
227
228/* =========================================================
229 NORMALIZER
230 ========================================================= */
231
232protected function normalize_str(?string $v,bool $upper=false): string {
233
234 if($v===null) return '';
235
236 $v=$this->dbx_clean_utf8($v);
237
238 if($upper){
239 $v=mb_strtoupper($v,'UTF-8');
240 }
241
242 return $v;
243}
244
245
246/* =========================================================
247 LEERZEILE ERKENNEN
248 ========================================================= */
249
250protected function is_empty_lkw_row(array $record): bool {
251
252 $fields=['TRACTOR','REMOLQUE','TIPO'];
253
254 foreach ($fields as $f) {
255
256 $v = (string)($record[$f] ?? '');
257
258 /* alle whitespace entfernen */
259 $v = preg_replace('/\s+/u','',$v);
260
261 if ($v === '' || $v === '-' || $v === '.' || strtolower($v)==='null') {
262 continue;
263 }
264
265 return false;
266 }
267
268 return true;
269}
270
271/* =========================================================
272 EXCEL COL → INDEX
273 ========================================================= */
274
275protected function excel_col_to_index(string $col): int {
276
277 $col=strtoupper(trim($col));
278
279 $len=strlen($col);
280 $num=0;
281
282 for($i=0;$i<$len;$i++){
283 $num=$num*26+(ord($col[$i])-64);
284 }
285
286 return $num-1;
287}
288
289
290/* =========================================================
291 CSV → DB
292 ========================================================= */
293
294protected function map_csv_row_to_lkw(array $r): array {
295
296 $out=[
297 'DOMICILIO'=>$this->normalize_str($r['DOMICILIO'] ?? ''),
298 'TRACTOR'=>$this->normalize_str($r['TRACTOR'] ?? '',true),
299 'ITV_TRACT'=>$this->normalize_str($r['ITV_TRACT'] ?? ''),
300 'TIPO'=>$this->normalize_str($r['TIPO'] ?? '',true),
301 'REMOLQUE'=>$this->normalize_str($r['REMOLQUE'] ?? '',true),
302 'ITV_REMOL'=>$this->normalize_str($r['ITV_REMOL'] ?? ''),
303 'CONDUCTOR'=>$this->normalize_str($r['CONDUCTOR'] ?? ''),
304 'TELF'=>$this->normalize_str($r['TELF'] ?? ''),
305 'EMPRESA'=>$this->normalize_str($r['EMPRESA'] ?? ''),
306 'EXT'=>$this->normalize_str($r['EXT'] ?? ''),
307 'MANT'=>$this->normalize_str($r['MANT'] ?? ''),
308 'EVENTOS'=>$this->normalize_str($r['EVENTOS'] ?? ''),
309 'BUJES'=>$this->normalize_str($r['BUJES'] ?? ''),
310 'VENCIMIENTO'=>$this->normalize_str($r['VENCIMIENTO'] ?? ''),
311 'ANOTACIONES'=>$this->normalize_str($r['ANOTACIONES'] ?? ''),
312 'ODOMETRO'=>$this->normalize_str($r['ODOMETRO'] ?? ''),
313 ];
314
315 foreach(['d0','d1','d2','d3','d4'] as $d){
316
317 if(empty($r["__{$d}__"])) continue;
318
319 $out["{$d}_origen_region"]=$this->normalize_str($r["__{$d}__"]['origen_region'] ?? '',true);
320 $out["{$d}_origen_lugar"]=$this->normalize_str($r["__{$d}__"]['origen_lugar'] ?? '');
321 $out["{$d}_carga_region"]=$this->normalize_str($r["__{$d}__"]['carga_region'] ?? '',true);
322 $out["{$d}_carga_lugar"]=$this->normalize_str($r["__{$d}__"]['carga_lugar'] ?? '');
323 $out["{$d}_observaciones"]=$this->normalize_str($r["__{$d}__"]['obs'] ?? '');
324 }
325
326 return $out;
327}
328
329
330/* =========================================================
331 RUN
332 ========================================================= */
333
334public function run(){
335
336 $modul=dbx()->get_system_var('dbx_activ_modul');
337 $work=dbx()->get_modul_var('dbx_run2','import');
338 $content="";
339
340 switch($work){
341
342 case 'import':
343 $content=$this->import();
344 break;
345
346 default:
347
348 $oTPL=dbx()->get_system_obj('dbxTPL');
349
350 $content=$oTPL->get_tpl('dbx|alert-warning',[
351 'msg'=>"Modul=($modul) Work=($work) is undef."
352 ]);
353 }
354
355 return $content;
356}
357
358}
normalize_str(?string $v, bool $upper=false)
$fields[]
Definition config.dd.php:16
dbx_get_file_dir()
Definition index.php:265
$path_file
Definition index.php:270
if( $syncRequest)
Definition index.php:520
DBX schema administration.