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

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