Chris@0
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 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 ProjectsController < ApplicationController
|
Chris@0
|
19 menu_item :overview
|
Chris@0
|
20 menu_item :roadmap, :only => :roadmap
|
Chris@0
|
21 menu_item :settings, :only => :settings
|
Chris@909
|
22
|
chris@22
|
23 before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ]
|
chris@22
|
24 before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
|
chris@22
|
25 before_filter :authorize_global, :only => [:new, :create]
|
Chris@0
|
26 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
|
Chris@507
|
27 accept_rss_auth :index
|
Chris@507
|
28 accept_api_auth :index, :show, :create, :update, :destroy
|
chris@22
|
29
|
chris@22
|
30 after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller|
|
Chris@0
|
31 if controller.request.post?
|
Chris@1115
|
32 controller.send :expire_action, :controller => 'welcome', :action => 'robots'
|
Chris@0
|
33 end
|
Chris@0
|
34 end
|
chris@22
|
35
|
Chris@0
|
36 helper :sort
|
Chris@0
|
37 include SortHelper
|
Chris@0
|
38 helper :custom_fields
|
Chris@909
|
39 include CustomFieldsHelper
|
Chris@0
|
40 helper :issues
|
Chris@0
|
41 helper :queries
|
Chris@0
|
42 include QueriesHelper
|
Chris@0
|
43 helper :repositories
|
Chris@0
|
44 include RepositoriesHelper
|
Chris@0
|
45 include ProjectsHelper
|
Chris@1464
|
46 helper :members
|
Chris@909
|
47
|
Chris@0
|
48 # Lists visible projects
|
Chris@0
|
49 def index
|
Chris@0
|
50 respond_to do |format|
|
Chris@909
|
51 format.html {
|
Chris@1115
|
52 scope = Project
|
Chris@1115
|
53 unless params[:closed]
|
Chris@1115
|
54 scope = scope.active
|
Chris@1115
|
55 end
|
Chris@1115
|
56 @projects = scope.visible.order('lft').all
|
Chris@0
|
57 }
|
Chris@119
|
58 format.api {
|
Chris@119
|
59 @offset, @limit = api_offset_and_limit
|
Chris@119
|
60 @project_count = Project.visible.count
|
Chris@1464
|
61 @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all
|
Chris@0
|
62 }
|
Chris@0
|
63 format.atom {
|
Chris@1464
|
64 projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
|
Chris@0
|
65 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
|
Chris@0
|
66 }
|
Chris@0
|
67 end
|
Chris@0
|
68 end
|
Chris@909
|
69
|
chris@22
|
70 def new
|
Chris@1464
|
71 @issue_custom_fields = IssueCustomField.sorted.all
|
Chris@1115
|
72 @trackers = Tracker.sorted.all
|
Chris@929
|
73 @project = Project.new
|
Chris@929
|
74 @project.safe_attributes = params[:project]
|
chris@22
|
75 end
|
chris@22
|
76
|
chris@22
|
77 def create
|
Chris@1464
|
78 @issue_custom_fields = IssueCustomField.sorted.all
|
Chris@1115
|
79 @trackers = Tracker.sorted.all
|
Chris@119
|
80 @project = Project.new
|
Chris@119
|
81 @project.safe_attributes = params[:project]
|
chris@22
|
82
|
chris@22
|
83 if validate_parent_id && @project.save
|
chris@22
|
84 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
|
Chris@1464
|
85 # Add current user as a project member if current user is not admin
|
chris@22
|
86 unless User.current.admin?
|
chris@22
|
87 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
|
chris@22
|
88 m = Member.new(:user => User.current, :roles => [r])
|
chris@22
|
89 @project.members << m
|
chris@22
|
90 end
|
chris@22
|
91 respond_to do |format|
|
Chris@909
|
92 format.html {
|
chris@22
|
93 flash[:notice] = l(:notice_successful_create)
|
Chris@1464
|
94 if params[:continue]
|
Chris@1464
|
95 attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
|
Chris@1464
|
96 redirect_to new_project_path(attrs)
|
Chris@1464
|
97 else
|
Chris@1464
|
98 redirect_to settings_project_path(@project)
|
Chris@1464
|
99 end
|
chris@22
|
100 }
|
Chris@119
|
101 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
|
chris@22
|
102 end
|
Chris@0
|
103 else
|
chris@22
|
104 respond_to do |format|
|
chris@22
|
105 format.html { render :action => 'new' }
|
Chris@119
|
106 format.api { render_validation_errors(@project) }
|
Chris@0
|
107 end
|
chris@22
|
108 end
|
Chris@0
|
109 end
|
Chris@909
|
110
|
Chris@0
|
111 def copy
|
Chris@1464
|
112 @issue_custom_fields = IssueCustomField.sorted.all
|
Chris@1115
|
113 @trackers = Tracker.sorted.all
|
Chris@0
|
114 @source_project = Project.find(params[:id])
|
Chris@0
|
115 if request.get?
|
Chris@0
|
116 @project = Project.copy_from(@source_project)
|
Chris@1115
|
117 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
|
Chris@0
|
118 else
|
Chris@0
|
119 Mailer.with_deliveries(params[:notifications] == '1') do
|
Chris@119
|
120 @project = Project.new
|
Chris@119
|
121 @project.safe_attributes = params[:project]
|
Chris@0
|
122 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
|
Chris@0
|
123 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
|
Chris@0
|
124 flash[:notice] = l(:notice_successful_create)
|
Chris@1464
|
125 redirect_to settings_project_path(@project)
|
Chris@0
|
126 elsif !@project.new_record?
|
Chris@0
|
127 # Project was created
|
Chris@0
|
128 # But some objects were not copied due to validation failures
|
Chris@0
|
129 # (eg. issues from disabled trackers)
|
Chris@0
|
130 # TODO: inform about that
|
Chris@1464
|
131 redirect_to settings_project_path(@project)
|
Chris@0
|
132 end
|
Chris@0
|
133 end
|
Chris@0
|
134 end
|
Chris@0
|
135 rescue ActiveRecord::RecordNotFound
|
Chris@1115
|
136 # source_project not found
|
Chris@1115
|
137 render_404
|
Chris@0
|
138 end
|
Chris@1464
|
139
|
Chris@0
|
140 # Show @project
|
Chris@0
|
141 def show
|
Chris@1464
|
142 # try to redirect to the requested menu item
|
Chris@1464
|
143 if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
|
Chris@1464
|
144 return
|
Chris@0
|
145 end
|
Chris@909
|
146
|
Chris@0
|
147 @users_by_role = @project.users_by_role
|
Chris@441
|
148 @subprojects = @project.children.visible.all
|
Chris@1464
|
149 @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all
|
Chris@0
|
150 @trackers = @project.rolled_up_trackers
|
Chris@909
|
151
|
Chris@0
|
152 cond = @project.project_condition(Setting.display_subprojects_issues?)
|
Chris@909
|
153
|
Chris@1115
|
154 @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker)
|
Chris@1115
|
155 @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker)
|
Chris@909
|
156
|
Chris@441
|
157 if User.current.allowed_to?(:view_time_entries, @project)
|
Chris@1464
|
158 @total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f
|
Chris@0
|
159 end
|
Chris@909
|
160
|
Chris@0
|
161 @key = User.current.rss_key
|
Chris@909
|
162
|
Chris@0
|
163 respond_to do |format|
|
Chris@0
|
164 format.html
|
Chris@119
|
165 format.api
|
Chris@0
|
166 end
|
Chris@0
|
167 end
|
Chris@0
|
168
|
Chris@0
|
169 def settings
|
Chris@1464
|
170 @issue_custom_fields = IssueCustomField.sorted.all
|
Chris@0
|
171 @issue_category ||= IssueCategory.new
|
Chris@0
|
172 @member ||= @project.members.new
|
Chris@1115
|
173 @trackers = Tracker.sorted.all
|
Chris@0
|
174 @wiki ||= @project.wiki
|
Chris@0
|
175 end
|
Chris@909
|
176
|
Chris@0
|
177 def edit
|
chris@22
|
178 end
|
chris@22
|
179
|
chris@22
|
180 def update
|
Chris@119
|
181 @project.safe_attributes = params[:project]
|
chris@22
|
182 if validate_parent_id && @project.save
|
chris@22
|
183 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
|
chris@22
|
184 respond_to do |format|
|
Chris@909
|
185 format.html {
|
chris@22
|
186 flash[:notice] = l(:notice_successful_update)
|
Chris@1464
|
187 redirect_to settings_project_path(@project)
|
chris@22
|
188 }
|
Chris@1115
|
189 format.api { render_api_ok }
|
chris@22
|
190 end
|
Chris@0
|
191 else
|
chris@22
|
192 respond_to do |format|
|
Chris@909
|
193 format.html {
|
chris@22
|
194 settings
|
chris@22
|
195 render :action => 'settings'
|
chris@22
|
196 }
|
Chris@119
|
197 format.api { render_validation_errors(@project) }
|
Chris@0
|
198 end
|
Chris@0
|
199 end
|
Chris@0
|
200 end
|
Chris@119
|
201
|
Chris@0
|
202 def modules
|
Chris@119
|
203 @project.enabled_module_names = params[:enabled_module_names]
|
Chris@0
|
204 flash[:notice] = l(:notice_successful_update)
|
Chris@1464
|
205 redirect_to settings_project_path(@project, :tab => 'modules')
|
Chris@0
|
206 end
|
Chris@0
|
207
|
Chris@0
|
208 def archive
|
Chris@0
|
209 if request.post?
|
Chris@0
|
210 unless @project.archive
|
Chris@0
|
211 flash[:error] = l(:error_can_not_archive_project)
|
Chris@0
|
212 end
|
Chris@0
|
213 end
|
Chris@1464
|
214 redirect_to admin_projects_path(:status => params[:status])
|
Chris@0
|
215 end
|
Chris@909
|
216
|
Chris@0
|
217 def unarchive
|
Chris@0
|
218 @project.unarchive if request.post? && !@project.active?
|
Chris@1464
|
219 redirect_to admin_projects_path(:status => params[:status])
|
Chris@0
|
220 end
|
Chris@909
|
221
|
Chris@1115
|
222 def close
|
Chris@1115
|
223 @project.close
|
Chris@1115
|
224 redirect_to project_path(@project)
|
Chris@1115
|
225 end
|
Chris@1115
|
226
|
Chris@1115
|
227 def reopen
|
Chris@1115
|
228 @project.reopen
|
Chris@1115
|
229 redirect_to project_path(@project)
|
Chris@1115
|
230 end
|
Chris@1115
|
231
|
Chris@0
|
232 # Delete @project
|
Chris@0
|
233 def destroy
|
Chris@0
|
234 @project_to_destroy = @project
|
Chris@1115
|
235 if api_request? || params[:confirm]
|
Chris@1115
|
236 @project_to_destroy.destroy
|
Chris@1115
|
237 respond_to do |format|
|
Chris@1464
|
238 format.html { redirect_to admin_projects_path }
|
Chris@1115
|
239 format.api { render_api_ok }
|
Chris@0
|
240 end
|
Chris@0
|
241 end
|
Chris@0
|
242 # hide project in layout
|
Chris@0
|
243 @project = nil
|
Chris@0
|
244 end
|
Chris@0
|
245
|
Chris@1115
|
246 private
|
Chris@0
|
247
|
Chris@0
|
248 # Validates parent_id param according to user's permissions
|
Chris@0
|
249 # TODO: move it to Project model in a validation that depends on User.current
|
Chris@0
|
250 def validate_parent_id
|
Chris@0
|
251 return true if User.current.admin?
|
Chris@0
|
252 parent_id = params[:project] && params[:project][:parent_id]
|
Chris@0
|
253 if parent_id || @project.new_record?
|
Chris@0
|
254 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
|
Chris@0
|
255 unless @project.allowed_parents.include?(parent)
|
Chris@0
|
256 @project.errors.add :parent_id, :invalid
|
Chris@0
|
257 return false
|
Chris@0
|
258 end
|
Chris@0
|
259 end
|
Chris@0
|
260 true
|
Chris@0
|
261 end
|
Chris@0
|
262 end
|