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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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. render :layout => 'admin' if params[:admin_projects]
  50. end
  51. def create
  52. @query = query_class.new
  53. @query.user = User.current
  54. @query.project = @project
  55. update_query_from_params
  56. if @query.save
  57. flash[:notice] = l(:notice_successful_create)
  58. redirect_to_items(:query_id => @query, :admin_projects => params[:admin_projects])
  59. else
  60. render :action => 'new', :layout => !request.xhr?
  61. end
  62. end
  63. def edit
  64. render :layout => 'admin' if params[:admin_projects]
  65. end
  66. def update
  67. update_query_from_params
  68. if @query.save
  69. flash[:notice] = l(:notice_successful_update)
  70. redirect_to_items(:query_id => @query, :admin_projects => params[:admin_projects])
  71. else
  72. render :action => 'edit'
  73. end
  74. end
  75. def destroy
  76. @query.destroy
  77. redirect_to_items(:set_filter => 1)
  78. end
  79. # Returns the values for a query filter
  80. def filter
  81. q = query_class.new
  82. if params[:project_id].present?
  83. q.project = Project.find(params[:project_id])
  84. end
  85. unless User.current.allowed_to?(q.class.view_permission, q.project, :global => true)
  86. raise Unauthorized
  87. end
  88. filter = q.available_filters[params[:name].to_s]
  89. values = filter ? filter.values : []
  90. render :json => values
  91. rescue ActiveRecord::RecordNotFound
  92. render_404
  93. end
  94. def current_menu_item
  95. @query ? @query.queried_class.to_s.underscore.pluralize.to_sym : nil
  96. end
  97. def current_menu(project)
  98. super if params[:admin_projects].nil?
  99. end
  100. private
  101. def find_query
  102. @query = Query.find(params[:id])
  103. @query.admin_projects = params[:admin_projects] if @query.is_a?(ProjectQuery)
  104. @project = @query.project
  105. render_403 unless @query.editable_by?(User.current)
  106. rescue ActiveRecord::RecordNotFound
  107. render_404
  108. end
  109. def update_query_from_params
  110. @query.project = params[:query_is_for_all] ? nil : @project
  111. @query.build_from_params(params)
  112. @query.column_names = nil if params[:default_columns]
  113. @query.sort_criteria = (params[:query] && params[:query][:sort_criteria]) || @query.sort_criteria
  114. @query.name = params[:query] && params[:query][:name]
  115. if User.current.allowed_to?(:manage_public_queries, @query.project) || User.current.admin?
  116. @query.visibility = (params[:query] && params[:query][:visibility]) || Query::VISIBILITY_PRIVATE
  117. @query.role_ids = params[:query] && params[:query][:role_ids]
  118. else
  119. @query.visibility = Query::VISIBILITY_PRIVATE
  120. end
  121. @query
  122. end
  123. def redirect_to_items(options)
  124. method = "redirect_to_#{@query.class.name.underscore}"
  125. send method, options
  126. end
  127. def redirect_to_issue_query(options)
  128. if params[:gantt]
  129. if @project
  130. redirect_to project_gantt_path(@project, options)
  131. else
  132. redirect_to issues_gantt_path(options)
  133. end
  134. elsif params[:calendar]
  135. if @project
  136. redirect_to project_calendar_path(@project, options)
  137. else
  138. redirect_to issues_calendar_path(options)
  139. end
  140. else
  141. redirect_to _project_issues_path(@project, options)
  142. end
  143. end
  144. def redirect_to_time_entry_query(options)
  145. redirect_to _time_entries_path(@project, nil, options)
  146. end
  147. def redirect_to_project_query(options)
  148. if params[:admin_projects]
  149. redirect_to admin_projects_path(options)
  150. else
  151. redirect_to projects_path(options)
  152. end
  153. end
  154. def redirect_to_user_query(options)
  155. redirect_to users_path(options)
  156. end
  157. # Returns the Query subclass, IssueQuery by default
  158. # for compatibility with previous behaviour
  159. def query_class
  160. Query.get_subclass(params[:type] || 'IssueQuery')
  161. end
  162. end