2014-03-12 04:42:59 +00:00
|
|
|
require "json"
|
|
|
|
module ApplicationHelper
|
|
|
|
def render_widget(widget)
|
2014-05-29 10:32:27 +00:00
|
|
|
file = File.join("../templates", "#{@key}", "modules/#{widget}")
|
2014-03-12 04:42:59 +00:00
|
|
|
render :partial => file
|
|
|
|
end
|
|
|
|
|
2014-05-02 10:19:57 +00:00
|
|
|
def render_partial(partial)
|
2014-05-29 10:32:27 +00:00
|
|
|
file = File.join("../templates", "#{@key}", "partial/#{partial}")
|
2014-05-02 10:19:57 +00:00
|
|
|
render :partial => file
|
|
|
|
end
|
|
|
|
|
2014-03-12 04:42:59 +00:00
|
|
|
def link_to_show(module_name)
|
|
|
|
"/module/#{module_name}/show"
|
|
|
|
end
|
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
def render_header
|
|
|
|
site = Site.first
|
2014-05-29 10:32:27 +00:00
|
|
|
header_file = File.join('../templates', "#{@key}", "/home/header.html.erb")
|
2014-05-22 11:19:25 +00:00
|
|
|
header_file_html = render :file => header_file
|
|
|
|
header = Nokogiri::HTML(header_file_html, nil, "UTF-8")
|
|
|
|
sub_menu_html = site.sub_menu
|
|
|
|
html = header.to_s
|
|
|
|
html = html.gsub("{{site_name}}",site.title)
|
2014-05-27 10:18:23 +00:00
|
|
|
html = html.gsub("%7B%7Blogo_url%7D%7D",(site.site_logo.url.nil? ? "/assets/site-logo.png" : site.site_logo.url))
|
2014-05-22 11:19:25 +00:00
|
|
|
if site.sitemap_menu_in_header
|
|
|
|
sub_menu_html = sub_menu_html + "<a href='/#{I18n.locale.to_s}#{site.site_map_link}'>Sitemap</a>"
|
|
|
|
end
|
2014-05-26 04:57:47 +00:00
|
|
|
sub_menu_html = sub_menu_html.nil? ? "" : sub_menu_html
|
2014-05-22 11:19:25 +00:00
|
|
|
html = html.gsub("{{header-data}}",sub_menu_html)
|
|
|
|
html.html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_footer
|
|
|
|
site = Site.first
|
2014-05-29 10:32:27 +00:00
|
|
|
footer_file = File.join('../templates', "#{@key}", "/home/footer.html.erb")
|
2014-05-22 11:19:25 +00:00
|
|
|
footer_file_html = render :file => footer_file
|
|
|
|
footer = Nokogiri::HTML(footer_file_html, nil, "UTF-8")
|
|
|
|
html = footer.to_s
|
|
|
|
site_footer = site.footer
|
|
|
|
if site.enable_terms_of_use
|
|
|
|
site_footer = site_footer + "<a href='/#{I18n.locale.to_s}#{site.terms_of_use_link}'>Terms of use</a>"
|
|
|
|
end
|
2014-05-26 04:57:47 +00:00
|
|
|
site_footer = site_footer.nil? ? "" : site_footer
|
2014-05-22 11:19:25 +00:00
|
|
|
html = html.gsub("{{footer-data}}",site_footer)
|
|
|
|
html.html_safe
|
|
|
|
end
|
|
|
|
|
2014-05-24 09:12:19 +00:00
|
|
|
def render_orbit_bar
|
|
|
|
orbit_bar_file = File.join('../views', "orbit_bar", 'index.html.erb')
|
|
|
|
orbit_bar_file_html = render :file => orbit_bar_file
|
|
|
|
orbit_bar_file_html.html_safe
|
|
|
|
end
|
|
|
|
|
2014-03-12 04:42:59 +00:00
|
|
|
def render_menu
|
2014-04-01 07:10:21 +00:00
|
|
|
# json_file = File.read(File.join(Rails.root, 'public', "menu.json"))
|
|
|
|
# @items = JSON.parse(json_file)
|
2014-05-22 11:19:25 +00:00
|
|
|
@pages = Page.root.sorted_published_child_pages
|
2014-04-01 07:10:21 +00:00
|
|
|
|
|
|
|
def create_json(pages)
|
|
|
|
item = {}
|
|
|
|
pages.each do |page|
|
|
|
|
if page.child_page.size > 0
|
2014-04-21 07:19:29 +00:00
|
|
|
item["#{page.name}"] = {"url"=> "/#{locale.to_s}" + page.url, "children"=>create_json(page.sorted_published_child_pages)}
|
2014-04-01 07:10:21 +00:00
|
|
|
else
|
|
|
|
item["#{page.name}"] = {"url"=> "/#{locale.to_s}" + page.url}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
item
|
|
|
|
end
|
|
|
|
@items = create_json(@pages)
|
2014-05-29 10:32:27 +00:00
|
|
|
menu_file = File.open(File.join(Rails.root, 'app', 'templates', "#{@key}", "/home/menu.html.erb"))
|
2014-04-01 08:37:43 +00:00
|
|
|
doc = Nokogiri::HTML(menu_file, nil, "UTF-8")
|
2014-03-12 04:42:59 +00:00
|
|
|
menu_file.close
|
|
|
|
|
|
|
|
temp = []
|
|
|
|
@menus = []
|
|
|
|
@menus_items = []
|
|
|
|
|
|
|
|
temp << doc.css("*[data-menu-level='0']")
|
|
|
|
temp << doc.css("*[data-menu-level='1']")
|
|
|
|
temp << doc.css("*[data-menu-level='2']")
|
|
|
|
|
|
|
|
|
|
|
|
temp[0] = temp[0].to_s.gsub(temp[1].to_s,"{{level}}")
|
|
|
|
temp[1] = temp[1].to_s.gsub(temp[2].to_s,"{{level}}")
|
|
|
|
temp[2] = temp[2].to_s
|
|
|
|
|
|
|
|
|
|
|
|
temp.each_with_index do |menu,i|
|
2014-04-01 08:37:43 +00:00
|
|
|
t = Nokogiri::HTML(menu, nil, "UTF-8")
|
2014-03-12 04:42:59 +00:00
|
|
|
a = t.css("*[data-menu-link='true']")
|
|
|
|
a[0]["href"] = "href_here"
|
|
|
|
li = t.css("*[data-menu-level='#{i}'] > *")
|
|
|
|
@menus_items << li.to_html
|
|
|
|
ul = t.css("*[data-menu-level='#{i}']")
|
|
|
|
ul[0].inner_html = "{{here}}"
|
|
|
|
@menus << ul[0].to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_menu(items,level)
|
|
|
|
html = ""
|
|
|
|
items.each do |key,item|
|
|
|
|
li = @menus_items[level].gsub("href_here",item["url"])
|
|
|
|
li = li.gsub("{{link_name}}",key)
|
2014-05-27 11:47:56 +00:00
|
|
|
|
|
|
|
li = request.original_fullpath == item['url'] ? li.gsub("{{active}}","active") : li.gsub("{{active}}","")
|
|
|
|
|
2014-04-21 07:19:29 +00:00
|
|
|
if item["children"] && !item["children"].empty?
|
2014-03-12 04:42:59 +00:00
|
|
|
li = li.gsub("{{level}}",create_menu(item["children"],level + 1))
|
|
|
|
else
|
|
|
|
li = li.gsub("{{level}}","")
|
|
|
|
end
|
|
|
|
html = html + li
|
|
|
|
end
|
|
|
|
html = @menus[level].gsub("{{here}}",html)
|
|
|
|
html = html.gsub("{{class_level}}",level.to_s)
|
|
|
|
html
|
|
|
|
end
|
|
|
|
h = create_menu(@items,0)
|
|
|
|
h.html_safe
|
|
|
|
end
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
def render_view
|
2014-05-14 11:47:25 +00:00
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
def parsing_repeats_again(elements,d,level)
|
|
|
|
newhtml = []
|
|
|
|
oldhtml = []
|
|
|
|
elements.each do |el|
|
|
|
|
html_to_render = ""
|
|
|
|
data_name = el.attr("data-list")
|
|
|
|
wrap_elements = el.css("*[data-list][data-level='#{level}']")
|
|
|
|
if d["#{data_name}"]
|
|
|
|
d["#{data_name}"].each_with_index do |item,i|
|
|
|
|
element = el.inner_html
|
|
|
|
if wrap_elements.count > 0
|
|
|
|
htmls = parsing_repeats_again(wrap_elements,d["#{data_name}"][i], level + 1)
|
|
|
|
htmls[0].each_with_index do |html,i|
|
|
|
|
element = element.gsub(html,htmls[1][i])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
item.each do |key,value|
|
|
|
|
if !value.kind_of?(Array)
|
|
|
|
value = value.nil? ? "" : value
|
|
|
|
element = element.gsub("{{#{key}}}",value.to_s)
|
|
|
|
element = element.gsub("%7B%7B#{key}%7D%7D",value.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
html_to_render = html_to_render + element
|
|
|
|
end
|
|
|
|
temp = el.to_s
|
|
|
|
oldhtml << temp
|
|
|
|
temp = temp.gsub(el.inner_html, html_to_render)
|
|
|
|
newhtml << temp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
[oldhtml,newhtml]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-01 07:10:21 +00:00
|
|
|
if params[:target_action] == "index"
|
2014-05-29 10:32:27 +00:00
|
|
|
file = File.open(File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', params[:target_controller].singularize, "#{params[:target_action]}.html.erb"))
|
2014-04-01 08:37:43 +00:00
|
|
|
doc = Nokogiri::HTML(file, nil, "UTF-8")
|
2014-04-01 07:10:21 +00:00
|
|
|
file.close
|
2014-04-21 07:19:29 +00:00
|
|
|
controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new
|
2014-04-01 07:10:21 +00:00
|
|
|
data = controller.send("#{params[:target_action]}")
|
2014-04-02 06:07:04 +00:00
|
|
|
keys = data.keys
|
2014-05-14 11:47:25 +00:00
|
|
|
wrap_elements = doc.css("*[data-list][data-level='0']")
|
|
|
|
htmls = parsing_repeats_again(wrap_elements,data,1)
|
2014-05-22 11:19:25 +00:00
|
|
|
html = doc.to_s
|
|
|
|
htmls[0].each_with_index do |h,i|
|
|
|
|
html = html.gsub(h,htmls[1][i])
|
|
|
|
end
|
|
|
|
|
|
|
|
if keys[1]
|
|
|
|
(data[keys[1]].kind_of?(Array) ? data[keys[0]] : data[keys[1]]).each do |key,value|
|
|
|
|
value = value.nil? ? "" : value
|
|
|
|
html = html.gsub("{{#{key}}}",value.to_s)
|
|
|
|
html = html.gsub("%7B%7B#{key}%7D%7D",value.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
html.html_safe
|
2014-04-01 07:10:21 +00:00
|
|
|
elsif params[:target_action] == "show"
|
2014-05-29 10:32:27 +00:00
|
|
|
file = File.open(File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', params[:target_controller].singularize, "#{params[:target_action]}.html.erb"))
|
2014-04-01 08:37:43 +00:00
|
|
|
doc = Nokogiri::HTML(file, nil, "UTF-8")
|
2014-04-01 07:10:21 +00:00
|
|
|
file.close
|
2014-04-21 07:19:29 +00:00
|
|
|
controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new
|
2014-04-01 07:10:21 +00:00
|
|
|
data = controller.send("#{params[:target_action]}")
|
2014-05-22 11:19:25 +00:00
|
|
|
|
2014-05-02 10:19:57 +00:00
|
|
|
if data.blank? || data.empty?
|
2014-05-05 06:04:50 +00:00
|
|
|
file = File.open("#{Rails.root}/public/404.html")
|
|
|
|
doc = Nokogiri::HTML(file, nil, "UTF-8")
|
|
|
|
file.close
|
|
|
|
doc.to_html.html_safe
|
2014-05-02 10:19:57 +00:00
|
|
|
else
|
2014-05-22 11:19:25 +00:00
|
|
|
unless data['impressionist'].blank?
|
|
|
|
impression = data['impressionist'].impressions.create
|
|
|
|
impression.user_id = request.session['user_id']
|
|
|
|
impression.controller_name = params[:target_controller]
|
|
|
|
impression.action_name = params[:target_action]
|
|
|
|
impression.ip_address = request.remote_ip
|
|
|
|
impression.session_hash = request.session.id
|
|
|
|
impression.request_hash = @impressionist_hash
|
|
|
|
impression.referrer = request.referrer
|
|
|
|
impression.save
|
|
|
|
data['impressionist'].view_count = data['impressionist'].impressions.count
|
|
|
|
data['impressionist'].save
|
|
|
|
end
|
2014-05-14 11:47:25 +00:00
|
|
|
wrap_elements = doc.css("*[data-list][data-level='0']")
|
2014-05-02 10:19:57 +00:00
|
|
|
if wrap_elements.count == 0
|
2014-05-05 06:04:50 +00:00
|
|
|
wrap_element_html = doc.to_s
|
2014-05-02 10:19:57 +00:00
|
|
|
el = wrap_element_html
|
|
|
|
data.each do |key,value|
|
2014-05-22 11:19:25 +00:00
|
|
|
next if key.eql? 'impressionist'
|
2014-05-02 10:19:57 +00:00
|
|
|
value = value.nil? ? "" : value
|
2014-05-22 11:19:25 +00:00
|
|
|
el = el.gsub("{{#{key}}}",value.to_s)
|
|
|
|
el = el.gsub("%7B%7B#{key}%7D%7D",value.to_s)
|
2014-05-02 10:19:57 +00:00
|
|
|
end
|
|
|
|
el.html_safe
|
|
|
|
else
|
|
|
|
keys = data.keys
|
2014-05-05 06:04:50 +00:00
|
|
|
not_array_key = nil
|
|
|
|
data.keys.each do |key|
|
|
|
|
not_array_key = key if data["#{key}"].kind_of?(Hash)
|
|
|
|
end
|
2014-05-22 11:19:25 +00:00
|
|
|
htmls = parsing_repeats_again(wrap_elements,data,1)
|
|
|
|
html = doc.to_s
|
|
|
|
htmls[0].each_with_index do |h,i|
|
|
|
|
html = html.gsub(h,htmls[1][i])
|
|
|
|
end
|
|
|
|
if data["#{not_array_key}"]
|
|
|
|
data["#{not_array_key}"].each do |key,value|
|
|
|
|
next if key.eql? 'impressionist'
|
|
|
|
value = value.nil? ? "" : value
|
|
|
|
html = html.gsub("{{#{key}}}",value.to_s)
|
|
|
|
html = html.gsub("%7B%7B#{key}%7D%7D",value.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
html.html_safe
|
2014-05-02 10:19:57 +00:00
|
|
|
end
|
2014-04-01 07:10:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_data
|
2014-05-05 06:04:50 +00:00
|
|
|
controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new
|
2014-04-01 07:10:21 +00:00
|
|
|
data = controller.send("#{params[:target_action]}")
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2014-04-09 10:30:13 +00:00
|
|
|
def active_for_controllers(*controller_names)
|
|
|
|
(controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? 'active' : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def visible_for_controllers(*controller_names)
|
|
|
|
(controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? '' : 'hide'
|
|
|
|
end
|
|
|
|
|
2014-04-08 10:46:27 +00:00
|
|
|
def active_for_action(controller_name, action_name)
|
|
|
|
((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil
|
|
|
|
end
|
2014-04-14 10:40:17 +00:00
|
|
|
|
2014-05-09 06:03:55 +00:00
|
|
|
def link_back(custom_class=nil)
|
|
|
|
case custom_class
|
|
|
|
when nil
|
|
|
|
link_to t('back'), get_go_back, :class => 'nav'
|
|
|
|
else
|
|
|
|
link_to t('back'), get_go_back, :class => custom_class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-24 09:12:19 +00:00
|
|
|
def switch_language(loc)
|
|
|
|
url = request.original_fullpath
|
|
|
|
if loc != locale
|
|
|
|
url = url.gsub("/#{locale.to_s}/","/#{loc.to_s}/")
|
|
|
|
url = url.gsub("locale=#{locale.to_s}","locale=#{loc.to_s}") if url == request.original_fullpath
|
2014-05-29 03:44:35 +00:00
|
|
|
url = url + "#{url.include?("?") ? "&" : "?"}locale=#{loc.to_s}" if url == request.original_fullpath
|
2014-05-24 09:12:19 +00:00
|
|
|
end
|
|
|
|
url
|
|
|
|
end
|
|
|
|
|
2014-05-09 06:03:55 +00:00
|
|
|
# Clean the link back
|
|
|
|
def get_go_back
|
|
|
|
begin
|
|
|
|
if request.url.include?('locale=')
|
|
|
|
session[:last_page]
|
|
|
|
else
|
|
|
|
session[:last_page] = remove_locale(request.referer)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
eval(params[:controller].split('/').join('_') << '_url')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_avatar(user)
|
|
|
|
image_tag(user.avatar.thumb.url)
|
|
|
|
end
|
|
|
|
|
2014-05-22 11:19:25 +00:00
|
|
|
def render_sitemap
|
|
|
|
items = action_data
|
|
|
|
def node(items)
|
|
|
|
html = "<ul>"
|
|
|
|
items.each do |key,item|
|
|
|
|
if item["children"] && !item["children"].empty?
|
|
|
|
url = item["url"]
|
|
|
|
html = html + "<li><a href='#{url}'>#{key}</a>"
|
|
|
|
html = html + node(item["children"])
|
|
|
|
html = html + "</li>"
|
|
|
|
else
|
|
|
|
url = item["url"]
|
|
|
|
html = html + "<li><a href='#{url}'>#{key}</a></li>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
html = html + "</ul>"
|
|
|
|
html
|
|
|
|
end
|
|
|
|
html = node(items)
|
|
|
|
html.html_safe
|
|
|
|
end
|
|
|
|
|
2014-03-12 04:42:59 +00:00
|
|
|
end
|