To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / controllers / projects_controller.rb @ 919:b03f28dd9026
History | View | Annotate | Download (11.2 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 441:cbce1fd3b1b7 | Chris | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 4 | # This program is free software; you can redistribute it and/or
|
||
| 5 | # modify it under the terms of the GNU General Public License
|
||
| 6 | # as published by the Free Software Foundation; either version 2
|
||
| 7 | # of the License, or (at your option) any later version.
|
||
| 8 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | # GNU General Public License for more details.
|
||
| 13 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 15 | # along with this program; if not, write to the Free Software
|
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 17 | |||
| 18 | class ProjectsController < ApplicationController |
||
| 19 | menu_item :overview
|
||
| 20 | menu_item :roadmap, :only => :roadmap |
||
| 21 | menu_item :settings, :only => :settings |
||
| 22 | 909:cbb26bc654de | Chris | |
| 23 | 22:40f7cfd4df19 | chris | before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] |
| 24 | before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] |
||
| 25 | before_filter :authorize_global, :only => [:new, :create] |
||
| 26 | 0:513646585e45 | Chris | before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] |
| 27 | 507:0c939c159af4 | Chris | accept_rss_auth :index
|
| 28 | accept_api_auth :index, :show, :create, :update, :destroy |
||
| 29 | 22:40f7cfd4df19 | chris | |
| 30 | after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| |
||
| 31 | 0:513646585e45 | Chris | if controller.request.post?
|
| 32 | controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt' |
||
| 33 | end
|
||
| 34 | end
|
||
| 35 | 22:40f7cfd4df19 | chris | |
| 36 | 0:513646585e45 | Chris | helper :sort
|
| 37 | include SortHelper
|
||
| 38 | helper :custom_fields
|
||
| 39 | 909:cbb26bc654de | Chris | include CustomFieldsHelper
|
| 40 | 0:513646585e45 | Chris | helper :issues
|
| 41 | helper :queries
|
||
| 42 | include QueriesHelper
|
||
| 43 | helper :repositories
|
||
| 44 | include RepositoriesHelper
|
||
| 45 | include ProjectsHelper
|
||
| 46 | 909:cbb26bc654de | Chris | |
| 47 | 205:05f9a2a9c753 | chris | # Lists visible projects. Paginator is for top-level projects only
|
| 48 | # (subprojects belong to them)
|
||
| 49 | 0:513646585e45 | Chris | def index |
| 50 | respond_to do |format|
|
||
| 51 | 909:cbb26bc654de | Chris | format.html {
|
| 52 | 205:05f9a2a9c753 | chris | sort_init 'name'
|
| 53 | sort_update %w(name lft created_on updated_on)
|
||
| 54 | 131:513e61d1b4da | chris | @limit = per_page_option
|
| 55 | 205:05f9a2a9c753 | chris | @project_count = Project.visible_roots.count |
| 56 | 131:513e61d1b4da | chris | @project_pages = Paginator.new self, @project_count, @limit, params['page'] |
| 57 | @offset ||= @project_pages.current.offset |
||
| 58 | 767:dd33798e514d | luis | @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause) |
| 59 | 131:513e61d1b4da | chris | if User.current.logged? |
| 60 | 418:a9921f3e9088 | chris | # seems sort_by gives us case-sensitive ordering, which we don't want
|
| 61 | # @user_projects = User.current.projects.sort_by(&:name)
|
||
| 62 | @user_projects = User.current.projects.all(:order => :name) |
||
| 63 | 131:513e61d1b4da | chris | end
|
| 64 | 919:b03f28dd9026 | Chris | render :template => 'projects/index.html.erb', :layout => !request.xhr? |
| 65 | 0:513646585e45 | Chris | } |
| 66 | 117:af80e5618e9b | Chris | format.api {
|
| 67 | @offset, @limit = api_offset_and_limit |
||
| 68 | @project_count = Project.visible.count |
||
| 69 | @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft') |
||
| 70 | 0:513646585e45 | Chris | } |
| 71 | format.atom {
|
||
| 72 | projects = Project.visible.find(:all, :order => 'created_on DESC', |
||
| 73 | :limit => Setting.feeds_limit.to_i) |
||
| 74 | render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") |
||
| 75 | } |
||
| 76 | end
|
||
| 77 | end
|
||
| 78 | 909:cbb26bc654de | Chris | |
| 79 | 22:40f7cfd4df19 | chris | def new |
| 80 | 0:513646585e45 | Chris | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
| 81 | @trackers = Tracker.all |
||
| 82 | @project = Project.new(params[:project]) |
||
| 83 | 22:40f7cfd4df19 | chris | end
|
| 84 | |||
| 85 | 117:af80e5618e9b | Chris | verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } |
| 86 | 22:40f7cfd4df19 | chris | def create |
| 87 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
||
| 88 | @trackers = Tracker.all |
||
| 89 | 117:af80e5618e9b | Chris | @project = Project.new |
| 90 | @project.safe_attributes = params[:project] |
||
| 91 | 22:40f7cfd4df19 | chris | |
| 92 | 831:7614169e14ed | luis | |
| 93 | # todo: luisf: this should be removed from here...
|
||
| 94 | if params && params[:project] && !params[:project][:tag_list].nil? |
||
| 95 | new_tags = params[:project][:tag_list].to_s.downcase |
||
| 96 | |||
| 97 | @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags) |
||
| 98 | end
|
||
| 99 | # end of code to be removed
|
||
| 100 | |||
| 101 | |||
| 102 | 22:40f7cfd4df19 | chris | if validate_parent_id && @project.save |
| 103 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
||
| 104 | # Add current user as a project member if he is not admin
|
||
| 105 | unless User.current.admin? |
||
| 106 | r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first |
||
| 107 | m = Member.new(:user => User.current, :roles => [r]) |
||
| 108 | @project.members << m
|
||
| 109 | end
|
||
| 110 | respond_to do |format|
|
||
| 111 | 909:cbb26bc654de | Chris | format.html {
|
| 112 | 22:40f7cfd4df19 | chris | flash[:notice] = l(:notice_successful_create) |
| 113 | 909:cbb26bc654de | Chris | redirect_to(params[:continue] ?
|
| 114 | {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
|
||
| 115 | {:controller => 'projects', :action => 'settings', :id => @project}
|
||
| 116 | ) |
||
| 117 | 22:40f7cfd4df19 | chris | } |
| 118 | 117:af80e5618e9b | Chris | format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
|
| 119 | 22:40f7cfd4df19 | chris | end
|
| 120 | 0:513646585e45 | Chris | else
|
| 121 | 22:40f7cfd4df19 | chris | respond_to do |format|
|
| 122 | format.html { render :action => 'new' }
|
||
| 123 | 117:af80e5618e9b | Chris | format.api { render_validation_errors(@project) }
|
| 124 | 0:513646585e45 | Chris | end
|
| 125 | 22:40f7cfd4df19 | chris | end
|
| 126 | 909:cbb26bc654de | Chris | |
| 127 | 0:513646585e45 | Chris | end
|
| 128 | 909:cbb26bc654de | Chris | |
| 129 | 0:513646585e45 | Chris | def copy |
| 130 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
||
| 131 | @trackers = Tracker.all |
||
| 132 | @root_projects = Project.find(:all, |
||
| 133 | :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}", |
||
| 134 | :order => 'name') |
||
| 135 | @source_project = Project.find(params[:id]) |
||
| 136 | if request.get?
|
||
| 137 | @project = Project.copy_from(@source_project) |
||
| 138 | if @project |
||
| 139 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? |
||
| 140 | else
|
||
| 141 | redirect_to :controller => 'admin', :action => 'projects' |
||
| 142 | 909:cbb26bc654de | Chris | end
|
| 143 | 0:513646585e45 | Chris | else
|
| 144 | Mailer.with_deliveries(params[:notifications] == '1') do |
||
| 145 | 117:af80e5618e9b | Chris | @project = Project.new |
| 146 | @project.safe_attributes = params[:project] |
||
| 147 | 0:513646585e45 | Chris | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) |
| 148 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
||
| 149 | flash[:notice] = l(:notice_successful_create) |
||
| 150 | 117:af80e5618e9b | Chris | redirect_to :controller => 'projects', :action => 'settings', :id => @project |
| 151 | 0:513646585e45 | Chris | elsif !@project.new_record? |
| 152 | # Project was created
|
||
| 153 | # But some objects were not copied due to validation failures
|
||
| 154 | # (eg. issues from disabled trackers)
|
||
| 155 | # TODO: inform about that
|
||
| 156 | 117:af80e5618e9b | Chris | redirect_to :controller => 'projects', :action => 'settings', :id => @project |
| 157 | 0:513646585e45 | Chris | end
|
| 158 | end
|
||
| 159 | end
|
||
| 160 | rescue ActiveRecord::RecordNotFound |
||
| 161 | redirect_to :controller => 'admin', :action => 'projects' |
||
| 162 | end
|
||
| 163 | |||
| 164 | # Show @project
|
||
| 165 | def show |
||
| 166 | if params[:jump] |
||
| 167 | # try to redirect to the requested menu item
|
||
| 168 | redirect_to_project_menu_item(@project, params[:jump]) && return |
||
| 169 | end
|
||
| 170 | 909:cbb26bc654de | Chris | |
| 171 | 0:513646585e45 | Chris | @users_by_role = @project.users_by_role |
| 172 | 441:cbce1fd3b1b7 | Chris | @subprojects = @project.children.visible.all |
| 173 | 0:513646585e45 | Chris | @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") |
| 174 | @trackers = @project.rolled_up_trackers |
||
| 175 | 909:cbb26bc654de | Chris | |
| 176 | 0:513646585e45 | Chris | cond = @project.project_condition(Setting.display_subprojects_issues?) |
| 177 | 909:cbb26bc654de | Chris | |
| 178 | 0:513646585e45 | Chris | @open_issues_by_tracker = Issue.visible.count(:group => :tracker, |
| 179 | :include => [:project, :status, :tracker], |
||
| 180 | :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false]) |
||
| 181 | @total_issues_by_tracker = Issue.visible.count(:group => :tracker, |
||
| 182 | :include => [:project, :status, :tracker], |
||
| 183 | :conditions => cond)
|
||
| 184 | 909:cbb26bc654de | Chris | |
| 185 | 441:cbce1fd3b1b7 | Chris | if User.current.allowed_to?(:view_time_entries, @project) |
| 186 | @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f |
||
| 187 | 0:513646585e45 | Chris | end
|
| 188 | 909:cbb26bc654de | Chris | |
| 189 | 0:513646585e45 | Chris | @key = User.current.rss_key |
| 190 | 909:cbb26bc654de | Chris | |
| 191 | 0:513646585e45 | Chris | respond_to do |format|
|
| 192 | format.html |
||
| 193 | 117:af80e5618e9b | Chris | format.api |
| 194 | 0:513646585e45 | Chris | end
|
| 195 | end
|
||
| 196 | |||
| 197 | def settings |
||
| 198 | @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") |
||
| 199 | @issue_category ||= IssueCategory.new |
||
| 200 | @member ||= @project.members.new |
||
| 201 | @trackers = Tracker.all |
||
| 202 | @repository ||= @project.repository |
||
| 203 | @wiki ||= @project.wiki |
||
| 204 | end
|
||
| 205 | 909:cbb26bc654de | Chris | |
| 206 | 0:513646585e45 | Chris | def edit |
| 207 | 22:40f7cfd4df19 | chris | end
|
| 208 | |||
| 209 | 117:af80e5618e9b | Chris | # TODO: convert to PUT only
|
| 210 | verify :method => [:post, :put], :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
||
| 211 | 22:40f7cfd4df19 | chris | def update |
| 212 | 117:af80e5618e9b | Chris | @project.safe_attributes = params[:project] |
| 213 | 22:40f7cfd4df19 | chris | if validate_parent_id && @project.save |
| 214 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
||
| 215 | respond_to do |format|
|
||
| 216 | 909:cbb26bc654de | Chris | format.html {
|
| 217 | 22:40f7cfd4df19 | chris | flash[:notice] = l(:notice_successful_update) |
| 218 | redirect_to :action => 'settings', :id => @project |
||
| 219 | } |
||
| 220 | 117:af80e5618e9b | Chris | format.api { head :ok }
|
| 221 | 22:40f7cfd4df19 | chris | end
|
| 222 | 0:513646585e45 | Chris | else
|
| 223 | 22:40f7cfd4df19 | chris | respond_to do |format|
|
| 224 | 909:cbb26bc654de | Chris | format.html {
|
| 225 | 22:40f7cfd4df19 | chris | settings |
| 226 | render :action => 'settings' |
||
| 227 | } |
||
| 228 | 117:af80e5618e9b | Chris | format.api { render_validation_errors(@project) }
|
| 229 | 0:513646585e45 | Chris | end
|
| 230 | end
|
||
| 231 | end
|
||
| 232 | 117:af80e5618e9b | Chris | |
| 233 | verify :method => :post, :only => :modules, :render => {:nothing => true, :status => :method_not_allowed } |
||
| 234 | 443:350acce374a2 | Chris | |
| 235 | 351:ebf53b46f3f3 | chris | def overview |
| 236 | @project.has_welcome_page = params[:has_welcome_page] |
||
| 237 | if @project.save |
||
| 238 | flash[:notice] = l(:notice_successful_update) |
||
| 239 | end
|
||
| 240 | redirect_to :action => 'settings', :id => @project, :tab => 'overview' |
||
| 241 | end
|
||
| 242 | |||
| 243 | 0:513646585e45 | Chris | def modules |
| 244 | 117:af80e5618e9b | Chris | @project.enabled_module_names = params[:enabled_module_names] |
| 245 | 0:513646585e45 | Chris | flash[:notice] = l(:notice_successful_update) |
| 246 | redirect_to :action => 'settings', :id => @project, :tab => 'modules' |
||
| 247 | end
|
||
| 248 | |||
| 249 | def archive |
||
| 250 | if request.post?
|
||
| 251 | unless @project.archive |
||
| 252 | flash[:error] = l(:error_can_not_archive_project) |
||
| 253 | end
|
||
| 254 | end
|
||
| 255 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
||
| 256 | end
|
||
| 257 | 909:cbb26bc654de | Chris | |
| 258 | 0:513646585e45 | Chris | def unarchive |
| 259 | @project.unarchive if request.post? && !@project.active? |
||
| 260 | redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status])) |
||
| 261 | end
|
||
| 262 | 909:cbb26bc654de | Chris | |
| 263 | 0:513646585e45 | Chris | # Delete @project
|
| 264 | def destroy |
||
| 265 | @project_to_destroy = @project |
||
| 266 | if request.get?
|
||
| 267 | # display confirmation view
|
||
| 268 | else
|
||
| 269 | 117:af80e5618e9b | Chris | if api_request? || params[:confirm] |
| 270 | 0:513646585e45 | Chris | @project_to_destroy.destroy
|
| 271 | respond_to do |format|
|
||
| 272 | format.html { redirect_to :controller => 'admin', :action => 'projects' }
|
||
| 273 | 117:af80e5618e9b | Chris | format.api { head :ok }
|
| 274 | 0:513646585e45 | Chris | end
|
| 275 | end
|
||
| 276 | end
|
||
| 277 | # hide project in layout
|
||
| 278 | @project = nil |
||
| 279 | end
|
||
| 280 | |||
| 281 | private |
||
| 282 | def find_optional_project |
||
| 283 | return true unless params[:id] |
||
| 284 | @project = Project.find(params[:id]) |
||
| 285 | authorize |
||
| 286 | rescue ActiveRecord::RecordNotFound |
||
| 287 | render_404 |
||
| 288 | end
|
||
| 289 | |||
| 290 | # Validates parent_id param according to user's permissions
|
||
| 291 | # TODO: move it to Project model in a validation that depends on User.current
|
||
| 292 | def validate_parent_id |
||
| 293 | return true if User.current.admin? |
||
| 294 | parent_id = params[:project] && params[:project][:parent_id] |
||
| 295 | if parent_id || @project.new_record? |
||
| 296 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) |
||
| 297 | unless @project.allowed_parents.include?(parent) |
||
| 298 | @project.errors.add :parent_id, :invalid |
||
| 299 | return false |
||
| 300 | end
|
||
| 301 | end
|
||
| 302 | true
|
||
| 303 | end
|
||
| 304 | end |