queries_controller.rb 2.81 KB
Newer Older
1
#-- encoding: UTF-8
2 3
#-- copyright
# ChiliProject is a project management system.
4
#
Holger Just's avatar
Holger Just committed
5
# Copyright (C) 2010-2013 the ChiliProject Team
6
#
7 8 9 10
# 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.
11
#
12 13 14
# See doc/COPYRIGHT.rdoc for more details.
#++

15
class QueriesController < ApplicationController
16
  menu_item :issues
17
  before_filter :find_query, :except => :new
18
  before_filter :find_optional_project, :only => :new
19

20 21
  def new
    @query = Query.new(params[:query])
22
    @query.project = params[:query_is_for_all] ? nil : @project
23
    @query.user = User.current
24
    @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
25

26
    @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) if params[:fields] || params[:f]
27
    @query.group_by ||= params[:group_by]
28 29
    @query.column_names = params[:c] if params[:c]
    @query.column_names = nil if params[:default_columns]
30

31
    if request.post? && params[:confirm] && @query.save
32
      flash[:notice] = l(:notice_successful_create)
33
      redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
34 35 36 37
      return
    end
    render :layout => false if request.xhr?
  end
38

39 40 41
  def edit
    if request.post?
      @query.filters = {}
42
      @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) if params[:fields] || params[:f]
43
      @query.attributes = params[:query]
44
      @query.project = nil if params[:query_is_for_all]
45
      @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
46 47
      @query.group_by ||= params[:group_by]
      @query.column_names = params[:c] if params[:c]
48
      @query.column_names = nil if params[:default_columns]
49

50 51
      if @query.save
        flash[:notice] = l(:notice_successful_update)
52
        redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
53 54 55 56 57 58
      end
    end
  end

  def destroy
    @query.destroy if request.post?
59
    redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :set_filter => 1
60
  end
61

62
private
63 64 65 66 67 68 69
  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
70

71 72
  def find_optional_project
    @project = Project.find(params[:project_id]) if params[:project_id]
73
    render_403 unless User.current.allowed_to?(:save_queries, @project, :global => true)
74 75
  rescue ActiveRecord::RecordNotFound
    render_404
76 77
  end
end