/** * Function uploads the supplied tsv file into the table * * * @return string */ function uploadTSVFile() { $tsvfilename = $_FILES['category_file']['tmp_name']; $legal_extentions = array("tsv"); $file_ext = strtolower(end(explode(".", $_FILES['category_file']['name']))); if (!in_array($file_ext, $legal_extentions)) { return '<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> The file you are attempting to upload is not supported by this server</div>'; } $file = explode(".", $_FILES['category_file']['name']); if (count($file) > 2 || $file[1] != 'tsv') { return '<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> The file you are attempting to upload is not supported by this server</div>'; } if (file_exists($tsvfilename) > 0) { // $stpath="uploadedtsvfile/".date("YmdHis").$tsvfilename; $stpath = "uploadedtsvfile/" . date("YmdHis") . "category.tsv"; if (move_uploaded_file($tsvfilename, $stpath)) { $targetpath = $stpath; if (file_exists($targetpath)) { $fp = fopen($targetpath, 'r'); $records = array(); $rowfirst = fgets($fp); $rowfirst = str_replace("\n", "", $rowfirst); $tmpfirst = explode("\t", trim($rowfirst)); $chkfieldsarr = array('category_name', 'description', 'type', 'category_parent_id'); $cmp_arr = array_diff($tmpfirst, $chkfieldsarr); if (empty($cmp_arr)) { $pro_fields = implode(',', $chkfieldsarr); $pro_cnt = 0; $fail_cnt = 0; while ($row = fgets($fp)) { $row = str_replace("\n", " ", $row); $pro = $inv = explode("\t", addslashes(trim($row))); $check = new Core_CCategoryBulkUpload(); $check->checkCatExists($pro[0], $pro[3]); if ($check->checkCatExists($pro[0], $pro[3])) { return '<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> ' . $pro[0] . ' Category Name Already Exists </div>'; } if ($pro[3] != 0 && $pro[2] != 'parent') { $dflt = new Core_CCategoryBulkUpload(); $dflt->checkIsParentCategory($pro[3]); if ($dflt->checkIsParentCategory($pro[3])) { $pro = "'" . implode("','", array_splice($pro, 0, 4)) . "'"; $pro = str_replace("child", "1", $pro); $sql = ' INSERT INTO category_table(category_name, category_desc, category_status,category_parent_id) VALUES (' . $pro . ')'; $obj = new Bin_Query(); if ($obj->updateQuery($sql)) { $pro_cnt++; } else { $fail_cnt++; } } else { $fail_cnt++; } } else { $pro = "'" . implode("','", array_splice($pro, 0, 3)) . "'"; $pro = str_replace("parent", "0", $pro); $sql = 'INSERT INTO category_table(category_name,category_desc, category_parent_id,category_status) VALUES (' . $pro . ',1)'; $obj = new Bin_Query(); if ($obj->updateQuery($sql)) { $pro_cnt++; } else { $fail_cnt++; } } //exit(); /*if ($dflt->checkIsParentCategory($pro[3])) { $pro="'". implode("','",array_splice($pro,0,16))."'"; $inv=implode(",",array_splice($inv,16,2)); $sql=' INSERT INTO products_table( category_id, title, description, sku, brand, model, msrp, price, weight, dimension, shipping_cost, status , tag, meta_desc, meta_keywords, is_featured ) VALUES ('.$pro.')'; $obj=new Bin_Query(); if($obj->updateQuery($sql)) { $sql=' INSERT INTO product_inventory_table( product_id, soh, rol ) VALUES ('.$obj->insertid.',' .$inv.')'; if($obj->updateQuery($sql)) $pro_cnt++; else $fail_cnt++; } else $fail_cnt++; } else $fail_cnt++; */ } return '<div class="alert alert-success"> <button type="button" class="close" data-dismiss="alert">×</button> ' . $pro_cnt . ' Categories Created Successfullly . ' . $fail_cnt . ' Categories Not Created </div>'; } else { return '<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> Please Check The Format Of TSV File </div>'; } fclose($fp); } else { return '<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> Error Uploading File</div>'; } } else { return '<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> TSV File is not created </div>'; } } }
/** * Function displays a template for downloading the TSV file used for bulk uploading * at the admin side * * @return array */ function downloadTSVSample() { include 'classes/Core/CCategoryBulkUpload.php'; $default = new Core_CCategoryBulkUpload(); $default->downloadTSVSample(); }