annotate app/controllers/projects_controller.rb @ 1158:00e9a262ffe5 redmine-2.2-integration

Fix Javascript so as to update star properly
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 15 Jan 2013 16:08:00 +0000
parents cd2db8872493
children 16826c3afbba
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@1009 23 before_filter :find_project, :except => [ :index, :list, :explore, :new, :create, :copy ]
chris@1009 24 before_filter :authorize, :except => [ :index, :list, :explore, :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@1011 46 include ActivitiesHelper
chris@1011 47 helper :activities
Chris@909 48
chris@205 49 # Lists visible projects. Paginator is for top-level projects only
chris@205 50 # (subprojects belong to them)
Chris@0 51 def index
Chris@0 52 respond_to do |format|
Chris@909 53 format.html {
chris@205 54 sort_init 'name'
chris@205 55 sort_update %w(name lft created_on updated_on)
chris@131 56 @limit = per_page_option
chris@205 57 @project_count = Project.visible_roots.count
chris@131 58 @project_pages = Paginator.new self, @project_count, @limit, params['page']
chris@131 59 @offset ||= @project_pages.current.offset
luis@1128 60 @projects = Project.visible_roots.all(:offset => @offset, :limit => @limit, :order => sort_clause)
chris@131 61 if User.current.logged?
chris@418 62 # seems sort_by gives us case-sensitive ordering, which we don't want
chris@418 63 # @user_projects = User.current.projects.sort_by(&:name)
chris@418 64 @user_projects = User.current.projects.all(:order => :name)
chris@131 65 end
Chris@919 66 render :template => 'projects/index.html.erb', :layout => !request.xhr?
Chris@1116 67
Chris@1116 68 ## Redmine 2.2:
Chris@1116 69 # scope = Project
Chris@1116 70 # unless params[:closed]
Chris@1116 71 # scope = scope.active
Chris@1116 72 # end
Chris@1116 73 # @projects = scope.visible.order('lft').all
Chris@0 74 }
Chris@117 75 format.api {
Chris@117 76 @offset, @limit = api_offset_and_limit
Chris@117 77 @project_count = Project.visible.count
Chris@117 78 @projects = Project.visible.all(:offset => @offset, :limit => @limit, :order => 'lft')
Chris@0 79 }
Chris@0 80 format.atom {
Chris@0 81 projects = Project.visible.find(:all, :order => 'created_on DESC',
Chris@0 82 :limit => Setting.feeds_limit.to_i)
Chris@0 83 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
Chris@0 84 }
Chris@0 85 end
Chris@0 86 end
Chris@909 87
chris@1009 88 # A different view of projects using explore boxes
chris@1009 89 def explore
chris@1007 90 respond_to do |format|
chris@1007 91 format.html {
chris@1007 92 @projects = Project.visible
chris@1009 93 render :template => 'projects/explore.html.erb', :layout => !request.xhr?
chris@1007 94 }
chris@1007 95 end
chris@1007 96 end
chris@1007 97
chris@22 98 def new
Chris@0 99 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@1115 100 @trackers = Tracker.sorted.all
Chris@929 101 @project = Project.new
Chris@929 102 @project.safe_attributes = params[:project]
chris@22 103 end
chris@22 104
chris@22 105 def create
chris@22 106 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@1115 107 @trackers = Tracker.sorted.all
Chris@117 108 @project = Project.new
Chris@117 109 @project.safe_attributes = params[:project]
chris@22 110
luis@831 111
luis@831 112 # todo: luisf: this should be removed from here...
luis@831 113 if params && params[:project] && !params[:project][:tag_list].nil?
luis@831 114 new_tags = params[:project][:tag_list].to_s.downcase
luis@831 115
luis@831 116 @project.tag_list = ActionController::Base.helpers.strip_tags(new_tags)
luis@831 117 end
luis@831 118 # end of code to be removed
luis@831 119
chris@1034 120 if validate_is_public_key && validate_parent_id && @project.save
chris@22 121 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 122 # Add current user as a project member if he is not admin
chris@22 123 unless User.current.admin?
chris@22 124 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
chris@22 125 m = Member.new(:user => User.current, :roles => [r])
chris@22 126 @project.members << m
chris@22 127 end
chris@22 128 respond_to do |format|
Chris@909 129 format.html {
chris@22 130 flash[:notice] = l(:notice_successful_create)
Chris@909 131 redirect_to(params[:continue] ?
Chris@909 132 {:controller => 'projects', :action => 'new', :project => {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}} :
Chris@909 133 {:controller => 'projects', :action => 'settings', :id => @project}
Chris@909 134 )
chris@22 135 }
Chris@117 136 format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
chris@22 137 end
Chris@0 138 else
chris@22 139 respond_to do |format|
chris@22 140 format.html { render :action => 'new' }
Chris@117 141 format.api { render_validation_errors(@project) }
Chris@0 142 end
chris@22 143 end
Chris@909 144
Chris@0 145 end
Chris@909 146
Chris@0 147 def copy
Chris@0 148 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@1115 149 @trackers = Tracker.sorted.all
Chris@0 150 @root_projects = Project.find(:all,
Chris@0 151 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
Chris@0 152 :order => 'name')
Chris@0 153 @source_project = Project.find(params[:id])
Chris@0 154 if request.get?
Chris@0 155 @project = Project.copy_from(@source_project)
Chris@1115 156 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
Chris@0 157 else
Chris@0 158 Mailer.with_deliveries(params[:notifications] == '1') do
Chris@117 159 @project = Project.new
Chris@117 160 @project.safe_attributes = params[:project]
Chris@0 161 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
Chris@0 162 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
Chris@0 163 flash[:notice] = l(:notice_successful_create)
Chris@117 164 redirect_to :controller => 'projects', :action => 'settings', :id => @project
Chris@0 165 elsif !@project.new_record?
Chris@0 166 # Project was created
Chris@0 167 # But some objects were not copied due to validation failures
Chris@0 168 # (eg. issues from disabled trackers)
Chris@0 169 # TODO: inform about that
Chris@117 170 redirect_to :controller => 'projects', :action => 'settings', :id => @project
Chris@0 171 end
Chris@0 172 end
Chris@0 173 end
Chris@0 174 rescue ActiveRecord::RecordNotFound
Chris@1115 175 # source_project not found
Chris@1115 176 render_404
Chris@0 177 end
luis@1128 178
Chris@0 179 # Show @project
Chris@0 180 def show
Chris@0 181 if params[:jump]
Chris@0 182 # try to redirect to the requested menu item
Chris@0 183 redirect_to_project_menu_item(@project, params[:jump]) && return
Chris@0 184 end
Chris@909 185
Chris@0 186 @users_by_role = @project.users_by_role
Chris@441 187 @subprojects = @project.children.visible.all
Chris@0 188 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
Chris@0 189 @trackers = @project.rolled_up_trackers
Chris@909 190
Chris@0 191 cond = @project.project_condition(Setting.display_subprojects_issues?)
Chris@909 192
Chris@1115 193 @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker)
Chris@1115 194 @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker)
Chris@909 195
Chris@441 196 if User.current.allowed_to?(:view_time_entries, @project)
Chris@441 197 @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f
Chris@0 198 end
Chris@909 199
Chris@0 200 @key = User.current.rss_key
Chris@909 201
Chris@0 202 respond_to do |format|
Chris@0 203 format.html
Chris@117 204 format.api
Chris@0 205 end
Chris@0 206 end
Chris@0 207
Chris@0 208 def settings
Chris@0 209 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
Chris@0 210 @issue_category ||= IssueCategory.new
Chris@0 211 @member ||= @project.members.new
Chris@1115 212 @trackers = Tracker.sorted.all
Chris@0 213 @wiki ||= @project.wiki
Chris@0 214 end
Chris@909 215
Chris@0 216 def edit
chris@22 217 end
chris@22 218
chris@22 219 def update
Chris@117 220 @project.safe_attributes = params[:project]
chris@22 221 if validate_parent_id && @project.save
chris@22 222 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
chris@22 223 respond_to do |format|
Chris@909 224 format.html {
chris@22 225 flash[:notice] = l(:notice_successful_update)
chris@22 226 redirect_to :action => 'settings', :id => @project
chris@22 227 }
Chris@1115 228 format.api { render_api_ok }
chris@22 229 end
Chris@0 230 else
chris@22 231 respond_to do |format|
Chris@909 232 format.html {
chris@22 233 settings
chris@22 234 render :action => 'settings'
chris@22 235 }
Chris@117 236 format.api { render_validation_errors(@project) }
Chris@0 237 end
Chris@0 238 end
Chris@0 239 end
Chris@117 240
chris@351 241 def overview
chris@351 242 @project.has_welcome_page = params[:has_welcome_page]
chris@351 243 if @project.save
chris@351 244 flash[:notice] = l(:notice_successful_update)
chris@351 245 end
chris@351 246 redirect_to :action => 'settings', :id => @project, :tab => 'overview'
chris@351 247 end
chris@351 248
Chris@0 249 def modules
Chris@117 250 @project.enabled_module_names = params[:enabled_module_names]
Chris@0 251 flash[:notice] = l(:notice_successful_update)
Chris@0 252 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
Chris@0 253 end
Chris@0 254
Chris@0 255 def archive
Chris@0 256 if request.post?
Chris@0 257 unless @project.archive
Chris@0 258 flash[:error] = l(:error_can_not_archive_project)
Chris@0 259 end
Chris@0 260 end
Chris@0 261 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@0 262 end
Chris@909 263
Chris@0 264 def unarchive
Chris@0 265 @project.unarchive if request.post? && !@project.active?
Chris@0 266 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
Chris@0 267 end
Chris@909 268
Chris@1115 269 def close
Chris@1115 270 @project.close
Chris@1115 271 redirect_to project_path(@project)
Chris@1115 272 end
Chris@1115 273
Chris@1115 274 def reopen
Chris@1115 275 @project.reopen
Chris@1115 276 redirect_to project_path(@project)
Chris@1115 277 end
Chris@1115 278
Chris@0 279 # Delete @project
Chris@0 280 def destroy
Chris@0 281 @project_to_destroy = @project
Chris@1115 282 if api_request? || params[:confirm]
Chris@1115 283 @project_to_destroy.destroy
Chris@1115 284 respond_to do |format|
Chris@1115 285 format.html { redirect_to :controller => 'admin', :action => 'projects' }
Chris@1115 286 format.api { render_api_ok }
Chris@0 287 end
Chris@0 288 end
Chris@0 289 # hide project in layout
Chris@0 290 @project = nil
Chris@0 291 end
Chris@0 292
Chris@1115 293 private
Chris@0 294
chris@1034 295 def validate_is_public_key
chris@1034 296 # Although is_public isn't mandatory in the project model (it gets
chris@1034 297 # defaulted), it must be present in params -- it can be true or
chris@1034 298 # false, but it must be there. This permits us to make forms in
chris@1034 299 # which the user _has_ to select public or private (rather than
chris@1034 300 # defaulting it) if we want to
chris@1034 301 if params.nil? || params[:project].nil? || !params[:project].has_key?(:is_public)
chris@1034 302 @project.errors.add :is_public, :public_or_private
chris@1034 303 return false
chris@1034 304 end
chris@1034 305 true
chris@1034 306 end
chris@1034 307
Chris@0 308 # Validates parent_id param according to user's permissions
Chris@0 309 # TODO: move it to Project model in a validation that depends on User.current
Chris@0 310 def validate_parent_id
Chris@0 311 return true if User.current.admin?
Chris@0 312 parent_id = params[:project] && params[:project][:parent_id]
Chris@0 313 if parent_id || @project.new_record?
Chris@0 314 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
Chris@0 315 unless @project.allowed_parents.include?(parent)
Chris@0 316 @project.errors.add :parent_id, :invalid
Chris@0 317 return false
Chris@0 318 end
Chris@0 319 end
Chris@0 320 true
Chris@0 321 end
Chris@0 322 end