Patch aus Bug 1343: csv in ../users speichern
[kivitendo-erp.git] / lxo-import / contactB.php
1 <html>
2 <LINK REL="stylesheet" HREF="../css/lx-office-erp.css" TYPE="text/css" TITLE="Lx-Office stylesheet">
3 <body>
4 <?
5 /*
6 Kontaktimport mit Browser nach Lx-Office ERP
7
8 Copyright (C) 2005
9 Author: Holger Lindemann
10 Email: hli@lx-system.de
11 Web: http://lx-system.de
12
13 */
14 if (!$_SESSION["db"]) {
15         $conffile="../config/authentication.pl";
16         if (!is_file($conffile)) {
17                 ende(4);
18         }
19 }
20 require ("import_lib.php");
21
22 if (!anmelden()) ende(5);
23
24 /* get DB instance */
25 $db=$_SESSION["db"]; //new myDB($login);
26
27
28 $crm=checkCRM();
29
30 if ($_POST["ok"]) {
31         $test=$_POST["test"];
32
33         if ($crm) {
34                 $kunde_fld = array_keys($contactscrm);
35                 $contact=$contactscrm;
36         } else {
37                 $kunde_fld = array_keys($contacts);
38                 $contact=$contacts;
39         }
40         $nun=time();
41
42         function ende($nr) {
43                 echo "Abbruch: $nr\n";
44                 exit($nr);
45         }
46         if ($_POST["ok"]=="Hilfe") {
47                 echo "Importfelder:<br>";
48                 echo "Feldname => Bedeutung<br>";
49                 foreach($contact as $key=>$val) {
50                         echo "$key => $val<br>";
51                 }
52                 exit(0);
53         };
54
55         clearstatcache ();
56
57         $trenner=($_POST["trenner"])?$_POST["trenner"]:",";
58
59 if (!empty($_FILES["Datei"]["name"])) { 
60         $file=$_POST["ziel"];
61         if (!move_uploaded_file($_FILES["Datei"]["tmp_name"],$file."_contact.csv")) {
62                 $file=false;
63                 echo "Upload von ".$_FILES["Datei"]["name"]." fehlerhaft. (".$_FILES["Datei"]["error"].")<br>";
64         } 
65 } else if (is_file($_POST["ziel"]."_contact.csv")) {
66         $file=$_POST["ziel"];
67 } else {
68         $file=false;
69
70
71 if (!$file) ende (2);
72
73 if (!file_exists($file."_contact.csv")) ende(5);
74
75 $prenumber=$_POST["prenumber"];
76
77 $employee=chkUsr($_SESSION["employee"]);
78 if (!$employee) ende(4);
79
80 if (!$db->chkcol($file)) ende(6);
81
82 $f=fopen($file."_contact.csv","r");
83 $zeile=fgetcsv($f,2000,$trenner);
84
85 $first=true;
86
87 foreach ($zeile as $fld) {
88         $fld = strtolower(trim(strtr($fld,array("\""=>"","'"=>""))));
89         $in_fld[]=$fld;
90 }
91 $j=0;
92 $zeile=fgetcsv($f,2000,$trenner);
93 while (!feof($f)){
94         $i=-1;
95         $firma="";
96         $name=false;
97         $id=false;
98         $sql="insert into contacts ";
99         $keys="(";
100         $vals=" values (";
101         foreach($zeile as $data) {
102                 $i++;
103                 if (!in_array($in_fld[$i],$kunde_fld)) {
104                         continue;
105                 }
106                 $data=addslashes(trim($data));
107                 if ($in_fld[$i]=="firma" && $data) { 
108                         $data=suchFirma($file,$data);
109                         if ($data) {
110                                 $id=$data["cp_cv_id"];
111                         }
112                         continue;
113                 } else if ($in_fld[$i]=="firma") {
114                         continue;
115                 } ;
116                 if ($in_fld[$i]=="cp_cv_id" && $data) {
117                         $data=chkKdId($data);
118                         if ($data) {
119                                 $id = $data;
120                         };
121                         continue;
122                 } else  if($in_fld[$i]=="cp_cv_id") {
123                         continue;
124                 }
125                 if ($in_fld[$i]==$file."number" && $data) {
126                         if (!$id) {
127                                 $tmp=getFirma($data,$file);
128                                 if ($tmp) {
129                                         $id=$tmp;
130                                 }
131                         }
132                         continue;
133                 } else if  ($in_fld[$i]==$file."number") {
134                         continue;
135                 }
136                 if ($in_fld[$i]=="cp_id" && $data) {
137                         $tmp=chkContact($data);
138                         if ($tmp) {
139                                 $keys.="cp_id,";
140                                 $vals.="$tmp,";
141                         } 
142                         continue;
143                 } else if ($in_fld[$i]=="cp_id") {
144                         continue;
145                 }
146
147                 $keys.=$in_fld[$i].",";
148                 
149                 if ($data==false or empty($data) or !$data) {
150                         $vals.="null,";
151                 } else {
152                         if (in_array($in_fld[$i],array("cp_fax","cp_phone1","cp_phone2"))) {
153                                 $data=$prenumber.$data;
154                         } else if ($in_fld[$i]=="cp_country" && $data) {
155                                 $data=mkland($data);
156                         }
157                         if ($in_fld[$i]=="cp_name") $name=true;
158                         $vals.="'".$data."',";
159                         // bei jedem gefuellten Datenfeld erhoehen
160                         $val_count++;
161                 }
162         }
163         if (!$name) {
164                 $zeile=fgetcsv($f,1200,$trenner);
165                 continue;
166         }
167         if ($id) {
168                         $vals.=$id.",";
169                         $keys.="cp_cv_id,";
170         }
171         if ($keys<>"(" && $val_count>2) {
172                 if ($test) {
173                         if ($first) {
174                                 echo "<table border='1'>\n";
175                                 echo "<tr><th>".str_replace(",","</th><th>",substr($keys,1,-1))."</th></tr>\n";
176                                 $first=false;
177                         };
178                         $vals=str_replace("',","'</td><td>",substr($vals,9,-1));
179                         echo "<tr><td>".str_replace("null,","null</td><td>",$vals)."</td></tr>\n";
180                         flush();
181                 } else {
182                         $sql.=substr($keys,0,-1).")";
183                         $sql.=substr($vals,0,-1).")";
184                         $rc=$db->query($sql);
185                         if (!$rc) echo "Fehler: ".$sql."\n";
186                 }
187                 $j++;
188         };
189         $zeile=fgetcsv($f,1200,$trenner);
190 }
191 fclose($f);
192 echo $j." $file importiert.\n";} else {
193 ?>
194 <p class="listtop">Kontakt-Adressimport f&uuml;r die ERP</p>
195 <form name="import" method="post" enctype="multipart/form-data" action="contactB.php">
196 <input type="hidden" name="MAX_FILE_SIZE" value="300000">
197 <table>
198 <tr><td></td><td><input type="submit" name="ok" value="Hilfe"></td></tr>
199 <tr><td>Zieltabelle</td><td><input type="radio" name="ziel" value="customer" checked>customer <input type="radio" name="ziel" value="vendor">vendor</td></tr>
200 <tr><td>Trennzeichen</td><td><input type="text" size="2" maxlength="1" name="trenner" value=";"></td></tr>
201 <tr><td>Telefonvorwahl</td><td><input type="text" size="4" maxlength="1" name="prenumber" value=""></td></tr>
202 <tr><td>Test</td><td><input type="checkbox" name="test" value="1">ja</td></tr>
203 <tr><td>Daten</td><td><input type="file" name="Datei"></td></tr>
204 <tr><td></td><td><input type="submit" name="ok" value="Import"></td></tr>
205 </table>
206 </form>
207 <? }; ?>