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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. include QueriesHelper
  33. helper :projects_queries
  34. include ProjectsQueriesHelper
  35. helper :repositories
  36. helper :members
  37. helper :trackers
  38. # Lists visible projects
  39. def index
  40. # try to redirect to the requested menu item
  41. if params[:jump] && redirect_to_menu_item(params[:jump])
  42. return
  43. end
  44. retrieve_project_query
  45. scope = project_scope
  46. respond_to do |format|
  47. format.html {
  48. @entry_count = scope.count
  49. @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
  50. @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
  51. }
  52. format.api {
  53. @offset, @limit = api_offset_and_limit
  54. @project_count = scope.count
  55. @projects = scope.offset(@offset).limit(@limit).to_a
  56. }
  57. format.atom {
  58. projects = scope.reorder(:created_on => :desc).limit(Setting.feeds_limit.to_i).to_a
  59. render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
  60. }
  61. format.csv {
  62. # Export all entries
  63. @entries = scope.to_a
  64. send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'projects.csv')
  65. }
  66. end
  67. end
  68. def autocomplete
  69. respond_to do |format|
  70. format.js {
  71. if params[:q].present?
  72. @projects = Project.visible.like(params[:q]).to_a
  73. else
  74. @projects = User.current.projects.to_a
  75. end
  76. }
  77. end
  78. end
  79. def new
  80. @issue_custom_fields = IssueCustomField.sorted.to_a
  81. @trackers = Tracker.sorted.to_a
  82. @project = Project.new
  83. @project.safe_attributes = params[:project]
  84. end
  85. def create
  86. @issue_custom_fields = IssueCustomField.sorted.to_a
  87. @trackers = Tracker.sorted.to_a
  88. @project = Project.new
  89. @project.safe_attributes = params[:project]
  90. if @project.save
  91. unless User.current.admin?
  92. @project.add_default_member(User.current)
  93. end
  94. respond_to do |format|
  95. format.html {
  96. flash[:notice] = l(:notice_successful_create)
  97. if params[:continue]
  98. attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?}
  99. redirect_to new_project_path(attrs)
  100. else
  101. redirect_to settings_project_path(@project)
  102. end
  103. }
  104. format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
  105. end
  106. else
  107. respond_to do |format|
  108. format.html { render :action => 'new' }
  109. format.api { render_validation_errors(@project) }
  110. end
  111. end
  112. end
  113. def copy
  114. @issue_custom_fields = IssueCustomField.sorted.to_a
  115. @trackers = Tracker.sorted.to_a
  116. @source_project = Project.find(params[:id])
  117. if request.get?
  118. @project = Project.copy_from(@source_project)
  119. @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
  120. else
  121. Mailer.with_deliveries(params[:notifications] == '1') do
  122. @project = Project.new
  123. @project.safe_attributes = params[:project]
  124. if @project.copy(@source_project, :only => params[:only])
  125. flash[:notice] = l(:notice_successful_create)
  126. redirect_to settings_project_path(@project)
  127. elsif !@project.new_record?
  128. # Project was created
  129. # But some objects were not copied due to validation failures
  130. # (eg. issues from disabled trackers)
  131. # TODO: inform about that
  132. redirect_to settings_project_path(@project)
  133. end
  134. end
  135. end
  136. rescue ActiveRecord::RecordNotFound
  137. # source_project not found
  138. render_404
  139. end
  140. # Show @project
  141. def show
  142. # try to redirect to the requested menu item
  143. if params[:jump] && redirect_to_project_menu_item(@project, params[:jump])
  144. return
  145. end
  146. @users_by_role = @project.users_by_role
  147. @subprojects = @project.children.visible.to_a
  148. @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").to_a
  149. @trackers = @project.rolled_up_trackers.visible
  150. cond = @project.project_condition(Setting.display_subprojects_issues?)
  151. @open_issues_by_tracker = Issue.visible.open.where(cond).group(:tracker).count
  152. @total_issues_by_tracker = Issue.visible.where(cond).group(:tracker).count
  153. if User.current.allowed_to_view_all_time_entries?(@project)
  154. @total_hours = TimeEntry.visible.where(cond).sum(:hours).to_f
  155. @total_estimated_hours = Issue.visible.where(cond).sum(:estimated_hours).to_f
  156. end
  157. @key = User.current.rss_key
  158. respond_to do |format|
  159. format.html
  160. format.api
  161. end
  162. end
  163. def settings
  164. @issue_custom_fields = IssueCustomField.sorted.to_a
  165. @issue_category ||= IssueCategory.new
  166. @member ||= @project.members.new
  167. @trackers = Tracker.sorted.to_a
  168. @version_status = params[:version_status] || 'open'
  169. @version_name = params[:version_name]
  170. @versions = @project.shared_versions.status(@version_status).like(@version_name).sorted
  171. end
  172. def edit
  173. end
  174. def update
  175. @project.safe_attributes = params[:project]
  176. if @project.save
  177. respond_to do |format|
  178. format.html {
  179. flash[:notice] = l(:notice_successful_update)
  180. redirect_to settings_project_path(@project, params[:tab])
  181. }
  182. format.api { render_api_ok }
  183. end
  184. else
  185. respond_to do |format|
  186. format.html {
  187. settings
  188. render :action => 'settings'
  189. }
  190. format.api { render_validation_errors(@project) }
  191. end
  192. end
  193. end
  194. def archive
  195. unless @project.archive
  196. flash[:error] = l(:error_can_not_archive_project)
  197. end
  198. redirect_to_referer_or admin_projects_path(:status => params[:status])
  199. end
  200. def unarchive
  201. unless @project.active?
  202. @project.unarchive
  203. end
  204. redirect_to_referer_or admin_projects_path(:status => params[:status])
  205. end
  206. def bookmark
  207. jump_box = Redmine::ProjectJumpBox.new User.current
  208. if request.delete?
  209. jump_box.delete_project_bookmark @project
  210. elsif request.post?
  211. jump_box.bookmark_project @project
  212. end
  213. respond_to do |format|
  214. format.js
  215. format.html { redirect_to project_path(@project) }
  216. end
  217. end
  218. def close
  219. @project.close
  220. redirect_to project_path(@project)
  221. end
  222. def reopen
  223. @project.reopen
  224. redirect_to project_path(@project)
  225. end
  226. # Delete @project
  227. def destroy
  228. @project_to_destroy = @project
  229. if api_request? || params[:confirm]
  230. @project_to_destroy.destroy
  231. respond_to do |format|
  232. format.html { redirect_to admin_projects_path }
  233. format.api { render_api_ok }
  234. end
  235. end
  236. # hide project in layout
  237. @project = nil
  238. end
  239. private
  240. # Returns the ProjectEntry scope for index
  241. def project_scope(options={})
  242. @query.results_scope(options)
  243. end
  244. def retrieve_project_query
  245. retrieve_query(ProjectQuery, false)
  246. end
  247. end