| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * Horde_Data implementation for tab-separated data (TSV). 4 * 5 * $Horde: framework/Data/Data/tsv.php,v 1.23.10.10 2006/08/03 02:50:40 selsky Exp $ 6 * 7 * Copyright 1999-2006 Jan Schneider <jan@horde.org> 8 * 9 * See the enclosed file COPYING for license information (LGPL). If you 10 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 11 * 12 * @author Jan Schneider <jan@horde.org> 13 * @author Chuck Hagenbuch <chuck@horde.org> 14 * @since Horde 1.3 15 * @package Horde_Data 16 */ 17 class Horde_Data_tsv extends Horde_Data { 18 19 var $_extension = 'tsv'; 20 var $_contentType = 'text/tab-separated-values'; 21 22 function importData($contents, $header = false, $delimiter = "\t") 23 { 24 $contents = explode("\n", $contents); 25 $data = array(); 26 if ($header) { 27 $head = explode($delimiter, array_shift($contents)); 28 } 29 foreach ($contents as $line) { 30 if (trim($line) == '') { 31 continue; 32 } 33 $line = explode($delimiter, $line); 34 if (!isset($head)) { 35 $data[] = $line; 36 } else { 37 $newline = array(); 38 for ($i = 0; $i < count($head); $i++) { 39 $newline[$head[$i]] = empty($line[$i]) ? '' : $line[$i]; 40 } 41 $data[] = $newline; 42 } 43 } 44 return $data; 45 } 46 47 /** 48 * Builds a TSV file from a given data structure and returns it as a 49 * string. 50 * 51 * @param array $data A two-dimensional array containing the data set. 52 * @param boolean $header If true, the rows of $data are associative 53 * arrays with field names as their keys. 54 * 55 * @return string The TSV data. 56 */ 57 function exportData($data, $header = false) 58 { 59 if (!is_array($data) || count($data) == 0) { 60 return ''; 61 } 62 $export = ''; 63 $head = array_keys(current($data)); 64 if ($header) { 65 $export = implode("\t", $head) . "\n"; 66 } 67 foreach ($data as $row) { 68 foreach ($head as $key) { 69 $cell = $row[$key]; 70 if (!empty($cell) || $cell === 0) { 71 $export .= $cell; 72 } 73 $export .= "\t"; 74 } 75 $export = substr($export, 0, -1) . "\n"; 76 } 77 return $export; 78 } 79 80 /** 81 * Builds a TSV file from a given data structure and triggers its download. 82 * It DOES NOT exit the current script but only outputs the correct headers 83 * and data. 84 * 85 * @param string $filename The name of the file to be downloaded. 86 * @param array $data A two-dimensional array containing the data 87 * set. 88 * @param boolean $header If true, the rows of $data are associative 89 * arrays with field names as their keys. 90 */ 91 function exportFile($filename, $data, $header = false) 92 { 93 $export = $this->exportData($data, $header); 94 $GLOBALS['browser']->downloadHeaders($filename, 'text/tab-separated-values', false, strlen($export)); 95 echo $export; 96 } 97 98 /** 99 * Takes all necessary actions for the given import step, parameters and 100 * form values and returns the next necessary step. 101 * 102 * @param integer $action The current step. One of the IMPORT_* constants. 103 * @param array $param An associative array containing needed 104 * parameters for the current step. 105 * 106 * @return mixed Either the next step as an integer constant or imported 107 * data set after the final step. 108 */ 109 function nextStep($action, $param = array()) 110 { 111 switch ($action) { 112 case IMPORT_FILE: 113 $next_step = parent::nextStep($action, $param); 114 if (is_a($next_step, 'PEAR_Error')) { 115 return $next_step; 116 } 117 118 if ($_SESSION['import_data']['format'] == 'mulberry' || 119 $_SESSION['import_data']['format'] == 'pine') { 120 $_SESSION['import_data']['data'] = $this->importFile($_FILES['import_file']['tmp_name']); 121 $format = $_SESSION['import_data']['format']; 122 if ($format == 'mulberry') { 123 $appKeys = array('alias', 'name', 'email', 'company', 'workAddress', 'workPhone', 'homePhone', 'fax', 'notes'); 124 $dataKeys = array(0, 1, 2, 3, 4, 5, 6, 7, 9); 125 } elseif ($format == 'pine') { 126 $appKeys = array('alias', 'name', 'email', 'notes'); 127 $dataKeys = array(0, 1, 2, 4); 128 } 129 foreach ($appKeys as $key => $app) { 130 $map[$dataKeys[$key]] = $app; 131 } 132 $data = array(); 133 foreach ($_SESSION['import_data']['data'] as $row) { 134 $hash = array(); 135 if ($format == 'mulberry') { 136 if (preg_match("/^Grp:/", $row[0])) { 137 continue; 138 } 139 $row[1] = preg_replace('/^([^,"]+),\s*(.*)$/', '$2 $1', $row[1]); 140 foreach ($dataKeys as $key) { 141 if (array_key_exists($key, $row)) { 142 $hash[$key] = stripslashes(preg_replace('/\\\\r/', "\n", $row[$key])); 143 } 144 } 145 } elseif ($format == 'pine') { 146 if (count($row) < 3 || preg_match("/^#DELETED/", $row[0]) || preg_match("/[()]/", $row[2])) { 147 continue; 148 } 149 $row[1] = preg_replace('/^([^,"]+),\s*(.*)$/', '$2 $1', $row[1]); 150 /* Address can be a full RFC822 address */ 151 require_once 'Mail/RFC822.php'; 152 $addr_arr = Mail_RFC822::parseAddressList($row[2]); 153 if (empty($addr_arr[0]->mailbox)) { 154 continue; 155 } 156 $row[2] = $addr_arr[0]->mailbox . '@' . $addr_arr[0]->host; 157 if (empty($row[1]) && !empty($addr_arr[0]->personal)) { 158 $row[1] = $addr_arr[0]->personal; 159 } 160 foreach ($dataKeys as $key) { 161 if (array_key_exists($key, $row)) { 162 $hash[$key] = $row[$key]; 163 } 164 } 165 } 166 $data[] = $hash; 167 } 168 $_SESSION['import_data']['data'] = $data; 169 $_SESSION['import_data']['map'] = $map; 170 $ret = $this->nextStep(IMPORT_DATA, $param); 171 return $ret; 172 } 173 174 /* Move uploaded file so that we can read it again in the next step 175 after the user gave some format details. */ 176 $uploaded = Browser::wasFileUploaded('import_file', _("TSV file")); 177 if (is_a($uploaded, 'PEAR_Error')) { 178 return PEAR::raiseError($uploaded->getMessage()); 179 } 180 $file_name = Horde::getTempFile('import', false); 181 if (!move_uploaded_file($_FILES['import_file']['tmp_name'], $file_name)) { 182 return PEAR::raiseError(_("The uploaded file could not be saved.")); 183 } 184 $_SESSION['import_data']['file_name'] = $file_name; 185 186 /* Read the file's first two lines to show them to the user. */ 187 $_SESSION['import_data']['first_lines'] = ''; 188 $fp = @fopen($file_name, 'r'); 189 if ($fp) { 190 $line_no = 1; 191 while ($line_no < 3 && $line = fgets($fp)) { 192 $newline = String::length($line) > 100 ? "\n" : ''; 193 $_SESSION['import_data']['first_lines'] .= substr($line, 0, 100) . $newline; 194 $line_no++; 195 } 196 } 197 return IMPORT_TSV; 198 break; 199 200 case IMPORT_TSV: 201 $_SESSION['import_data']['header'] = Util::getFormData('header'); 202 $import_data = $this->importFile($_SESSION['import_data']['file_name'], 203 $_SESSION['import_data']['header']); 204 $_SESSION['import_data']['data'] = $import_data; 205 unset($_SESSION['import_data']['map']); 206 return IMPORT_MAPPED; 207 break; 208 209 default: 210 return parent::nextStep($action, $param); 211 break; 212 } 213 } 214 215 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |