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.

queries_controller.rb 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 QueriesController < ApplicationController
  19. menu_item :issues
  20. before_action :find_query, :only => [:edit, :update, :destroy]
  21. before_action :find_optional_project, :only => [:new, :create]
  22. accept_api_auth :index
  23. include QueriesHelper
  24. def index
  25. case params[:format]
  26. when 'xml', 'json'
  27. @offset, @limit = api_offset_and_limit
  28. else
  29. @limit = per_page_option
  30. end
  31. scope = query_class.visible
  32. @query_count = scope.count
  33. @query_pages = Paginator.new @query_count, @limit, params['page']
  34. @queries = scope.
  35. order("#{Query.table_name}.name").
  36. limit(@limit).
  37. offset(@offset).
  38. to_a
  39. respond_to do |format|
  40. format.html {render_error :status => 406}
  41. format.api
  42. end
  43. end
  44. def new
  45. @query = query_class.new
  46. @query.user = User.current
  47. @query.project = @project
  48. @query.build_from_params(params)
  49. end
  50. def create
  51. @query = query_class.new
  52. @query.user = User.current
  53. @query.project = @project
  54. update_query_from_params
  55. if @query.save
  56. flash[:notice] = l(:notice_successful_create)
  57. redirect_to_items(:query_id => @query)
  58. else
  59. render :action => 'new', :layout => !request.xhr?
  60. end
  61. end
  62. def edit
  63. end
  64. def update
  65. update_query_from_params
  66. if @query.save
  67. flash[:notice] = l(:notice_successful_update)
  68. redirect_to_items(:query_id => @query)
  69. else
  70. render :action => 'edit'
  71. end
  72. end
  73. def destroy
  74. @query.destroy
  75. redirect_to_items(:set_filter => 1)
  76. end
  77. # Returns the values for a query filter
  78. def filter
  79. q = query_class.new
  80. if params[:project_id].present?
  81. q.project = Project.find(params[:project_id])
  82. end
  83. unless User.current.allowed_to?(q.class.view_permission, q.project, :global => true)
  84. raise Unauthorized
  85. end
  86. filter = q.available_filters[params[:name].to_s]
  87. values = filter ? filter.values : []
  88. render :json => values
  89. rescue ActiveRecord::RecordNotFound
  90. render_404
  91. end
  92. def current_menu_item
  93. @query ? @query.queried_class.to_s.underscore.pluralize.to_sym : nil
  94. end
  95. private
  96. def find_query
  97. @query = Query.find(params[:id])
  98. @project = @query.project
  99. render_403 unless @query.editable_by?(User.current)
  100. rescue ActiveRecord::RecordNotFound
  101. render_404
  102. end
  103. def update_query_from_params
  104. @query.project = params[:query_is_for_all] ? nil : @project
  105. @query.build_from_params(params)
  106. @query.column_names = nil if params[:default_columns]
  107. @query.sort_criteria = (params[:query] && params[:query][:sort_criteria]) || @query.sort_criteria
  108. @query.name = params[:query] && params[:query][:name]
  109. if User.current.allowed_to?(:manage_public_queries, @query.project) || User.current.admin?
  110. @query.visibility = (params[:query] && params[:query][:visibility]) || Query::VISIBILITY_PRIVATE
  111. @query.role_ids = params[:query] && params[:query][:role_ids]
  112. else
  113. @query.visibility = Query::VISIBILITY_PRIVATE
  114. end
  115. @query
  116. end
  117. def redirect_to_items(options)
  118. method = "redirect_to_#{@query.class.name.underscore}"
  119. send method, options
  120. end
  121. def redirect_to_issue_query(options)
  122. if params[:gantt]
  123. if @project
  124. redirect_to project_gantt_path(@project, options)
  125. else
  126. redirect_to issues_gantt_path(options)
  127. end
  128. elsif params[:calendar]
  129. if @project
  130. redirect_to project_calendar_path(@project, options)
  131. else
  132. redirect_to issues_calendar_path(options)
  133. end
  134. else
  135. redirect_to _project_issues_path(@project, options)
  136. end
  137. end
  138. def redirect_to_time_entry_query(options)
  139. redirect_to _time_entries_path(@project, nil, options)
  140. end
  141. def redirect_to_project_query(options)
  142. redirect_to projects_path(options)
  143. end
  144. # Returns the Query subclass, IssueQuery by default
  145. # for compatibility with previous behaviour
  146. def query_class
  147. Query.get_subclass(params[:type] || 'IssueQuery')
  148. end
  149. end