예제 #1
0
 /**
  * read the CSV file, store results in $this->headings and $this->data
  */
 function readCSV($userfile_name)
 {
     $config =& JFactory::getConfig();
     $tmp_dir = $config->getValue('config.tmp_path');
     $baseDir = JPath::clean($tmp_dir);
     $this->headings = array();
     $this->data = array();
     $tabDelimiter = JRequest::getVar('tabdelimited');
     $field_delimiter = $tabDelimiter == 1 ? "\t" : JRequest::getVar('field_delimiter', ',');
     $text_delimiter = stripslashes(JRequest::getVar('text_delimiter', '"'));
     $csv = new csv_bv($baseDir . '/' . $userfile_name, $field_delimiter, $text_delimiter, '\\');
     $csv->inPutFormat = JRequest::getVar('inPutFormat', 'csv');
     $csv->SkipEmptyRows(TRUE);
     // Will skip empty rows. TRUE by default. (Shown here for example only).
     $csv->TrimFields(TRUE);
     // Remove leading and trailing \s and \t. TRUE by default.
     $model =& $this->getTableModel();
     $tableParams =& $model->getParams();
     $mode = $tableParams->get('csvfullname');
     while ($arr_data = $csv->NextLine()) {
         if (empty($this->headings)) {
             foreach ($arr_data as &$heading) {
                 // remove UFT8 Byte-Order-Mark if present
                 if (substr($heading, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
                     $heading = substr($heading, 3);
                 }
                 if ($mode != 2) {
                     // $$$ rob don't bother cleaning at all as dots (.) are replaced with "_"
                     //$heading = FabrikString::clean($heading);
                     // $$$ rob replacing with this as per thread - http://fabrikar.com/forums/showthread.php?p=83304
                     $heading = str_replace(' ', '_', $heading);
                 }
             }
             $this->headings = $arr_data;
         } else {
             if (function_exists('iconv')) {
                 foreach ($arr_data as &$d) {
                     //strip any none uft-8 characters from the import data
                     //if we don't do this then the site's session is destroyed and you are logged out
                     $d = iconv("utf-8", "utf-8//IGNORE", $d);
                 }
             }
             if (count($arr_data) == 1 && $arr_data[0] == '') {
                 //csv import from excel saved as unicode has blank record @ end
             } else {
                 $this->data[] = $arr_data;
             }
         }
     }
     fclose($csv->mHandle);
     $session =& JFactory::getSession();
     $session->set('com_fabrik.csvdata', $this->data);
     $session->set('com_fabrik.matchedHeadings', $this->matchedHeadings);
     JFile::delete($baseDir . '/' . $userfile_name);
 }
예제 #2
0
function CSV_import($file, $table, $db)
{
    $result = array();
    $csv = new csv_bv($file, ',', '"', '\\');
    $csv->SkipEmptyRows(true);
    // Remove leading and trailing \s and \t.
    $csv->TrimFields(false);
    $header = $csv->NextLine();
    $rs = $db->Execute('select * from ' . $table);
    if (!$rs) {
        $result['result'] = 0;
        //failure
        $result['error'] = $db->ErrorMsg();
    } else {
        $fcount = 0;
        $icount = 0;
        $metatypes = array();
        $xml_parser = xml_parser_create();
        if ($fp = fopen(str_replace('.csv', '.xml', $file), 'r')) {
            $data = fread($fp, filesize($file));
            fclose($fp);
            xml_parse_into_struct($xml_parser, $data, $vals, $index);
            xml_parser_free($xml_parser);
            if (is_array($index['FIELD'])) {
                foreach ($index['FIELD'] as $value) {
                    $metatypes[$vals[$value]['attributes']['NAME']] = $vals[$value]['attributes']['TYPE'];
                }
            }
        } else {
            for ($i = 0; $i < $rs->FieldCount(); $i++) {
                $curr_field = $rs->FetchField($i);
                $curr = $curr_field->name;
                $type = $rs->MetaType($curr_field->type);
                $metatypes[$curr] = $type;
            }
        }
        $m = 3;
        //camila_get_translation('camila.dateformat.monthpos');
        $d = 0;
        //camila_get_translation('camila.dateformat.daypos');
        $y = 6;
        //camila_get_translation('camila.dateformat.yearpos');
        while ($arr_data = $csv->NextLine()) {
            $record = array();
            $count = 0;
            foreach ($header as $value) {
                if ($metatypes[$value] == 'D') {
                    if ($arr_data[$count] != '') {
                        $record[$value] = $db->BindDate(substr($arr_data[$count], $y, 4) . '-' . substr($arr_data[$count], $m, 2) . '-' . substr($arr_data[$count], $d, 2));
                    }
                } else {
                    $record[$value] = $arr_data[$count];
                }
                $count++;
            }
            $insertSQL = $db->GetInsertSQL($rs, $record);
            $icount++;
            $rs2 = $db->Execute($insertSQL);
            # Insert the record into the database
            if (!$rs2) {
                $fcount++;
            }
        }
        if ($fcount > 0) {
            $result['result'] = 1;
            //errors
            $result['processed'] = $icount;
            $result['failed'] = $fcount;
            $result['skipped'] = $csv->SkippedRowCount();
        } else {
            $result['result'] = 2;
            //success
            $result['processed'] = $icount;
            $result['failed'] = $fcount;
            $result['skipped'] = $csv->SkippedRowCount();
        }
    }
    return $result;
}
예제 #3
0
 /**
  * read the CSV file, store results in $this->headings and $this->data
  */
 function readCSV($userfile_name)
 {
     $baseDir = $this->getBaseDir();
     $this->headings = array();
     $this->data = array();
     $data = $this->getFormData();
     $field_delimiter = $this->getFieldDelimiter();
     $text_delimiter = stripslashes(JArrayHelper::getValue($data, 'text_delimiter', '"'));
     $csv = new csv_bv($baseDir . '/' . $userfile_name, $field_delimiter, $text_delimiter, '\\');
     $csv->inPutFormat = JArrayHelper::getValue($data, 'inPutFormat', 'csv');
     $csv->SkipEmptyRows(TRUE);
     // Will skip empty rows. TRUE by default. (Shown here for example only).
     $csv->TrimFields(TRUE);
     // Remove leading and trailing \s and \t. TRUE by default.
     $model = $this->getlistModel();
     $tableParams = $model->getParams();
     $mode = $tableParams->get('csvfullname');
     while ($arr_data = $csv->NextLine()) {
         if (empty($this->headings)) {
             foreach ($arr_data as &$heading) {
                 // remove UFT8 Byte-Order-Mark if present
                 if (substr($heading, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
                     $heading = substr($heading, 3);
                 }
                 if ($mode != 2) {
                     // $$$ rob don't bother cleaning at all as dots (.) are replaced with "_"
                     //$heading = FabrikString::clean($heading);
                     // $$$ rob replacing with this as per thread - http://fabrikar.com/forums/showthread.php?p=83304
                     $heading = str_replace(' ', '_', $heading);
                 }
             }
             if (!$this->getSelectKey()) {
                 //if no table loaded and the user asked to automatically add a key then put id at the beginning of the new headings
                 $idheading = 'id';
                 if (in_array($idheading, $arr_data)) {
                     $idheading .= rand(0, 9);
                 }
                 array_unshift($arr_data, $idheading);
             }
             $this->headings = $arr_data;
         } else {
             if (function_exists('iconv')) {
                 foreach ($arr_data as &$d) {
                     //strip any none uft-8 characters from the import data
                     //if we don't do this then the site's session is destroyed and you are logged out
                     $d = iconv("utf-8", "utf-8//IGNORE", $d);
                 }
             }
             if (!$this->getSelectKey()) {
                 array_unshift($arr_data, '');
             }
             if (count($arr_data) == 1 && $arr_data[0] == '') {
                 //csv import from excel saved as unicode has blank record @ end
             } else {
                 $this->data[] = $arr_data;
             }
         }
     }
     fclose($csv->mHandle);
     // $$$ hugh - remove the temp file, but don't clear session
     // $$$ rob 07/11/2011 - NO!!! as import in admin reads the file twice.
     // once for getting the headings and a second time for importing/
     // $this->removeCSVFile(false);
 }