public function action_edit($id = null) { if (!Auth::has_access('news.edit')) { Session::set_flash('warning', 'You don\'t have the right to edit a news'); Response::redirect('admin'); } is_null($id) and Response::redirect('News'); $news = Model_News::find($id); $val = Model_News::validate('edit'); if ($val->run()) { $news->title = Input::post('title'); $news->body = Input::post('body'); if ($news->save()) { Session::set_flash('success', 'Updated news #' . $id); Response::redirect('news'); } else { Session::set_flash('error', 'Could not update news #' . $id); } } else { if (Input::method() == 'POST') { $news->title = $val->validated('title'); $news->body = $val->validated('body'); Session::set_flash('error', $val->error()); } $this->template->set_global('news', $news, false); } $this->template->title = "News"; $this->template->content = View::forge('news/edit'); }
public function action_edit($id = null) { is_null($id) and Response::redirect('News'); $news = Model_News::find($id); $val = Model_News::validate('edit'); if ($val->run()) { $news->title = Input::post('title'); $news->article = Input::post('article'); $news->call_center_id = Input::post('call_center_id'); $news->user_id = Input::post('user_id'); if ($news->save()) { Session::set_flash('success', 'Updated news #' . $id); Response::redirect('news'); } else { Session::set_flash('error', 'Could not update news #' . $id); } } else { if (Input::method() == 'POST') { $news->title = $val->validated('title'); $news->article = $val->validated('article'); $news->call_center_id = $val->validated('call_center_id'); $news->user_id = $val->validated('user_id'); Session::set_flash('error', $val->error()); } $this->template->set_global('news', $news, false); } $this->template->title = "News"; $this->template->content = View::forge('news/edit'); }
public function action_edit($id = null) { is_null($id) and Response::redirect('news'); $this->theme->set_template('edit'); $this->theme->get_template()->set_global('current_menu', "News", 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' => "News", 'icon' => "eicon-newspaper", 'link' => Uri::create('news/index'), 'active' => false), array('title' => "Edit", 'icon' => "", 'link' => "", 'active' => true))); $this->theme->get_template()->set_global('mode', "edit", false); if (!($news = Model_News::find($id))) { Session::set_flash('error', 'Could not find news #' . $id); Response::redirect('news'); } if (Input::method() == 'POST') { $file = Input::file('news_photo_file'); $val = Model_News::validate('edit'); if ($val->run()) { $allowList = array(".jpeg", ".jpg", ".png"); $error = false; $path = realpath(DOCROOT . "/../../uploads/news_photo/") . DS; $news_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($news->news_photo)) { @unlink($path . $news->news_photo); } $filename = md5(time()); if (@copy($file['tmp_name'], $path . $filename . $ext)) { $news_photo = $filename . $ext; /* small thumbnail */ parent::create_cropped_thumbnail($path . $filename . $ext, 64, 64, "-s"); parent::create_cropped_thumbnail($path . $filename . $ext, 128, 128, "-s@2x"); /* */ /* medium thumbnail */ parent::create_cropped_thumbnail($path . $filename . $ext, 360, 240, "-m"); parent::create_cropped_thumbnail($path . $filename . $ext, 720, 480, "-m@2x"); /* */ } else { Session::set_flash('error', 'ไม่สามารถอัพโหลดไฟล์ภาพได้ โปรดลองใหม่อีกครั้ง'); $error = true; } } if (!$error) { if (strlen($news_photo) && strlen($news->news_photo)) { $old_ext = strtolower(substr($news->news_photo, strrpos($news->news_photo, "."))); $old_filename = substr($news->news_photo, 0, strrpos($news->news_photo, ".")); @unlink($path . $old_filename . $old_ext); @unlink($path . $old_filename . "-s" . $old_ext); @unlink($path . $old_filename . "-s@2x" . $old_ext); @unlink($path . $old_filename . "-m" . $old_ext); @unlink($path . $old_filename . "-m@2x" . $old_ext); } $news->news_title = Input::post('news_title'); $news->news_short_detail = Input::post('news_short_detail'); $news->news_detail = Input::post('news_detail'); if (strlen($news_photo)) { $news->news_photo = $news_photo; } $news->news_published = Input::post('news_published'); if ($news->published_at == 0 && Input::post('news_published') == 1) { $news->published_at = time(); } if ($news->save()) { Session::set_flash('success', 'อัพเดตข้อมูลข่าว #' . $id . ' เรียบร้อยแล้ว'); } else { Session::set_flash('error', 'Could not update news #' . $id); } } } else { $msg = '<ul>'; foreach ($val->error() as $field => $error) { $msg .= '<li>' . $error->get_message() . '</li>'; } $msg .= '</ul>'; Session::set_flash('error', $msg); } } $this->theme->get_template()->set_global('news', $news, false); $this->theme->get_template()->set_global('path', "http://www.buffohero.com/uploads/news_photo/", false); $this->theme->set_partial('sidebar', 'common/sidebar'); $this->theme->get_template()->set('page_specific_js', "form_news.js"); $this->theme->set_partial('left', 'news/edit'); }
public function action_update($id = false) { if (!is_numeric($id)) { \Response::redirect('admin/news/list'); } // Get news item to edit if (!($item = Model_News::find_one_by_id($id))) { \Response::redirect('admin/news/list'); } \View::set_global('title', 'Edit News'); if (\Input::post()) { $val = Model_News::validate('update'); // Upload image and display errors if there are any $image = $this->upload_image(); if (!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images)) { // No previous images and image is not selected and it is required \Messages::error('<strong>There was an error while trying to upload content image</strong>'); \Messages::error('You have to select image'); } elseif ($image['errors']) { \Messages::error('<strong>There was an error while trying to upload content image</strong>'); foreach ($image['errors'] as $error) { \Messages::error($error); } } if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images))) { /** IMAGES **/ // Get all alt texts to update if there is no image change foreach (\Arr::filter_prefixed(\Input::post(), 'alt_text_') as $image_id => $alt_text) { if (strpos($image_id, 'new_') === false) { $item_images[$image_id] = array('id' => $image_id, 'data' => array('alt_text' => \Input::post('alt_text_' . $image_id, ''))); } } // Save images if new files are submitted if (isset($this->_image_data)) { foreach ($this->_image_data as $image_data) { $cover_count = count($item->images); if (strpos($image_data['field'], 'new_') === false) { // Update existing image if (str_replace('image_', '', $image_data['field']) != 0) { $image_id = (int) str_replace('image_', '', $image_data['field']); $cover_count--; $item_images[$image_id] = array('id' => $image_id, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, ''))); $this->delete_image(\Input::post('image_db_' . $image_id, '')); } } else { // Save new image $image_tmp = str_replace('image_new_', '', $image_data['field']); $item_images[0] = array('id' => 0, 'data' => array('content_id' => $item->id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); } } } Model_News::bind_images($item_images); /** END OF IMAGES **/ // Get POST values $insert = \Input::post(); // Prepare some values $insert['published_at'] = !empty($insert['published_at']) ? strtotime($insert['published_at']) : \Date::forge()->get_timestamp(); $insert['active_from'] = !empty($insert['active_from']) ? strtotime($insert['active_from']) : NULL; $insert['active_to'] = !empty($insert['active_to']) ? strtotime($insert['active_to']) : NULL; if ($insert['status'] != 2) { unset($insert['active_from']); unset($insert['active_to']); } $item->set($insert); try { $item->save(); \Messages::success('News successfully updated.'); \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/news/list/') : \Uri::admin('current')); } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update news</strong>'); // Uncomment lines below to show database errors //$errors = $e->getMessage(); //\Messages::error($errors); } } else { // Delete uploaded images if there is news saving error if (isset($this->_image_data)) { foreach ($this->_image_data as $image_data) { $this->delete_image($image_data['saved_as']); } } if ($val->error() != array()) { // show validation errors \Messages::error('<strong>There was an error while trying to update news</strong>'); foreach ($val->error() as $e) { \Messages::error($e->get_message()); } } } } $news = Model_News::find_one_by_id($id); \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('news', $news); }