/**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Sanpham();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Sanpham'])) {
         $model->attributes = $_POST['Sanpham'];
         if ($model->save()) {
             $this->redirect(array('index'));
         }
     }
     $this->render('create', array('model' => $model));
 }
 public function postThemSanpham()
 {
     $input = Input::all();
     $validation = Validator::make($input, Sanpham::$rules);
     if ($validation->passes()) {
         $image = Input::file('hinhanh');
         $imageName = $image->getClientOriginalName();
         $nameArray = explode('.', $imageName);
         $imageType = end($nameArray);
         $imageRules = array("jpg", "jpeg", "png");
         if (in_array($imageType, $imageRules)) {
             //neu dung thi tien hanh insert vao csdl
             $imageNewName = uniqid(rand(), true);
             $imageNewName = md5($imageNewName);
             $imageNewName = substr($imageNewName, 0, 6);
             $imageNewName .= "." . $imageType;
             Input::file('hinhanh')->move('uploads', $imageNewName);
             // upload hinh anh
             //sau khi upload thi insert vao csdl
             $sp = new Sanpham();
             $sp->tensp = Input::get('tensp');
             $sp->donvitinh = Input::get('donvitinh');
             $sp->hinhanh = $imageNewName;
             $sp->chitietsp = Input::get('chitietsp');
             $sp->khuyenmai = Input::get('khuyenmai');
             $sp->id_loai = Input::get('id_loai');
             $sp->save();
             // luu logfile
             Logfileadmin::addData("Thêm", "Sản Phẩm", $sp->id, $sp->tensp);
             return Redirect::to('sanpham/xem-sanpham');
         } else {
             return Redirect::to('sanpham/them-sanpham')->with('errorImage', "Không đúng định dạng hình ảnh");
         }
     } else {
         return Redirect::to('sanpham/them-sanpham')->withInput()->withErrors($validation)->with('message', 'There were validation errors.');
     }
 }