search_helper.rb
6.28 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module SearchHelper
# FIXME remove it after search_controler refactored
include EventsHelper
def relevance_for(hit)
n = (hit.ferret_score if hit.respond_to?(:ferret_score))
n ||= 1.0
(n * 100.0).round
end
def display_results(use_map = true)
unless use_map && GoogleMaps.enabled?(environment.default_hostname)
return render(:partial => 'display_results')
end
data =
if params[:display] == 'map'
{
:partial => 'google_maps',
:toggle => button(:search, _('Display in list'), params.merge(:display => 'list'), :class => "map-toggle-button" ),
:class => 'map' ,
}
else
{
:partial => 'display_results',
:toggle => button(:search, _('Display in map'), params.merge(:display => 'map'), :class => "map-toggle-button" ),
:class => 'list' ,
}
end
content_tag('div', data[:toggle] + (render :partial => data[:partial]), :class => "map-or-list-search-results #{data[:class]}")
end
def display_item_map_info(item)
if item.kind_of?(Profile)
display_profile_info(item)
elsif item.kind_of?(Product)
display_product_info(item)
end
end
def display_profile_info(profile)
data = ''
unless profile.contact_email.nil?
data << content_tag('strong', _('E-Mail: ')) + profile.contact_email + '<br/>'
end
unless profile.contact_phone.nil?
data << content_tag('strong', _('Phone(s): ')) + profile.contact_phone + '<br/>'
end
unless profile.region.nil?
data << content_tag('strong', _('Location: ')) + profile.region.name + '<br/>'
end
unless profile.address.nil?
data << content_tag('strong', _('Address: ')) + profile.address + '<br/>'
end
unless profile.products.empty?
data << content_tag('strong', _('Products/Services: ')) + profile.products.map{|i| link_to(i.name, :controller => 'manage_products', :profile => profile.identifier, :action => 'show', :id => i.id)}.join(', ') + '<br/>'
end
if profile.respond_to?(:distance) and !profile.distance.nil?
data << content_tag('strong', _('Distance: ')) + "%.2f%" % profile.distance + '<br/>'
end
content_tag('table',
content_tag('tr',
content_tag('td', content_tag('div', profile_image(profile, :thumb), :class => 'profile-info-picture')) +
content_tag('td', content_tag('strong', link_to(profile.name, url_for(profile.url))) + '<br/>' + data
)
),
:class => 'profile-info'
)
end
def display_product_info(product)
data = ''
unless product.price.nil?
data << content_tag('strong', _('Price: ')) + product.price + '<br/>'
end
unless product.enterprise.nil?
data << content_tag('strong', _('Provider: ')) + link_to_profile(product.enterprise.name, product.enterprise.identifier)
end
unless product.product_category.nil?
data << content_tag('strong', _('Category: ')) + link_to(product.product_category.name, :controller => 'search', :action => 'assets', :asset => 'products', :product_category => product.product_category.id)
end
content_tag('table',
content_tag('tr',
content_tag('td', content_tag('div', image_tag(product.image ? product.image.public_filename(:thumb) : '/images/icons-app/product-default-pic-portrait.png'), :class => 'profile-info-picture')) +
content_tag('td', content_tag('strong', link_to(product.name, :controller => 'catalog', :profile => product.enterprise.identifier, :id => product.id)) + '<br/>' + data)
), :class => 'profile-info')
end
def product_categories_menu(asset, product_category, object_ids = nil)
cats = ProductCategory.menu_categories(@product_category, environment)
cats += cats.select { |c| c.children_count > 0 }.map(&:children).flatten
product_categories_ids = cats.map(&:id)
counts = @noosfero_finder.product_categories_count(asset, product_categories_ids, object_ids)
product_categories_menu = ProductCategory.menu_categories(product_category, environment).map do |cat|
hits = counts[cat.id]
childs = []
if hits
if cat.children_count > 0
childs = cat.children.map do |child|
child_hits = counts[child.id]
[child, child_hits]
end.select{|child, child_hits| child_hits }
else
childs = []
end
end
[cat, hits, childs]
end.select{|cat, hits| hits }
render(:partial => 'product_categories_menu', :object => product_categories_menu)
end
def facets_menu(asset, _facets)
@asset_class = asset_class(asset)
@facets = _facets
render(:partial => 'facets_menu')
end
def facets_unselect_menu(asset)
@asset_class = asset_class(asset)
render(:partial => 'facets_unselect_menu')
end
def asset_class(asset)
asset.to_s.singularize.camelize.constantize
end
def asset_table(asset)
asset_class(asset).table_name
end
def order_by(asset)
options = {
:products => [[_('Best match'), ''], [_('Name'), 'name_sort asc'], [_('Lower price'), 'price asc'], [_('Higher price'), 'price desc']],
:events => [[_('Best match'), ''], [_('Name'), 'name_sort asc']],
:articles => [[_('Best match'), ''], [_('Name'), 'name_sort asc'], [_('Most recent'), 'updated_at desc']],
:enterprises => [[_('Best match'), ''], [_('Name'), 'name_sort asc']],
:people => [[_('Best match'), ''], [_('Name'), 'name_sort asc']],
:communities => [[_('Best match'), ''], [_('Name'), 'name_sort asc']],
}
content_tag('div', _('Order by ') +
select_tag(asset.to_s + '[order]', options_for_select(options[asset], params[:order_by]),
{:onchange => "window.location=jQuery.param.querystring(window.location.href, { 'order_by' : this.options[this.selectedIndex].value})"}),
:class => "search-ordering")
end
def label_total_found(asset, total_found)
labels = {
:products => _("%s products offers found"),
:articles => _("%s articles found"),
:events => _("%s events found"),
:people => _("%s people found"),
:enterprises => _("%s enterprises found"),
:communities => _("%s communities found"),
}
if labels[asset]
content_tag('span', labels[asset] % total_found,
:class => "total-pages-found")
else
''
end
end
end