comparison app/controllers/.svn/text-base/projects_controller.rb.svn-base @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents 0c939c159af4
children
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
22 22
23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] 23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] 24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
25 before_filter :authorize_global, :only => [:new, :create] 25 before_filter :authorize_global, :only => [:new, :create]
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] 26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
27 accept_key_auth :index 27 accept_rss_auth :index
28 accept_api_auth :index, :show, :create, :update, :destroy
28 29
29 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| 30 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
30 if controller.request.post? 31 if controller.request.post?
31 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt' 32 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
32 end 33 end
33 end 34 end
34
35 # TODO: convert to PUT only
36 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
37 35
38 helper :sort 36 helper :sort
39 include SortHelper 37 include SortHelper
40 helper :custom_fields 38 helper :custom_fields
41 include CustomFieldsHelper 39 include CustomFieldsHelper
50 def index 48 def index
51 respond_to do |format| 49 respond_to do |format|
52 format.html { 50 format.html {
53 @projects = Project.visible.find(:all, :order => 'lft') 51 @projects = Project.visible.find(:all, :order => 'lft')
54 } 52 }
55 format.xml { 53 format.api {
56 @projects = Project.visible.find(:all, :order => 'lft') 54 @offset, @limit = api_offset_and_limit
55 @project_count = Project.visible.count
56 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
57 } 57 }
58 format.atom { 58 format.atom {
59 projects = Project.visible.find(:all, :order => 'created_on DESC', 59 projects = Project.visible.find(:all, :order => 'created_on DESC',
60 :limit => Setting.feeds_limit.to_i) 60 :limit => Setting.feeds_limit.to_i)
61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") 61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
65 65
66 def new 66 def new
67 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") 67 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
68 @trackers = Tracker.all 68 @trackers = Tracker.all
69 @project = Project.new(params[:project]) 69 @project = Project.new(params[:project])
70 70 end
71 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? 71
72 @project.trackers = Tracker.all 72 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
73 @project.is_public = Setting.default_projects_public?
74 @project.enabled_module_names = Setting.default_projects_modules
75 end
76
77 def create 73 def create
78 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") 74 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
79 @trackers = Tracker.all 75 @trackers = Tracker.all
80 @project = Project.new(params[:project]) 76 @project = Project.new
81 77 @project.safe_attributes = params[:project]
82 @project.enabled_module_names = params[:enabled_modules] 78
83 if validate_parent_id && @project.save 79 if validate_parent_id && @project.save
84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') 80 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
85 # Add current user as a project member if he is not admin 81 # Add current user as a project member if he is not admin
86 unless User.current.admin? 82 unless User.current.admin?
87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first 83 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
91 respond_to do |format| 87 respond_to do |format|
92 format.html { 88 format.html {
93 flash[:notice] = l(:notice_successful_create) 89 flash[:notice] = l(:notice_successful_create)
94 redirect_to :controller => 'projects', :action => 'settings', :id => @project 90 redirect_to :controller => 'projects', :action => 'settings', :id => @project
95 } 91 }
96 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } 92 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
97 end 93 end
98 else 94 else
99 respond_to do |format| 95 respond_to do |format|
100 format.html { render :action => 'new' } 96 format.html { render :action => 'new' }
101 format.xml { render :xml => @project.errors, :status => :unprocessable_entity } 97 format.api { render_validation_errors(@project) }
102 end 98 end
103 end 99 end
104 100
105 end 101 end
106 102
118 else 114 else
119 redirect_to :controller => 'admin', :action => 'projects' 115 redirect_to :controller => 'admin', :action => 'projects'
120 end 116 end
121 else 117 else
122 Mailer.with_deliveries(params[:notifications] == '1') do 118 Mailer.with_deliveries(params[:notifications] == '1') do
123 @project = Project.new(params[:project]) 119 @project = Project.new
124 @project.enabled_module_names = params[:enabled_modules] 120 @project.safe_attributes = params[:project]
125 if validate_parent_id && @project.copy(@source_project, :only => params[:only]) 121 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
126 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') 122 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
127 flash[:notice] = l(:notice_successful_create) 123 flash[:notice] = l(:notice_successful_create)
128 redirect_to :controller => 'projects', :action => 'settings' 124 redirect_to :controller => 'projects', :action => 'settings', :id => @project
129 elsif !@project.new_record? 125 elsif !@project.new_record?
130 # Project was created 126 # Project was created
131 # But some objects were not copied due to validation failures 127 # But some objects were not copied due to validation failures
132 # (eg. issues from disabled trackers) 128 # (eg. issues from disabled trackers)
133 # TODO: inform about that 129 # TODO: inform about that
134 redirect_to :controller => 'projects', :action => 'settings' 130 redirect_to :controller => 'projects', :action => 'settings', :id => @project
135 end 131 end
136 end 132 end
137 end 133 end
138 rescue ActiveRecord::RecordNotFound 134 rescue ActiveRecord::RecordNotFound
139 redirect_to :controller => 'admin', :action => 'projects' 135 redirect_to :controller => 'admin', :action => 'projects'
145 # try to redirect to the requested menu item 141 # try to redirect to the requested menu item
146 redirect_to_project_menu_item(@project, params[:jump]) && return 142 redirect_to_project_menu_item(@project, params[:jump]) && return
147 end 143 end
148 144
149 @users_by_role = @project.users_by_role 145 @users_by_role = @project.users_by_role
150 @subprojects = @project.children.visible 146 @subprojects = @project.children.visible.all
151 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") 147 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
152 @trackers = @project.rolled_up_trackers 148 @trackers = @project.rolled_up_trackers
153 149
154 cond = @project.project_condition(Setting.display_subprojects_issues?) 150 cond = @project.project_condition(Setting.display_subprojects_issues?)
155 151
158 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) 154 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
159 @total_issues_by_tracker = Issue.visible.count(:group => :tracker, 155 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
160 :include => [:project, :status, :tracker], 156 :include => [:project, :status, :tracker],
161 :conditions => cond) 157 :conditions => cond)
162 158
163 TimeEntry.visible_by(User.current) do 159 if User.current.allowed_to?(:view_time_entries, @project)
164 @total_hours = TimeEntry.sum(:hours, 160 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
165 :include => :project, 161 end
166 :conditions => cond).to_f 162
167 end
168 @key = User.current.rss_key 163 @key = User.current.rss_key
169 164
170 respond_to do |format| 165 respond_to do |format|
171 format.html 166 format.html
172 format.xml 167 format.api
173 end 168 end
174 end 169 end
175 170
176 def settings 171 def settings
177 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") 172 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
183 end 178 end
184 179
185 def edit 180 def edit
186 end 181 end
187 182
183 # TODO: convert to PUT only
184 verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
188 def update 185 def update
189 @project.attributes = params[:project] 186 @project.safe_attributes = params[:project]
190 if validate_parent_id && @project.save 187 if validate_parent_id && @project.save
191 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') 188 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
192 respond_to do |format| 189 respond_to do |format|
193 format.html { 190 format.html {
194 flash[:notice] = l(:notice_successful_update) 191 flash[:notice] = l(:notice_successful_update)
195 redirect_to :action => 'settings', :id => @project 192 redirect_to :action => 'settings', :id => @project
196 } 193 }
197 format.xml { head :ok } 194 format.api { head :ok }
198 end 195 end
199 else 196 else
200 respond_to do |format| 197 respond_to do |format|
201 format.html { 198 format.html {
202 settings 199 settings
203 render :action => 'settings' 200 render :action => 'settings'
204 } 201 }
205 format.xml { render :xml => @project.errors, :status => :unprocessable_entity } 202 format.api { render_validation_errors(@project) }
206 end 203 end
207 end 204 end
208 end 205 end
209 206
207 verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed }
210 def modules 208 def modules
211 @project.enabled_module_names = params[:enabled_modules] 209 @project.enabled_module_names = params[:enabled_module_names]
212 flash[:notice] = l(:notice_successful_update) 210 flash[:notice] = l(:notice_successful_update)
213 redirect_to :action => 'settings', :id => @project, :tab => 'modules' 211 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
214 end 212 end
215 213
216 def archive 214 def archive
231 def destroy 229 def destroy
232 @project_to_destroy = @project 230 @project_to_destroy = @project
233 if request.get? 231 if request.get?
234 # display confirmation view 232 # display confirmation view
235 else 233 else
236 if params[:format] == 'xml' || params[:confirm] 234 if api_request? || params[:confirm]
237 @project_to_destroy.destroy 235 @project_to_destroy.destroy
238 respond_to do |format| 236 respond_to do |format|
239 format.html { redirect_to :controller => 'admin', :action => 'projects' } 237 format.html { redirect_to :controller => 'admin', :action => 'projects' }
240 format.xml { head :ok } 238 format.api { head :ok }
241 end 239 end
242 end 240 end
243 end 241 end
244 # hide project in layout 242 # hide project in layout
245 @project = nil 243 @project = nil