Fixed problem when importing fewer than 5 fields.
[squirrelmail.git] / src / addressbook_csvimport.php
1 <?php
2 /**
3 ** addressbook_csvimport.php
4 **
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** Import csv files for address book
9 ** This takes a comma delimited file uploaded from addressbook.php
10 ** and allows the user to rearrange the field order to better
11 ** fit the address book. A subset of data is manipulated to save time.
12 **
13 **/
14
15
16 session_start();
17
18 if(!isset($username)) {
19 echo "You need a valid user and password to access this page!";
20 exit;
21 }
22 if (!isset($config_php))
23 include("../config/config.php");
24 if (!isset($i18n_php))
25 include("../functions/i18n.php");
26 if (!isset($page_header_php))
27 include("../functions/page_header.php");
28 if (!isset($addressbook_php))
29 include("../functions/addressbook.php");
30 if (!isset($strings_php))
31 include("../functions/strings.php");
32
33 include("../src/load_prefs.php");
34
35 /** commented out until a way to deal with file rnaming is figured out.
36 displayPageHeader($color, "None");
37
38 if (!isset($smusercsv) || $smusercsv == "none") {
39 echo "<br><br>\n";
40 echo "<table align=\"center\">\n";
41 echo " <tr>\n";
42 echo " <td>" . _("Please select a file for uploading. You can do this by clicking on the browse button on the ") . "<a href=\"addressbook.php\">" . _("Address Book") . "</a> " . _("page.") . "</td>\n";
43 echo " </tr>\n";
44 echo "</table>\n";
45 echo "</body></html>\n";
46 exit;
47 }
48 **/
49
50 /**
51 ** Script-wide vars. Used all over the place or something you might want to change later.
52 ** $example -->Sets how many rows of user data the script will use to play with before final write.
53 ** $abook ---->Setup the addressbook functions for Pallo's Addressbook.
54 ** $tabindex ->A starting value for the loop to assign a tabindex value for the generated text input fields.
55 ** As long as this value is greater than the number of static submit buttons all should work fine.
56 **/
57 $tabindex = 10;
58 $example = 6;
59 $abook = addressbook_init(true, true);
60
61 /** See if the submit button has been clicked, if not, set up arrays with empty elements
62 ** To change the number of text boxes displayed in headers, add or delete ,"" 's
63 **/
64 if(!$submit || $reset){
65 $nickname = array("","","");
66 $firstname = array("","","");
67 $lastname = array("","","");
68 $email = "";
69 $label = array("","","","","","","","","");
70 }
71
72 if($flag <= 1) { // If first run of script, setup the filename to copy
73 $tempfilename = ($username . (time()+2592000));
74 $nameholder = $tempfilename;
75 if(copy($smusercsv, "$attachment_dir$tempfilename")) { // Set up variable to use in printing status
76 $goodcopy = true;
77 } else {
78 $goodcopy = false;
79 }
80 } elseif($flag >= 2) { // If not use the name already set up
81 $tempfilename = $nameholder;
82 }
83
84 // table with directions
85 if(!$finish) {
86 echo "<FORM METHOD=\"post\">\n";
87 echo "<CENTER><TABLE BGCOLOR=\"$color[9]\" WIDTH=\"70%\" FRAME=\"void\" NOWRAP>\n";
88 echo " <TR>\n";
89 echo " <TD ALIGN=\"center\">", _("All the steps required to complete the process are listed below"), "</TD>\n";
90 echo " </TR>\n";
91 echo " <TR>\n";
92 echo " <TD>";
93 echo _("You uploaded a file named: ");
94 echo "<B>$smusercsv_name </B>";
95 echo "</TD>\n";
96 echo " </TR>\n";
97 echo " <TR>\n";
98 echo " <TD>";
99 if(!$goodcopy && $flag == 0) { // print correct status of file copying
100 echo _("Failed to create working copy, Please try again.");
101 } else {
102 echo _("Created working copy, continuing with process...");
103 }
104 echo "</TD></TR>\n";
105 echo " <TR>\n";
106 echo " <TD>", _("Displaying a small set of your data."), "</TD>";
107 echo " </TR>\n";
108 echo " <TR>\n";
109 echo " <TD>", _("Arrange your data to fit the 5 address book fields. "), _("Do this by inserting the data's field number under the field for which you wish it to be included into the address book. "), _("For example: fields 5, 6, and 7 need to go into the info field 5. "), _("The boxes under field 5 would contain 5, 6, and 7 in seperate boxes. "), "</TD>\n";
110 echo " <TR>\n";
111 echo " <TD>", _("Submit Your reorganized data."), "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp", "\n";
112 echo " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Submit\" TABINDEX=\"1\">", "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp\n";
113 echo " <INPUT TYPE=\"hidden\" NAME=\"flag\" VALUE=\"2\">\n";
114 echo " <INPUT TYPE=\"hidden\" NAME=\"nameholder\" VALUE=$nameholder>\n";
115 echo " <INPUT TYPE=\"hidden\" NAME=\"email\" VALUE=$email>\n";
116 echo " ", _("Erase entries and re-enter field numbers."), "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp", "\n";
117 echo " <INPUT TYPE=reset VALUE=reset TABINDEX=\"2\">\n";
118 echo " </TD>\n";
119 echo " </TR>\n";
120 echo " <TR>\n";
121 echo " <TD>", _("View full set of imported records in their new format."), "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp\n";
122 echo " <INPUT TYPE=\"submit\" NAME=\"all\" VALUE=\"Show all Records\" TABINDEX=\"3\">\n";
123 echo " <INPUT TYPE=\"hidden\" NAME=\"submit\" VALUE=\"Submit\">";
124 echo " </TD>";
125 echo " </TR>\n";
126 echo " <TR>\n";
127 echo " <TD>", _("Omit individual records which are not to be included."), "\n";
128 echo " ", _("To the left of each field below the \"Omit\" heading is a checkbox."), "\n";
129 echo " ", _("Click this checkbox to omit individual records."), "\n";
130 echo " </TD>";
131 echo " </TR>\n";
132 echo " <TR>\n";
133 echo " <TD>", _("Final approval. ");
134 echo " ", _("After reviewing the rearranged data for accuracy, click \"Finish\"."), "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp\n";
135 echo " <INPUT TYPE=\"submit\" NAME=\"finish\" VALUE=\"Finish\" TABINDEX=\"4\">\n";
136 echo " </TD>";
137 echo " </TR>\n";
138 echo "</TABLE>\n";
139 } else {
140 echo "<BR><BR><H1><STRONG><CENTER>", _("Upload Completed!"), "</STRONG></H1>", _("Click on the link below to verify your work."), "</CENTER>";
141 echo "<BR><BR><CENTER><A HREF=\"addressbook.php\">" . _("Addresses") . "</A></CENTER>\n";
142 }
143
144 /**
145 ** Use fgetcsv to make an array out of the uploaded file
146 ** Display a sample to see if the data looks good to the user.
147 **/
148
149 // open the correct filename to work with
150 if($flag <= 1) { // before submit
151 $fp = fopen("$attachment_dir$tempfilename", "r");
152 } elseif($flag >= 2) { // after submit
153 $fp = fopen("$attachment_dir$nameholder", "r");
154 }
155
156 echo "<CENTER><TABLE WIDTH=\"95%\" FRAME=\"void\" CELLSPACING=\"1\">\n"; // user's data table
157
158 // This loop sets up a table of the data they uploaded to save time while the user rearranges it.
159 $row = 0;
160 do {
161 ($data = fgetcsv($fp, 4096));
162 if(count($data) >= 5) {
163 $cols = count($data);
164 } else {
165 $cols = 5;
166 }
167 // $cols = count($data);
168 $row++;
169
170 if($flag == 0 && !$finish) { // Table header on initial import
171 echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">";
172 echo "<STRONG>", _("This table shows your data after uploading it."), "</STRONG>";
173 echo "</TH><BR>\n";
174 } elseif($flag == 2 && !$finish) { // Table header after field changes made
175 echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">";
176 echo "<STRONG>", _("This table shows your data after you reorganized it."), "</STRONG>";
177 echo "</TH><BR>\n";
178 }
179
180 switch($flag) { // This switch sets up a method so that proper looping can be done for the table output
181
182 case 0:
183 case 1:
184 case 2: // Should probably be header since thats really what they are, maybe later
185
186 if(!$finish) { // Set up column headers unless we are finished
187 echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n"; // fill in above the omit field
188 echo " <TD>", "&nbsp", "</TD>\n";
189 echo " <TD>", _("Field Number: "), "0", "<BR>\n"; // number the fields so they know what to put where
190 reset($nickname);
191 while(list($k,$v)=each($nickname)) { // print out the text boxes for this var
192 echo" <INPUT TYPE=text NAME=nickname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
193 $tabindex++;
194 }
195 echo " </TD>\n";
196 echo " <TD>", _("Field Number: "), "1", "<BR>\n"; // number the fields so they know what to put where
197 reset($firstname);
198 while(list($k,$v)=each($firstname)) { // print out the text boxes for this var
199 echo" <INPUT TYPE=text NAME=firstname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
200 $tabindex++;
201 }
202 echo " </TD>\n";
203 echo " <TD>", _("Field Number: "), "2", "<BR>\n"; // number the fields so they know what to put where
204 reset($lastname);
205 while(list($k,$v)=each($lastname)) { // print out the text boxes for this var
206 echo" <INPUT TYPE=text NAME=lastname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
207 $tabindex++;
208 }
209 echo " </TD>\n"; // email isn't an array so no loop needed
210 echo " <TD>", _("Field Number: "), "3", "<BR>\n"; // number the fields so they know what to put where
211 echo " <INPUT TYPE=text NAME=email MAXLENGTH=\"2\" SIZE=\"2\" TABINDEX=\"".$tabindex."\" VALUE=$email>\n";
212 $tabindex++;
213 echo " </TD>\n";
214 echo " <TD>", _("Field Number: "), "4", "<BR>\n"; // number the fields so they know what to put where
215 reset($label);
216 while(list($k,$v)=each($label)) { // print out the text boxes for this var
217 echo" <INPUT TYPE=text NAME=label[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
218 $tabindex++;
219 }
220 echo " </TD>\n";
221
222 // print field numbers for importable fields
223 $fcols = $cols;
224 $i = 5;
225 for($cols > 4;$fcols > 5 ; --$fcols) {
226 echo " <TD>", _("Field Number: "), "$i", "</TD>\n";
227 $i++;
228 }
229 // give the imported columns a name so the user knows what they will be put into
230 echo " </TR>\n";
231 echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n";
232 echo " <TD WIDTH=\"1\">", _("Omit"), "</TD>\n";
233 echo " <TD>", _("Nickname:"), "</TD>\n";
234 echo " <TD>", _("First name:"), "</TD>\n";
235 echo " <TD>", _("Last name:"), "</TD>\n";
236 echo " <TD>", _("E-mail address:"), "</TD>\n";
237 echo " <TD>", _("Additional info:"), "</TD>\n";
238
239 // print some instruction in the header above the fields that need to be combined into other fields
240 $fcols = $cols;
241 for($cols > 4;$fcols > 5 ; --$fcols) {
242 echo " <TD>", _("Move to field"), "<BR>", _("1-5 to include."), "</TD>\n";
243 }
244 echo " </TR>";
245 echo "</TH><BR>\n";
246 }
247
248 case 3: // user's table data
249
250 if($row % 2 && !$finish) { // Set up the alternating colored rows
251 echo " <TR BGCOLOR=\"$color[0]\">\n";
252 } elseif(!$finish){
253 echo " <TR>\n";
254 }
255 echo " <TD WIDTH=\"1\"><INPUT TYPE=checkbox NAME=\"sel[]\" VALUE=\"omit\">"; // Print the omit checkbox, to be checked before write
256
257 for($c=0; $c<$cols; $c++) { // Spit out the table cells
258 // concatenate fields based on user input into text boxes.
259 if($submit) {
260 switch($c) { // This switch puts the correct data into the correct fields
261 case 0: // concactenate nickname field
262
263 reset($nickname);
264 $j = 0;
265 while(list($k,$v)=each($nickname)) {
266 if($v != "" && $j == 0) {
267 $reorg = "$data[$v]"; // put data in without coma
268 } elseif($v != "") {
269 $reorg .= "; $data[$v]"; // put data in with coma
270 } else "&nbsp;"; // put in space to keep the row colors going
271 $j++;
272 }
273 $addaddr["nickname"] = $reorg; // assign value for writing
274 break;
275
276 case 1: // concactenate firstname field
277
278 reset($firstname);
279 $j = 0;
280 while(list($k,$v)=each($firstname)) {
281 if($v != "" && $j == 0) {
282 $reorg = "$data[$v]";
283 } elseif($v != "") {
284 $reorg .= "; $data[$v]";
285 } else "&nbsp;";
286 $j++;
287 }
288 $addaddr["firstname"] = $reorg;
289 break;
290
291 case 2: // concactenate lastname field
292
293 reset($lastname);
294 $j = 0;
295 while(list($k,$v)=each($lastname)) {
296 if($v != "" && $j == 0) {
297 $reorg = "$data[$v]";
298 } elseif($v != "") {
299 $reorg .= "; $data[$v]";
300 } else "&nbsp;";
301 $j++;
302 }
303 $addaddr["lastname"] = $reorg;
304 break;
305
306 case 3: // should only have one field in $email
307
308 $reorg = $data[$email];
309 $addaddr["email"] = $reorg;
310 break;
311
312 case 4: // concactenate label field
313
314 reset($label);
315 $j = 0;
316 while(list($k,$v)=each($label)) {
317 if($v != "" && $j == 0) {
318 $reorg = "$data[$v]";
319 } elseif($v != "") {
320 $reorg .= "; $data[$v]";
321 } else "&nbsp;";
322 $j++;
323 }
324 $addaddr["label"] = $reorg;
325 }
326 } else $reorg = $data[$c];
327
328 if($reorg != "" && !$finish) { // if not empty, put data in cell.
329 trim($reorg);
330 echo " <TD NOWRAP>$reorg</TD>\n";
331 } elseif(!$finish) { // if empty, put space in cell keeping colors correct.
332 echo " <TD>&nbsp;</TD>\n";
333 } else
334 $reorg = "";
335 }
336 echo " </TR>\n";
337 $flag = 3;
338 break;
339 }
340 // If finished, do the import. This uses Pallo's excellent class and object stuff
341 if($finish && !$sel[$row]) {
342 $r = $abook->add($addaddr,$abook->localbackend);
343 if(!r) {
344 print $this->error;
345 }
346 unset($addaddr);
347 }
348 // How far should we loop through the users' data.
349 if($row < $example && (!$all && !$finish)){
350 $loop = true;
351 } elseif(!feof($fp) && ($all || $finish)) {
352 $loop = true;
353 } else {
354 $loop = false;
355 }
356
357 } while($loop);
358
359 echo "</TABLE>";
360
361 fclose($fp);
362 // unset each element in the arrays. For some reason, this doesn't work when included in the same loop as the <INPUT.
363 reset($nickname);
364 while(list($k,$v)=each($nickname)){
365 unset($nickname[$k]);
366 }
367 reset($lastname);
368 while(list($k,$v)=each($lastname)){
369 unset($lastname[$k]);
370 }
371 reset($firstname);
372 while(list($k,$v)=each($firstname)){
373 unset($firstname[$k]);
374 }
375 reset($label);
376 while(list($k,$v)=each($label)){
377 unset($label[$k]);
378 }
379
380 // Send the field numbers entered in the text boxes by the user back to this script for more processing
381 // email is handled differently, not being an array
382 if($submit == "Submit"){
383 // loop through each array and send each element
384 reset($nickname);
385 while(list($k,$v)=each($nickname)){
386 echo" <INPUT TYPE=hidden NAME=nickname[$k] VALUE=\"".$v."\">\n";
387 }
388 reset($lastname);
389 while(list($k,$v)=each($lastname)){
390 echo" <INPUT TYPE=hidden NAME=lastname[$k] VALUE=\"".$v."\">\n";
391 }
392 reset($firstname);
393 while(list($k,$v)=each($firstname)){
394 echo" <INPUT TYPE=hidden NAME=firstname[$k] VALUE=\"".$v."\">\n";
395 }
396 reset($label);
397 while(list($k,$v)=each($label)){
398 echo" <INPUT TYPE=hidden NAME=label[$k] VALUE=\"".$v."\">\n";
399 }
400 }
401
402 // Clean up after ourselves.
403 if($finish) {
404 unlink ("$attachment_dir$tempfilename");
405 }
406
407 ?>
408 </FORM>
409 </BODY>
410 </HTML>