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

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