3 ** addressbook_csvimport.php
5 ** Copyright (c) 1999-2000 The SquirrelMail development team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
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.
18 if(!isset($username)) {
19 echo "You need a valid user and password to access this page!";
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");
33 include("../src/load_prefs.php");
35 /** commented out until a way to deal with file rnaming is figured out.
36 displayPageHeader($color, "None");
38 if (!isset($smusercsv) || $smusercsv == "none") {
40 echo "<table align=\"center\">\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";
45 echo "</body></html>\n";
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.
59 $abook = addressbook_init(true, true);
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
64 if(!$submit ||
$reset){
65 $nickname = array("","","");
66 $firstname = array("","","");
67 $lastname = array("","","");
69 $label = array("","","","","","","","","");
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
80 } elseif($flag >= 2) { // If not use the name already set up
81 $tempfilename = $nameholder;
84 // table with directions
86 echo "<FORM METHOD=\"post\">\n";
87 echo "<CENTER><TABLE BGCOLOR=\"$color[9]\" WIDTH=\"70%\" FRAME=\"void\" NOWRAP>\n";
89 echo " <TD ALIGN=\"center\">", _("All the steps required to complete the process are listed below"), "</TD>\n";
93 echo _("You uploaded a file named: ");
94 echo "<B>$smusercsv_name </B>";
99 if(!$goodcopy && $flag == 0) { // print correct status of file copying
100 echo _("Failed to create working copy, Please try again.");
102 echo _("Created working copy, continuing with process...");
106 echo " <TD>", _("Displaying a small set of your data."), "</TD>";
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";
111 echo " <TD>", _("Submit Your reorganized data."), "      ", "\n";
112 echo " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Submit\" TABINDEX=\"1\">", "      \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."), "      ", "\n";
117 echo " <INPUT TYPE=reset VALUE=reset TABINDEX=\"2\">\n";
121 echo " <TD>", _("View full set of imported records in their new format."), "      \n";
122 echo " <INPUT TYPE=\"submit\" NAME=\"all\" VALUE=\"Show all Records\" TABINDEX=\"3\">\n";
123 echo " <INPUT TYPE=\"hidden\" NAME=\"submit\" VALUE=\"Submit\">";
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";
133 echo " <TD>", _("Final approval. ");
134 echo " ", _("After reviewing the rearranged data for accuracy, click \"Finish\"."), "      \n";
135 echo " <INPUT TYPE=\"submit\" NAME=\"finish\" VALUE=\"Finish\" TABINDEX=\"4\">\n";
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";
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.
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");
156 echo "<CENTER><TABLE WIDTH=\"95%\" FRAME=\"void\" CELLSPACING=\"1\">\n"; // user's data table
158 // This loop sets up a table of the data they uploaded to save time while the user rearranges it.
161 ($data = fgetcsv($fp, 4096));
162 if(count($data) >= 5) {
163 $cols = count($data);
167 // $cols = count($data);
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>";
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>";
180 switch($flag) { // This switch sets up a method so that proper looping can be done for the table output
184 case 2: // Should probably be header since thats really what they are, maybe later
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>", " ", "</TD>\n";
189 echo " <TD>", _("Field Number: "), "0", "<BR>\n"; // number the fields so they know what to put where
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";
196 echo " <TD>", _("Field Number: "), "1", "<BR>\n"; // number the fields so they know what to put where
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";
203 echo " <TD>", _("Field Number: "), "2", "<BR>\n"; // number the fields so they know what to put where
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";
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";
214 echo " <TD>", _("Field Number: "), "4", "<BR>\n"; // number the fields so they know what to put where
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";
222 // print field numbers for importable fields
225 for($cols > 4;$fcols > 5 ; --$fcols) {
226 echo " <TD>", _("Field Number: "), "$i", "</TD>\n";
229 // give the imported columns a name so the user knows what they will be put into
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";
239 // print some instruction in the header above the fields that need to be combined into other fields
241 for($cols > 4;$fcols > 5 ; --$fcols) {
242 echo " <TD>", _("Move to field"), "<BR>", _("1-5 to include."), "</TD>\n";
248 case 3: // user's table data
250 if($row %
2 && !$finish) { // Set up the alternating colored rows
251 echo " <TR BGCOLOR=\"$color[0]\">\n";
255 echo " <TD WIDTH=\"1\"><INPUT TYPE=checkbox NAME=\"sel[]\" VALUE=\"omit\">"; // Print the omit checkbox, to be checked before write
257 for($c=0; $c<$cols; $c++
) { // Spit out the table cells
258 // concatenate fields based on user input into text boxes.
260 switch($c) { // This switch puts the correct data into the correct fields
261 case 0: // concactenate nickname field
265 while(list($k,$v)=each($nickname)) {
266 if($v != "" && $j == 0) {
267 $reorg = "$data[$v]"; // put data in without coma
269 $reorg .= "; $data[$v]"; // put data in with coma
270 } else " "; // put in space to keep the row colors going
273 $addaddr["nickname"] = $reorg; // assign value for writing
276 case 1: // concactenate firstname field
280 while(list($k,$v)=each($firstname)) {
281 if($v != "" && $j == 0) {
282 $reorg = "$data[$v]";
284 $reorg .= "; $data[$v]";
288 $addaddr["firstname"] = $reorg;
291 case 2: // concactenate lastname field
295 while(list($k,$v)=each($lastname)) {
296 if($v != "" && $j == 0) {
297 $reorg = "$data[$v]";
299 $reorg .= "; $data[$v]";
303 $addaddr["lastname"] = $reorg;
306 case 3: // should only have one field in $email
308 $reorg = $data[$email];
309 $addaddr["email"] = $reorg;
312 case 4: // concactenate label field
316 while(list($k,$v)=each($label)) {
317 if($v != "" && $j == 0) {
318 $reorg = "$data[$v]";
320 $reorg .= "; $data[$v]";
324 $addaddr["label"] = $reorg;
326 } else $reorg = $data[$c];
328 if($reorg != "" && !$finish) { // if not empty, put data in cell.
330 echo " <TD NOWRAP>$reorg</TD>\n";
331 } elseif(!$finish) { // if empty, put space in cell keeping colors correct.
332 echo " <TD> </TD>\n";
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
);
348 // How far should we loop through the users' data.
349 if($row < $example && (!$all && !$finish)){
351 } elseif(!feof($fp) && ($all ||
$finish)) {
362 // unset each element in the arrays. For some reason, this doesn't work when included in the same loop as the <INPUT.
364 while(list($k,$v)=each($nickname)){
365 unset($nickname[$k]);
368 while(list($k,$v)=each($lastname)){
369 unset($lastname[$k]);
372 while(list($k,$v)=each($firstname)){
373 unset($firstname[$k]);
376 while(list($k,$v)=each($label)){
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
385 while(list($k,$v)=each($nickname)){
386 echo" <INPUT TYPE=hidden NAME=nickname[$k] VALUE=\"".$v."\">\n";
389 while(list($k,$v)=each($lastname)){
390 echo" <INPUT TYPE=hidden NAME=lastname[$k] VALUE=\"".$v."\">\n";
393 while(list($k,$v)=each($firstname)){
394 echo" <INPUT TYPE=hidden NAME=firstname[$k] VALUE=\"".$v."\">\n";
397 while(list($k,$v)=each($label)){
398 echo" <INPUT TYPE=hidden NAME=label[$k] VALUE=\"".$v."\">\n";
402 // Clean up after ourselves.
404 unlink ("$attachment_dir$tempfilename");