From: <nel...@us...> - 2007-03-27 12:33:48
|
Author: nelius_weiss Date: Tue Mar 27 14:24:51 2007 New Revision: 23538 URL: http://www.egroupware.org/viewvc?rev=3D3D23538&view=3D3Drev Log: fix not working csv_import Modified: branches/1.4/importexport/inc/class.import_csv.inc.php branches/1.4/importexport/inc/class.import_export_helper_functions.inc.= =3D php Modified: branches/1.4/importexport/inc/class.import_csv.inc.php URL: http://www.egroupware.org/viewvc/branches/1.4/importexport/inc/class.i= =3D mport_csv.inc.php?rev=3D3D23538&r1=3D3D23537&r2=3D3D23538&view=3D3Ddiff =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D= =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D= =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D= =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D --- branches/1.4/importexport/inc/class.import_csv.inc.php (original) +++ branches/1.4/importexport/inc/class.import_csv.inc.php Tue Mar 27 14:24= =3D :51 2007 @@ -19,11 +19,6 @@ * This a an abstract implementation of interface iface_import_record * An record is e.g. a single address or or single event. * No mater where the records come from, at the end the get_record method = =3D comes out - * @todo whipe out the bool returns and use exeptions instead!!! - * @todo do we realy need the stepping? otherwise we also could deal with = =3D stream resources. - * The advantage of resources is, that we don't need to struggle whth stre= =3D am options! - * @todo just checked out iterators, but not shure if we can use it. - * they iterate over public functions and not over datastructures :-( */ class import_csv implements iface_import_record { //, Iterator { =3D @@ -38,6 +33,18 @@ */ =3D /*** Attributes: ***/ + =3D + /** + * array with field mapping in form column number =3D3D> new_field_name + * @access public + */ + public $mapping =3D3D array(); + + /** + * array with conversions to be done in form: new_field_name =3D3D> conve= rs=3D ion_string + * @access public + */ + public $conversion =3D3D array(); =3D /** * array holding the current record @@ -59,31 +66,11 @@ protected $num_of_records =3D3D 0; =3D /** - * array with field mapping in form column number =3D3D> new_field_name - * @access protected - */ - protected $mapping =3D3D array(); - - /** - * array with conversions to be done in form: new_field_name =3D3D> conve= rs=3D ion_string - * @access protected - */ - protected $conversion =3D3D array(); - - /** * csv resource * @access private */ private $resource; =3D - /** - * holds the string of the resource - * needed e.g. to reopen resource - * @var string - * @access private - */ - private $csv_resourcename =3D3D ''; - =3D /** * fieldseperator for csv file * @access private @@ -99,19 +86,15 @@ private $csv_charset; =3D /** - * Opens resource, returns false if something fails - * * @param string _resource resource containing data. May be each valid ph= =3D p-stream * @param array _options options for the resource array with keys: charse= =3D t and fieldsep - * @return bool * @access public */ public function __construct( $_resource, $_options =3D3D array() ) { - $this->csv_resourcename =3D3D $_resource; + $this->resource =3D3D $_resource; $this->csv_fieldsep =3D3D $_options['fieldsep']; $this->csv_charset =3D3D $_options['charset']; - =3D - //return $this->open_resource(); + return; } // end of member function __construct =3D /** @@ -121,20 +104,28 @@ * @access public */ public function __destruct( ) { - //$this->close_resource(); } // end of member function __destruct =3D /** * Returns array with the record found at position and updates the positi= =3D on * * @param mixed _position may be: {current|first|last|next|previous|somen= =3D umber} - * @return array + * @return mixed array with data / false if no furtor records * @access public */ public function get_record( $_position =3D3D 'next' ) { - $this->get_raw_record( $_position ); - $this->do_fieldmapping(); - $this->do_conversions(); + if ($this->get_raw_record( $_position ) =3D3D=3D3D=3D3D false) { + return false; + } + =3D + if ( !empty( $this->mapping ) ) { + $this->do_fieldmapping(); + } + =3D + if ( !empty( $this->conversion ) ) { + $this->do_conversions(); + } + =3D return $this->record; } // end of member function get_record =3D @@ -149,13 +140,13 @@ switch ($_position) { case 'current' : if ($this->current_position =3D3D=3D3D 0) { - return false; + return; } break; case 'first' : if (!$this->current_position =3D3D=3D3D 0) { - $this->close_resource(); - $this->open_resource(); + $this->current_position =3D3D 0; + rewind($this->resource); } =3D case 'next' : @@ -164,17 +155,16 @@ return false; } $this->current_position++; - $this->record =3D3D $csv_data; - //$this->record =3D3D $GLOBALS['egw']->translation->convert($csv_data,= $=3D this->csv_charset); + $this->record =3D3D $GLOBALS['egw']->translation->convert($csv_data, $= th=3D is->csv_charset); break; =3D case 'previous' : if ($this->current_position < 2) { - return false; + throw new Exception('Error: There is no previous record!'); } $final_position =3D3D --$this->current_position; - $this->close_resource(); - $this->open_resource(); + $this->current_position =3D3D 0; + rewind($this->resource); while ($this->current_position !=3D3D=3D3D $final_position) { $this->get_raw_record(); } @@ -185,20 +175,22 @@ break; =3D default: //somenumber - if (!is_int($_position)) return false; + if (!is_int($_position)) { + throw new Exception('Error: $position must be one of {current|first|l= =3D ast|next|previous} or an integer value'); + } if ($_position =3D3D=3D3D $this->current_position) { break; } elseif ($_position < $this->current_position) { - $this->close_resource(); - $this->open_resource(); + $this->current_position =3D3D 0; + rewind($this->resource); } while ($this->current_position !=3D3D=3D3D $_position) { $this->get_raw_record(); } break; =3D } - return true; + return; } // end of member function get_raw_record =3D /** @@ -238,12 +230,12 @@ * @access protected */ protected function do_fieldmapping( ) { + $record =3D3D $this->record; + $this->record =3D3D array(); foreach ($this->mapping as $cvs_idx =3D3D> $new_idx) { - $record =3D3D $this->record; - $this->record =3D3D array(); $this->record[$new_idx] =3D3D $record[$cvs_idx]; - return true; - } + } + return; } // end of member function do_fieldmapping =3D /** @@ -255,45 +247,10 @@ protected function do_conversions( ) { if ( $record =3D3D import_export_helper_functions::conversion( $this->re= co=3D rd, $this->conversion )) { $this->record =3D3D $record; - return true; - } - else return false; + return; + } + throw new Exception('Error: Could not applay conversions to record'); } // end of member function do_conversions - - - /** - * opens the csv resource (php-stream) - * - * @param string _resource resource containing data. May be each valid ph= =3D p-stream - * @param array _options options for the resource array with keys: charse= =3D t and fieldsep - * @return =3D - * @access private - */ - private function open_resource() { - if ( !is_readable ( $this->csv_resourcename )) { - error_log('error: file '. $this->csv_resourcename .' is not readable by= =3D webserver '.__FILE__.__LINE__); - return false; - } - =3D - $this->resource =3D3D fopen ($this->csv_resourcename, 'rb'); - =3D - if (!is_resource($this->resource)) { - // some error handling - return false; - } - $this->current_position =3D3D 0; - return true; - } // end of member function open_resource - - /** - * closes the csv resource (php-stream) - * - * @return bool - * @access private - */ - private function close_resource() { - return fclose( $this->resource ); - } // end of member function close_resource - + =3D } // end of import_csv ?> Modified: branches/1.4/importexport/inc/class.import_export_helper_function= =3D s=2Einc.php URL: http://www.egroupware.org/viewvc/branches/1.4/importexport/inc/class.i= =3D mport_export_helper_functions.inc.php?rev=3D3D23538&r1=3D3D23537&r2=3D3D235= 38&vie=3D w=3D3Ddiff =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D= =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D= =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D= =3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D3D=3D =3D3D=3D3D=3D3D --- branches/1.4/importexport/inc/class.import_export_helper_functions.inc.= =3D php (original) +++ branches/1.4/importexport/inc/class.import_export_helper_functions.inc.= =3D php Tue Mar 27 14:24:51 2007 @@ -153,6 +153,7 @@ * @todo replace this function with a function dealing with reg expressio= =3D ns! */ public static function conversion( $_record, $_conversion ) { + if (empty( $_conversion ) ) return $_record; =3D $PSep =3D3D '||'; // Pattern-Separator, separats the pattern-replacement= -p=3D airs in conversion $ASep =3D3D '|>'; // Assignment-Separator, separats pattern and replaces= me=3D nt @@ -183,6 +184,7 @@ //unset($_conversion[$record_idx] ); } =3D + $val =3D3D $record_value; if(!empty($_conversion[$record_idx])) { //$conv_record =3D3D $_conversion[$record_idx]; |