| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 require_once 'PEAR.php'; 4 5 // Import constants 6 /** Import already mapped csv data. */ define('IMPORT_MAPPED', 1); 7 /** Map date and time entries of csv data. */ define('IMPORT_DATETIME', 2); 8 /** Import generic CSV data. */ define('IMPORT_CSV', 3); 9 /** Import MS Outlook data. */ define('IMPORT_OUTLOOK', 4); 10 /** Import vCalendar/iCalendar data. */ define('IMPORT_ICALENDAR', 5); 11 /** Import vCards. */ define('IMPORT_VCARD', 6); 12 /** Import generic tsv data. */ define('IMPORT_TSV', 7); 13 /** Import Mulberry address book data */ define('IMPORT_MULBERRY', 8); 14 /** Import Pine address book data. */ define('IMPORT_PINE', 9); 15 /** Import file. */ define('IMPORT_FILE', 11); 16 /** Import data. */ define('IMPORT_DATA', 12); 17 18 // Export constants 19 /** Export generic CSV data. */ define('EXPORT_CSV', 100); 20 /** Export iCalendar data. */ define('EXPORT_ICALENDAR', 101); 21 /** Export vCards. */ define('EXPORT_VCARD', 102); 22 /** Export TSV data. */ define('EXPORT_TSV', 103); 23 /** Export Outlook CSV data. */ define('EXPORT_OUTLOOKCSV', 104); 24 25 /** 26 * Abstract class to handle different kinds of Data formats and to 27 * help data exchange between Horde applications and external sources. 28 * 29 * $Horde: framework/Data/Data.php,v 1.80.10.12 2006/08/08 17:15:22 jan Exp $ 30 * 31 * Copyright 1999-2006 Jan Schneider <jan@horde.org> 32 * 33 * See the enclosed file COPYING for license information (LGPL). If you 34 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 35 * 36 * @author Jan Schneider <jan@horde.org> 37 * @author Chuck Hagenbuch <chuck@horde.org> 38 * @since Horde 1.3 39 * @package Horde_Data 40 */ 41 class Horde_Data extends PEAR { 42 43 var $_extension; 44 var $_contentType = 'text/plain'; 45 46 /** 47 * A list of warnings raised during the last operation. 48 * 49 * @since Horde 3.1 50 */ 51 var $_warnings = array(); 52 53 /** 54 * Stub to import passed data. 55 */ 56 function importData() 57 { 58 } 59 60 /** 61 * Stub to return exported data. 62 */ 63 function exportData() 64 { 65 } 66 67 /** 68 * Stub to import a file. 69 */ 70 function importFile($filename, $header = false) 71 { 72 $data = file_get_contents($filename); 73 return $this->importData($data, $header); 74 } 75 76 /** 77 * Stub to export data to a file. 78 */ 79 function exportFile() 80 { 81 } 82 83 /** 84 * Tries to determine the expected newline character based on the 85 * platform information passed by the browser's agent header. 86 * 87 * @return string The guessed expected newline characters, either \n, \r 88 * or \r\n. 89 */ 90 function getNewline() 91 { 92 require_once 'Horde/Browser.php'; 93 $browser = &Browser::singleton(); 94 95 switch ($browser->getPlatform()) { 96 case 'win': 97 return "\r\n"; 98 99 case 'mac': 100 return "\r"; 101 102 case 'unix': 103 default: 104 return "\n"; 105 } 106 } 107 108 function getFilename($basename) 109 { 110 return $basename . '.' . $this->_extension; 111 } 112 113 function getContentType() 114 { 115 return $this->_contentType; 116 } 117 118 /** 119 * Returns a list of warnings that have been raised during the last 120 * operation. 121 * 122 * @since Horde 3.1 123 * 124 * @return array A (possibly empty) list of warnings. 125 */ 126 function warnings() 127 { 128 return $this->_warnings; 129 } 130 131 /** 132 * Attempts to return a concrete Horde_Data instance based on $format. 133 * 134 * @param mixed $format The type of concrete Horde_Data subclass to 135 * return. If $format is an array, then we will look 136 * in $format[0]/lib/Data/ for the subclass 137 * implementation named $format[1].php. 138 * 139 * @return Horde_Data The newly created concrete Horde_Data instance, or 140 * false on an error. 141 */ 142 function &factory($format) 143 { 144 if (is_array($format)) { 145 $app = $format[0]; 146 $format = $format[1]; 147 } 148 149 $format = basename($format); 150 151 if (empty($format) || (strcmp($format, 'none') == 0)) { 152 $data =& new Horde_Data(); 153 return $data; 154 } 155 156 if (!empty($app)) { 157 require_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/Data/' . $format . '.php'; 158 } else { 159 require_once 'Horde/Data/' . $format . '.php'; 160 } 161 $class = 'Horde_Data_' . $format; 162 if (class_exists($class)) { 163 $data =& new $class(); 164 } else { 165 $data = PEAR::raiseError('Class definition of ' . $class . ' not found.'); 166 } 167 168 return $data; 169 } 170 171 /** 172 * Attempts to return a reference to a concrete Horde_Data instance 173 * based on $format. It will only create a new instance if no Horde_Data 174 * instance with the same parameters currently exists. 175 * 176 * This should be used if multiple data sources (and, thus, multiple 177 * Horde_Data instances) are required. 178 * 179 * This method must be invoked as: $var = &Horde_Data::singleton() 180 * 181 * @param string $format The type of concrete Horde_Data subclass to 182 * return. 183 * 184 * @return Horde_Data The concrete Horde_Data reference, or false on an 185 * error. 186 */ 187 function &singleton($format) 188 { 189 static $instances; 190 if (!isset($instances)) { 191 $instances = array(); 192 } 193 194 $signature = serialize($format); 195 if (!isset($instances[$signature])) { 196 $instances[$signature] = &Horde_Data::factory($format); 197 } 198 199 return $instances[$signature]; 200 } 201 202 /** 203 * Maps a date/time string to an associative array. 204 * 205 * The method signature has changed in Horde 3.1.3. 206 * 207 * @access private 208 * 209 * @param string $date The date. 210 * @param string $type One of 'date', 'time' or 'datetime'. 211 * @param array $params Two-dimensional array with additional information 212 * about the formatting. Possible keys are:<pre> 213 * delimiter -- The character that seperates the 214 * different date/time parts. 215 * format -- If 'ampm' and $date contains a time we 216 * assume that it is in AM/PM format. 217 * order -- If $type is 'datetime' the order of the 218 * day and time parts: -1 (timestamp), 0 219 * (day/time), 1 (time/day). 220 * @param integer $key The key to use for $params. 221 * 222 * @return string The date or time in ISO format. 223 */ 224 function mapDate($date, $type, $params, $key) 225 { 226 switch ($type) { 227 case 'date': 228 case 'monthday': 229 case 'monthdayyear': 230 $dates = explode($params['delimiter'][$key], $date); 231 if (count($dates) != 3) { 232 return $date; 233 } 234 $index = array_flip(explode('/', $params['format'][$key])); 235 return $dates[$index['year']] . '-' . $dates[$index['month']] . '-' . $dates[$index['mday']]; 236 237 case 'time': 238 $dates = explode($params['delimiter'][$key], $date); 239 if (count($dates) < 2 || count($dates) > 3) { 240 return $date; 241 } 242 if ($params['format'][$key] == 'ampm') { 243 if (strpos(strtolower($dates[count($dates)-1]), 'pm') !== false) { 244 if ($dates[0] !== '12') { 245 $dates[0] += 12; 246 } 247 } elseif ($dates[0] == '12') { 248 $dates[0] = '0'; 249 } 250 $dates[count($dates) - 1] = sprintf('%02d', $dates[count($dates)-1]); 251 } 252 return $dates[0] . ':' . $dates[1] . (count($dates) == 3 ? (':' . $dates[2]) : ':00'); 253 254 case 'datetime': 255 switch ($params['order'][$key]) { 256 case -1: 257 return (string)(int)$date == $date 258 ? date('Y-m-d H:i:s', $date) 259 : $date; 260 case 0: 261 list($day, $time) = explode(' ', $date, 2); 262 break; 263 case 1: 264 list($time, $day) = explode(' ', $date, 2); 265 break; 266 } 267 $date = $this->mapDate($day, 'date', 268 array('delimiter' => $params['day_delimiter'], 269 'format' => $params['day_format']), 270 $key); 271 $time = $this->mapDate($time, 'time', 272 array('delimiter' => $params['time_delimiter'], 273 'format' => $params['time_format']), 274 $key); 275 return $date . ' ' . $time; 276 277 } 278 } 279 280 /** 281 * Takes all necessary actions for the given import step, parameters and 282 * form values and returns the next necessary step. 283 * 284 * @param integer $action The current step. One of the IMPORT_* constants. 285 * @param array $param An associative array containing needed 286 * parameters for the current step. 287 * 288 * @return mixed Either the next step as an integer constant or imported 289 * data set after the final step. 290 */ 291 function nextStep($action, $param = array()) 292 { 293 /* First step. */ 294 if (is_null($action)) { 295 $_SESSION['import_data'] = array(); 296 return IMPORT_FILE; 297 } 298 299 switch ($action) { 300 case IMPORT_FILE: 301 /* Sanitize uploaded file. */ 302 $import_format = Util::getFormData('import_format'); 303 $check_upload = Browser::wasFileUploaded('import_file', $param['file_types'][$import_format]); 304 if (is_a($check_upload, 'PEAR_Error')) { 305 return $check_upload; 306 } 307 if ($_FILES['import_file']['size'] <= 0) { 308 return PEAR::raiseError(_("The file contained no data.")); 309 } 310 $_SESSION['import_data']['format'] = $import_format; 311 break; 312 313 case IMPORT_MAPPED: 314 $dataKeys = Util::getFormData('dataKeys', ''); 315 $appKeys = Util::getFormData('appKeys', ''); 316 if (empty($dataKeys) || empty($appKeys)) { 317 global $registry; 318 return PEAR::raiseError(sprintf(_("You didn't map any fields from the imported file to the corresponding fields in %s."), 319 $registry->get('name'))); 320 } 321 $dataKeys = explode("\t", $dataKeys); 322 $appKeys = explode("\t", $appKeys); 323 $map = array(); 324 $dates = array(); 325 foreach ($appKeys as $key => $app) { 326 $map[$dataKeys[$key]] = $app; 327 if (isset($param['time_fields']) && 328 isset($param['time_fields'][$app])) { 329 $dates[$dataKeys[$key]]['type'] = $param['time_fields'][$app]; 330 $dates[$dataKeys[$key]]['values'] = array(); 331 $i = 0; 332 /* Build an example array of up to 10 date/time fields. */ 333 while ($i < count($_SESSION['import_data']['data']) && count($dates[$dataKeys[$key]]['values']) < 10) { 334 if (!empty($_SESSION['import_data']['data'][$i][$dataKeys[$key]])) { 335 $dates[$dataKeys[$key]]['values'][] = $_SESSION['import_data']['data'][$i][$dataKeys[$key]]; 336 } 337 $i++; 338 } 339 } 340 } 341 $_SESSION['import_data']['map'] = $map; 342 if (count($dates) > 0) { 343 $_SESSION['import_data']['dates'] = $dates; 344 return IMPORT_DATETIME; 345 } 346 return $this->nextStep(IMPORT_DATA, $param); 347 348 case IMPORT_DATETIME: 349 case IMPORT_DATA: 350 if ($action == IMPORT_DATETIME) { 351 $params = array('delimiter' => Util::getFormData('delimiter'), 352 'format' => Util::getFormData('format'), 353 'order' => Util::getFormData('order'), 354 'day_delimiter' => Util::getFormData('day_delimiter'), 355 'day_format' => Util::getFormData('day_format'), 356 'time_delimiter' => Util::getFormData('time_delimiter'), 357 'time_format' => Util::getFormData('time_format')); 358 } 359 if (!isset($_SESSION['import_data']['data'])) { 360 return PEAR::raiseError(_("The uploaded data was lost since the previous step.")); 361 } 362 /* Build the result data set as an associative array. */ 363 $data = array(); 364 foreach ($_SESSION['import_data']['data'] as $row) { 365 $data_row = array(); 366 foreach ($row as $key => $val) { 367 if (isset($_SESSION['import_data']['map'][$key])) { 368 $mapped_key = $_SESSION['import_data']['map'][$key]; 369 if ($action == IMPORT_DATETIME && 370 !empty($val) && 371 isset($param['time_fields']) && 372 isset($param['time_fields'][$mapped_key])) { 373 $val = $this->mapDate($val, $param['time_fields'][$mapped_key], $params, $key); 374 } 375 $data_row[$_SESSION['import_data']['map'][$key]] = $val; 376 } 377 } 378 $data[] = $data_row; 379 } 380 return $data; 381 } 382 } 383 384 /** 385 * Cleans the session data up and removes any uploaded and moved 386 * files. If a function called "_cleanup()" exists, this gets 387 * called too. 388 * 389 * @return mixed If _cleanup() was called, the return value of this call. 390 * This should be the value of the first import step. 391 */ 392 function cleanup() 393 { 394 if (isset($_SESSION['import_data']['file_name'])) { 395 @unlink($_SESSION['import_data']['file_name']); 396 } 397 $_SESSION['import_data'] = array(); 398 if (function_exists('_cleanup')) { 399 return _cleanup(); 400 } 401 } 402 403 }
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 |