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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class QueriesController < ApplicationController
  18. menu_item :issues
  19. before_action :find_query, :only => [:edit, :update, :destroy]
  20. before_action :find_optional_project, :only => [:new, :create]
  21. accept_api_auth :index
  22. include QueriesHelper
  23. def index
  24. case params[:format]
  25. when 'xml', 'json'
  26. @offset, @limit = api_offset_and_limit
  27. else
  28. @limit = per_page_option
  29. end
  30. scope = query_class.visible
  31. @query_count = scope.count
  32. @query_pages = Paginator.new @query_count, @limit, params['page']
  33. @queries = scope.
  34. order("#{Query.table_name}.name").
  35. limit(@limit).
  36. offset(@offset).
  37. to_a
  38. respond_to do |format|
  39. format.html {render_error :status => 406}
  40. format.api
  41. end
  42. end
  43. def new
  44. @query = query_class.new
  45. @query.user = User.current
  46. @query.project = @project
  47. @query.build_from_params(params)
  48. end
  49. def create
  50. @query = query_class.new
  51. @query.user = User.current
  52. @query.project = @project
  53. update_query_from_params
  54. if @query.save
  55. flash[:notice] = l(:notice_successful_create)
  56. redirect_to_items(:query_id => @query)
  57. else
  58. render :action => 'new', :layout => !request.xhr?
  59. end
  60. end
  61. def edit
  62. end
  63. def update
  64. update_query_from_params
  65. if @query.save
  66. flash[:notice] = l(:notice_successful_update)
  67. redirect_to_items(:query_id => @query)
  68. else
  69. render :action => 'edit'
  70. end
  71. end
  72. def destroy
  73. @query.destroy
  74. redirect_to_items(:set_filter => 1)
  75. end
  76. # Returns the values for a query filter
  77. def filter
  78. q = query_class.new
  79. if params[:project_id].present?
  80. q.project = Project.find(params[:project_id])
  81. end
  82. unless User.current.allowed_to?(q.class.view_permission, q.project, :global => true)
  83. raise Unauthorized
  84. end
  85. filter = q.available_filters[params[:name].to_s]
  86. values = filter ? filter.values : []
  87. render :json => values
  88. rescue ActiveRecord::RecordNotFound
  89. render_404
  90. end
  91. def current_menu_item
  92. @query ? @query.queried_class.to_s.underscore.pluralize.to_sym : nil
  93. end
  94. private
  95. def find_query
  96. @query = Query.find(params[:id])
  97. @project = @query.project
  98. render_403 unless @query.editable_by?(User.current)
  99. rescue ActiveRecord::RecordNotFound
  100. render_404
  101. end
  102. def update_query_from_params
  103. @query.project = params[:query_is_for_all] ? nil : @project
  104. @query.build_from_params(params)
  105. @query.column_names = nil if params[:default_columns]
  106. @query.sort_criteria = (params[:query] && params[:query][:sort_criteria]) || @query.sort_criteria
  107. @query.name = params[:query] && params[:query][:name]
  108. if User.current.allowed_to?(:manage_public_queries, @query.project) || User.current.admin?
  109. @query.visibility = (params[:query] && params[:query][:visibility]) || Query::VISIBILITY_PRIVATE
  110. @query.role_ids = params[:query] && params[:query][:role_ids]
  111. else
  112. @query.visibility = Query::VISIBILITY_PRIVATE
  113. end
  114. @query
  115. end
  116. def redirect_to_items(options)
  117. method = "redirect_to_#{@query.class.name.underscore}"
  118. send method, options
  119. end
  120. def redirect_to_issue_query(options)
  121. if params[:gantt]
  122. if @project
  123. redirect_to project_gantt_path(@project, options)
  124. else
  125. redirect_to issues_gantt_path(options)
  126. end
  127. else
  128. redirect_to _project_issues_path(@project, options)
  129. end
  130. end
  131. def redirect_to_time_entry_query(options)
  132. redirect_to _time_entries_path(@project, nil, options)
  133. end
  134. # Returns the Query subclass, IssueQuery by default
  135. # for compatibility with previous behaviour
  136. def query_class
  137. Query.get_subclass(params[:type] || 'IssueQuery')
  138. end
  139. end