From a51a7339be4d7caeb9c9f2768ff10b7da7ba88d9 Mon Sep 17 00:00:00 2001 From: Miles Lott Date: Sun, 18 Mar 2001 06:31:24 +0000 Subject: [PATCH] Working export to CSV or LDIF, I hope --- addressbook/export.php | 28 +++++--- addressbook/export/Export_to_CSV | 87 ++++++++++++++---------- addressbook/export/Export_to_LDIF | 26 ++++++- addressbook/templates/default/export.tpl | 2 +- 4 files changed, 93 insertions(+), 50 deletions(-) diff --git a/addressbook/export.php b/addressbook/export.php index 43a2514261..184b7491c2 100644 --- a/addressbook/export.php +++ b/addressbook/export.php @@ -12,6 +12,18 @@ /* $Id$ */ + if ($download =='on') { + $phpgw_info["flags"] = array( + "noheader" => True, + "nonavbar" => True + ); + } else { + $phpgw_info["flags"] = array( + "noheader" => False, + "nonavbar" => False + ); + } + $phpgw_info["flags"]["currentapp"] = "addressbook"; $phpgw_info["flags"]["enable_contacts_class"] = True; include("../header.inc.php"); @@ -42,7 +54,7 @@ $t->set_var("cancel_url",$phpgw->link("/addressbook/index.php")); $t->set_var("navbar_bg",$phpgw_info["theme"]["navbar_bg"]); $t->set_var("navbar_text",$phpgw_info["theme"]["navbar_text"]); - $t->set_var("export_text",lang("Export from Addressbook to CSV or LDIF - NOT WORKING YET")); + $t->set_var("export_text",lang("Export from Addressbook to CSV or LDIF")); $t->set_var("action_url",$phpgw->link("/addressbook/export.php")); $t->set_var("filename",lang("Export file name")); $t->set_var("conv",$conv); @@ -77,7 +89,6 @@ $buffer = $this->export_start_file($buffer); for ($i=0;$iids);$i++) { - $this->id = $this->ids[$i]; $buffer = $this->export_start_record($buffer); while( list($name,$value) = each($this->currentrecord) ) { $buffer = $this->export_new_attrib($buffer,$name,$value); @@ -85,25 +96,20 @@ $buffer = $this->export_end_record($buffer); } + // Here, buffer becomes a string suitable for printing $buffer = $this->export_end_file($buffer); $tsvfilename = $phpgw_info['server']['temp_dir'].$sep.$tsvfilename; - if ($download == "Submit") { + if ($download == "on") { header("Content-disposition: attachment; filename=\"".$tsvfilename."\""); header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); -// while(list($name,$value) = each($buffer)) { - //echo $name.': '.$value."\n"; -// } + echo $buffer; } else { echo "
\n";
-			$i=0;
-			while(list($name,$value) = each($buffer[$i])) {
-				echo $i.' - '.$name.': '.$value."\n";
-				$i++;
-			}
+			echo $buffer;
 			echo "\n
\n"; echo ''.lang("OK").''; $phpgw->common->phpgw_footer(); diff --git a/addressbook/export/Export_to_CSV b/addressbook/export/Export_to_CSV index dd4bda44a4..8310c484b8 100644 --- a/addressbook/export/Export_to_CSV +++ b/addressbook/export/Export_to_CSV @@ -65,49 +65,64 @@ "note" => "Notes" ); - function export_start_file($buffer,$j="",$k="") { + // This will store the contacts object + var $contacts = ''; + + function export_start_file($buffer) { + $this->id=-1; + $this->contacts = CreateObject('phpgwapi.contacts'); + $tmp = $this->contacts->read(0,''); + for ($i=0;$iids[$i] = $tmp[$i]['id']; + } + // $ids is now an array of all id's for this user, e.g. $ids[0] = 21, etc... + // $buffer is still empty + return $buffer; + } + + // Read each entry + function export_start_record($buffer) { + $this->id++; + $top = $this->contacts->read_single_entry($this->ids[$this->id],$this->qfields); + $this->currentrecord = $top[0]; + return $buffer; + } + + // Read each attribute, populate buffer + // name/value are the fields from the export array above + function export_new_attrib($buffer,$name,$value) { + if ($this->export[$name]) { + $buffer[$this->id][$this->export[$name]] = $value; + //echo '
'.$this->id.' - '.$this->export[$name].': '.$buffer[$this->id][$this->export[$name]]; + } + return $buffer; + } + + // Tack on some extra values + function export_end_record($buffer) { return $buffer; } function export_end_file($buffer) { - global $phpgw,$phpgw_info; + // Build the header for the file (field list) + reset($this->export); + while (list($name,$value)=each($this->export)) { + $entries .= $value . ','; + } + $entries = substr($entries,0,-1); + $entries .= "\r\n"; - $contacts = CreateObject("phpgwapi.contacts"); - echo '
'; - for ($i=1;$i<=count($buffer);$i++) { - while ( list($name,$value) = @each($buffer[$i]) ) { - //echo '
'.$i.': '.$name.' => '.$value; - $entry[$i][$name] = $value; + // Now add all the data + reset($this->ids); + for ($i=0;$iids);$i++) { + reset($this->export); + while (list($name,$value)=each($this->export)) { + $entries .= $buffer[$i][$value] . ','; } - //echo '
'; - $contacts->add($phpgw_info["user"]["account_id"],$entry[$i]); + $entries = substr($entries,0,-1); + $entries .= "\r\n"; } - $num = $i - 1; - return "Successfully exported $num records from your addressbook."; - } - - function export_start_record($buffer) { - $top=array(); - ++$this->id; - $this->currentrecord = $top; - return $buffer; - } - - function export_end_record($buffer,$private="private") { - global $phpgw_info; - $buffer[$this->id]=""; - while ( list($name, $value) = each($this->currentrecord)) { - $buffer[$this->id][$name] = $value; - //echo '
'.$name.' => '.$value; - } - return $buffer; - } - - function export_new_attrib($buffer,$name,$value) { - $value = str_replace("\n","
",$value); - $value = str_replace("\r","",$value); - $this->currentrecord += array($name => $value); - + $buffer = $entries; return $buffer; } } diff --git a/addressbook/export/Export_to_LDIF b/addressbook/export/Export_to_LDIF index 16b0c3cf42..406d43ea17 100644 --- a/addressbook/export/Export_to_LDIF +++ b/addressbook/export/Export_to_LDIF @@ -50,6 +50,7 @@ // Read full list of user's contacts only to get id's for each function export_start_file($buffer) { + $this->id=-1; $this->contacts = CreateObject('phpgwapi.contacts'); $tmp = $this->contacts->read(0,''); for ($i=0;$icontacts->read_single_entry($this->id,$this->qfields); + $this->id++; + $top = $this->contacts->read_single_entry($this->ids[$this->id],$this->qfields); $this->currentrecord = $top[0]; return $buffer; } @@ -83,12 +85,32 @@ $buffer[$this->id]["xmozillauseconferenceserver"] = ""; $buffer[$this->id]["xmozillanickname"] = ""; $buffer[$this->id]["xmozillausehtmlmail"] = ""; - $buffer[$this->id]["objectClass"] = "person"; //echo '
'.$this->id.' - '.$buffer[$this->id]['dn']; return $buffer; } function export_end_file($buffer) { + reset($this->ids); + for ($i=0;$iids);$i++) { + $entries .= $buffer[$i]['dn'] . "\n"; + reset($this->export); + while (list($name,$value)=each($this->export)) { + if ($value != 'dn') { + $entries .= $value . ": " . $buffer[$i][$value] . "\n"; + } + } + $entries .= "xmozillauseconferenceserver: " . $buffer[$i]["xmozillauseconferenceserver"] . "\n"; + $entries .= "xmozillanickname: " . $buffer[$i]["xmozillanickname"] . "\n"; + $entries .= "xmozillausehtmlmail: " . $buffer[$i]["xmozillausehtmlmail"] . "\n"; + $entries .= "objectClass: person\n"; + $entries .= "account\n"; + $entries .= "organizationalPerson\n"; + $entries .= "posixAccount\n"; + $entries .= "objectClass: inetOrgPerson\n"; + $entries .= "objectClass: shadowAccount\n"; + $entries .= "\n"; + } + $buffer = $entries; return $buffer; } } diff --git a/addressbook/templates/default/export.tpl b/addressbook/templates/default/export.tpl index 3316ef183b..4f914a8322 100644 --- a/addressbook/templates/default/export.tpl +++ b/addressbook/templates/default/export.tpl @@ -19,7 +19,7 @@

  • {filename}:
  • -
  • Download export file (Uncheck to debug output in browser)
  • +
  • Download export file (Uncheck to debug output in browser)