You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

projects_controller.rb 9.3KB

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