orbit4-5/app/helpers/orbit_helper.rb

541 lines
14 KiB
Ruby

module OrbitHelper
def self.set_params(params,current_user)
@params = params
@current_user = current_user
end
def self.set_page_categories(categories)
@categories = categories;
end
def self.set_page_tags(tags)
@tags = tags;
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 rescue nil
if !custom_value.nil?
self.set_widget_custom_value custom_value
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)
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?
'/'+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?
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"
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.params
@params
end
def self.url_to_show(slug)
if @params[:url]
"/#{@site_locale}#{@params[:url]}/#{slug}"
else
page = Page.where(:module => self.current_widget_module).first
"/#{@site_locale}#{page.url}/#{slug}"
end
end
def self.url_to_plugin_show(slug,module_app)
page = Page.find_by(:module => module_app) rescue ""
@url_to_plugin_show = "#{page.url}/#{slug}" rescue "#"
end
def self.override_widget_module_app(module_app)
self.set_widget_module_app module_app
@url_widget_for_show = ""
pages = Page.where(:module => module_app)
widget_categories = self.get_current_widget.categories rescue []
finalpage = nil
pages.each do |p|
if p.categories == widget_categories
finalpage = p
end
break if !finalpage.nil?
end
if finalpage.nil?
pages.each do |p|
if self.array_include(p.categories, widget_categories)
finalpage = p
end
break if !finalpage.nil?
end
end
finalpage = pages.first if finalpage.nil?
if !finalpage.nil?
@url_widget_for_show = "/#{@site_locale.to_s}#{finalpage.url}"
end
end
def self.set_widget_item_url(widget)
@url_widget_for_show = ""
module_app = widget.module
module_app = self.widget_module_app.key if module_app.nil?
pages = Page.where(:module => module_app)
widget_categories = widget.categories
finalpage = nil
pages.each do |p|
if p.categories == widget_categories
finalpage = p
end
break if !finalpage.nil?
end
if finalpage.nil?
pages.each do |p|
if self.array_include(p.categories, widget_categories)
finalpage = p
end
break if !finalpage.nil?
end
end
finalpage = pages.first if finalpage.nil?
if !finalpage.nil?
@url_widget_for_show = "/#{@site_locale.to_s}#{finalpage.url}"
end
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 = []
categories.each do |c|
category = Category.find(c) rescue nil
if !category.nil? && !category.disable
@widget_categories << c
end
end
@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
module_app = ModuleApp.where(:key => page.module).first
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.render_css_in_head(css=[])
@css_to_render_in_head = css
end
def self.get_css_to_render_in_head
css_html = ""
@css_to_render_in_head.each do |css|
css_html = css_html + "<link rel='stylesheet' media='screen' href='/assets/#{css}'>\n"
end
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)
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)
cat = Category.find(feed.merge_with_category).title
final_d = []
d[ma.key.pluralize].each{|a|
a["category"] = cat
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], 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.first
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