1 # Redmine - project management software
2 # Copyright (C) 2006-2011 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
18 class QueriesController < ApplicationController
19 menu_item :issues
20 before_filter :find_query, :except => [:new, :create, :index]
21 before_filter :find_optional_project, :only => [:new, :create]
22
23 accept_api_auth :index
24
25 include QueriesHelper
26
27 def index
28 case params[:format]
29 when 'xml', 'json'
30 @offset, @limit = api_offset_and_limit
31 else
32 @limit = per_page_option
33 end
34
35 @query_count = Query.visible.count
36 @query_pages = Paginator.new self, @query_count, @limit, params['page']
37 @queries = Query.visible.all(:limit => @limit, :offset => @offset, :order => "#{Query.table_name}.name")
38
39 respond_to do |format|
40 format.html { render :nothing => true }
41 format.api
42 end
43 end
44
45 def new
46 @query = Query.new
47 @query.user = User.current
48 @query.project = @project
49 @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
50 build_query_from_params
51 end
52
53 def create
54 @query = Query.new(params[:query])
55 @query.user = User.current
56 @query.project = params[:query_is_for_all] ? nil : @project
57 @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
58 build_query_from_params
59 @query.column_names = nil if params[:default_columns]
60
61 if @query.save
62 flash[:notice] = l(:notice_successful_create)
63 redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
64 else
65 render :action => 'new', :layout => !request.xhr?
66 end
67 end
68
69 def edit
70 end
71
72 def update
73 @query.attributes = params[:query]
74 @query.project = nil if params[:query_is_for_all]
75 @query.is_public = false unless User.current.allowed_to?(:manage_public_queries, @project) || User.current.admin?
76 build_query_from_params
77 @query.column_names = nil if params[:default_columns]
78
79 if @query.save
80 flash[:notice] = l(:notice_successful_update)
81 redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :query_id => @query
82 else
83 render :action => 'edit'
84 end
85 end
86
87 def destroy
88 @query.destroy
89 redirect_to :controller => 'issues', :action => 'index', :project_id => @project, :set_filter => 1
90 end
91
92 private
93 def find_query
94 @query = Query.find(params[:id])
95 @project = @query.project
96 render_403 unless @query.editable_by?(User.current)
97 rescue ActiveRecord::RecordNotFound
98 render_404
99 end
100
101 def find_optional_project
102 @project = Project.find(params[:project_id]) if params[:project_id]
103 render_403 unless User.current.allowed_to?(:save_queries, @project, :global => true)
104 rescue ActiveRecord::RecordNotFound
105 render_404
106 end
107 end