Beispiel #1
0
 /**
  * News preview
  * 
  * @access  public
  * @params  integer
  * @return  Response
  */
 public function action_preview($slug = null)
 {
     if (!($news = Model_News::get4slug($slug, false))) {
         throw new \HttpNotFoundException();
     }
     $token = \Input::get('token');
     if (!$token || $token != $news->token) {
         throw new \HttpNotFoundException();
     }
     $images = \Config::get('news.image.isEnabled') ? \News\Model_NewsImage::get4news_id($news->id) : array();
     $files = \Config::get('news.file.isEnabled') ? \News\Model_NewsFile::get4news_id($news->id) : array();
     $tags = \Config::get('news.tags.isEnabled') ? \News\Model_NewsTag::get_names4news_id($news->id) : array();
     $title = array('name' => $news->title);
     $header_info = self::get_prview_header_info($news->is_published, $news->published_at);
     $this->set_title_and_breadcrumbs($title, null, null, null, $header_info, true);
     $this->template->subtitle = \View::forge('_parts/news_subinfo', array('news' => $news));
     $this->template->content = \View::forge('detail', array('news' => $news, 'images' => $images, 'files' => $files, 'tags' => $tags));
     if (Site_Util::check_editor_enabled()) {
         $this->template->content->set_safe('html_body', $news->body);
     }
 }
Beispiel #2
0
 /**
  * News edit
  * 
  * @access  public
  * @params  integer
  * @return  Response
  */
 public function action_edit($id = null)
 {
     $news = \News\Model_News::check_authority($id);
     $val = self::get_validation_object($news);
     $news_images = array();
     $news_files = array();
     $images = array();
     $files = array();
     $is_enabled_image = conf('image.isEnabled', 'news');
     $is_insert_body_image = conf('image.isInsertBody', 'news');
     $is_modal_upload_image = conf('image.isModalUpload', 'news');
     if ($is_enabled_image && !$is_modal_upload_image) {
         $news_images = \News\Model_NewsImage::get4news_id($news->id);
         $images = \Site_Upload::get_file_objects($news_images, $news->id, true, null, 'img', $is_insert_body_image);
     }
     if ($is_enabled_file = \Config::get('news.file.isEnabled')) {
         $news_files = \News\Model_NewsFile::get4news_id($news->id);
         $files = \Site_Upload::get_file_objects($news_files, $news->id, true, null, 'file');
     }
     $posted_links = array();
     $saved_links = array();
     if ($is_enabled_link = \Config::get('news.link.isEnabled')) {
         $saved_links = $this->get_saved_links($news->id);
     }
     $tags = \Config::get('news.tags.isEnabled') ? \News\Model_NewsTag::get_names4news_id($news->id) : array();
     $image_tmps = array();
     $file_tmps = array();
     if (\Input::method() == 'POST') {
         \Util_security::check_csrf();
         if ($is_enabled_link) {
             $posted_links = $this->get_posted_links();
             $val = $this->add_validation_object_posted_links($val, $saved_links, true);
             $val = $this->add_validation_object_posted_links($val, $posted_links);
         }
         $moved_images = array();
         $moved_files = array();
         $news_image_ids = array();
         $news_file_ids = array();
         $error_message = '';
         try {
             if ($is_enabled_image) {
                 $image_tmps = \Site_FileTmp::get_file_tmps_and_check_filesize();
             }
             if ($is_enabled_file) {
                 $file_tmps = \Site_FileTmp::get_file_tmps_and_check_filesize(null, null, 'file');
             }
             // 識別名の変更がない場合は unique を確認しない
             if (trim(\Input::post('slug')) == $news->slug) {
                 $val->fieldset()->field('slug')->delete_rule('unique');
             }
             if (!$val->run()) {
                 throw new \FuelException($val->show_errors());
             }
             $post = $val->validated();
             if ($post['format'] == 2) {
                 $post['body'] = preg_replace('/^\\>/um', '>', $post['body']);
             }
             $news->set_values($post);
             $message = sprintf('%sを%sしました。', term('news.view'), term('form.edit'));
             if ($is_published = !$news->is_published && empty($post['is_draft'])) {
                 $news->is_published = 1;
                 $message = sprintf('%sを%sしました。', term('news.view'), term('form.publish'));
             }
             if ($post['published_at_time'] && !\Util_Date::check_is_same_minute($post['published_at_time'], $news->published_at)) {
                 $news->published_at = $post['published_at_time'] . ':00';
             } elseif ($is_published) {
                 $news->published_at = date('Y-m-d H:i:s');
             }
             \DB::start_transaction();
             $news->save();
             if ($is_enabled_image) {
                 list($moved_images, $news_image_ids) = \Site_FileTmp::save_images($image_tmps, $news->id, 'news_id', 'news_image');
                 \Site_Upload::update_image_objs4file_objects($news_images, $images);
             }
             if ($is_enabled_file) {
                 list($moved_files, $news_file_ids) = \Site_FileTmp::save_images($file_tmps, $news->id, 'news_id', 'news_file', null, 'file');
                 \Site_Upload::update_image_objs4file_objects($news_files, $files);
             }
             if ($is_enabled_link) {
                 $this->save_posted_links($saved_links, $news->id, true);
                 $this->save_posted_links($posted_links, $news->id);
             }
             if (\Config::get('news.tags.isEnabled')) {
                 \News\Model_NewsTag::save_tags($post['tags'], $news->id);
             }
             //// timeline 投稿
             //if (is_enabled('timeline'))
             //{
             //	if ($is_published)
             //	{
             //		\Timeline\Site_Model::save_timeline($this->u->id, $note->public_flag, 'note', $note->id);
             //	}
             //	elseif ($is_update_public_flag)
             //	{
             //		// timeline の public_flag の更新
             //		\Timeline\Model_Timeline::update_public_flag4foreign_table_and_foreign_id($note->public_flag, 'note', $note->id, \Config::get('timeline.types.note'));
             //	}
             //}
             \DB::commit_transaction();
             // thumbnail 作成 & tmp_file thumbnail 削除
             \Site_FileTmp::make_and_remove_thumbnails($moved_images);
             \Session::set_flash('message', $message);
             \Response::redirect('admin/news/detail/' . $news->id);
         } catch (\Database_Exception $e) {
             $error_message = \Site_Controller::get_error_message($e, true);
         } catch (\FuelException $e) {
             $error_message = $e->getMessage();
         }
         if ($error_message) {
             if (\DB::in_transaction()) {
                 \DB::rollback_transaction();
             }
             if ($moved_images) {
                 \Site_FileTmp::move_files_to_tmp_dir($moved_images);
             }
             if ($moved_files) {
                 \Site_FileTmp::move_files_to_tmp_dir($moved_files);
             }
             $image_tmps = \Site_FileTmp::get_file_objects($image_tmps, $this->u->id, true, 'img');
             $file_tmps = \Site_FileTmp::get_file_objects($file_tmps, $this->u->id, true, 'file');
             \Session::set_flash('error', $error_message);
         }
     }
     $images = array_merge($images, $image_tmps);
     $files = array_merge($files, $file_tmps);
     $this->set_title_and_breadcrumbs(term('form.edit'), array('admin/news' => term('news.view', 'admin.view'), 'admin/news/' . $news->id => $news->title));
     $this->template->post_header = \View::forge('news/_parts/form_header');
     $this->template->post_footer = \View::forge('news/_parts/form_footer', array('news' => $news));
     $this->template->content = \View::forge('news/_parts/form', array('val' => $val, 'saved_links' => $saved_links, 'posted_links' => $posted_links, 'news' => $news, 'is_edit' => true, 'images' => $images, 'files' => $files, 'tags' => $tags));
 }