From a26ae1a9b3774792b28e35701bb6aa1e2a93a4a8 Mon Sep 17 00:00:00 2001 From: objecttothis <17935339+objecttothis@users.noreply.github.com> Date: Fri, 1 Nov 2019 17:25:01 +0400 Subject: [PATCH 1/6] Refactor to reflect Excel --> CSV wording change --- application/language/en-US/items_lang.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/application/language/en-US/items_lang.php b/application/language/en-US/items_lang.php index 7c4c842c0..bdf11676f 100644 --- a/application/language/en-US/items_lang.php +++ b/application/language/en-US/items_lang.php @@ -32,14 +32,14 @@ $lang["items_edit_multiple_items"] = "Editing Multiple Items"; $lang["items_empty_upc_items"] = "Empty Barcode Items"; $lang["items_error_adding_updating"] = "Error adding/updating item"; $lang["items_error_updating_multiple"] = "Error updating items"; -$lang["items_excel_import_failed"] = "Excel import failed"; -$lang["items_excel_import_nodata_wrongformat"] = "The uploaded file has no data or is formatted incorrectly."; -$lang["items_excel_import_partially_failed"] = "There were %1 item import failure(s) on line(s): %2. No rows were imported."; -$lang["items_excel_import_success"] = "Item import successful."; +$lang["items_csv_import_failed"] = "CSV import failed"; +$lang["items_csv_import_nodata_wrongformat"] = "The uploaded CSV file has no data or is formatted incorrectly."; +$lang["items_csv_import_partially_failed"] = "There were %1 item import failure(s) on line(s): %2. No rows were imported."; +$lang["items_csv_import_success"] = "Item CSV import successful."; $lang["items_generate_barcodes"] = "Generate Barcodes"; $lang["items_hsn_code"] = "Harmonized System Nomenclature"; $lang["items_image"] = "Avatar"; -$lang["items_import_items_excel"] = "Item Import from Excel"; +$lang["items_import_items_csv"] = "Item Import from CSV"; $lang["items_info_provided_by"] = "Information provided by"; $lang["items_inventory"] = "Inventory"; $lang["items_inventory_comments"] = "Comments"; From 0180de5490e77558f76848a8f7c1135dd7f3ca05 Mon Sep 17 00:00:00 2001 From: objecttothis Date: Mon, 18 Nov 2019 14:04:07 +0400 Subject: [PATCH 2/6] Correctly check for BOM and skip if exists No characters are skipped if BOM doesn't exist --- application/helpers/importfile_helper.php | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/application/helpers/importfile_helper.php b/application/helpers/importfile_helper.php index 4914a758e..ff2dd878e 100644 --- a/application/helpers/importfile_helper.php +++ b/application/helpers/importfile_helper.php @@ -62,6 +62,12 @@ function get_csv_file($file_name) if(($csv_file = fopen($file_name,'r')) !== FALSE) { + //Skip Byte-Order Mark + if(bom_exists($csv_file) === TRUE) + { + fseek($csv_file, 3); + } + while (($data = fgetcsv($csv_file)) !== FALSE) { $line_array[] = $data; @@ -74,4 +80,27 @@ function get_csv_file($file_name) return $line_array; } + +/** + * Checks the first three characters of a file for the Byte-Order Mark then returns the file position to the first character. + * + * @param object $file_handle File handle to check + * @return bool Returns TRUE if the BOM exists and FALSE otherwise. + */ +function bom_exists(&$file_handle) +{ + $str = fread($file_handle,3); + rewind($file_handle); + + $bom = pack("CCC", 0xef, 0xbb, 0xbf); + + if (0 === strncmp($str, $bom, 3)) + { + return TRUE; + } + else + { + return FALSE; + } +} ?> \ No newline at end of file From 35a59fc4005f2fd985f271bc2b541e327387136e Mon Sep 17 00:00:00 2001 From: objecttothis Date: Mon, 18 Nov 2019 14:39:08 +0400 Subject: [PATCH 3/6] refactor variable names to reflect Excel --> CSV wording --- application/controllers/Items.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/application/controllers/Items.php b/application/controllers/Items.php index ea01dd713..9025b8d75 100644 --- a/application/controllers/Items.php +++ b/application/controllers/Items.php @@ -785,9 +785,9 @@ class Items extends Secure_Controller } /* - Items import from excel spreadsheet + Items import from csv spreadsheet */ - public function excel() + public function csv() { $name = 'import_items.csv'; $allowed_locations = $this->Stock_location->get_allowed_locations(); @@ -796,19 +796,19 @@ class Items extends Secure_Controller force_download($name, $data, TRUE); } - public function excel_import() + public function csv_import() { - $this->load->view('items/form_excel_import', NULL); + $this->load->view('items/form_csv_import', NULL); } /** * Imports items from CSV formatted file. */ - public function do_excel_import() + public function do_csv_import() { if($_FILES['file_path']['error'] != UPLOAD_ERR_OK) { - echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('items_excel_import_failed'))); + echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('items_csv_import_failed'))); } else { @@ -876,19 +876,19 @@ class Items extends Secure_Controller if(count($failCodes) > 0) { - $message = $this->lang->line('items_excel_import_partially_failed', count($failCodes), implode(', ', $failCodes)); + $message = $this->lang->line('items_csv_import_partially_failed', count($failCodes), implode(', ', $failCodes)); $this->db->trans_rollback(); echo json_encode(array('success' => FALSE, 'message' => $message)); } else { $this->db->trans_commit(); - echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('items_excel_import_success'))); + echo json_encode(array('success' => TRUE, 'message' => $this->lang->line('items_csv_import_success'))); } } else { - echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('items_excel_import_nodata_wrongformat'))); + echo json_encode(array('success' => FALSE, 'message' => $this->lang->line('items_csv_import_nodata_wrongformat'))); } } } @@ -1035,7 +1035,7 @@ class Items extends Secure_Controller 'location_id' => $location_id ); - $excel_data = array( + $csv_data = array( 'trans_items' => $item_data['item_id'], 'trans_user' => $employee_id, 'trans_comment' => $comment, @@ -1047,16 +1047,16 @@ class Items extends Secure_Controller $item_quantity_data['quantity'] = $line['location_' . $location_name]; $this->Item_quantity->save($item_quantity_data, $item_data['item_id'], $location_id); - $excel_data['trans_inventory'] = $line['location_' . $location_name]; - $this->Inventory->insert($excel_data); + $csv_data['trans_inventory'] = $line['location_' . $location_name]; + $this->Inventory->insert($csv_data); } else { $item_quantity_data['quantity'] = 0; $this->Item_quantity->save($item_quantity_data, $item_data['item_id'], $line[$col]); - $excel_data['trans_inventory'] = 0; - $this->Inventory->insert($excel_data); + $csv_data['trans_inventory'] = 0; + $this->Inventory->insert($csv_data); } } } From 6294079ce3add69efa7a62db2faedc54c5fccc09 Mon Sep 17 00:00:00 2001 From: objecttothis Date: Mon, 25 Nov 2019 15:50:41 +0400 Subject: [PATCH 4/6] File rename and variable refactor to correct error in import. --- .../items/{form_excel_import.php => form_csv_import.php} | 0 application/views/items/manage.php | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename application/views/items/{form_excel_import.php => form_csv_import.php} (100%) diff --git a/application/views/items/form_excel_import.php b/application/views/items/form_csv_import.php similarity index 100% rename from application/views/items/form_excel_import.php rename to application/views/items/form_csv_import.php diff --git a/application/views/items/manage.php b/application/views/items/manage.php index baf0651f7..d0b8f060e 100644 --- a/application/views/items/manage.php +++ b/application/views/items/manage.php @@ -58,9 +58,9 @@ $(document).ready(function()