public function action_edit($id = null) { is_null($id) and Response::redirect('blog'); if (!($blog = Model_Blog::find($id))) { Session::set_flash('error', 'Could not find blog #' . $id); Response::redirect('blog'); } $val = Model_Blog::validate('edit'); if ($val->run()) { $blog->title = Input::post('title'); $blog->content = Input::post('content'); if ($blog->save()) { Session::set_flash('success', 'Updated blog #' . $id); Response::redirect('blog'); } else { Session::set_flash('error', 'Could not update blog #' . $id); } } else { if (Input::method() == 'POST') { $blog->title = $val->validated('title'); $blog->content = $val->validated('content'); Session::set_flash('error', $val->error()); } $this->template->set_global('blog', $blog, false); } $this->template->title = "Blogs"; $this->template->content = View::forge('blog/edit'); }
public function action_edit($id = null) { is_null($id) and Response::redirect('blog'); $this->theme->set_template('edit'); $this->theme->get_template()->set_global('current_menu', "Blogs", false); $this->theme->get_template()->set_global('current_menu_desc', "จัดการบล็อกทั้งหมดในระบบ", false); $this->theme->get_template()->set('breadcrumb', array(array('title' => "Home", 'icon' => "fa-home", 'link' => Uri::create('home'), 'active' => false), array('title' => "Blogs", 'icon' => "fa-rss", 'link' => Uri::create('blog/index'), 'active' => false), array('title' => "Edit", 'icon' => "", 'link' => "", 'active' => true))); $this->theme->get_template()->set_global('mode', "edit", false); if (!($blog = Model_Blog::find($id))) { Session::set_flash('error', 'Could not find blog #' . $id); Response::redirect('blog'); } if (Input::method() == 'POST') { if (Input::post('action') == "edit_blog") { $file = Input::file('blog_cover_photo_file'); $val = Model_Blog::validate('edit'); if ($val->run()) { $allowList = array(".jpeg", ".jpg", ".png"); $error = false; $path = realpath(DOCROOT . "/../../uploads/blog_cover/") . DS; $blog_cover_photo = ""; if ($file['size'] > 0) { $ext = strtolower(substr($file['name'], strrpos($file['name'], "."))); if (!in_array($ext, $allowList)) { Session::set_flash('error', 'ชนิดของไฟล์ภาพไม่ถูกต้อง'); $error = true; } if (strlen($blog->blog_cover_photo)) { @unlink($path . $blog->blog_cover_photo); } $filename = md5(time()) . $ext; if (@copy($file['tmp_name'], $path . $filename)) { $blog_cover_photo = $filename; } else { Session::set_flash('error', 'ไม่สามารถอัพโหลดไฟล์ภาพได้ โปรดลองใหม่อีกครั้ง'); $error = true; } } if (!$error) { $blog->blog_title = Input::post('blog_title'); $blog->blog_short_detail = Input::post('blog_short_detail'); $blog->blog_detail = Input::post('blog_detail'); if (strlen($blog_cover_photo)) { $blog->blog_cover_photo = $blog_cover_photo; } $blog->blog_published = Input::post('blog_published'); if ($blog->published_at == 0 && Input::post('blog_published') == 1) { $blog->published_at = time(); } if ($blog->save()) { Session::set_flash('success', 'อัพเดตข้อมูลบล็อก #' . $id . ' เรียบร้อยแล้ว'); } else { Session::set_flash('error', 'Could not update blog #' . $id); } } } else { $msg = '<ul>'; foreach ($val->error() as $field => $error) { $msg .= '<li>' . $error->get_message() . '</li>'; } $msg .= '</ul>'; Session::set_flash('error', $msg); } } else { if (Input::post('action') == "upload_photos") { /*print "<pre>"; print_r(Input::post()); print_r(Input::file()); print "</pre>"; exit();*/ $files = Input::file('blog_photo_file'); $photos = array(); $allowList = array(".jpeg", ".jpg", ".png"); $error = false; $path = realpath(DOCROOT . "/../../uploads/blog_photo/") . DS; /*print_r($path); exit();*/ for ($i = 0; $i < 5; $i++) { if ($files['size'][$i] > 0) { $ext = strtolower(substr($files['name'][$i], strrpos($files['name'][$i], "."))); if (!in_array($ext, $allowList)) { Session::set_flash('error', 'ชนิดของไฟล์ภาพไม่ถูกต้อง'); $error = true; break; } $filename = md5(time() + $i) . $ext; if (@copy($files['tmp_name'][$i], $path . $filename)) { $photos[$i] = $filename; } else { Session::set_flash('error', 'ไม่สามารถอัพโหลดไฟล์ภาพได้ โปรดลองใหม่อีกครั้ง'); $error = true; break; } } } if (!$error) { foreach ($photos as $p) { $blogphoto = Model_BlogPhoto::forge(array('blog_id' => Input::post('blog_id'), 'photo_file_name' => $p, 'created_at' => time())); $blogphoto->save(); } } } else { if (Input::post('action') == "delete_photo") { $blogphoto = Model_BlogPhoto::find(Input::post('photo_id')); $path = realpath(DOCROOT . "/../../uploads/blog_photo/") . DS; @unlink($path . $blogphoto->photo_file_name); $blogphoto->delete(); } } } } $data['photos'] = Model_BlogPhoto::get_blog_photos($id); $this->theme->get_template()->set_global('blog', $blog, false); $this->theme->get_template()->set_global('path', "http://www.buffohero.com/uploads/blog_photo/", false); $this->theme->set_partial('sidebar', 'common/sidebar'); $this->theme->get_template()->set('page_specific_js', "form_blog.js"); $this->theme->set_partial('left', 'blog/edit'); $this->theme->set_partial('right', 'blog/_imageupload')->set($data); }