-
Notifications
You must be signed in to change notification settings - Fork 223
/
agents_controller.rb
122 lines (113 loc) · 4.18 KB
/
agents_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class AgentsController < ApplicationController
include ResultInfo
include TreeApis
skip_before_filter :verify_authenticity_token
DEFAULT_AG_TYPES = %w{repository resource archival_object digital_object}
DEFAULT_AG_FACET_TYPES = %w{primary_type subjects}
DEFAULT_AG_SEARCH_OPTS = {
'sort' => 'title_sort asc',
'resolve[]' => ['repository:id', 'resource:id@compact_resource', 'ancestors:id@compact_resource'],
'facet.mincount' => 1
}
DEFAULT_AG_SEARCH_PARAMS = {
:q => ['*'],
:limit => 'agent',
:op => ['OR'],
:field => ['title']
}
def index
repo_id = params.fetch(:rid, nil)
Rails.logger.debug("repo_id: #{repo_id}")
if !params.fetch(:q, nil)
DEFAULT_AG_SEARCH_PARAMS.each do |k, v|
params[k] = v unless params.fetch(k,nil)
end
end
search_opts = default_search_opts(DEFAULT_AG_SEARCH_OPTS)
search_opts['fq'] = ["used_within_published_repository:\"/repositories/#{repo_id}\""] if repo_id
@base_search = repo_id ? "/repositories/#{repo_id}/agents?" : '/agents?'
default_facets = DEFAULT_AG_FACET_TYPES
default_facets.push('used_within_published_repository') unless repo_id
page = Integer(params.fetch(:page, "1"))
begin
set_up_and_run_search( DEFAULT_AG_TYPES, default_facets, search_opts, params)
rescue Exception => error
flash[:error] = error
redirect_back(fallback_location: '/') and return
end
@context = repo_context(repo_id, 'agent')
if @results['total_hits'] > 1
@search[:dates_within] = false
@search[:text_within] = true
end
@page_title = I18n.t('agent._plural')
@results_type = @page_title
all_sorts = Search.get_sort_opts
@sort_opts = []
%w(title_sort_asc title_sort_desc).each do |type|
@sort_opts.push(all_sorts[type])
end
if params[:q].size > 1 || params[:q][0] != '*'
@sort_opts.unshift(all_sorts['relevance'])
end
@no_statement = true
render 'search/search_results'
end
def search
# need at least q[]=WHATEVER&op[]=OR&field[]=title&from_year[]=&to_year[]=&limit=pui_agent
@base_search = '/agents/search?'
page = Integer(params.fetch(:page, "1"))
begin
set_up_and_run_search( DEFAULT_AG_TYPES, DEFAULT_AG_FACET_TYPES, DEFAULT_AG_SEARCH_OPTS, params)
rescue Exception => error
flash[:error] = error
redirect_back(fallback_location: '/agents') and return
end
@page_title = I18n.t('agent._plural')
@results_type = @page_title
@search_title = I18n.t('search_results.search_for', {:type => I18n.t('agent._plural'), :term => params.fetch(:q)[0]})
render 'search/search_results'
end
def show
uri = "/agents/#{params[:eid]}/#{params[:id]}"
@criteria = {}
@criteria['resolve[]'] = ['repository:id', 'resource:id@compact_resource','related_agent_uris:id' ]
begin
@result = archivesspace.get_record(uri, @criteria)
@results = fetch_agent_results(@result['title'],uri, params)
if !@results.blank?
params[:q] = '*'
#Pry::ColorPrinter.pp(@results['results'])
@pager = Pager.new("#{uri}?q=#{params.fetch(:q,'*')}", @results['this_page'],@results['last_page'])
else
@pager = nil
end
@page_title = strip_mixed_content(@result['json']['title']) || "#{I18n.t('an_agent')}: #{uri}"
Rails.logger.debug("Agent title: #{@page_title}")
@context = []
rescue RecordNotFound
@type = I18n.t('pui_agent._singular')
@page_title = I18n.t('errors.error_404', :type => @type)
@uri = uri
@back_url = request.referer || ''
render 'shared/not_found'
end
end
private
def fetch_agent_results(title, uri, params)
@results = []
qry = "agents:\"#{title}\" AND types:pui"
@base_search = "#{uri}?"
set_up_search(DEFAULT_AG_TYPES, DEFAULT_AG_FACET_TYPES, DEFAULT_AG_SEARCH_OPTS, params,qry)
# we do this to compensate for the way @base_search gets munged in the setup
@base_search= @base_search.sub("q=#{qry}", '')
page = Integer(params.fetch(:page, "1"))
@results = archivesspace.search(qry,page, @criteria)
if @results['total_hits'] > 0
process_search_results(@base_search)
else
@results = []
end
@results
end
end