3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 /****************************************************************
20 ** copyright 2000 Lewis Bergman - lbergman@abi.tconline.net
22 ** Import csv files for address book
23 ** This takes a comma delimited file uploaded from addressbook.php
24 ** and allows the user to rearrange the field order to better
25 ** fit the address book. A subset of data is manipulated to save time.
27 ****************************************************************/
32 if(!isset($username)) {
33 echo "You need a valid user and password to access this page!";
36 if (!isset($config_php))
37 include("../config/config.php");
38 if (!isset($i18n_php))
39 include("../functions/i18n.php");
40 if (!isset($page_header_php))
41 include("../functions/page_header.php");
42 if (!isset($addressbook_php))
43 include("../functions/addressbook.php");
44 if (!isset($strings_php))
45 include("../functions/strings.php");
47 include("../src/load_prefs.php");
49 displayPageHeader($color, "None");
51 if (!isset($smusercsv) ||
$smusercsv == "none") {
53 echo "<table align=\"center\">\n";
55 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";
58 echo "</body></html>\n";
63 ** Script-wide vars. Used all over the place or something you might want to change later.
64 ** $example -->Sets how many rows of user data the script will use to play with before final write.
65 ** $abook ---->Setup the addressbook functions for Pallo's Addressbook.
68 $abook = addressbook_init(true, true);
70 /** See if the submit button has been clicked, if not, set up arrays with empty elements
71 ** To change the number of text boxes displayed in headers, add or delete ,"" 's
73 if(!$submit ||
$reset){
74 $nickname = array("","","");
75 $firstname = array("","","");
76 $lastname = array("","","");
78 $label = array("","","","","","","","","");
81 if($flag <= 1) { // If first run of script, setup the filename to copy
82 $tempfilename = ($username . (time()+
2592000));
83 $nameholder = $tempfilename;
84 if(copy($smusercsv, "$attachment_dir$tempfilename")) { // Set up variable to use in printing status
89 } elseif($flag >= 2) { // If not use the name already set up
90 $tempfilename = $nameholder;
93 // table with directions
95 echo "<FORM METHOD=\"post\">\n";
96 echo "<CENTER><TABLE BGCOLOR=\"$color[9]\" WIDTH=\"70%\" FRAME=\"void\" NOWRAP>\n";
98 echo " <TD ALIGN=\"center\">", _("All the steps required to complete the process are listed below"), "</TD>\n";
102 echo _("You uploaded a file named: ");
103 echo "<B>$smusercsv_name </B>";
108 if(!$goodcopy && $flag == 0) { // print correct status of file copying
109 echo _("Failed to create working copy, Please try again.");
111 echo _("Created working copy, continuing with process...");
115 echo " <TD>", _("Displaying a small set of your data."), "</TD>";
118 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";
120 echo " <TD>", _("Submit Your reorganized data."), "      ", "\n";
121 echo " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Submit\">", "      \n";
122 echo " <INPUT TYPE=\"hidden\" NAME=\"flag\" VALUE=\"2\">\n";
123 echo " <INPUT TYPE=\"hidden\" NAME=\"nameholder\" VALUE=$nameholder>\n";
124 echo " <INPUT TYPE=\"hidden\" NAME=\"email\" VALUE=$email>\n";
125 echo " ", _("Erase entries and re-enter field numbers."), "      ", "\n";
126 echo " <INPUT TYPE=reset VALUE=reset>\n";
130 echo " <TD>", _("View full set of imported records in their new format."), "      \n";
131 echo " <INPUT TYPE=\"submit\" NAME=\"all\" VALUE=\"Show all Records\">\n";
132 echo " <INPUT TYPE=\"hidden\" NAME=\"submit\" VALUE=\"Submit\">";
136 echo " <TD>", _("Omit individual records which are not to be included."), "\n";
137 echo " ", _("To the left of each field below the \"Omit\" heading is a checkbox."), "\n";
138 echo " ", _("Click this checkbox to omit individual records."), "\n";
142 echo " <TD>", _("Final approval. ");
143 echo " ", _("After reviewing the rearranged data for accuracy, click \"Finish\"."), "      \n";
144 echo " <INPUT TYPE=\"submit\" NAME=\"finish\" VALUE=\"Finish\">\n";
149 echo "<BR><BR><H1><STRONG><CENTER>", _("Upload Completed!"), "</STRONG></H1>", _("Click on the link below to verify your work."), "</CENTER>";
150 echo "<BR><BR><CENTER><A HREF=\"addressbook.php\">" . _("Addresses") . "</A></CENTER>\n";
154 ** Use fgetcsv to make an array out of the uploaded file
155 ** Display a sample to see if the data looks good to the user.
158 // open the correct filename to work with
159 if($flag <= 1) { // before submit
160 $fp = fopen("$attachment_dir$tempfilename", "r");
161 } elseif($flag >= 2) { // after submit
162 $fp = fopen("$attachment_dir$nameholder", "r");
165 echo "<CENTER><TABLE WIDTH=\"95%\" FRAME=\"void\" CELLSPACING=\"1\">\n"; // user's data table
167 // This loop sets up a table of the data they uploaded to save time while the user rearranges it.
170 ($data = fgetcsv($fp, 4096));
171 $cols = count($data);
174 if($flag == 0 && !$finish) { // Table header on initial import
175 echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">";
176 echo "<STRONG>", _("This table shows your data after uploading it."), "</STRONG>";
178 } elseif($flag == 2 && !$finish) { // Table header after field changes made
179 echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">";
180 echo "<STRONG>", _("This table shows your data after you reorganized it."), "</STRONG>";
184 switch($flag) { // This switch sets up a method so that proper looping can be done for the table output
188 case 2: // Should probably be header since thats really what they are, maybe later
190 if(!$finish) { // Set up column headers unless we are finished
191 echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n"; // fill in above the omit field
192 echo " <TD>", " ", "</TD>\n";
193 echo " <TD>", _("Field Number: "), "0", "<BR>\n"; // number the fields so they know what to put where
195 while(list($k,$v)=each($nickname)) { // print out the text boxes for this var
196 echo" <INPUT TYPE=text NAME=nickname[$k] MAXLENGTH=2 SIZE=2 VALUE=\"".$v."\"><BR>\n";
199 echo " <TD>", _("Field Number: "), "1", "<BR>\n"; // number the fields so they know what to put where
201 while(list($k,$v)=each($firstname)) { // print out the text boxes for this var
202 echo" <INPUT TYPE=text NAME=firstname[$k] MAXLENGTH=2 SIZE=2 VALUE=\"".$v."\"><BR>\n";
205 echo " <TD>", _("Field Number: "), "2", "<BR>\n"; // number the fields so they know what to put where
207 while(list($k,$v)=each($lastname)) { // print out the text boxes for this var
208 echo" <INPUT TYPE=text NAME=lastname[$k] MAXLENGTH=2 SIZE=2 VALUE=\"".$v."\"><BR>\n";
210 echo " </TD>\n"; // email isn't an array so no loop needed
211 echo " <TD>", _("Field Number: "), "3", "<BR>\n"; // number the fields so they know what to put where
212 echo " <INPUT TYPE=text NAME=email MAXLENGTH=\"2\" SIZE=\"2\" 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 VALUE=\"".$v."\"><BR>\n";
221 // print field numbers for importable fields
224 for($cols > 4;$fcols > 5 ; --$fcols) {
225 echo " <TD>", _("Field Number: "), "$i", "</TD>\n";
228 // give the imported columns a name so the user knows what they will be put into
230 echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n";
231 echo " <TD WIDTH=\"1\">", _("Omit"), "</TD>\n";
232 echo " <TD>", _("Nickname:"), "</TD>\n";
233 echo " <TD>", _("First name:"), "</TD>\n";
234 echo " <TD>", _("Last name:"), "</TD>\n";
235 echo " <TD>", _("E-mail address:"), "</TD>\n";
236 echo " <TD>", _("Additional info:"), "</TD>\n";
238 // print some instruction in the header above the fields that need to be combined into other fields
240 for($cols > 4;$fcols > 5 ; --$fcols) {
241 echo " <TD>", _("Move to field"), "<BR>", _("1-5 to include."), "</TD>\n";
247 case 3: // user's table data
249 if($row %
2 && !$finish) { // Set up the alternating colored rows
250 echo " <TR BGCOLOR=\"$color[0]\">\n";
254 echo " <TD WIDTH=\"1\"><INPUT TYPE=checkbox NAME=\"sel[]\" VALUE=\"omit\">"; // Print the omit checkbox, to be checked before write
256 for($c=0; $c<$cols; $c++
) { // Spit out the table cells
257 // concatenate fields based on user input into text boxes.
259 switch($c) { // This switch puts the correct data into the correct fields
260 case 0: // concactenate nickname field
264 while(list($k,$v)=each($nickname)) {
265 if($v != "" && $j == 0) {
266 $reorg = "$data[$v]"; // put data in without coma
268 $reorg .= "; $data[$v]"; // put data in with coma
269 } else " "; // put in space to keep the row colors going
272 $addaddr["nickname"] = $reorg; // assign value for writing
275 case 1: // concactenate firstname field
279 while(list($k,$v)=each($firstname)) {
280 if($v != "" && $j == 0) {
281 $reorg = "$data[$v]";
283 $reorg .= "; $data[$v]";
287 $addaddr["firstname"] = $reorg;
290 case 2: // concactenate lastname field
294 while(list($k,$v)=each($lastname)) {
295 if($v != "" && $j == 0) {
296 $reorg = "$data[$v]";
298 $reorg .= "; $data[$v]";
302 $addaddr["lastname"] = $reorg;
305 case 3: // should only have one field in $email
307 $reorg = $data[$email];
308 $addaddr["email"] = $reorg;
311 case 4: // concactenate label field
315 while(list($k,$v)=each($label)) {
316 if($v != "" && $j == 0) {
317 $reorg = "$data[$v]";
319 $reorg .= "; $data[$v]";
323 $addaddr["label"] = $reorg;
325 } else $reorg = $data[$c];
327 if($reorg != "" && !$finish) { // if not empty, put data in cell.
329 echo " <TD NOWRAP>$reorg</TD>\n";
330 } elseif(!$finish) { // if empty, put space in cell keeping colors correct.
331 echo " <TD> </TD>\n";
339 // If finished, do the import. This uses Pallo's excellent class and object stuff
340 if($finish && !$sel[$row]) {
341 $r = $abook->add($addaddr,$abook->localbackend
);
347 // How far should we loop through the users' data.
348 if($row < $example && (!$all && !$finish)){
350 } elseif(!feof($fp) && ($all ||
$finish)) {
361 // unset each element in the arrays. For some reason, this doesn't work when included in the same loop as the <INPUT.
363 while(list($k,$v)=each($nickname)){
364 unset($nickname[$k]);
367 while(list($k,$v)=each($lastname)){
368 unset($lastname[$k]);
371 while(list($k,$v)=each($firstname)){
372 unset($firstname[$k]);
375 while(list($k,$v)=each($label)){
379 // Send the field numbers entered in the text boxes by the user back to this script for more processing
380 // email is handled differently, not being an array
381 if($submit == "Submit"){
382 // loop through each array and send each element
384 while(list($k,$v)=each($nickname)){
385 echo" <INPUT TYPE=hidden NAME=nickname[$k] VALUE=\"".$v."\">\n";
388 while(list($k,$v)=each($lastname)){
389 echo" <INPUT TYPE=hidden NAME=lastname[$k] VALUE=\"".$v."\">\n";
392 while(list($k,$v)=each($firstname)){
393 echo" <INPUT TYPE=hidden NAME=firstname[$k] VALUE=\"".$v."\">\n";
396 while(list($k,$v)=each($label)){
397 echo" <INPUT TYPE=hidden NAME=label[$k] VALUE=\"".$v."\">\n";
401 // Clean up after ourselves.
403 unlink ("$attachment_dir$tempfilename");