dbXapp 2.0
RAD, CMS, Module und Runtime-IDE fuer dbXapp
Loading...
Searching...
No Matches
dbxCSVreader.class.php
Go to the documentation of this file.
1<?php
2
3
4class dbxCSVreader extends dbxObj {
5
6
7 public function clear() {
8 $section=$this->_section;
9 if ($section) {
10 $this->del_property('*');
11 } else {
12 $this->_properties=array();
13 }
14 }
15
16
17 public function init($process) {
18 $this->_section=$process;
19 $status=$this->get_property('status','init');
20 //dbx_debug("#get status=($status) Process=($process)") ;
21
22 if ($status=='end') $status='init';
23 if ($status=='init') {
24 $this->clear();
25 $this->set_property('status','run');
26 $this->set_property('done',0);
27 $this->set_property('percent',0);
28 $this->set_property('errors',0);
29
30 }
31 //dbx_debug("######init=($status) process=($process)");
32 return $status;
33 }
34
35
36 private function make_fildset($line,$remap=0) {
37 If(!$remap) $remap=$this->get_property('remap',-1);
38 $sep = $this->get_property('seperator',';');
39 $fieldset_clean=array();
40 $fieldset = explode($sep, $line);
41 foreach ($fieldset as $no => $fld) {
42 if (strpos($fld,'(')) $fld=substr($fld,0,strpos($fld,'(',0));
43 if (strpos($fld,'/')) $fld=substr($fld,0,strpos($fld,'/',0));
44 if (strpos($fld,' ')) $fld=substr($fld,0,strpos($fld,' ',0));
45 if (is_array($remap)) {
46 foreach ($remap as $old => $new) {
47 If ($fld==$old) $fld=$new;
48 }
49 }
50 if ($fld) $fieldset_clean[]=$fld;
51 }
52
53 //dbx_debug ("#make fieldset FIELDSET clean mit remap ($line)",$fieldset_clean);
54 $this->set_property('fieldset',$fieldset_clean);
55
56 }
57
58 private function get_fildset() {
59 $fieldset=array();
60 $fieldset=$this->get_property('fieldset',$fieldset);
61 return $fieldset;
62 }
63
64 private function sql_literal($value, $oDB, $server) {
65 if ($value === null || strtoupper((string)$value) === 'NULL') {
66 return 'NULL';
67 }
68
69 if (dbx()->is_int_value($value)) {
70 return (string)(int)$value;
71 }
72
73 if ($oDB) {
74 return "'" . $oDB->escape((string)$value, $server) . "'";
75 }
76
77 return "'" . str_replace("'", "''", (string)$value) . "'";
78 }
79
80 private function make_where($where,$record,$oDB=null,$dd='') {
81 if (!$oDB && $dd) {
82 $oDB = dbx()->get_system_obj('dbxDB');
83 }
84 $server = ($oDB && $dd) ? $oDB->get_dd_server($dd) : 'default';
85
86 foreach ($record as $name => $value) {
87 $xkey='{'.$name.'}';
88 if (strpos((string)$where, "'" . $xkey . "'") !== false) {
89 $where=str_replace("'" . $xkey . "'", $this->sql_literal($value, $oDB, $server), $where);
90 }
91 if (strpos((string)$where, '"' . $xkey . '"') !== false) {
92 $where=str_replace('"' . $xkey . '"', $this->sql_literal($value, $oDB, $server), $where);
93 }
94 $where=str_replace($xkey, $this->sql_literal($value, $oDB, $server), $where);
95 if (!(strpos($where, '}'))) break;
96 }
97 return $where;
98 }
99
100
101 private function encodeFLEX($str) {
102 $str=str_replace("„","ä",$str);
103 $str=str_replace("","ü",$str);
104 $str=str_replace("”","ö",$str);
105 $str=str_replace("á","ß",$str);
106 $str=str_replace("¯","Ä",$str);
107 $str=str_replace("�","Ü",$str);
108 $str=str_replace('³','ü',$str);
109 $str=str_replace('õ','ö',$str);
110 return $str;
111 }
112
113
114 private function encodeToUtf8($string) {
115 //$string = $this->encodeFLEX($string);
116 //$string = @iconv( "cp437", "ISO-8859-15",$string);
117
118 $string=mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "Windows-1252, ISO-8859-15, ISO-8859-1, ISO-8859-2, CP850, ISO-8859-15, UTF-8", true));
119
120 //$string = urlencode(mb_convert_encoding ($string, "UTF-8", 'Windows-1252'));
121
122 $string = $this->encodeFLEX($string);
123 return $string;
124 }
125
126
127 public function import() {
128
129 $percent=0;
130
131
132 //$properties=$this->_properties;
133
134 $file=$this->get_property('path_file','nofile');
135 $errorFile= $file.'.err'; // tmp file for error
136 if (!file_exists($file)) return 'end';
137
138 $inf = new SplFileInfo($file);
139 $ext = ($inf->getExtension());
140 $max = ini_get('max_execution_time');
141 $this->set_property('max_time',$max);
142 $this->set_property('file_ext',$ext);
143
144 $max_quick=dbx()->get_config('dbxAdmin','max_quick');
145
146
147
148 $dd = $this->get_property('dd');
149
150 $bytes = $this->get_property('run_bytes',9600); // line max bytes
151 $maxRuntime = $this->get_property('run_time',10); // less then your max script execution limit
152 $maxLines = $this->get_property('run_lines',$max_quick); // Read in Loop max Lines
153 $owner = $this->get_property('owner',null); // owner im Datensatz setzen
154 $pass = $this->get_property('pass',0); // passwort
155 $utf8 = $this->get_property('utf8',1); // umwandeln
156 $all = $this->get_property('records_all',0); // bei 0 werden alle Datensätze, be 1 nur MaxLines Zeilen pro Loop
157 $sep = $this->get_property('seperator',';');
158 $where = $this->get_property('dd_where','');
159
160 //$max = $this->get_property('max',100);
161
162
163 $deadline = time()+$maxRuntime;
164 $filesize = filesize($file);
165 $filePos = $this->get_property('filepos',0);
166 $queryCount = $this->get_property('querys' ,0);
167 $lineCount = $this->get_property('lines' ,0);
168 $errors = $this->get_property('errors' ,0);
169 $remap = $this->get_property('remap' ,0);
170
171 $empty=array();
172 $this->set_property('records',$empty);
173
174
175 if ($dd) {
176 $oDB = dbx()->get_system_obj('dbxDB');
177 } else {
178 $maxLines=1; // return every line
179 }
180 dbx()->debug("#IMPORT File=($file) Pos=($filePos) von ($filesize) Lines=($lineCount) Max=($maxLines) where =($where)");
181
182 if (!$filePos) $this->set_property('status','init');
183
184 ($fp = fopen($file, 'r')) OR die('failed to open file:'.$file);
185 if($filePos) fseek($fp, $filePos);
186
187 while( $deadline >= time() AND ( $line=fgets($fp, $bytes ) ) ) {
188 $line=str_replace (array("\r\n","\n","\r"),'',$line);
189 if ($utf8) $line=$this->encodeToUtf8($line);
190
191 dbx()->debug2($line."\n");
192
193 if(trim($line)=='') { continue; }
194
195 if (!$lineCount) {
196 $this->make_fildset($line,$remap);
197 } else {
198 if (!$all) $maxLines--;
199 $fieldset=$this->get_fildset();
200 $record=array();
201 $data =explode($sep, $line);
202
203 //dbx_debug("fieldset=",$fieldset,$data);
204
205
206 foreach ($fieldset as $no => $name) {
207 $record[$name]=$data[$no];
208 }
209
210 // $records=$this->get_property('records');
211 // $records[]=$record;
212 // if (!$dd) {
213 // $this->set_property('record' ,$record);
214 // $this->set_property('records',$records);
215 // }
216
217 if ($dd) {
218 if ($owner != null) {
219 if (!isset($record['owner'])) {
220 $record['owner']=$owner;
221 if (isset($record['userid'])) $record['owner']=$record['userid'];
222 }
223 }
224 if ($pass) {
225 if (isset($record['pass'])) $record['pass']=password_hash((string)$record['pass'], PASSWORD_DEFAULT);
226 }
227
228 if ($where) {
229 $rowWhere = $this->make_where($where,$record,$oDB,$dd);
230 $ok=$oDB->save($dd,$record,$rowWhere,0,1,0,0); // save($dd,$field_values,$where,$verify_access=1,$verify_fields=1,$verify_values=1,$trace=1)
231 } else {
232 $ok=$oDB->insert($dd,$record,0,1,0,0); //insert($dd,$field_values,$verify_access=1,$verify_fields=1,$verify_values=1,$trace=1) {
233 }
234
235 if (!$ok) $errors++;
236 if ( $ok) $queryCount++;
237
238 dbx()->debug("IMPORT CSV ($dd) OK=($ok) Where=($rowWhere ?? $where) Querys=($queryCount) errors=($errors) Record=",$record);
239
240
241 }
242 }
243 $lineCount++;
244 $this->set_property('lines' ,$lineCount);
245 if (!$maxLines) break; // Zeilenweise
246 } // read line
247
248 if( feof($fp) ){
249 $done=$filesize;
250 $this->set_property('status','end');
251 $this->set_property('percent',100);
252 $this->set_property('errors',$errors);
253 }else{
254 $done =ftell($fp);
255 $percent =(round($done/$filesize, 2)*100);
256 $this->set_property('status','run');
257 $this->set_property('percent',$percent);
258 }
259
260 $this->set_property('filesize',$filesize);
261 $this->set_property('querys' ,$queryCount);
262 $this->set_property('lines' ,$lineCount);
263 $this->set_property('filepos' ,$done);
264 $this->set_property('errors' ,$errors);
265
266 $status=$this->get_property('status','init');
267
268 //dbx_debug("#dbImport# Status=($status) QeryCount=($queryCount) Line=($lineCount) Filesize=($filesize) Done ($done) Prozent=($percent) ");
269
270
271 //if ($status=='end') $this->clear();
272 return $status;
273 } // import
274 // - - - - - - - - - -
275
276
277
278
279
280 public function run() {
281 //$this->init();
282 $status=$this->import();
283 return $status;
284 }
285}
286?>
Gemeinsame Basisklasse fuer dbXapp-System-, Modul- und Include-Objekte.
Definition dbxKernel.php:63
get_property($name, $default='', $section='', $modul='modul')
Liest eine Objekt- oder Session-Property.
set_property($name, $value, $section='', $modul='modul')
Speichert eine Objekt- oder Session-Property.
Public $_section
Definition dbxKernel.php:66
del_property($name, $section='', $modul='modul')
Loescht eine Objekt- oder Session-Property.
DBX schema administration.