queries_controller.rb
100 lines
| 3.7 KiB
| text/x-ruby
|
RubyLexer
|
r6066 | # Redmine - project management software | ||
# Copyright (C) 2006-2011 Jean-Philippe Lang | ||||
|
r92 | # | ||
# 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. | ||||
# | ||||
# 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. | ||||
# | ||||
# 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 | ||||
|
r1062 | menu_item :issues | ||
|
r6066 | before_filter :find_query, :except => [:new, :index] | ||
|
r1297 | before_filter :find_optional_project, :only => :new | ||
|
r563 | |||
|
r6077 | accept_api_auth :index | ||
|
r6066 | |||
def index | ||||
case params[:format] | ||||
when 'xml', 'json' | ||||
@offset, @limit = api_offset_and_limit | ||||
else | ||||
@limit = per_page_option | ||||
end | ||||
@query_count = Query.visible.count | ||||
@query_pages = Paginator.new self, @query_count, @limit, params['page'] | ||||
@queries = Query.visible.all(:limit => @limit, :offset => @offset, :order => "#{Query.table_name}.name") | ||||
respond_to do |format| | ||||
format.html { render :nothing => true } | ||||
format.api | ||||
end | ||||
end | ||||
|
r563 | def new | ||
@query = Query.new(params[:query]) | ||||
|
r1296 | @query.project = params[:query_is_for_all] ? nil : @project | ||
|
r906 | @query.user = User.current | ||
|
r2627 | @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin? | ||
|
r563 | |||
|
r5159 | @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) if params[:fields] || params[:f] | ||
|
r2604 | @query.group_by ||= params[:group_by] | ||
|
r5184 | @query.column_names = params[:c] if params[:c] | ||
@query.column_names = nil if params[:default_columns] | ||||
|
r563 | |||
|
r784 | if request.post? && params[:confirm] && @query.save | ||
|
r563 | flash[:notice] = l(:notice_successful_create) | ||
|
r874 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query | ||
|
r563 | return | ||
end | ||||
render :layout => false if request.xhr? | ||||
end | ||||
|
r92 | def edit | ||
if request.post? | ||||
@query.filters = {} | ||||
|
r5159 | @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) if params[:fields] || params[:f] | ||
|
r92 | @query.attributes = params[:query] | ||
|
r1296 | @query.project = nil if params[:query_is_for_all] | ||
|
r2627 | @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin? | ||
|
r5184 | @query.group_by ||= params[:group_by] | ||
@query.column_names = params[:c] if params[:c] | ||||
|
r772 | @query.column_names = nil if params[:default_columns] | ||
|
r92 | if @query.save | ||
flash[:notice] = l(:notice_successful_update) | ||||
|
r874 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query | ||
|
r92 | end | ||
end | ||||
end | ||||
def destroy | ||||
@query.destroy if request.post? | ||||
|
r1296 | redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :set_filter => 1 | ||
|
r92 | end | ||
private | ||||
|
r1296 | def find_query | ||
@query = Query.find(params[:id]) | ||||
@project = @query.project | ||||
render_403 unless @query.editable_by?(User.current) | ||||
rescue ActiveRecord::RecordNotFound | ||||
render_404 | ||||
end | ||||
|
r1297 | def find_optional_project | ||
@project = Project.find(params[:project_id]) if params[:project_id] | ||||
|
r3497 | render_403 unless User.current.allowed_to?(:save_queries, @project, :global => true) | ||
|
r130 | rescue ActiveRecord::RecordNotFound | ||
render_404 | ||||
|
r92 | end | ||
end | ||||