Chris@441
|
1 # Redmine - project management software
|
Chris@441
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class NewsController < ApplicationController
|
Chris@0
|
19 default_search_scope :news
|
Chris@0
|
20 model_object News
|
chris@37
|
21 before_filter :find_model_object, :except => [:new, :create, :index]
|
chris@37
|
22 before_filter :find_project_from_association, :except => [:new, :create, :index]
|
chris@37
|
23 before_filter :find_project, :only => [:new, :create]
|
chris@37
|
24 before_filter :authorize, :except => [:index]
|
Chris@0
|
25 before_filter :find_optional_project, :only => :index
|
Chris@507
|
26 accept_rss_auth :index
|
Chris@507
|
27 accept_api_auth :index
|
Chris@909
|
28
|
Chris@441
|
29 helper :watchers
|
Chris@909
|
30
|
Chris@0
|
31 def index
|
Chris@119
|
32 case params[:format]
|
Chris@119
|
33 when 'xml', 'json'
|
Chris@119
|
34 @offset, @limit = api_offset_and_limit
|
Chris@119
|
35 else
|
Chris@119
|
36 @limit = 10
|
Chris@119
|
37 end
|
Chris@909
|
38
|
Chris@119
|
39 scope = @project ? @project.news.visible : News.visible
|
Chris@909
|
40
|
Chris@119
|
41 @news_count = scope.count
|
Chris@119
|
42 @news_pages = Paginator.new self, @news_count, @limit, params['page']
|
Chris@119
|
43 @offset ||= @news_pages.current.offset
|
Chris@119
|
44 @newss = scope.all(:include => [:author, :project],
|
Chris@119
|
45 :order => "#{News.table_name}.created_on DESC",
|
Chris@119
|
46 :offset => @offset,
|
Chris@119
|
47 :limit => @limit)
|
Chris@909
|
48
|
Chris@0
|
49 respond_to do |format|
|
Chris@909
|
50 format.html {
|
Chris@909
|
51 @news = News.new # for adding news inline
|
Chris@909
|
52 render :layout => false if request.xhr?
|
Chris@909
|
53 }
|
Chris@119
|
54 format.api
|
Chris@0
|
55 format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
|
Chris@0
|
56 end
|
Chris@0
|
57 end
|
Chris@909
|
58
|
Chris@0
|
59 def show
|
Chris@0
|
60 @comments = @news.comments
|
Chris@0
|
61 @comments.reverse! if User.current.wants_comments_in_reverse_order?
|
Chris@0
|
62 end
|
Chris@0
|
63
|
Chris@0
|
64 def new
|
Chris@0
|
65 @news = News.new(:project => @project, :author => User.current)
|
chris@22
|
66 end
|
chris@22
|
67
|
chris@22
|
68 def create
|
chris@22
|
69 @news = News.new(:project => @project, :author => User.current)
|
Chris@929
|
70 @news.safe_attributes = params[:news]
|
Chris@0
|
71 if request.post?
|
Chris@0
|
72 if @news.save
|
Chris@0
|
73 flash[:notice] = l(:notice_successful_create)
|
Chris@0
|
74 redirect_to :controller => 'news', :action => 'index', :project_id => @project
|
chris@22
|
75 else
|
chris@22
|
76 render :action => 'new'
|
Chris@0
|
77 end
|
Chris@0
|
78 end
|
Chris@0
|
79 end
|
chris@22
|
80
|
chris@22
|
81 def edit
|
chris@22
|
82 end
|
Chris@909
|
83
|
chris@22
|
84 def update
|
Chris@929
|
85 @news.safe_attributes = params[:news]
|
Chris@929
|
86 if request.put? and @news.save
|
Chris@0
|
87 flash[:notice] = l(:notice_successful_update)
|
Chris@0
|
88 redirect_to :action => 'show', :id => @news
|
chris@22
|
89 else
|
chris@22
|
90 render :action => 'edit'
|
Chris@0
|
91 end
|
Chris@0
|
92 end
|
Chris@0
|
93
|
Chris@0
|
94 def destroy
|
Chris@0
|
95 @news.destroy
|
Chris@0
|
96 redirect_to :action => 'index', :project_id => @project
|
Chris@0
|
97 end
|
Chris@909
|
98
|
Chris@0
|
99 private
|
Chris@0
|
100 def find_project
|
Chris@0
|
101 @project = Project.find(params[:project_id])
|
Chris@0
|
102 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
103 render_404
|
Chris@0
|
104 end
|
Chris@909
|
105
|
Chris@0
|
106 def find_optional_project
|
Chris@0
|
107 return true unless params[:project_id]
|
Chris@0
|
108 @project = Project.find(params[:project_id])
|
Chris@0
|
109 authorize
|
Chris@0
|
110 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
111 render_404
|
Chris@0
|
112 end
|
Chris@0
|
113 end
|