示例#1
0
文件: CSV.php 项目: villos/tree_admin
 /**
  * Front to call getPointer and moving the resource to the
  * beginning of the file
  * Reset it if you like.
  *
  * @param string $file The name of the file
  * @param array  &$conf The configuration
  * @param string $mode The open node (ex: FILE_MODE_READ or FILE_MODE_WRITE)
  *
  * @return boolean true on success false on failure
  */
 function resetPointer($file, &$conf, $mode)
 {
     if (!File_CSV::getPointer($file, $conf, $mode, true)) {
         return false;
     }
     return true;
 }
示例#2
0
 /**
  * Writes a struc (array) in a file as CSV
  *
  * @param string $file   The filename where to write the data
  * @param array  $fields Ordered array with the data
  * @param array  &$conf   The configuration of the dest CSV
  *
  * @return bool True on success false otherwise
  */
 function write($file, $fields, &$conf)
 {
     if (!($fp = File_CSV::getPointer($file, $conf, FILE_MODE_WRITE))) {
         return false;
     }
     if (count($fields) != $conf['fields']) {
         File_CSV::raiseError("Wrong fields number count: '" . count($fields) . "' expected " . $conf['fields']);
         return true;
     }
     $write = '';
     for ($i = 0; $i < count($fields); $i++) {
         if (!is_numeric($fields[$i]) && $conf['quote']) {
             $write .= $conf['quote'] . $fields[$i] . $conf['quote'];
         } else {
             $write .= $fields[$i];
         }
         if ($i < count($fields) - 1) {
             $write .= $conf['sep'];
         } else {
             $write .= $conf['crlf'];
         }
     }
     if (!fwrite($fp, $write)) {
         return File_CSV::raiseError('Can not write to file');
     }
     return true;
 }
 function import($tempfile, $csv_format_type_id, $ignore_first_line, $unlink = true)
 {
     global $ProductCSV;
     global $format;
     global $data;
     global $messageStack;
     global $line_count;
     global $success_count;
     global $error_count;
     global $missing_count;
     global $body;
     $format = $ProductCSV->getFormatById($csv_format_type_id);
     $conf = File_CSV::DiscoverFormat($tempfile);
     File_CSV::getPointer($tempfile, $conf);
     // check format
     if ($conf['fields'] == count($format['columns'])) {
         $count = 1;
         // skip first line
         if ($ignore_first_line) {
             File_CSV::read($tempfile, $conf);
             $count++;
         }
         // output table header
         $body = '<tr><td><table border="1">';
         $body .= '<tr>';
         $body .= '<th>' . PRODUCT_CSV_TABLE_LINE_NUMBER . '</th>';
         foreach ($format['columns'] as $val) {
             $body .= '<th>' . $val['csv_column_name'] . '</th>';
         }
         $body .= '<th>' . PRODUCT_CSV_TABLE_HEADER . '</th>';
         $body .= '</tr>';
         // init count
         $line_count = 0;
         $success_count = 0;
         $error_count = 0;
         $missing_count = 0;
         // read lines
         while (($data = File_CSV::read($tempfile, $conf)) !== false) {
             if (count($data) == 0) {
                 continue;
             }
             echo ' ';
             flush();
             // convert charactr set to internal encoding
             foreach ($data as $key => $val) {
                 $data[$key] = mb_convert_encoding($val, MODULE_PRODUCT_CSV_INTERNAL_CHARACTER, MODULE_PRODUCT_CSV_IMPORT_CHARACTER);
             }
             $ProductCSV->messageStack->reset();
             switch ($format['csv_format_type_id']) {
                 case 1:
                     $import_status = $ProductCSV->importProduct($data, $format);
                     break;
                 case 2:
                     $format = zen_add_number_to_format($format);
                     $import_status = $ProductCSV->importCategory($data, $format);
                     break;
                 case 3:
                     $import_status = $ProductCSV->importOption($data, $format);
                     break;
             }
             // store success/error/line count
             $line_count++;
             if ($import_status === true) {
                 $success_count++;
             } elseif ($import_status === false) {
                 $error_count++;
             } else {
                 $missing_count++;
             }
             $body .= '<tr>';
             $body .= '<td>' . $count++ . '</td>';
             foreach ($data as $val) {
                 if ($val == '') {
                     $val = '&nbsp;';
                 }
                 $body .= '<td>' . $val . '</td>';
             }
             $body .= '<td>' . $ProductCSV->messageStack->output() . '</td>';
             $body .= '</tr>';
         }
         $body .= '</table></td></tr>';
         $messageStack->add(sprintf(PRODUCT_CSV_MESSAGE_IMPORT_STATUS, $line_count, $success_count, $error_count), 'success');
         if ($unlink) {
             unlink($tempfile);
         }
     } else {
         $messageStack->add(PRODUCT_CSV_ERROR_INVALID_FORMAT, 'caution');
     }
 }
            foreach ($format['columns'] as $val) {
                $csv_format_column[$val['csv_format_column_number']] = $val['csv_column_id'];
            }
        }
    }
    // read uploaded file
    if ($action == 'save') {
        if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) {
            $tempfile = DIR_FS_CATALOG . '/temp/csv_format_' . date('YmdHis');
            $_SESSION['product_csv']['upfile'] = $tempfile;
            move_uploaded_file($_FILES['file']['tmp_name'], $tempfile);
        }
        if (is_readable($_SESSION['product_csv']['upfile'])) {
            $tempfile = $_SESSION['product_csv']['upfile'];
            $conf = File_CSV::discoverFormat($tempfile);
            File_CSV::getPointer($tempfile, $conf);
            $data = File_CSV::read($tempfile, $conf);
        }
        if ($data != false) {
            $hidden_fields .= zen_draw_hidden_field('csv_format_name', $csv_format_name);
            $disable_format_name = ' disabled="disabled"';
            $name_csv_format_name = 'current_csv_format_name';
            $setting_new = '<table border="1">';
            $setting_new .= '<tr><th>' . FORM_FORMAT_COLUMN_HEADER1 . '</th><th>' . FORM_FORMAT_COLUMN_HEADER2 . '</th><th>' . FORM_FORMAT_COLUMN_HEADER3 . '</th></tr>';
            $count = 0;
            foreach ($data as $val) {
                $count++;
                $setting_new .= '
<tr><td>' . sprintf(FORM_FORMAT_COLUMN_NAME, $count) . '</td><td>' . mb_convert_encoding($val, MODULE_PRODUCT_CSV_INTERNAL_CHARACTER, 'auto') . '</td><td>' . zen_draw_pull_down_menu('csv_format_column[' . $count . ']', $format_columns, isset($csv_format_column) ? $csv_format_column[$count] : $format_columns[$count - 1]['id']);
            }
            $setting_new .= '</table>';
示例#5
0
    $return_button .= zen_draw_hidden_field('csv_format_id', $_POST['csv_format_id']);
    $return_button .= zen_draw_hidden_field('ignore_first_line', $_POST['ignore_first_line']);
    $return_button .= '<input type="submit" value="' . PRODUCT_CSV_RETURN_TEXT . '" name="submit"/></form>';
} elseif ($action == 'export') {
    // prepare conf for File::CSV
    $format = $ProductCSV->getFormatById($_POST['csv_format_id']);
    $conf['fields'] = count($format['columns']);
    $conf['sep'] = MODULE_PRODUCT_CSV_EXPORT_CONFIG_SEP;
    $conf['quote'] = MODULE_PRODUCT_CSV_EXPORT_CONFIG_QUOTE;
    $conf['crlf'] = MODULE_PRODUCT_CSV_EXPORT_CONFIG_EOL;
    // write header line to tempfile
    foreach ($format['columns'] as $val) {
        $arr[] = mb_convert_encoding($val['csv_column_name'], MODULE_PRODUCT_CSV_EXPORT_CHARACTER, MODULE_PRODUCT_CSV_INTERNAL_CHARACTER);
    }
    $tempfile = DIR_FS_CATALOG . '/temp/export_' . date('YmdHis') . '.csv';
    File_CSV::getPointer($tempfile, $conf, FILE_MODE_WRITE);
    File_CSV::write($tempfile, $arr, $conf);
    switch ($format['csv_format_type_id']) {
        case 1:
            $prefix = 'products_';
            // get products_id
            $categories_products_id_list = array();
            $products_ids = zen_get_categories_products_list($_POST['category_id'], true, true);
            $products_ids = array_unique($products_ids);
            // write line
            foreach ($products_ids as $val) {
                $data = $ProductCSV->getExportDataProduct($val, $format);
                foreach ($data as $key => $d) {
                    $data[$key] = mb_convert_encoding($d, MODULE_PRODUCT_CSV_EXPORT_CHARACTER, MODULE_PRODUCT_CSV_INTERNAL_CHARACTER);
                    $data[$key] = str_replace("\r\n", "\n", $data[$key]);
                }