Orbit/app/controllers/default_widget_controller.rb

109 lines
4.5 KiB
Ruby
Raw Normal View History

2012-12-03 10:52:36 +00:00
class DefaultWidgetController< OrbitWidgetController
def front_end_available(var)
2013-01-21 08:56:37 +00:00
@page_part = case params[:item_type]
when "page"
Page.find params[:page_id]
when "page_part"
PagePart.find params[:part_id]
end
2012-12-03 10:52:36 +00:00
@page_part.module_app.enable_frontend?
end
def query_for_default_widget
2013-08-23 03:03:28 +00:00
@ori_class_name = @default_widget["query"].split('.')[0]
@ori_query = @default_widget["query"]
result = nil
result_objects = nil
if !params["tag_id"].blank?
2013-03-05 10:34:41 +00:00
if params["category_id"].blank? #has tag no cate
2013-09-02 08:33:50 +00:00
result = eval(@ori_query)
result.selector[:tagged_ids] = { "$in" => params['tag_id'].collect{|t| BSON::ObjectId(t)}}
2013-03-05 10:34:41 +00:00
else #has tag and cate
2013-09-02 08:33:50 +00:00
result = eval(@ori_query)
result.selector[get_category_field_name] = { "$in" => params['category_id'].collect{|t| BSON::ObjectId(t)}}
result.selector[:tagged_ids] = { "$in" => params['tag_id'].collect{|t| BSON::ObjectId(t)}}
end
2013-03-05 10:34:41 +00:00
elsif params["category_id"].blank? #no tag no cate
2013-08-23 03:03:28 +00:00
result = eval(@ori_query)
2013-03-20 18:29:56 +00:00
else #no tag has cate
2013-09-02 08:33:50 +00:00
result = eval(@ori_query)
result.selector[get_category_field_name] = { "$in" => params['category_id'].collect{|t| BSON::ObjectId(t)}}
end
2013-09-02 08:33:50 +00:00
eval("result.#{@default_widget[:sorting_query]}")
end
2013-06-05 05:00:21 +00:00
def search_result
@search = query_for_default_widget.solr_search do
fulltext params[:search_query]
end
search_result = @search.results.collect{|result| result.id}
@items = eval(@ori_class_name).all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result)
end
2012-12-03 10:52:36 +00:00
def default_widget
2013-06-05 05:00:21 +00:00
@search = params["search_query"].blank? ? false : true
2013-07-15 09:09:48 +00:00
if !params[:id].blank? and !params["clicked_field_name"].blank?
2013-04-03 11:03:16 +00:00
clicked_field_name = params["clicked_field_name"].to_sym
redirect_to eval("#{@page_part.module_app.widget_fields_link_method[clicked_field_name][:method]}('#{params[:id]}', {inner: #{params[:inner] || true}})")
2013-07-15 09:09:48 +00:00
elsif !params[:id].blank? and params["preview"]
clicked_field_name = :title
@preview_pages = @page_part.module_app.widget_fields_link_method.find_all{|t| t[1].has_key?(:preview)}.collect{|t| t[0]}
redirect_to eval("#{@page_part.module_app.widget_fields_link_method[clicked_field_name][:method]}('#{params[:id]}', {preview: true,inner: #{params[:inner] || true},clicked_field_name: '#{@preview_pages.first.to_s}'})")
else
@tag_class = nil
@default_widget = @page_part.module_app.get_default_widget
@widget_image_field = @default_widget["image"] || @default_widget[:image]
case @page_part
when Page
2013-04-01 09:06:10 +00:00
@frontend_field_names = @page_part.frontend_field_name
@frontend_classes = @page_part.frontend_class
@frontend_sat_to_links = @page_part.frontend_sat_to_link
@data_limit = @page_part.frontend_data_count.is_a?(Fixnum) ? @page_part.frontend_data_count : (@page_part.frontend_data_count.to_i rescue 3)
@paginate = true
@page_id = @page_part.id
when PagePart
2013-04-01 09:06:10 +00:00
@frontend_field_names = @page_part.widget_field_name
@frontend_classes = @page_part.widget_class
@frontend_sat_to_links = @page_part.widget_sat_to_link
@fields = @page_part.widget_field
@data_limit = @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3)
end
2013-06-05 05:00:21 +00:00
if @paginate || @search
if @search
@data = search_result.page(params[:page_main]).per(@data_limit) rescue []
else #regular listing
@data = query_for_default_widget.includes(@widget_image_field).page(params[:page_main]).per(@data_limit)
end
# @data = query_for_default_widget.limit(@data_limit).includes(@widget_image_field)
else
2013-03-20 18:29:56 +00:00
@data = query_for_default_widget.includes(@widget_image_field).limit(@data_limit)
# @data = eval(@default_widget["query"]).limit(@data_limit).includes(@widget_image_field).desc(:created_at)
end
case params[:type]
when "typeA"
@tag_class = 'default_widget_typeA'
@partial = "typeA"
when /typeB_/
@tag_class = "default_widget_#{params[:type]}"
@partial = "typeB"
when "typeC"
@tag_class = 'default_widget_typeC'
@partial = "typeC"
end
2013-01-21 08:56:37 +00:00
end
2012-12-03 10:52:36 +00:00
end
def get_category_field_name
2013-09-02 08:33:50 +00:00
@ori_class_name = @ori_class_name.constantize unless @ori_class_name.is_a? Class
2013-06-05 05:00:21 +00:00
@ori_class_name.fields.each_key do |key|
2013-09-02 08:33:50 +00:00
return key.to_s if key.include?('category_id')
2013-03-05 10:34:41 +00:00
end
nil
end
2012-12-03 10:52:36 +00:00
end