Exemple #1
0
function submit_file($new_file, $user_id, $owner)
{
    $result = array();
    // result["status"]( 0:successed or 1:failed ) and result["error"] or result["warning"] message.
    $result["status"] = 0;
    $xlsx_path = "../files/goods_xlsx";
    demo_check();
    // if file submited check file extension
    if ($img = $new_file[name]) {
        if (!preg_match("/\\.(xlsx)\$/i", $img)) {
            $result["status"] = 1;
            $result["error"] = "ERROR: The file is not in xlsx format.";
            return $result;
        }
        $new_file_urlencode = time() . ".xlsx";
        $xlsx_upload_path = "{$xlsx_path}/{$new_file_urlencode}";
        move_uploaded_file($new_file[tmp_name], $xlsx_upload_path);
        chmod($xlsx_upload_path, 0606);
    } else {
        $result["status"] = 1;
        $result["error"] = "ERROR: Please Attach the Excel file.";
        return $result;
    }
    // check if it is regular file
    if (!is_file($xlsx_upload_path)) {
        $result["status"] = 1;
        $result["error"] = "ERROR: {$xlsx_upload_path} is not found in ther server";
        return $result;
    } else {
        try {
            // Parse products info, then store into DB
            require_once $_SERVER['DOCUMENT_ROOT'] . '/library/PHPExcel_1/Classes/PHPExcel.php';
            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
            $objReader->setReadDataOnly(true);
            $objPHPExcel = $objReader->load($xlsx_upload_path);
            unlink($xlsx_upload_path);
            $dataSheet = $objPHPExcel->getSheet(0);
            $dataSheet->unfreezePane();
            $dataSheet->removeColumn("AE", 7);
            // get info from data table and option table
            $data = array();
            $options = array();
            $highestRow = $dataSheet->getHighestRow();
            $highestColumn = $dataSheet->getHighestColumn();
            for ($i = 16; $i < $highestRow; $i++) {
                if (!is_null($dataSheet->getCellByColumnAndRow(0, $i)->getValue())) {
                    $arr = array();
                    for ($j = 0; $j < 25; $j++) {
                        array_push($arr, mysql_real_escape_string(trim($dataSheet->getCellByColumnAndRow($j, $i)->getValue())));
                    }
                    //array_push($data,$arr);
                    $data[$arr[0]] = $arr;
                }
                if (!is_null($dataSheet->getCellByColumnAndRow(26, $i)->getValue())) {
                    $arr = array();
                    for ($j = 26; $j < 30; $j++) {
                        array_push($arr, mysql_real_escape_string(trim($dataSheet->getCellByColumnAndRow($j, $i)->getValue())));
                    }
                    array_push($options, $arr);
                }
            }
            $result = validateBulkInput($data, $options, $owner, $result);
            if ($result["status"] === 0) {
                // insert data to temp DB
                $dataCount = insert_goods_tmp_from_array($data, $user_id, $owner);
                // located in function.php
                // insert options to tmep DB
                $optionCount = insert_goods_option_temp_from_array($options, $owner);
                // located in function.php
                $result["success"] = "File submition succeedeed! {$dataCount} products are ready to upload.";
            }
        } catch (exception $e) {
            $err = "ERROR: ";
            $err .= $e->getMessage();
            $result["error"] = $err;
            $result["status"] = 1;
            return $result;
        }
        return $result;
    }
}
Exemple #2
0
 public function onSave()
 {
     demo_check();
     Db::saveSafe($this->table, $this->getData());
 }