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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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 :settings, :only => :settings
  20. menu_item :projects, :only => [:index, :new, :copy, :create]
  21. before_action :find_project, :except => [ :index, :list, :new, :create, :copy ]
  22. before_action :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
  23. before_action :authorize_global, :only => [:new, :create]
  24. before_action :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
  25. accept_rss_auth :index
  26. accept_api_auth :index, :show, :create, :update, :destroy
  27. require_sudo_mode :destroy
  28. helper :custom_fields
  29. helper :issues
  30. helper :queries
  31. helper :repositories
  32. helper :members
  33. # Lists visible projects
  34. def index
  35. # try to redirect to the requested menu item
  36. if params[:jump] && redirect_to_menu_item(params[:jump])
  37. return
  38. end
  39. scope = Project.visible.sorted
  40. respond_to do |format|
  41. format.html {
  42. unless params[:closed]
  43. scope = scope.active
  44. end
  45. @projects = scope.to_a
  46. }
  47. format.js {
  48. if params[:q].present?
  49. @projects = Project.visible.like(params[:q]).to_a
  50. else
  51. @projects = User.current.projects.to_a
  52. end
  53. }
  54. format.api {
  55. @offset, @limit = api_offset_and_limit
  56. @project_count = scope.count
  57. @projects = scope.offset(@offset).limit(@limit).to_a
  58. }
  59. format.atom {
  60. projects = scope.reorder(:created_on => :desc).limit(Setting.feeds_limit.to_i).to_a
  61. render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
  62. }
  63. end
  64. end
  65. def new
  66. @issue_custom_fields = IssueCustomField.sorted.to_a
  67. @trackers = Tracker.sorted.to_a
  68. @project = Project.new
  69. @project.safe_attributes = params[:project]
  70. end
  71. def create
  72. @issue_custom_fields = IssueCustomField.sorted.to_a
  73. @trackers = Tracker.sorted.to_a
  74. @project = Project.new
  75. @project.safe_attributes = params[:project]
  76. if @project.save
  77. unless User.current.admin?
  78. @project.add_default_member(User.current)
  79. end
  80. respond_to do |format|
  81. format.html {
  82. flash[:notice] = l(:notice_successful_create)
  83. if params[:continue]
  84. attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
  85. redirect_to new_project_path(attrs)
  86. else
  87. redirect_to settings_project_path(@project)
  88. end
  89. }
  90. format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
  91. end
  92. else
  93. respond_to do |format|
  94. format.html { render :action => 'new' }
  95. format.api { render_validation_errors(@project) }
  96. end
  97. end
  98. end
  99. def copy
  100. @issue_custom_fields = IssueCustomField.sorted.to_a
  101. @trackers = Tracker.sorted.to_a
  102. @source_project = Project.find(params[:id])
  103. if request.get?
  104. @project = Project.copy_from(@source_project)
  105. @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
  106. else
  107. Mailer.with_deliveries(params[:notifications] == '1') do
  108. @project = Project.new
  109. @project.safe_attributes = params[:project]
  110. if @project.copy(@source_project, :only => params[:only])
  111. flash[:notice] = l(:notice_successful_create)
  112. redirect_to settings_project_path(@project)
  113. elsif !@project.new_record?
  114. # Project was created
  115. # But some objects were not copied due to validation failures
  116. # (eg. issues from disabled trackers)
  117. # TODO: inform about that
  118. redirect_to settings_project_path(@project)
  119. end
  120. end
  121. end
  122. rescue ActiveRecord::RecordNotFound
  123. # source_project not found
  124. render_404
  125. end
  126. # Show @project
  127. def show
  128. # try to redirect to the requested menu item
  129. if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
  130. return
  131. end
  132. @users_by_role = @project.users_by_role
  133. @subprojects = @project.children.visible.to_a
  134. @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").to_a
  135. @trackers = @project.rolled_up_trackers.visible
  136. cond = @project.project_condition(Setting.display_subprojects_issues?)
  137. @open_issues_by_tracker = Issue.visible.open.where(cond).group(:tracker).count
  138. @total_issues_by_tracker = Issue.visible.where(cond).group(:tracker).count
  139. if User.current.allowed_to_view_all_time_entries?(@project)
  140. @total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f
  141. end
  142. @key = User.current.rss_key
  143. respond_to do |format|
  144. format.html
  145. format.api
  146. end
  147. end
  148. def settings
  149. @issue_custom_fields = IssueCustomField.sorted.to_a
  150. @issue_category ||= IssueCategory.new
  151. @member ||= @project.members.new
  152. @trackers = Tracker.sorted.to_a
  153. @version_status = params[:version_status] || 'open'
  154. @version_name = params[:version_name]
  155. @versions = @project.shared_versions.status(@version_status).like(@version_name)
  156. @wiki ||= @project.wiki || Wiki.new(:project => @project)
  157. end
  158. def edit
  159. end
  160. def update
  161. @project.safe_attributes = params[:project]
  162. if @project.save
  163. respond_to do |format|
  164. format.html {
  165. flash[:notice] = l(:notice_successful_update)
  166. redirect_to settings_project_path(@project)
  167. }
  168. format.api { render_api_ok }
  169. end
  170. else
  171. respond_to do |format|
  172. format.html {
  173. settings
  174. render :action => 'settings'
  175. }
  176. format.api { render_validation_errors(@project) }
  177. end
  178. end
  179. end
  180. def modules
  181. @project.enabled_module_names = params[:enabled_module_names]
  182. flash[:notice] = l(:notice_successful_update)
  183. redirect_to settings_project_path(@project, :tab => 'modules')
  184. end
  185. def archive
  186. unless @project.archive
  187. flash[:error] = l(:error_can_not_archive_project)
  188. end
  189. redirect_to_referer_or admin_projects_path(:status => params[:status])
  190. end
  191. def unarchive
  192. unless @project.active?
  193. @project.unarchive
  194. end
  195. redirect_to_referer_or admin_projects_path(:status => params[:status])
  196. end
  197. def close
  198. @project.close
  199. redirect_to project_path(@project)
  200. end
  201. def reopen
  202. @project.reopen
  203. redirect_to project_path(@project)
  204. end
  205. # Delete @project
  206. def destroy
  207. @project_to_destroy = @project
  208. if api_request? || params[:confirm]
  209. @project_to_destroy.destroy
  210. respond_to do |format|
  211. format.html { redirect_to admin_projects_path }
  212. format.api { render_api_ok }
  213. end
  214. end
  215. # hide project in layout
  216. @project = nil
  217. end
  218. end