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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class ProjectsController < ApplicationController
  19. menu_item :overview
  20. menu_item :settings, :only => :settings
  21. menu_item :projects, :only => [:index, :new, :copy, :create]
  22. before_action :find_project, :except => [ :index, :autocomplete, :list, :new, :create, :copy ]
  23. before_action :authorize, :except => [ :index, :autocomplete, :list, :new, :create, :copy, :archive, :unarchive, :destroy]
  24. before_action :authorize_global, :only => [:new, :create]
  25. before_action :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
  26. accept_rss_auth :index
  27. accept_api_auth :index, :show, :create, :update, :destroy
  28. require_sudo_mode :destroy
  29. helper :custom_fields
  30. helper :issues
  31. helper :queries
  32. helper :repositories
  33. helper :members
  34. helper :trackers
  35. # Lists visible projects
  36. def index
  37. # try to redirect to the requested menu item
  38. if params[:jump] && redirect_to_menu_item(params[:jump])
  39. return
  40. end
  41. scope = Project.visible.sorted
  42. respond_to do |format|
  43. format.html {
  44. unless params[:closed]
  45. scope = scope.active
  46. end
  47. @projects = scope.to_a
  48. }
  49. format.api {
  50. @offset, @limit = api_offset_and_limit
  51. @project_count = scope.count
  52. @projects = scope.offset(@offset).limit(@limit).to_a
  53. }
  54. format.atom {
  55. projects = scope.reorder(:created_on => :desc).limit(Setting.feeds_limit.to_i).to_a
  56. render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
  57. }
  58. end
  59. end
  60. def autocomplete
  61. respond_to do |format|
  62. format.js {
  63. if params[:q].present?
  64. @projects = Project.visible.like(params[:q]).to_a
  65. else
  66. @projects = User.current.projects.to_a
  67. end
  68. }
  69. end
  70. end
  71. def new
  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. end
  77. def create
  78. @issue_custom_fields = IssueCustomField.sorted.to_a
  79. @trackers = Tracker.sorted.to_a
  80. @project = Project.new
  81. @project.safe_attributes = params[:project]
  82. if @project.save
  83. unless User.current.admin?
  84. @project.add_default_member(User.current)
  85. end
  86. respond_to do |format|
  87. format.html {
  88. flash[:notice] = l(:notice_successful_create)
  89. if params[:continue]
  90. attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
  91. redirect_to new_project_path(attrs)
  92. else
  93. redirect_to settings_project_path(@project)
  94. end
  95. }
  96. format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
  97. end
  98. else
  99. respond_to do |format|
  100. format.html { render :action => 'new' }
  101. format.api { render_validation_errors(@project) }
  102. end
  103. end
  104. end
  105. def copy
  106. @issue_custom_fields = IssueCustomField.sorted.to_a
  107. @trackers = Tracker.sorted.to_a
  108. @source_project = Project.find(params[:id])
  109. if request.get?
  110. @project = Project.copy_from(@source_project)
  111. @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
  112. else
  113. Mailer.with_deliveries(params[:notifications] == '1') do
  114. @project = Project.new
  115. @project.safe_attributes = params[:project]
  116. if @project.copy(@source_project, :only => params[:only])
  117. flash[:notice] = l(:notice_successful_create)
  118. redirect_to settings_project_path(@project)
  119. elsif !@project.new_record?
  120. # Project was created
  121. # But some objects were not copied due to validation failures
  122. # (eg. issues from disabled trackers)
  123. # TODO: inform about that
  124. redirect_to settings_project_path(@project)
  125. end
  126. end
  127. end
  128. rescue ActiveRecord::RecordNotFound
  129. # source_project not found
  130. render_404
  131. end
  132. # Show @project
  133. def show
  134. # try to redirect to the requested menu item
  135. if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
  136. return
  137. end
  138. @users_by_role = @project.users_by_role
  139. @subprojects = @project.children.visible.to_a
  140. @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").to_a
  141. @trackers = @project.rolled_up_trackers.visible
  142. cond = @project.project_condition(Setting.display_subprojects_issues?)
  143. @open_issues_by_tracker = Issue.visible.open.where(cond).group(:tracker).count
  144. @total_issues_by_tracker = Issue.visible.where(cond).group(:tracker).count
  145. if User.current.allowed_to_view_all_time_entries?(@project)
  146. @total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f
  147. @total_estimated_hours = Issue.visible.where(cond).sum(:estimated_hours).to_f
  148. end
  149. @key = User.current.rss_key
  150. respond_to do |format|
  151. format.html
  152. format.api
  153. end
  154. end
  155. def settings
  156. @issue_custom_fields = IssueCustomField.sorted.to_a
  157. @issue_category ||= IssueCategory.new
  158. @member ||= @project.members.new
  159. @trackers = Tracker.sorted.to_a
  160. @version_status = params[:version_status] || 'open'
  161. @version_name = params[:version_name]
  162. @versions = @project.shared_versions.status(@version_status).like(@version_name).sorted
  163. end
  164. def edit
  165. end
  166. def update
  167. @project.safe_attributes = params[:project]
  168. if @project.save
  169. respond_to do |format|
  170. format.html {
  171. flash[:notice] = l(:notice_successful_update)
  172. redirect_to settings_project_path(@project, params[:tab])
  173. }
  174. format.api { render_api_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 archive
  187. unless @project.archive
  188. flash[:error] = l(:error_can_not_archive_project)
  189. end
  190. redirect_to_referer_or admin_projects_path(:status => params[:status])
  191. end
  192. def unarchive
  193. unless @project.active?
  194. @project.unarchive
  195. end
  196. redirect_to_referer_or admin_projects_path(:status => params[:status])
  197. end
  198. def bookmark
  199. jump_box = Redmine::ProjectJumpBox.new User.current
  200. if request.delete?
  201. jump_box.delete_project_bookmark @project
  202. elsif request.post?
  203. jump_box.bookmark_project @project
  204. end
  205. respond_to do |format|
  206. format.js
  207. format.html { redirect_to project_path(@project) }
  208. end
  209. end
  210. def close
  211. @project.close
  212. redirect_to project_path(@project)
  213. end
  214. def reopen
  215. @project.reopen
  216. redirect_to project_path(@project)
  217. end
  218. # Delete @project
  219. def destroy
  220. @project_to_destroy = @project
  221. if api_request? || params[:confirm]
  222. @project_to_destroy.destroy
  223. respond_to do |format|
  224. format.html { redirect_to admin_projects_path }
  225. format.api { render_api_ok }
  226. end
  227. end
  228. # hide project in layout
  229. @project = nil
  230. end
  231. end