aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/application_controller.rb98
-rw-r--r--app/controllers/general_controller.rb116
-rw-r--r--app/controllers/public_body_controller.rb23
-rw-r--r--app/controllers/request_controller.rb28
4 files changed, 207 insertions, 58 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 0df3e22da..cb64cb922 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -349,6 +349,104 @@ class ApplicationController < ActionController::Base
session[:last_body_id] = public_body.id
end
+ def param_exists(item)
+ return params[item] && !params[item].empty?
+ end
+
+ def get_request_variety_from_params
+ query = ""
+ sortby = "newest"
+ varieties = []
+ if params[:request_variety] && !(query =~ /variety:/)
+ if params[:request_variety].include? "sent"
+ varieties -= ['variety:sent', 'variety:followup_sent', 'variety:response', 'variety:comment']
+ varieties << ['variety:sent', 'variety:followup_sent']
+ end
+ if params[:request_variety].include? "response"
+ varieties << ['variety:response']
+ end
+ if params[:request_variety].include? "comment"
+ varieties << ['variety:comment']
+ end
+ end
+ if !varieties.empty?
+ query = " (#{varieties.join(' OR ')})"
+ end
+ return query
+ end
+
+ def get_status_from_params
+ query = ""
+ if params[:latest_status]
+ statuses = []
+ if params[:latest_status].class == String
+ params[:latest_status] = [params[:latest_status]]
+ end
+ if params[:latest_status].include?("recent") || params[:latest_status].include?("all")
+ query += " variety:sent"
+ end
+ if params[:latest_status].include? "successful"
+ statuses << ['latest_status:successful', 'latest_status:partially_successful']
+ end
+ if params[:latest_status].include? "unsuccessful"
+ statuses << ['latest_status:rejected', 'latest_status:not_held']
+ end
+ if params[:latest_status].include? "awaiting"
+ statuses << ['latest_status:waiting_response', 'latest_status:waiting_clarification', 'waiting_classification:true']
+ end
+ if params[:latest_status].include? "internal_review"
+ statuses << ['status:internal_review']
+ end
+ if params[:latest_status].include? "other"
+ statuses << ['latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin', 'latest_status:user_withdrawn']
+ end
+ if params[:latest_status].include? "gone_postal"
+ statuses << ['latest_status:gone_postal']
+ end
+ if !statuses.empty?
+ query = " (#{statuses.join(' OR ')})"
+ end
+ end
+ return query
+ end
+
+ def get_date_range_from_params
+ query = ""
+ if param_exists(:request_date_after) && !param_exists(:request_date_before)
+ params[:request_date_before] = Time.now.strftime("%d/%m/%Y")
+ query += " #{params[:request_date_after]}..#{params[:request_date_before]}"
+ elsif !param_exists(:request_date_after) && param_exists(:request_date_before)
+ params[:request_date_after] = "01/01/2001"
+ end
+ if param_exists(:request_date_after)
+ query = " #{params[:request_date_after]}..#{params[:request_date_before]}"
+ end
+ return query
+ end
+
+ def get_tags_from_params
+ query = ""
+ tags = []
+ if param_exists(:tags)
+ params[:tags].split().each do |tag|
+ tags << "tag:#{tag}"
+ end
+ end
+ if !tags.empty?
+ query = " (#{tags.join(' OR ')})"
+ end
+ return query
+ end
+
+ def make_query_from_params
+ query = params[:query] || "" if query.nil?
+ query += get_date_range_from_params
+ query += get_request_variety_from_params
+ query += get_status_from_params
+ query += get_tags_from_params
+ return query
+ end
+
# URL generating functions are needed by all controllers (for redirects),
# views (for links) and mailers (for use in emails), so include them into
# all of all.
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb
index 4fa603aab..2d0527d3a 100644
--- a/app/controllers/general_controller.rb
+++ b/app/controllers/general_controller.rb
@@ -82,20 +82,34 @@ class GeneralController < ApplicationController
# Just does a redirect from ?query= search to /query
def search_redirect
- @query = params[:query]
+ if params[:advanced].nil?
+ @query, _ = make_query_from_params
+ else
+ @query, _ = params[:query]
+ end
@sortby = params[:sortby]
- @bodies = params[:bodies]
+ path = request.path.split("/")
+ if path.size > 0 && (['newest', 'described', 'relevant'].include?(path[-1]))
+ @sort_postfix = path.pop
+ end
+ if path.size > 0 && (['bodies', 'requests', 'users', 'all'].include?(path[-1]))
+ @variety_postfix = path.pop
+ end
+ @variety_postfix = params[:bodies] if @variety_postfix.nil? && !params[:bodies].nil?
+ @variety_postfix = "all" if @variety_postfix.nil?
+ if @variety_postfix != "users"
+ @common_query = get_tags_from_params
+ end
+ [:latest_status, :request_variety, :request_date_after, :request_date_before, :query, :tags].each do |x|
+ session[x] = params[x]
+ end
if @query.nil? || @query.empty?
@query = nil
@page = 1
+ @advanced = !params[:advanced].nil?
render :action => "search"
else
- if (@bodies == '1') && (@sortby.nil? || @sortby.empty?)
- @postfix = 'bodies'
- else
- @postfix = @sortby
- end
- redirect_to search_url(@query, @postfix)
+ redirect_to search_url(@query, @variety_postfix, @sort_postfix, params[:advanced])
end
end
@@ -103,23 +117,49 @@ class GeneralController < ApplicationController
def search
# XXX Why is this so complicated with arrays and stuff? Look at the route
# in config/routes.rb for comments.
+ if !params[:commit].nil?
+ search_redirect
+ return
+ end
+ [:latest_status, :request_variety, :request_date_after, :request_date_before, :query, :tags].each do |x|
+ params[x] = session[x]
+ end
combined = params[:combined]
@sortby = nil
- @bodies = false # searching from front page, largely for a public authority
+ @bodies = @requests = @users = true
+ if combined.size > 0 && (['advanced'].include?(combined[-1]))
+ combined.pop
+ @advanced = true
+ else
+ @advanced = false
+ end
# XXX currently /described isn't linked to anywhere, just used in RSS and for /list/successful
# This is because it's confusingly different from /newest - but still useful for power users.
- if combined.size > 1 && (['newest', 'described', 'bodies', 'relevant'].include?(combined[-1]))
- @postfix = combined[-1]
- combined = combined[0..-2]
- if @postfix == 'bodies'
+ if combined.size > 0 && (['newest', 'described', 'relevant'].include?(combined[-1]))
+ @sort_postfix = combined.pop
+ @sortby = @sort_postfix
+ end
+
+ if combined.size > 0 && (['bodies', 'requests', 'users', 'all'].include?(combined[-1]))
+ @variety_postfix = combined.pop
+ case @variety_postfix
+ when 'bodies'
@bodies = true
- else
- @sortby = @postfix
+ @requests = false
+ @users = false
+ when 'requests'
+ @bodies = false
+ @requests = true
+ @users = false
+ when 'users'
+ @bodies = false
+ @requests = false
+ @users = true
end
end
@query = combined.join("/")
-
@inputted_sortby = @sortby
+ @common_query = get_tags_from_params
if @sortby.nil?
# Parse query, so can work out if it has prefix terms only - if so then it is a
# structured query which should show newest first, rather than a free text search
@@ -145,21 +185,41 @@ class GeneralController < ApplicationController
if params[:requests_per_page]
requests_per_page = params[:requests_per_page].to_i
end
- @xapian_requests = perform_search([InfoRequestEvent], @query, @sortby, 'request_collapse', requests_per_page)
- @requests_per_page = @per_page
- @xapian_bodies = perform_search([PublicBody], @query, @sortby, nil, 5)
- @bodies_per_page = @per_page
- @xapian_users = perform_search([User], @query, @sortby, nil, 5)
- @users_per_page = @per_page
-
- @this_page_hits = @xapian_requests.results.size + @xapian_bodies.results.size + @xapian_users.results.size
- @total_hits = @xapian_requests.matches_estimated + @xapian_bodies.matches_estimated + @xapian_users.matches_estimated
+ @this_page_hits = @total_hits = @xapian_requests_hits = @xapian_bodies_hits = @xapian_users_hits = 0
+ if @requests
+ @xapian_requests = perform_search([InfoRequestEvent], @query, @sortby, 'request_collapse', requests_per_page)
+ @requests_per_page = @per_page
+ @this_page_hits += @xapian_requests.results.size
+ @xapian_requests_hits = @xapian_requests.results.size
+ @xapian_requests_total_hits = @xapian_requests.matches_estimated
+ @total_hits += @xapian_requests.matches_estimated
+ end
+ if @bodies
+ @xapian_bodies = perform_search([PublicBody], @query, @sortby, nil, 5)
+ @bodies_per_page = @per_page
+ @this_page_hits += @xapian_bodies.results.size
+ @xapian_bodies_hits = @xapian_bodies.results.size
+ @xapian_bodies_total_hits = @xapian_bodies.matches_estimated
+ @total_hits += @xapian_bodies.matches_estimated
+ end
+ if @users
+ @xapian_users = perform_search([User], @query, @sortby, nil, 5)
+ @users_per_page = @per_page
+ @this_page_hits += @xapian_users.results.size
+ @xapian_users_hits = @xapian_users.results.size
+ @xapian_users_total_hits = @xapian_users.matches_estimated
+ @total_hits += @xapian_users.matches_estimated
+ end
# Spelling and highight words are same for all three queries
- @spelling_correction = @xapian_requests.spelling_correction
- @highlight_words = @xapian_requests.words_to_highlight
+ if !@xapian_requests.nil?
+ @highlight_words = @xapian_requests.words_to_highlight
+ if !(@xapian_requests.spelling_correction =~ /[a-z]+:/)
+ @spelling_correction = @xapian_requests.spelling_correction
+ end
+ end
- @track_thing = TrackThing.create_track_for_search_query(@query)
+ @track_thing = TrackThing.create_track_for_search_query(@query, @variety_postfix)
@feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ]
end
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb
index 05acf4868..830f37855 100644
--- a/app/controllers/public_body_controller.rb
+++ b/app/controllers/public_body_controller.rb
@@ -16,7 +16,6 @@ class PublicBodyController < ApplicationController
redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'body'), :status => :moved_permanently
return
end
-
@locale = self.locale_from_params()
PublicBody.with_locale(@locale) do
@public_body = PublicBody.find_by_url_name_with_historic(params[:url_name])
@@ -39,11 +38,16 @@ class PublicBodyController < ApplicationController
if !referrer.nil? && referrer.match(%r{^#{top_url}search/.*/bodies$})
@searched_to_send_request = true
end
+ @view = params[:view]
+ params[:latest_status] = @view
+ query = make_query_from_params
+ query += " requested_from:#{@public_body.url_name}"
# Use search query for this so can collapse and paginate easily
# XXX really should just use SQL query here rather than Xapian.
+ sortby = "described"
begin
- @xapian_requests = perform_search([InfoRequestEvent], 'requested_from:' + @public_body.url_name, 'newest', 'request_collapse')
+ @xapian_requests = perform_search([InfoRequestEvent], query, sortby, 'request_collapse')
if (@page > 1)
@page_desc = " (page " + @page.to_s + ")"
else
@@ -83,29 +87,30 @@ class PublicBodyController < ApplicationController
def list
long_cache
# XXX move some of these tag SQL queries into has_tag_string.rb
+ @query = "%#{params[:public_body_query].nil? ? "" : params[:public_body_query]}%"
@tag = params[:tag]
@locale = self.locale_from_params()
- locale_condition = 'public_body_translations.locale = ?'
- if @tag.nil?
+ locale_condition = "(upper(public_body_translations.name) LIKE upper(?) OR upper(public_body_translations.notes) LIKE upper (?)) AND public_body_translations.locale = ?"
+ if @tag.nil? or @tag == "all"
@tag = "all"
- conditions = [locale_condition, @locale]
+ conditions = [locale_condition, @query, @query, @locale]
elsif @tag == 'other'
category_list = PublicBodyCategories::CATEGORIES.map{|c| "'"+c+"'"}.join(",")
conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id
and has_tag_string_tags.model = \'PublicBody\'
- and has_tag_string_tags.name in (' + category_list + ')) = 0', @locale]
+ and has_tag_string_tags.name in (' + category_list + ')) = 0', @query, @query, @locale]
elsif @tag.size == 1
@tag.upcase!
- conditions = [locale_condition + ' AND public_body_translations.first_letter = ?', @locale, @tag]
+ conditions = [locale_condition + ' AND public_body_translations.first_letter = ?', @query, @query, @locale, @tag]
elsif @tag.include?(":")
name, value = HasTagString::HasTagStringTag.split_tag_into_name_value(@tag)
conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id
and has_tag_string_tags.model = \'PublicBody\'
- and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', @locale, name, value]
+ and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', @query, @query, @locale, name, value]
else
conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id
and has_tag_string_tags.model = \'PublicBody\'
- and has_tag_string_tags.name = ?) > 0', @locale, @tag]
+ and has_tag_string_tags.name = ?) > 0', @query, @query, @locale, @tag]
end
if @tag.size == 1
@description = _("beginning with") + " '" + @tag + "'"
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index c1a13273a..79b8e4816 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -129,27 +129,12 @@ class RequestController < ApplicationController
def list
medium_cache
@view = params[:view]
-
- if @view.nil?
- redirect_to request_list_url(:view => 'successful')
- return
- end
-
- if @view == 'recent'
- @title = _("Recently sent Freedom of Information requests")
- query = "variety:sent";
- sortby = "newest"
- @track_thing = TrackThing.create_track_for_all_new_requests
- elsif @view == 'successful'
- @title = _("Recently successful responses")
- query = 'variety:response (status:successful OR status:partially_successful)'
- sortby = "described"
- @track_thing = TrackThing.create_track_for_all_successful_requests
- else
- raise "unknown request list view " + @view.to_s
- end
-
+ params[:latest_status] = @view
+ query = make_query_from_params
+ @title = "View and search requests"
+ sortby = "newest"
@page = get_search_page_from_params if !@page # used in cache case, as perform_search sets @page as side effect
+
behavior_cache :tag => [@view, @page] do
xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_collapse')
@list_results = xapian_object.results.map { |r| r[:model] }
@@ -158,7 +143,8 @@ class RequestController < ApplicationController
@title = @title + " (page " + @page.to_s + ")" if (@page > 1)
- @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ]
+ # XXX need to reinstate the following; @track_thing had previously been set to "TrackThing.create_track_for_all_new_requests" and "TrackThing.create_track_for_all_successful_requests"
+ # @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ]
# Don't let robots go more than 20 pages in
if @page > 20