fields; } $success = TRUE; // determine if we need to open the file or not. if(is_string($filename)) { // open the file, if possible. $fp = fopen($filename, 'w'); if($fp === FALSE) { log_message('error', 'CSV Extension: Unable to open file ' . $filename); return FALSE; } } else { // assume file pointer. $fp = $filename; } if($include_header) { // Print out header line $success = fputcsv($fp, $fields); } if($success) { foreach($object as $o) { // convert each object into an array $result = array(); foreach($fields as $f) { $result[] = $o->{$f}; } // output CSV-formatted line $success = fputcsv($fp, $result); if(!$success) { // stop on first failure. break; } } } if(is_string($filename)) { fclose($fp); } return $success; } /** * Import objects from a CSV file. * * Completely empty rows are automatically skipped, as are rows that * start with a # sign (assumed to be comments). * * @param DataMapper $object The type of DataMapper Object to import * @param mixed $filename Name of CSV file, or a file pointer. * @param array $fields If empty, the database fields are used. Otherwise used to limit what fields are saved. * @param boolean $header_row If true, the first line is assumed to be a header row. Defaults to true. * @param mixed $callback A callback method for each row. Can return FALSE on failure to save, or 'stop' to stop the import. * @return array Array of imported objects, or FALSE if unable to import. */ function csv_import($object, $filename, $fields = '', $header_row = TRUE, $callback = NULL) { $class = get_class($object); if(empty($fields)) { $fields = $object->fields; } // determine if we need to open the file or not. if(is_string($filename)) { // open the file, if possible. $fp = fopen($filename, 'r'); if($fp === FALSE) { log_message('error', 'CSV Extension: Unable to open file ' . $filename); return FALSE; } } else { // assume file pointer. $fp = $filename; } if(empty($callback)) { $result = array(); } else { $result = 0; } $columns = NULL; while(($data = fgetcsv($fp)) !== FALSE) { // get column names if(is_null($columns)) { if($header_row) { // store header row for column names $columns = $data; // only include columns in $fields foreach($columns as $index => $name) { if( ! in_array($name, $fields)) { // mark column as false to skip $columns[$index] = FALSE; } } continue; } else { $columns = $fields; } } // skip on comments and empty rows if(empty($data) || $data[0][0] == '#' || implode('', $data) == '') { continue; } // create the object to save $o = new $class(); foreach($columns as $index => $key) { if(count($data) <= $index) { // more header columns than data columns break; } // skip columns that were determined to not be needed above. if($key === FALSE) { continue; } // finally, it's OK to save the data column. $o->{$key} = $data[$index]; } if( empty($callback)) { $result[] = $o; } else { $test = call_user_func($callback, $o); if($test === 'stop') { break; } if($test !== FALSE) { $result++; } } } if(is_string($filename)) { fclose($fp); } return $result; } } /* End of file csv.php */ /* Location: ./application/datamapper/csv.php */