| Class | SearchController |
| In: |
app/controllers/public/search_controller.rb
|
| Parent: | PublicController |
| category | [R] |
view the summary of one category
# File app/controllers/public/search_controller.rb, line 191
191: def category_index
192: @results = {}
193: @order = []
194: @names = {}
195: [
196: [ :people, _('People'), @finder.recent('people', limit) ],
197: [ :enterprises, __('Enterprises'), @finder.recent('enterprises', limit) ],
198: [ :products, _('Products'), @finder.recent('products', limit) ],
199: [ :events, _('Upcoming events'), @finder.upcoming_events({:per_page => limit}) ],
200: [ :communities, __('Communities'), @finder.recent('communities', limit) ],
201: [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles(limit) ],
202: [ :articles, _('Articles'), @finder.recent('text_articles', limit) ]
203: ].each do |key, name, list|
204: @order << key
205: @results[key] = list
206: @names[key] = name
207: end
208: end
# File app/controllers/public/search_controller.rb, line 142
142: def cities
143: @cities = City.find(:all, :order => 'name', :conditions => ['parent_id = ? and lat is not null and lng is not null', params[:state_id]])
144: render :action => 'cities', :layout => false
145: end
# File app/controllers/public/search_controller.rb, line 147
147: def complete_region
148: # FIXME this logic should be in the model
149: @regions = Region.find(:all, :conditions => [ '(name like ? or name like ?) and lat is not null and lng is not null', '%' + params[:region][:name] + '%', '%' + params[:region][:name].capitalize + '%' ])
150: render :action => 'complete_region', :layout => false
151: end
# File app/controllers/public/search_controller.rb, line 153
153: def index
154: @query = params[:query] || ''
155: @filtered_query = remove_stop_words(@query)
156: @product_category = ProductCategory.find(params[:product_category]) if params[:product_category]
157:
158: @region = City.find_by_id(params[:city]) if !params[:city].blank? && params[:city] =~ /^\d+$/
159:
160: # how many assets we are searching for?
161: number_of_result_assets = @searching.values.select{|v| v}.size
162:
163: @results = {}
164: @order = []
165: @names = {}
166:
167: where_to_search.select { |key,description| @searching[key] }.each do |key, description|
168: @order << key
169: @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]))
170: @names[key] = getterm(description)
171: end
172:
173: if @results.keys.size == 1
174: specific_action = @results.keys.first
175: if respond_to?(specific_action)
176: @asset_name = getterm(@names[@results.keys.first])
177: send(specific_action)
178: render :action => specific_action
179: return
180: end
181: end
182:
183: render :action => 'index'
184: end
# File app/controllers/public/search_controller.rb, line 225
225: def popup
226: @regions = Region.find(:all).select{|r|r.lat && r.lng}
227: render :action => 'popup', :layout => false
228: end
# File app/controllers/public/search_controller.rb, line 218
218: def tag
219: @tag = Tag.find_by_name(params[:tag])
220: @tagged = @tag.taggings.map(&:taggable)
221: end
# File app/controllers/public/search_controller.rb, line 211
211: def tags
212: @tags = Tag.find(:all).inject({}) do |memo,tag|
213: memo[tag.name] = tag.taggings.count
214: memo
215: end
216: end
# File app/controllers/public/search_controller.rb, line 131
131: def where_to_search
132: [
133: [ :articles, N_('Articles') ],
134: [ :enterprises, N_('Enterprises') ],
135: [ :people, N_('People') ],
136: [ :communities, N_('Communities') ],
137: [ :products, N_('Products') ],
138: [ :events, N_('Events') ]
139: ].select {|key, name| !environment.enabled?('disable_asset_' + key.to_s) }
140: end
# File app/controllers/public/search_controller.rb, line 82
82: def articles
83: #nothins, just to enable
84: end
# File app/controllers/public/search_controller.rb, line 101
101: def calculate_find_options(asset, limit, page, product_category, region, radius, year, month)
102: result = { :product_category => product_category, :per_page => limit, :page => page }
103: if [:enterprises, :people, :products].include?(asset) && region
104: result.merge!(:within => radius, :region => region.id)
105: end
106:
107: if month || year
108: result[:date_range] = Event.date_range(year, month)
109: end
110:
111: result
112: end
# File app/controllers/public/search_controller.rb, line 31
31: def check_search_whole_site
32: if params[:search_whole_site_yes] or params[:search_whole_site] == 'yes'
33: redirect_to params.merge(:category_path => [], :search_whole_site => nil, :search_whole_site_yes => nil)
34: end
35: end
# File app/controllers/public/search_controller.rb, line 37
37: def check_valid_assets
38: @asset = params[:asset].to_sym
39: if !where_to_search.map(&:first).include?(@asset)
40: render :text => 'go away', :status => 403
41: return
42: end
43: end
# File app/controllers/public/search_controller.rb, line 79
79: def communities
80: #nothing, just to enable
81: end
# File app/controllers/public/search_controller.rb, line 75
75: def enterprises
76: load_product_categories_menu(:enterprises)
77: @categories_menu = true
78: end
# File app/controllers/public/search_controller.rb, line 45
45: def events
46: @events = @results[:events]
47: @calendar = Event.date_range(params[:year], params[:month]).map do |date|
48: [
49: # the day itself
50: date,
51: # list of events of that day
52: @events.select do |event|
53: event.date_range.include?(date)
54: end,
55: # is this date in the current month?
56: true
57: ]
58: end
59:
60: # pad with days before
61: while @calendar.first.first.wday != 0
62: @calendar.unshift([@calendar.first.first - 1.day, [], false])
63: end
64:
65: # pad with days after (until Saturday)
66: while @calendar.last.first.wday != 6
67: @calendar << [@calendar.last.first + 1.day, [], false]
68: end
69:
70: end
limit the number of results per page TODO: dont hardcore like this
# File app/controllers/public/search_controller.rb, line 116
116: def limit
117: searching = @searching.values.select{|v|v}
118: if params[:display] == 'map'
119: 100
120: else
121: (searching.size == 1) ? 20 : 6
122: end
123: end
# File app/controllers/public/search_controller.rb, line 91
91: def load_product_categories_menu(asset)
92: @results[asset].uniq!
93: # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop
94:
95: if !@query.blank? || @region && !params[:radius].blank?
96: @result_ids = @finder.find(asset, @filtered_query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]).merge({:limit => :all}))
97: end
98:
99: end
# File app/controllers/public/search_controller.rb, line 15
15: def load_search_assets
16: @search_in = where_to_search
17: @searching = {}
18: @search_in.each do |key, name|
19: @searching[key] = (params[:asset].blank? && (params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s))) || (params[:asset] == key.to_s)
20: end
21: end
# File app/controllers/public/search_controller.rb, line 72
72: def people
73: #nothing, just to enable
74: end
# File app/controllers/public/search_controller.rb, line 23
23: def prepare_filter
24: if @category
25: @finder = CategoryFinder.new(@category)
26: else
27: @finder = EnvironmentFinder.new(@environment)
28: end
29: end