Exemplo n.º 1
0
 function on_submit()
 {
     $news_catid = Url::get('news_catid', 0);
     if (!$news_catid) {
         $this->setFormError('news_catid', "Bạn chưa chọn Danh mục cho Sản phẩm!");
     } else {
         $news_category = DB::select('news_categories', 'id=' . $news_catid);
         if (!$news_category) {
             $this->setFormError('news_catid', "Danh mục không tồn tại, mời bạn chọn Danh mục khác!");
         }
     }
     $title = Url::get('title');
     $brief = Url::get('brief', '');
     $content = Url::get('content', '');
     $hot = Url::get('hot', 0);
     $focus = Url::get('focus', 0);
     $status = Url::get('status', 2);
     //1:ẩn;2:hiện
     $image_title = Url::get('image_title');
     if ($status == 2) {
         $status = 1;
     } else {
         $status = 0;
     }
     /*if($image_title=='')
     		$image_title	=	AznetLib::convert_utf8_to_sms(AznetLib::subString($title,0,30,true));*/
     $pen_name = Url::get('pen_name');
     $source = Url::get('source', '');
     $get_source = Url::get('get_source', 0);
     $this->checkFormInput('Tiêu đề tin', "title", $title, 'str', true, '', 1, 255);
     $this->checkFormInput('Nội dung tóm tắt', "brief", $brief, 'str', true);
     $this->checkFormInput('Tiêu đề ảnh', "image_title", $image_title, 'str', false, '', 1, 100);
     $this->checkFormInput('Nội dung tin', "content", $content, 'str', true);
     if (!$this->errNum) {
         $news_image = array("name" => '', "img_server" => IMAGE_SERVER_NO, "title" => addslashes($image_title), 'time_create' => TIME_NOW, 'news_id' => 0, 'username' => User::user_name());
         if (isset($_FILES['main_image']) && $_FILES['main_image']['error'] == 0) {
             if ($_FILES['main_image']['name'] && getimagesize($_FILES['main_image']['tmp_name'])) {
                 $file_ext = AZLib::getExtension($_FILES['main_image']['name']);
                 if (in_array($file_ext, array('.jpg', '.jpeg', '.gif', '.png'))) {
                     if ($_FILES['main_image']['size'] <= Item::MAX_UPLOAD_SIZE) {
                         $uploadPath = AZLib::folderUpload(User::id(), "news");
                         $file_name = $uploadPath . date("YmdHis", TIME_NOW) . '_' . substr(AZLib::make_safe_name(basename(strtolower($_FILES['main_image']['name']), $file_ext)), 0, 36) . $file_ext;
                         if (AZLib::ftp_check_dir($uploadPath) && AZLib::ftp_image_put_file($file_name, $_FILES['main_image']['tmp_name'], IMAGE_SERVER_NO)) {
                             $news_image['name'] = $file_name;
                             $image_id = DB::insert('news_image', $news_image);
                             if ($image_id) {
                                 $news_image['id'] = $image_id;
                             } else {
                                 $this->setFormError('image', "Không cập nhật được <b>Ảnh đại diện</b>!");
                                 return;
                             }
                         } else {
                             $this->setFormError('image', "Không upload được <b>Ảnh đại diện</b>!");
                             return;
                         }
                     } else {
                         $this->setFormError('image', "Dung lượng ảnh quá lớn, dung lượng ảnh phải < 1 MB!");
                         return;
                     }
                 } else {
                     $this->setFormError('image', "Ảnh không đúng định dạng, chỉ chấp nhận các file .jpg, .jpeg, .gif, .png !");
                     return;
                 }
             } else {
                 $this->setFormError('image', "Lỗi <b>Ảnh đại diện</b>!");
                 return;
             }
         }
         if ($get_source) {
             //require_once('includes/news.php');
             //$content=News::convertContent(addslashes($content),$this->news['id']);
         }
         if ($brief == '') {
             $brief = AznetLib::subString($content, 0, 300, true);
         }
         $news_id = DB::insert('news', array('news_catid' => $news_catid, 'title' => addslashes($title), 'brief' => addslashes($brief), 'content' => addslashes($content), 'image' => $news_image['name'], 'image_title' => $news_image['title'], 'img_server' => $news_image['img_server'], 'status' => $status, 'pen_name' => addslashes($pen_name), 'source' => addslashes($source), 'get_source' => $get_source, 'time_created' => TIME_NOW, 'username' => User::user_name(), 'time_modify' => TIME_NOW, 'user_modify' => User::user_name(), 'hot' => $hot, 'focus' => $focus));
         if ($news_id) {
             if ($news_image['name'] && $news_image['id']) {
                 DB::update("news_image", array("news_id" => $news_id), "id={$news_image['id']}");
             }
             if (Url::get('href')) {
                 Url::redirect_url(urldecode(Url::get('href')));
             } else {
                 Url::redirect_current();
             }
         }
         $this->setFormError('title', "Không sửa được tin, mời bạn thử lại!");
     }
 }