| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-06 16:57:04 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2022-01-02 05:29:10 +00:00
										 |  |  | # Copyright (C) 2006-2022  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class QueriesController < ApplicationController | 
					
						
							| 
									
										
										
										
											2008-01-19 11:53:43 +00:00
										 |  |  |   menu_item :issues | 
					
						
							| 
									
										
										
										
											2017-01-09 19:59:54 +00:00
										 |  |  |   before_action :find_query, :only => [:edit, :update, :destroy] | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :find_optional_project, :only => [:new, :create] | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-09 08:56:07 +00:00
										 |  |  |   accept_api_auth :index | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |   include QueriesHelper | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-06 16:57:04 +00:00
										 |  |  |   def index | 
					
						
							|  |  |  |     case params[:format] | 
					
						
							|  |  |  |     when 'xml', 'json' | 
					
						
							|  |  |  |       @offset, @limit = api_offset_and_limit | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       @limit = per_page_option | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |     scope = query_class.visible | 
					
						
							|  |  |  |     @query_count = scope.count | 
					
						
							| 
									
										
										
										
											2013-01-08 20:18:12 +00:00
										 |  |  |     @query_pages = Paginator.new @query_count, @limit, params['page'] | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |     @queries = scope. | 
					
						
							| 
									
										
										
										
											2014-01-10 10:30:57 +00:00
										 |  |  |                     order("#{Query.table_name}.name"). | 
					
						
							|  |  |  |                     limit(@limit). | 
					
						
							|  |  |  |                     offset(@offset). | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |                     to_a | 
					
						
							| 
									
										
										
										
											2011-07-06 16:57:04 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       format.html {render_error :status => 406} | 
					
						
							| 
									
										
										
										
											2011-07-06 16:57:04 +00:00
										 |  |  |       format.api | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-23 13:49:29 +00:00
										 |  |  |   def new | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |     @query = query_class.new | 
					
						
							| 
									
										
										
										
											2007-11-20 15:40:16 +00:00
										 |  |  |     @query.user = User.current | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |     @query.project = @project | 
					
						
							| 
									
										
										
										
											2012-12-09 15:11:00 +00:00
										 |  |  |     @query.build_from_params(params) | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |     render :layout => 'admin' if params[:admin_projects] | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |   def create | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |     @query = query_class.new | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |     @query.user = User.current | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |     @query.project = @project | 
					
						
							|  |  |  |     update_query_from_params | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |     if @query.save | 
					
						
							| 
									
										
										
										
											2007-06-23 13:49:29 +00:00
										 |  |  |       flash[:notice] = l(:notice_successful_create) | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |       redirect_to_items(:query_id => @query, :admin_projects => params[:admin_projects]) | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       render :action => 'new', :layout => !request.xhr? | 
					
						
							| 
									
										
										
										
											2007-06-23 13:49:29 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  |   def edit | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |     render :layout => 'admin' if params[:admin_projects] | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |   def update | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |     update_query_from_params | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if @query.save | 
					
						
							|  |  |  |       flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |       redirect_to_items(:query_id => @query, :admin_projects => params[:admin_projects]) | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       render :action => 'edit' | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy | 
					
						
							| 
									
										
										
										
											2011-10-24 20:19:26 +00:00
										 |  |  |     @query.destroy | 
					
						
							| 
									
										
										
										
											2016-07-12 17:40:19 +00:00
										 |  |  |     redirect_to_items(:set_filter => 1) | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 19:59:54 +00:00
										 |  |  |   # Returns the values for a query filter | 
					
						
							|  |  |  |   def filter | 
					
						
							|  |  |  |     q = query_class.new | 
					
						
							|  |  |  |     if params[:project_id].present? | 
					
						
							|  |  |  |       q.project = Project.find(params[:project_id]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     unless User.current.allowed_to?(q.class.view_permission, q.project, :global => true) | 
					
						
							|  |  |  |       raise Unauthorized | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     filter = q.available_filters[params[:name].to_s] | 
					
						
							|  |  |  |     values = filter ? filter.values : [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     render :json => values | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-17 08:29:05 +00:00
										 |  |  |   def current_menu_item | 
					
						
							|  |  |  |     @query ? @query.queried_class.to_s.underscore.pluralize.to_sym : nil | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |   def current_menu(project) | 
					
						
							|  |  |  |     super if params[:admin_projects].nil? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 12:29:07 +00:00
										 |  |  |   def find_query | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |     @query = Query.find(params[:id]) | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |     @query.admin_projects = params[:admin_projects] if @query.is_a?(ProjectQuery) | 
					
						
							| 
									
										
										
										
											2008-03-30 12:29:07 +00:00
										 |  |  |     @project = @query.project | 
					
						
							|  |  |  |     render_403 unless @query.editable_by?(User.current) | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:02:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |   def update_query_from_params | 
					
						
							|  |  |  |     @query.project = params[:query_is_for_all] ? nil : @project | 
					
						
							|  |  |  |     @query.build_from_params(params) | 
					
						
							|  |  |  |     @query.column_names = nil if params[:default_columns] | 
					
						
							| 
									
										
										
										
											2018-10-04 12:46:32 +00:00
										 |  |  |     @query.sort_criteria = (params[:query] && params[:query][:sort_criteria]) || @query.sort_criteria | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |     @query.name = params[:query] && params[:query][:name] | 
					
						
							| 
									
										
										
										
											2019-11-20 15:01:24 +00:00
										 |  |  |     if User.current.allowed_to?(:manage_public_queries, @query.project) || User.current.admin? | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |       @query.visibility = (params[:query] && params[:query][:visibility]) || Query::VISIBILITY_PRIVATE | 
					
						
							| 
									
										
										
										
											2015-08-02 07:02:44 +00:00
										 |  |  |       @query.role_ids = params[:query] && params[:query][:role_ids] | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |       @query.visibility = Query::VISIBILITY_PRIVATE | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |     @query | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 17:40:19 +00:00
										 |  |  |   def redirect_to_items(options) | 
					
						
							|  |  |  |     method = "redirect_to_#{@query.class.name.underscore}" | 
					
						
							|  |  |  |     send method, options | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def redirect_to_issue_query(options) | 
					
						
							| 
									
										
										
										
											2013-07-14 13:41:30 +00:00
										 |  |  |     if params[:gantt] | 
					
						
							|  |  |  |       if @project | 
					
						
							|  |  |  |         redirect_to project_gantt_path(@project, options) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         redirect_to issues_gantt_path(options) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2019-11-30 00:58:45 +00:00
										 |  |  |     elsif params[:calendar] | 
					
						
							|  |  |  |       if @project | 
					
						
							|  |  |  |         redirect_to project_calendar_path(@project, options) | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         redirect_to issues_calendar_path(options) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-07-14 13:41:30 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       redirect_to _project_issues_path(@project, options) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 17:40:19 +00:00
										 |  |  |   def redirect_to_time_entry_query(options) | 
					
						
							|  |  |  |     redirect_to _time_entries_path(@project, nil, options) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 11:36:13 +00:00
										 |  |  |   def redirect_to_project_query(options) | 
					
						
							| 
									
										
										
										
											2022-04-01 15:08:52 +00:00
										 |  |  |     if params[:admin_projects] | 
					
						
							|  |  |  |       redirect_to admin_projects_path(options) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       redirect_to projects_path(options) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-10-19 11:36:13 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 17:41:46 +00:00
										 |  |  |   # Returns the Query subclass, IssueQuery by default | 
					
						
							|  |  |  |   # for compatibility with previous behaviour | 
					
						
							|  |  |  |   def query_class | 
					
						
							|  |  |  |     Query.get_subclass(params[:type] || 'IssueQuery') | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-12-16 13:37:32 +00:00
										 |  |  | end |