示例#1
0
 public function insertPost()
 {
     $code = trim(remove_slashes($_POST['code']));
     $category_id = (int) $_POST['category_id'];
     $brand_id = (int) $_POST['brand_id'];
     $price_fonds = trim(remove_slashes($_POST['price_fonds']));
     $price_sell = trim(remove_slashes($_POST['price_sell']));
     $price_sell = trim(remove_slashes($_POST['price_sell']));
     $description = trim(remove_slashes($_POST['description']));
     $pics = trim(remove_slashes($_POST['pics']));
     $seo_url = trim(remove_slashes($_POST['seo_url']));
     $this->registry->template->product = array('code' => htmlspecialchars($code), 'category_id' => $category_id, 'brand_id' => $brand_id, 'price_fonds' => htmlspecialchars($price_fonds), 'price_sell' => htmlspecialchars($price_sell), 'description' => htmlspecialchars($description), 'pics' => $pics, 'seo_url' => $seo_url);
     $messageStr = '';
     $productDAO = new ProductDAO(DataSource::getInstance());
     if (!isset($code) || strlen($code) == 0) {
         $messageStr .= 'Mã sản phẩm: không được bỏ trống.<br />';
     } else {
         if ($productDAO->isExistedCode($code)) {
             $messageStr .= 'Mã sản phẩm: đã tồn tại.<br />';
         }
     }
     if (!isset($price_fonds) || strlen($price_fonds) == 0) {
         $messageStr .= 'Giá vốn: không được bỏ trống.<br />';
     } else {
         if ((int) $price_fonds <= 0) {
             $messageStr .= 'Giá vốn: phải là số nguyên dương.<br />';
         } else {
             $price_fonds_int = (int) $price_fonds;
         }
     }
     if (!isset($price_sell) || strlen($price_sell) == 0) {
         $messageStr .= 'Giá bán: không được bỏ trống.<br />';
     } else {
         if ((int) $price_sell <= 0) {
             $messageStr .= 'Giá bán: phải là số nguyên dương.<br />';
         } else {
             $price_sell_int = (int) $price_sell;
         }
     }
     if (isset($price_fonds_int) && isset($price_sell_int)) {
         if ($price_fonds_int > $price_sell_int) {
             $messageStr .= 'Giá bán: không được thấp hơn giá vốn.<br />';
         }
     }
     if (strlen($seo_url) == 0) {
         $messageStr .= 'SEO URL: không được bỏ trống.<br />';
     } else {
         if (!check_seo_url($seo_url)) {
             $messageStr .= 'SEO URL: không hợp lệ.<br />';
         } else {
             if ($productDAO->isExistedSeoUrl($seo_url)) {
                 $messageStr .= 'SEO URL: đã tồn tại.<br />';
             }
         }
     }
     $messageStr = trim($messageStr);
     if ($messageStr != '') {
         $this->registry->template->message = array('type' => 'error', 'value' => $messageStr);
     } else {
         $last_id = $productDAO->insert($code, $brand_id, $category_id, $price_fonds, $price_sell, $description, $pics, $seo_url);
         if ($last_id > -1) {
             $this->registry->template->message = array('type' => 'info', 'value' => 'Thêm sản phẩm thành công. ' . ' <a href="' . __SITE_CONTEXT . 'admin/product/update?id=' . $last_id . '">[Sửa]</a>');
             $this->registry->template->product = NULL;
         } else {
             $this->registry->template->message = array('type' => 'error', 'value' => 'Có lỗi xảy ra.');
         }
     }
 }