announcement-test/temp_file/app/helpers/orbit_helper.rb

643 lines
20 KiB
Ruby

module OrbitHelper
def self.set_params(params,current_user)
@params = params
@current_user = current_user
end
def self.set_page_controller(c)
@page_controller = c
end
def self.get_page_controller
@page_controller
end
def self.set_page_categories(categories)
@categories = categories;
end
def self.set_page_tags(tags)
@tags = tags;
end
def self.create_item_json(root_page=nil)
locale = I18n.locale
pipeline = [{"$sort" => {"number" => 1}}]
if !root_page.nil? #item include root_page
pipeline << {"$match" => {"$or" => [{"url" => (root_page.url=='/' ? /^\// : /^#{root_page.url}\//)},{"_id" => root_page.id}]}}
end
pipeline += [
{"$project" => {"name" => "$name.#{locale}",
"url" => {"$cond" => [{"$gt"=>["$external_url.#{locale}", ""]},
"$external_url.#{locale}",
{"$concat" =>[{"$literal"=> (@prefix_url ? @prefix_url : "/#{locale.to_s}")},"$url"]},
]},
"parent_page_id" => "$parent_page_id",
"id" => "$_id",
"page_id" => "$page_id",
"module" => "$module",
"page_type" => {"$cond" => [{"$lte"=>["$page_type", nil]},
"page",
"$page_type"
]},
"access_level" => {"$cond" => [{"$lte"=>["$access_level", nil]},
"none",
"$access_level"
]},
"enabled_for" => "$enabled_for",
"menu_enabled_for" => "$menu_enabled_for",
"is_root" => {"$cond" => [{"$lte"=>["$parent_page_id", nil]},
true,
false
]}
}
},
{"$project" => {"name" => "$name",
"url"=>"$url",
"parent_page_id" => "$parent_page_id",
"id" => "$_id",
"page_id" => "$page_id",
"module" => "$module",
"page_type" => "$page_type",
"access_level" => "$access_level",
"enabled_for" => "$enabled_for",
"menu_enabled_for" => "$menu_enabled_for",
"is_root" => "$is_root",
"target"=>{"$cond" => [{"$eq" => [{"$substr" => ["$url",0,4]}, "http"]},
{"$literal"=> "_blank"},
{"$literal"=> "_self"},
]}
}
},
{"$group" => {"_id"=>"$parent_page_id","children"=>{"$push"=>"$$ROOT"}}}
]
return Page.collection.aggregate(pipeline).inject({}) do |r, s|
r.merge!({s['_id'] => s['children']})
end
end
def self.register_subpart(subpart_id)
subpart = SubPart.find(subpart_id) rescue nil
if !subpart.nil?
self.set_current_widget subpart
self.set_widget_data_count subpart.data_count
self.set_widget_categories subpart.categories
self.set_widget_module_app subpart.module
self.set_widget_item_url subpart
self.set_widget_title subpart.title
self.set_widget_categories subpart.categories || []
self.set_widget_tags subpart.tags || []
custom_value = subpart.custom_string_field || subpart.custom_array_field || subpart.custom_data_field rescue nil
if !custom_value.nil?
self.set_widget_custom_value custom_value
else
self.set_widget_custom_value nil
end
end
end
def self.set_page_role_status(role_status)
@role_status = role_status;
end
def self.set_member_sort_position(sort_position)
@sort_position = sort_position;
end
def self.set_page_data_count(data_count)
@data_count = data_count
end
def self.page_data_count
@data_count
end
def self.set_page_number(page)
@page_number = page
if @page_number == 0
@page_number = 1
end
end
def self.page_number
@page_number || 1
end
def self.page_categories
@categories
end
def self.page_tags
@tags
end
def self.page_role_status
@role_status
end
def self.member_sort_position
@sort_position
end
def self.current_user
@current_user
end
def self.page_for_tag(tag)
if @params[:url]
page_for_tag_url = "/#{@site_locale}#{@params[:url]}"
else
page_for_tag = nil
pages = Page.where(:module => @module_app.key)
pages.each do |page|
if page.tags.count ==1
if page.tags.include?(tag)
page_for_tag = page
end
end
break if !page_for_tag.nil?
end
if page_for_tag.nil?
pages.each do |page|
if page.tags.include?(tag)
page_for_tag = page
end
break if !page_for_tag.nil?
end
end
page_for_tag = pages.first if page_for_tag.nil?
page_for_tag_url = page_for_tag.url
end
'/'+I18n.locale.to_s + page_for_tag_url + '?tags[]='+tag.id.to_s
end
def self.this_module_app
@module_app
end
def self.widget_module_app
@widget_module_app
end
def self.set_this_module_app(module_app)
@module_app = ModuleApp.where(:key=>module_app).first
end
def self.set_widget_custom_value(value)
@widget_custom_value = value
end
def self.widget_custom_value
@widget_custom_value
end
def self.set_widget_module_app(module_app)
@widget_module_app = ModuleApp.where(:key=>module_app).first
end
def self.set_widget_title(title)
@widget_title = title
end
def self.widget_title
@widget_title
end
def self.set_widget_tags(tags)
@widget_tags = tags
end
def self.widget_tags
@widget_tags
end
def self.user_can_edit?(obj)
return false if @current_user.nil?
return true if @current_user.is_admin?
if obj.class == Page
@site ||= ApplicationHelper::CurrentSite
if @site.has_sub_home
if obj.tmp_root_page_id
new_obj = Page.find(obj.tmp_root_page_id)
if new_obj.bind_model.present?
new_obj2 = new_obj.bind_model.constantize.where(:uid=>new_obj.bind_uid).first rescue nil
obj = new_obj2 if new_obj2
end
end
end
end
return @current_user.nil? ? false : self.user_has_cateogry?(obj.category_id)
end
def self.user_access_level?
access_level = nil
if @current_user.nil?
access_level = "user"
elsif@current_user.is_admin?
access_level = "admin"
elsif @current_user.is_manager?(@module_app)
access_level = "manager"
elsif @current_user.is_sub_manager?(@module_app)
access_level = "sub_manager"
elsif @current_user.is_manager_with_role?(@module_app)
access_level = "manager"
elsif @current_user.is_sub_manager_with_role?(@module_app)
access_level = "sub_manager"
end
access_level
end
def self.user_has_cateogry?(cat)
return false if @current_user.nil?
if @current_user.is_admin? or @current_user.is_manager?(@module_app)
return true
else
category = Category.find(cat) rescue nil
return false if category.nil?
return @current_user.approved_categories.include?category rescue false
end
end
def self.page
@page
end
def self.set_page(page)
@page_show_url = nil
@plugin_show_url_map = nil
@page = page
end
def self.params
@params
end
def self.url_to_show(slug)
#pages = Page.where(:url=>@params[:url]).to_a
if self.page.nil?
"/#{@site_locale}#{@params[:url]}/#{slug}"
else
@page_show_url = @page_show_url || self.page.get_url
"/#{@site_locale}#{@page_show_url}/#{slug}"
end
end
def self.url_to_plugin_show(slug,module_app)
@plugin_show_url_map ||= {}
if @plugin_show_url_map[module_app].nil?
@plugin_show_url_map[module_app] = Page.find_by(:module => module_app).get_url rescue ""
end
if @plugin_show_url_map[module_app].blank?
@url_to_plugin_show = "#"
else
@url_to_plugin_show = "/#{@site_locale}#{@plugin_show_url_map[module_app]}/#{slug}" rescue "#"
end
end
def self.override_widget_module_app(module_app)
self.set_widget_module_app module_app
widget = self.get_current_widget
if self.page.nil?
self.set_page((widget.page_part.page rescue Page.root))
end
self.calc_widget_read_more_url(widget)
end
def self.calc_widget_read_more_page_for_module(widget,locale=I18n.locale) #read_more_page_id.blank? || read_more_page.nil?
module_app = widget.module
module_app = self.widget_module_app.key if module_app.nil?
pages = self.page.find_page(:module => module_app,:enabled_for.in=>[@site_locale.to_s])
current_module_app = (@widget_module_app.key==module_app rescue false) ? @widget_module_app : ModuleApp.where(:key=>module_app).first
order_method = (current_module_app.nil? || current_module_app.asc) ? :asc : :desc
finalpage = pages.where(:categories.all=> widget.categories).first
if finalpage.nil?
nils_cats, not_nils_cats = Category.where(:id.in => widget.categories,:disable.ne=>true).order_by(sort_number: order_method).partition { |cat| cat[:sort_number].nil? }
widget_categories = not_nils_cats + nils_cats
finalpage = widget_categories.first.nil? ? (pages.where(:categories=>['all']).first || pages.first) : (pages.where(:categories.in => [widget_categories.first.id]).first || pages.where(:categories=>['all']).first || pages.first)
end
return finalpage
end
def self.calc_widget_read_more_url(widget,locale=I18n.locale)
finalpage = widget.read_more_page_id.blank? ? nil : self.page.find_page(:page_id=> widget.read_more_page_id,:enabled_for=>locale).first rescue nil
if finalpage.nil?
finalpage = self.calc_widget_read_more_page_for_module(widget,locale)
end
if !finalpage.nil?
@url_widget_for_show = "/#{@site_locale.to_s}#{finalpage.get_url}"
else
@url_widget_for_show = "#"
end
@url_widget_for_show
end
def self.set_widget_item_url(widget)
if self.page.nil?
puts ['page is nil']
self.set_page((widget.page_part.page rescue Page.root))
end
@url_widget_for_show = widget.get_read_more_page_url
end
def self.array_include(arr1,arr2)
final = []
arr2.each do |el|
final.push(arr1.include?el)
end
!final.include?false
end
def self.widget_item_url(obj)
@url_widget_for_show + "/" + obj
end
def self.widget_more_url
@url_widget_for_show
end
def page_url(url)
"#{request.host_with_port}/#{locale}#{url}"
end
def self.set_request_object(request)
@site_request_object = request
end
def self.request
@site_request_object
end
def self.set_site_locale(locale)
@site_locale = locale
end
def self.set_widget_data_count(data_count)
@widget_data_count = data_count
end
def self.widget_data_count
@widget_data_count
end
def self.set_widget_categories(categories)
@widget_categories = []
disabled_categories = Category.where(:id.in=>categories,:disable=>true).pluck(:id).map(&:to_s)
@widget_categories = categories - disabled_categories
@widget_categories = ["all"] if @widget_categories.blank?
end
def self.widget_categories
@widget_categories
end
def self.get_site_locale
@site_locale
end
def self.current_widget_module
@controller_name
end
def self.set_current_widget_module(name)
@controller_name = name
end
def self.set_current_widget(widget)
@current_widget = widget
end
def self.get_current_widget
@current_widget
end
def get_item_module_infos(page)
if page['parent_page_id'].nil?
["Home","icons-house"]
else
@all_module_app = @all_module_app || ModuleApp.all.group_by(&:key)
module_app = @all_module_app[page['module']][0] rescue nil
unless module_app.nil?
[module_app.title, (module_app.get_registration.icon_class || module_app.get_registration.get_icon_class_no_sidebar)]
else
if page['module'].eql? "sitemap"
["sitemap", "icons-directions"]
else
["", "icon-minus-sign"]
end
end
end
end
def self.orbit_impressionist(object)
impressionist(object)
end
def self.is_mobile_view
!$mobile.blank?
end
def self.set_css_to_render_to_empty
@css_to_render_in_head = []
end
def self.render_css_in_head(css=[])
@css_to_render_in_head.concat(css)
end
def self.render_js_in_head(js=[])
@js_to_render_in_head = @js_to_render_in_head || Array(@js_to_render_in_head)
@js_to_render_in_head.concat(js)
end
def self.get_css_to_render_in_head
css_html = "\n" + ActionController::Base.helpers.stylesheet_link_tag(*Array(@css_to_render_in_head))
css_html = css_html + ActionController::Base.helpers.javascript_include_tag(*Array(@js_to_render_in_head))
return css_html
end
def self.render_meta_tags(metas=[])
@page_meta_tags = metas
end
def self.meta_tags_html
html = ""
if !@page_meta_tags.nil?
@page_meta_tags.each do |meta|
html = html + "<meta "
meta.keys.each do |attrib|
html = html + "#{attrib}='#{self.strip_html_tags(meta[attrib])}' "
end
html = html + ">"
end
end
html
end
def self.strip_html_tags(string)
ActionView::Base.full_sanitizer.sanitize(string)
end
# get feeds for the module
def self.get_feed_for_module(type)
if ModuleApp.where(:key => "feed").count == 0
return []
end
categories = []
ma = nil
if type == "index"
categories = @categories if !@categories.nil?
ma = @module_app
elsif type == "widget"
categories = @widget_categories if !@widget_categories.nil?
ma = @widget_module_app
end
if categories.first == "all"
feeds = SiteFeed.where(:channel_key => ma.key).enabled
else
feeds = SiteFeed.where(:channel_key => ma.key, :merge_with_category.in => categories).enabled
end
data = []
if feeds.count > 0
temp_ids = []
feeds.each do |feed|
file = File.join(Rails.root,"public","site_feeds",feed.id.to_s,feed.feed_uid.to_s + ".json")
if File.exists?(file)
d = File.read(file)
d = JSON.parse(d) rescue {ma.key.pluralize => []}
cat = Category.find(feed.merge_with_category).title
final_d = []
d[ma.key.pluralize].each{|a|
a["category"] = cat
a["source-site"] = feed.remote_site_url
a["source-site-title"] = (!feed.channel_title.nil? && feed.channel_title != "" ? feed.channel_title : "#{I18n.t("feed.source")}")
if !temp_ids.include?(a["id"])
temp_ids << a["id"]
a["params"] = a["params"] + "_" + feed.id.to_s + "h"
final_d << a
end
}
data.concat(final_d)
end
end
end
data
end
def self.is_object_from_feed?(uid)
return uid.ends_with?("h")
end
def self.get_from_feed(uidwithid)
temp = uidwithid.split("_")
feed_id = temp[1][0...-1]
uid = temp[0]
object = nil
feed = SiteFeed.find(feed_id) rescue nil
if !feed.nil?
file = File.join(Rails.root,"public","site_feeds",feed.id.to_s,feed.feed_uid.to_s + ".json")
if File.exists?(file)
d = File.read(file)
d = JSON.parse(d)
objects = d[@module_app.key.pluralize]
object = objects.select{|obj| obj["id"] == uid}.first
cat = Category.find(feed.merge_with_category)
object = {} if cat.disable
end
end
object
end
# ===============================================================
# Breadcrumbs
# ===============================================================
def back_end_breadcrumb
res = ''
divider = "<span class='divider'>/</span>"
res << "<li><a href='#{admin_dashboards_path}'>#{t(:dashboard_)}</a>#{divider}</li>"
case controller.controller_name
when 'authorizations'
res << "<li><a href='/#{params[:controller]}/#{@module_app.key}'>#{@module_app.title}</a>#{divider}</li>"
case params[:type]
when 'approval'
res << "<li class='active'>#{t(:approval_)}</li>"
when 'category'
res << "<li class='active'>#{t(:category_auth)}</li>"
when nil
res << "<li class='active'>#{t(:module_authorization)}</li>"
else
res << "<li class='active'>#{params[:type].underscore.humanize.capitalize} #{t(:authorization_)}</li>"
end
when 'images'
id = AlbumImage.find(params[:id]).album.id.to_s rescue ""
res << "<li><a href='/admin/galleries/#{id}'>#{t('module_name.'+@module_app.key)}</a>#{divider}</li>"
res << "<li class='active'>#{t(:theater)}</li>"
when 'categories'
if @module_app.key!='category'
res << "<li>#{t('module_name.'+@module_app.key)}#{divider}</li>"
res << "<li class='active'>#{t(:categories)}</li>"
else
res << "<li class='active'>#{t('module_name.category')}</li>"
end
when 'tags'
if @module_app.key!='tag'
res << "<li>#{t('module_name.'+@module_app.key)}#{divider}</li>"
res << "<li class='active'>#{t(:tags)}</li>"
else
res << "<li class='active'>#{t('module_name.tag')}</li>"
end
else
if params[:action].eql?('index')
res << "<li>#{t('module_name.'+@module_app.key)}</li>"
else
if params[:controller] == "admin/ad_images"
res << "<li><a href='/admin/ad_banners'>#{t('module_name.'+@module_app.key)}</a>#{divider}</li>"
else
res << "<li><a href='/#{params[:controller]}'>#{t('module_name.'+@module_app.key)}</a>#{divider}</li>"
end
res << "<li class='active'>#{t((params[:action].to_s=='update' ? 'update_' : params[:action]), scope: 'restful_actions')}</li>"
end
end
res.html_safe
end
def site_breadcrumb
res = ''
divider = "<span class='divider'>/</span>"
res << "<li>#{t(:settings)}#{divider}</li>"
site = @site
case controller.controller_name
when 'sites'
case controller.action_name
when 'sitemap'
res << "<li class='active'>#{t(:sitemap)}</li>"
when 'responsive_setting'
res << "<li class='active'>#{t(:mobile_settings)}</li>"
when 'mail_setting'
res << "<li class='active'>#{t(:mail_setting)}</li>"
when 'preference'
res << "<li class='active'>#{t(:preference)}</li>"
when 'search_engine'
res << "<li class='active'>#{t(:search_engine)}</li>"
when 'site_info'
res << "<li class='active'>#{t(:site_info)}</li>"
when 'update_manager'
res << "<li class='active'>#{t(:update_manager)}</li>"
when 'system_info'
res << "<li class='active'>#{t("site.system_preference")}</li>"
end
when 'items'
res << "<li class='active'>#{t(:structure)}</li>"
when 'module_store'
res << "<li class='active'>#{t(:modules)}</li>"
when 'designs'
res << "<li class='active'>#{t(:templates)}</li>"
end
res.html_safe
end
def active_when_current_locale_eq(locale)
locale.to_sym == I18n.locale ? 'active in': ''
end
end