2019-03-16 09:37:35 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-03-04 14:32:58 +00:00
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2007-03-12 17:59:02 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-30 14:13:52 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-08-30 14:13:52 +00:00
|
|
|
#
|
2007-03-12 17:59:02 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
class NewsController < ApplicationController
|
2025-09-28 21:20:39 +00:00
|
|
|
self.model_object = News
|
|
|
|
|
|
2009-10-21 17:07:18 +00:00
|
|
|
default_search_scope :news
|
2016-07-14 07:27:31 +00:00
|
|
|
before_action :find_model_object, :except => [:new, :create, :index]
|
|
|
|
|
before_action :find_project_from_association, :except => [:new, :create, :index]
|
2021-03-25 05:16:54 +00:00
|
|
|
before_action :find_project_by_project_id, :only => :create
|
|
|
|
|
before_action :authorize, :except => [:index, :new]
|
|
|
|
|
before_action :find_optional_project, :only => [:index, :new]
|
2022-03-19 09:56:46 +00:00
|
|
|
accept_atom_auth :index
|
2019-09-09 09:03:41 +00:00
|
|
|
accept_api_auth :index, :show, :create, :update, :destroy
|
2011-08-30 14:13:52 +00:00
|
|
|
|
2011-03-04 14:32:58 +00:00
|
|
|
helper :watchers
|
2012-01-29 22:39:06 +00:00
|
|
|
helper :attachments
|
2011-08-30 14:13:52 +00:00
|
|
|
|
2007-11-05 22:22:51 +00:00
|
|
|
def index
|
2010-12-12 17:00:52 +00:00
|
|
|
case params[:format]
|
|
|
|
|
when 'xml', 'json'
|
|
|
|
|
@offset, @limit = api_offset_and_limit
|
|
|
|
|
else
|
|
|
|
|
@limit = 10
|
|
|
|
|
end
|
2011-08-30 14:13:52 +00:00
|
|
|
|
2010-12-12 17:00:52 +00:00
|
|
|
scope = @project ? @project.news.visible : News.visible
|
2011-08-30 14:13:52 +00:00
|
|
|
|
2010-12-12 17:00:52 +00:00
|
|
|
@news_count = scope.count
|
2013-01-08 20:18:12 +00:00
|
|
|
@news_pages = Paginator.new @news_count, @limit, params['page']
|
|
|
|
|
@offset ||= @news_pages.offset
|
2014-01-10 08:13:47 +00:00
|
|
|
@newss = scope.includes([:author, :project]).
|
|
|
|
|
order("#{News.table_name}.created_on DESC").
|
|
|
|
|
limit(@limit).
|
|
|
|
|
offset(@offset).
|
2014-10-22 17:37:16 +00:00
|
|
|
to_a
|
2007-11-05 22:22:51 +00:00
|
|
|
respond_to do |format|
|
2020-10-22 14:17:28 +00:00
|
|
|
format.html do
|
2011-11-23 20:09:57 +00:00
|
|
|
@news = News.new # for adding news inline
|
|
|
|
|
render :layout => false if request.xhr?
|
2020-10-22 14:17:28 +00:00
|
|
|
end
|
2010-12-12 17:00:52 +00:00
|
|
|
format.api
|
2020-10-22 14:17:28 +00:00
|
|
|
format.atom do
|
|
|
|
|
render_feed(
|
|
|
|
|
@newss,
|
|
|
|
|
:title =>
|
|
|
|
|
(@project ? @project.name : Setting.app_title) +
|
|
|
|
|
": #{l(:label_news_plural)}"
|
|
|
|
|
)
|
|
|
|
|
end
|
2007-11-05 22:22:51 +00:00
|
|
|
end
|
|
|
|
|
end
|
2011-08-30 14:13:52 +00:00
|
|
|
|
2006-06-28 18:11:03 +00:00
|
|
|
def show
|
2025-05-11 07:59:16 +00:00
|
|
|
@comments = @news.comments.preload(:commented).to_a
|
2008-03-05 15:41:54 +00:00
|
|
|
@comments.reverse! if User.current.wants_comments_in_reverse_order?
|
2025-05-11 07:59:16 +00:00
|
|
|
|
|
|
|
|
Comment.preload_reaction_details(@comments)
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
|
|
|
|
|
2008-02-03 14:38:04 +00:00
|
|
|
def new
|
2021-03-25 05:16:54 +00:00
|
|
|
raise ::Unauthorized unless User.current.allowed_to?(:manage_news, @project, :global => true)
|
|
|
|
|
|
2008-02-03 14:38:04 +00:00
|
|
|
@news = News.new(:project => @project, :author => User.current)
|
2010-09-20 15:13:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
@news = News.new(:project => @project, :author => User.current)
|
2012-03-06 19:50:10 +00:00
|
|
|
@news.safe_attributes = params[:news]
|
2019-09-09 08:52:13 +00:00
|
|
|
@news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
|
2012-02-23 13:05:10 +00:00
|
|
|
if @news.save
|
2019-09-09 08:52:13 +00:00
|
|
|
respond_to do |format|
|
2020-11-08 13:02:19 +00:00
|
|
|
format.html do
|
2019-09-09 08:52:13 +00:00
|
|
|
render_attachment_warning_if_needed(@news)
|
|
|
|
|
flash[:notice] = l(:notice_successful_create)
|
2021-03-25 05:16:54 +00:00
|
|
|
redirect_to params[:cross_project] ? news_index_path : project_news_index_path(@project)
|
2020-11-08 13:02:19 +00:00
|
|
|
end
|
2020-11-22 12:58:46 +00:00
|
|
|
format.api {render_api_ok}
|
2019-09-09 08:52:13 +00:00
|
|
|
end
|
2012-02-23 13:05:10 +00:00
|
|
|
else
|
2019-09-09 08:52:13 +00:00
|
|
|
respond_to do |format|
|
2020-11-22 12:58:46 +00:00
|
|
|
format.html {render :action => 'new'}
|
|
|
|
|
format.api {render_validation_errors(@news)}
|
2019-09-09 08:52:13 +00:00
|
|
|
end
|
2008-02-03 14:38:04 +00:00
|
|
|
end
|
|
|
|
|
end
|
2010-09-21 15:20:37 +00:00
|
|
|
|
2006-06-28 18:11:03 +00:00
|
|
|
def edit
|
2010-09-21 15:20:37 +00:00
|
|
|
end
|
2011-08-30 14:13:52 +00:00
|
|
|
|
2010-09-21 15:20:37 +00:00
|
|
|
def update
|
2012-03-06 19:50:10 +00:00
|
|
|
@news.safe_attributes = params[:news]
|
2019-09-09 09:03:41 +00:00
|
|
|
@news.save_attachments(params[:attachments] || (params[:news] && params[:news][:uploads]))
|
2012-03-06 19:50:10 +00:00
|
|
|
if @news.save
|
2019-09-09 09:03:41 +00:00
|
|
|
respond_to do |format|
|
2020-11-08 13:02:19 +00:00
|
|
|
format.html do
|
2019-09-09 09:03:41 +00:00
|
|
|
render_attachment_warning_if_needed(@news)
|
|
|
|
|
flash[:notice] = l(:notice_successful_update)
|
|
|
|
|
redirect_to news_path(@news)
|
2020-11-08 13:02:19 +00:00
|
|
|
end
|
2020-11-22 12:58:46 +00:00
|
|
|
format.api {render_api_ok}
|
2019-09-09 09:03:41 +00:00
|
|
|
end
|
2010-09-21 15:20:37 +00:00
|
|
|
else
|
2019-09-09 09:03:41 +00:00
|
|
|
respond_to do |format|
|
2020-11-22 12:58:46 +00:00
|
|
|
format.html {render :action => 'edit'}
|
|
|
|
|
format.api {render_validation_errors(@news)}
|
2019-09-09 09:03:41 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
|
|
|
|
end
|
2010-09-21 15:20:37 +00:00
|
|
|
|
2007-01-28 00:00:21 +00:00
|
|
|
def destroy
|
|
|
|
|
@news.destroy
|
2019-09-09 08:57:29 +00:00
|
|
|
respond_to do |format|
|
2020-11-08 13:02:19 +00:00
|
|
|
format.html do
|
2020-04-04 00:43:28 +00:00
|
|
|
flash[:notice] = l(:notice_successful_delete)
|
|
|
|
|
redirect_to project_news_index_path(@project)
|
2020-11-08 13:02:19 +00:00
|
|
|
end
|
2020-11-22 12:58:46 +00:00
|
|
|
format.api {render_api_ok}
|
2019-09-09 08:57:29 +00:00
|
|
|
end
|
2007-03-12 17:59:02 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|