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 $cols = count($data);
165 if($flag == 0 && !$finish) { // Table header on initial import
166 echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">";
167 echo "<STRONG>", _("This table shows your data after uploading it."), "</STRONG>";
169 } elseif($flag == 2 && !$finish) { // Table header after field changes made
170 echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">";
171 echo "<STRONG>", _("This table shows your data after you reorganized it."), "</STRONG>";
175 switch($flag) { // This switch sets up a method so that proper looping can be done for the table output
179 case 2: // Should probably be header since thats really what they are, maybe later
181 if(!$finish) { // Set up column headers unless we are finished
182 echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n"; // fill in above the omit field
183 echo " <TD>", " ", "</TD>\n";
184 echo " <TD>", _("Field Number: "), "0", "<BR>\n"; // number the fields so they know what to put where
186 while(list($k,$v)=each($nickname)) { // print out the text boxes for this var
187 echo" <INPUT TYPE=text NAME=nickname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
191 echo " <TD>", _("Field Number: "), "1", "<BR>\n"; // number the fields so they know what to put where
193 while(list($k,$v)=each($firstname)) { // print out the text boxes for this var
194 echo" <INPUT TYPE=text NAME=firstname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
198 echo " <TD>", _("Field Number: "), "2", "<BR>\n"; // number the fields so they know what to put where
200 while(list($k,$v)=each($lastname)) { // print out the text boxes for this var
201 echo" <INPUT TYPE=text NAME=lastname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
204 echo " </TD>\n"; // email isn't an array so no loop needed
205 echo " <TD>", _("Field Number: "), "3", "<BR>\n"; // number the fields so they know what to put where
206 echo " <INPUT TYPE=text NAME=email MAXLENGTH=\"2\" SIZE=\"2\" TABINDEX=\"".$tabindex."\" VALUE=$email>\n";
209 echo " <TD>", _("Field Number: "), "4", "<BR>\n"; // number the fields so they know what to put where
211 while(list($k,$v)=each($label)) { // print out the text boxes for this var
212 echo" <INPUT TYPE=text NAME=label[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n";
217 // print field numbers for importable fields
220 for($cols > 4;$fcols > 5 ; --$fcols) {
221 echo " <TD>", _("Field Number: "), "$i", "</TD>\n";
224 // give the imported columns a name so the user knows what they will be put into
226 echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n";
227 echo " <TD WIDTH=\"1\">", _("Omit"), "</TD>\n";
228 echo " <TD>", _("Nickname:"), "</TD>\n";
229 echo " <TD>", _("First name:"), "</TD>\n";
230 echo " <TD>", _("Last name:"), "</TD>\n";
231 echo " <TD>", _("E-mail address:"), "</TD>\n";
232 echo " <TD>", _("Additional info:"), "</TD>\n";
234 // print some instruction in the header above the fields that need to be combined into other fields
236 for($cols > 4;$fcols > 5 ; --$fcols) {
237 echo " <TD>", _("Move to field"), "<BR>", _("1-5 to include."), "</TD>\n";
243 case 3: // user's table data
245 if($row %
2 && !$finish) { // Set up the alternating colored rows
246 echo " <TR BGCOLOR=\"$color[0]\">\n";
250 echo " <TD WIDTH=\"1\"><INPUT TYPE=checkbox NAME=\"sel[]\" VALUE=\"omit\">"; // Print the omit checkbox, to be checked before write
252 for($c=0; $c<$cols; $c++
) { // Spit out the table cells
253 // concatenate fields based on user input into text boxes.
255 switch($c) { // This switch puts the correct data into the correct fields
256 case 0: // concactenate nickname field
260 while(list($k,$v)=each($nickname)) {
261 if($v != "" && $j == 0) {
262 $reorg = "$data[$v]"; // put data in without coma
264 $reorg .= "; $data[$v]"; // put data in with coma
265 } else " "; // put in space to keep the row colors going
268 $addaddr["nickname"] = $reorg; // assign value for writing
271 case 1: // concactenate firstname field
275 while(list($k,$v)=each($firstname)) {
276 if($v != "" && $j == 0) {
277 $reorg = "$data[$v]";
279 $reorg .= "; $data[$v]";
283 $addaddr["firstname"] = $reorg;
286 case 2: // concactenate lastname field
290 while(list($k,$v)=each($lastname)) {
291 if($v != "" && $j == 0) {
292 $reorg = "$data[$v]";
294 $reorg .= "; $data[$v]";
298 $addaddr["lastname"] = $reorg;
301 case 3: // should only have one field in $email
303 $reorg = $data[$email];
304 $addaddr["email"] = $reorg;
307 case 4: // concactenate label field
311 while(list($k,$v)=each($label)) {
312 if($v != "" && $j == 0) {
313 $reorg = "$data[$v]";
315 $reorg .= "; $data[$v]";
319 $addaddr["label"] = $reorg;
321 } else $reorg = $data[$c];
323 if($reorg != "" && !$finish) { // if not empty, put data in cell.
325 echo " <TD NOWRAP>$reorg</TD>\n";
326 } elseif(!$finish) { // if empty, put space in cell keeping colors correct.
327 echo " <TD> </TD>\n";
335 // If finished, do the import. This uses Pallo's excellent class and object stuff
336 if($finish && !$sel[$row]) {
337 $r = $abook->add($addaddr,$abook->localbackend
);
343 // How far should we loop through the users' data.
344 if($row < $example && (!$all && !$finish)){
346 } elseif(!feof($fp) && ($all ||
$finish)) {
357 // unset each element in the arrays. For some reason, this doesn't work when included in the same loop as the <INPUT.
359 while(list($k,$v)=each($nickname)){
360 unset($nickname[$k]);
363 while(list($k,$v)=each($lastname)){
364 unset($lastname[$k]);
367 while(list($k,$v)=each($firstname)){
368 unset($firstname[$k]);
371 while(list($k,$v)=each($label)){
375 // Send the field numbers entered in the text boxes by the user back to this script for more processing
376 // email is handled differently, not being an array
377 if($submit == "Submit"){
378 // loop through each array and send each element
380 while(list($k,$v)=each($nickname)){
381 echo" <INPUT TYPE=hidden NAME=nickname[$k] VALUE=\"".$v."\">\n";
384 while(list($k,$v)=each($lastname)){
385 echo" <INPUT TYPE=hidden NAME=lastname[$k] VALUE=\"".$v."\">\n";
388 while(list($k,$v)=each($firstname)){
389 echo" <INPUT TYPE=hidden NAME=firstname[$k] VALUE=\"".$v."\">\n";
392 while(list($k,$v)=each($label)){
393 echo" <INPUT TYPE=hidden NAME=label[$k] VALUE=\"".$v."\">\n";
397 // Clean up after ourselves.
399 unlink ("$attachment_dir$tempfilename");