From d150a698cd059dcca7b52dc792308b8b75676c87 Mon Sep 17 00:00:00 2001 From: bohung Date: Mon, 24 Oct 2022 16:42:18 +0800 Subject: [PATCH] Update temp files. --- updatefiles/application_helper.rb | 1093 ++++++++++++++++++++--------- 1 file changed, 772 insertions(+), 321 deletions(-) diff --git a/updatefiles/application_helper.rb b/updatefiles/application_helper.rb index 9d71c2e..0c3593b 100644 --- a/updatefiles/application_helper.rb +++ b/updatefiles/application_helper.rb @@ -1,6 +1,10 @@ # encoding: utf-8 require "json" module ApplicationHelper + CurrentSite = Site.first #(OrbitHelper::SharedHash['current_site']['site'] rescue Site.first) + def self.CurrentSite + (OrbitHelper::SharedHash['current_site']['site'] rescue Site.first) + end def render_widget(widget) file = File.join(Rails.root, "app", "templates", "#{@key}", "modules/#{widget}") render :partial => file @@ -8,48 +12,226 @@ module ApplicationHelper def render_partial(partial) file = File.join(Rails.root, "app", "templates", "#{@key}", "partial/_#{partial}.html.erb") - render :file => file + render(:file => file, :formats => [:html]) end def link_to_show(module_name) "/module/#{module_name}/show" end - - def render_header - site = current_site - header_file = File.join(Rails.root, "app", "templates", "#{@key}", "/home/header.html.erb") - header_file_html = render :file => header_file + def self.render_header_for_extend(data_only=false) + $making_cache = data_only + if @site.nil? + @site = CurrentSite + @orbit_bar_not_display = @site.orbit_bar_not_display + end + @key = @site.template + if @custom_header + header_file = @custom_header + else + header_file = File.join(Rails.root, "app", "templates", "#{@key}", "/home/header.html.erb") + end + view = ActionView::Base.new(Orbit::Application.config.paths["app/views"].first) + view.extend ApplicationHelper + view.instance_variable_set(:@site,@site) + view.instance_eval{def current_site; @site; end} + view.instance_variable_set(:@key,@key) + view.instance_variable_set(:@url, (OrbitHelper.params[:url] rescue "/") ) + header_file_html = view.render(:file => header_file, :locals =>{:current_user => OrbitHelper.current_user}) rescue nil + if header_file_html.nil? && data_only + $making_cache = false + raise "#{header_file} not found" + end header = Nokogiri::HTML(header_file_html, nil, "UTF-8") - site_logo = header.css("img[src='{{logo_url}}']")[0] + site_logo = header.css("a[title='{{site_title}}']")[0] if !site_logo.nil? - site_logo.remove if site.site_logo.url.nil? + if @site.site_logo.url.nil? + if @site[:title_always_on].blank? #blank include false + site_logo.remove + site_logo = nil + else + site_logo.css("img").remove + end + end + end + site_logo_1 = header.css("a[title='{{site_title_1}}']")[0] + if !site_logo_1.nil? + if @site.site_logo_1.url.nil? + site_logo_1.remove + site_logo_1 = nil + end + end + t = @site.title rescue "" + site_logo_alt = "" + if t.blank? + site_logo_alt = "Site Logo" + else + site_logo_alt = t + (I18n.locale.to_s == "zh_tw" ? "的Logo" : " Logo") + end + already_fix_h2 = false + h2_element = nil + default_site_title = @site.title + h2_tag = "h2" + if @orbit_bar_not_display + h2_tag = 'h1' + end + if site_logo_1 + site_logo_1.css("img").attr("alt", site_logo_alt) + if (site_logo_1.parent.name.match(/h1/i) rescue false) + h2_element = site_logo.parent + elsif site_logo_1.css('h1').length != 0 + h2_element = site_logo_1.css('h1')[0] + elsif site_logo_1.css('h2').length == 0 + site_logo_1.inner_html = "<#{h2_tag} class=\"default_site_h1\">" + site_logo_1.inner_html + "#{default_site_title}" + end + already_fix_h2 = true + end + if site_logo + site_logo.css("img").attr("alt", site_logo_alt) + unless already_fix_h2 + if (site_logo.parent.name.match(/h1/i) rescue false) + h2_element = site_logo.parent + elsif site_logo.css('h1').length != 0 + h2_element = site_logo.css('h1')[0] + elsif site_logo.css('h2').length == 0 + site_logo.inner_html = "

" + site_logo.inner_html + "#{default_site_title}

" + end + already_fix_h2 = true + end + end + if h2_element + h2_element.name = h2_tag + h2_element['class'] = "default_site_h1" end - sub_menu_html = site.sub_menu html = header.to_s - t = site.title rescue "" - html = html.gsub("{{site_name}}",(site.display_title_in_frontend ? t : "")) - html = html.gsub("%7B%7Blogo_url%7D%7D",((site.site_logo.url.nil? ? "/assets/default-site-logo.png" : site.site_logo.url) rescue "/assets/default-site-logo.pngs")) - html = html.gsub("%7B%7Blogo_url_1%7D%7D",((site.site_logo_1.url.nil? ? "/assets/default-site-logo.png" : site.site_logo_1.url) rescue "/assets/default-site-logo.pngs")) - html = html.gsub("%7B%7Bhome_link%7D%7D",((site.home_link.strip.empty? ? "/" : site.home_link) rescue "/")) - html = html.gsub("%7B%7Bhome_link_1%7D%7D",((site.home_link_1.strip.empty? ? "/" : site.home_link_1) rescue "/")) - html = html.gsub("{{site_title}}",((site.site_title[I18n.locale.to_s].strip.empty? ? "home_page" : site.site_title[I18n.locale.to_s]) rescue "home_page")) - html = html.gsub("{{site_title_1}}",((site.site_title_1[I18n.locale.to_s].strip.empty? ? "home_page" : site.site_title_1[I18n.locale.to_s]) rescue "home_page")) - if site.sitemap_menu_in_header - sitemap = Page.find_by_key(:sitemap).name rescue "Sitemap" - sub_menu_html = sub_menu_html + "#{sitemap}" + html = html.gsub("{{site_name}}",(@site.display_title_in_frontend ? t : (already_fix_h2 ? "" : "#{t}"))) + html = html.gsub("%7B%7Blogo_url%7D%7D",((@site.site_logo.url.nil? ? "/assets/default-site-logo.png" : @site.site_logo.url) rescue "/assets/default-site-logo.pngs")) + html = html.gsub("%7B%7Blogo_url_1%7D%7D",((@site.site_logo_1.url.nil? ? "/assets/default-site-logo.png" : @site.site_logo_1.url) rescue "/assets/default-site-logo.pngs")) + html = html.gsub("%7B%7Bhome_link%7D%7D",((@site.home_link.strip.empty? ? "/" : @site.home_link) rescue "/")) + html = html.gsub("%7B%7Bhome_link_1%7D%7D",((@site.home_link_1.strip.empty? ? "/" : @site.home_link_1) rescue "/")) + html = html.gsub("{{site_title}}",((@site.site_title[I18n.locale.to_s].strip.empty? ? default_site_title : @site.site_title[I18n.locale.to_s]) rescue default_site_title)) + html = html.gsub("{{site_title_1}}",((@site.site_title_1[I18n.locale.to_s].strip.empty? ? default_site_title : @site.site_title_1[I18n.locale.to_s]) rescue default_site_title)) + if data_only + $making_cache = false + return html end - sub_menu_html = sub_menu_html.nil? ? "" : sub_menu_html - html = html.gsub("{{header-data}}",sub_menu_html) - html.html_safe + if @custom_header_data + html = html.gsub("{{header-data}}",@custom_header_data) + else + sub_menu_html = @site.sub_menu + if @site.sitemap_menu_in_header + sitemap = Page.find_by_key(:sitemap).name rescue "Sitemap" + sub_menu_html = sub_menu_html + "#{sitemap}" + end + sub_menu_html = sub_menu_html.nil? ? "" : sub_menu_html + html = html.gsub("{{header-data}}",sub_menu_html) + end + html.html_safe + view.instance_eval{render_social_share + render_popup_window(@page)} + end + def render_header + $making_cache = false + site = @site + cache_data = nil + if @custom_header + header_file = @custom_header + else + header_file = File.join(Rails.root, "app", "templates", "#{@key}", "/home/header.html.erb") + # cache_data = (site.get_cache_header_data($mobile) rescue nil) + end + if cache_data.blank? + header_file_html = render(:file => header_file, :formats => [:html]) + header = Nokogiri::HTML(header_file_html, nil, "UTF-8") + site_logo = header.css("a[title='{{site_title}}']")[0] + if !site_logo.nil? + if site.site_logo.url.nil? + if site[:title_always_on].blank? #blank include false + site_logo.remove + site_logo = nil + else + site_logo.css("img").remove + end + end + end + site_logo_1 = header.css("a[title='{{site_title_1}}']")[0] + if !site_logo_1.nil? + if site.site_logo_1.url.nil? + site_logo_1.remove + site_logo_1 = nil + end + end + t = site.title rescue "" + site_logo_alt = "" + if t.blank? + site_logo_alt = "Site Logo" + else + site_logo_alt = t + (I18n.locale.to_s == "zh_tw" ? "的Logo" : " Logo") + end + already_fix_h2 = false + h2_element = nil + default_site_title = site.title + h2_tag = "h2" + if @orbit_bar_not_display + h2_tag = 'h1' + end + if site_logo_1 + site_logo_1.css("img").attr("alt", site_logo_alt) + if (site_logo_1.parent.name.match(/h1/i) rescue false) + h2_element = site_logo.parent + elsif site_logo_1.css('h1').length != 0 + h2_element = site_logo_1.css('h1')[0] + elsif site_logo_1.css('h2').length == 0 + site_logo_1.inner_html = "

" + site_logo_1.inner_html + "#{default_site_title}

" + end + already_fix_h2 = true + end + if site_logo + site_logo.css("img").attr("alt", site_logo_alt) + unless already_fix_h2 + if (site_logo.parent.name.match(/h1/i) rescue false) + h2_element = site_logo.parent + elsif site_logo.css('h1').length != 0 + h2_element = site_logo.css('h1')[0] + elsif site_logo.css('h2').length == 0 + site_logo.inner_html = "<#{h2_tag} class=\"default_site_h1\">" + site_logo.inner_html + "#{default_site_title}" + end + already_fix_h2 = true + end + end + if h2_element + h2_element.name = h2_tag + h2_element['class'] = "default_site_h1" + end + html = header.to_s + html = html.gsub("{{site_name}}",(site.display_title_in_frontend ? t : (already_fix_h2 ? "" : "#{t}"))) + html = html.gsub("%7B%7Blogo_url%7D%7D",((site.site_logo.url.nil? ? "/assets/default-site-logo.png" : site.site_logo.url) rescue "/assets/default-site-logo.pngs")) + html = html.gsub("%7B%7Blogo_url_1%7D%7D",((site.site_logo_1.url.nil? ? "/assets/default-site-logo.png" : site.site_logo_1.url) rescue "/assets/default-site-logo.pngs")) + html = html.gsub("%7B%7Bhome_link%7D%7D",((site.home_link.strip.empty? ? "/" : site.home_link) rescue "/")) + html = html.gsub("%7B%7Bhome_link_1%7D%7D",((site.home_link_1.strip.empty? ? "/" : site.home_link_1) rescue "/")) + html = html.gsub("{{site_title}}",((site.site_title[I18n.locale.to_s].strip.empty? ? default_site_title : site.site_title[I18n.locale.to_s]) rescue default_site_title)) + html = html.gsub("{{site_title_1}}",((site.site_title_1[I18n.locale.to_s].strip.empty? ? default_site_title : site.site_title_1[I18n.locale.to_s]) rescue default_site_title)) + else + html = cache_data.sub("{{render_orbit_bar}}",render_orbit_bar) + end + if @custom_header_data + html = html.sub("{{header-data}}",@custom_header_data) + else + sub_menu_html = site.sub_menu + if site.sitemap_menu_in_header + sitemap = Page.find_by_key(:sitemap).name rescue "Sitemap" + sub_menu_html = sub_menu_html + "#{sitemap}" + end + sub_menu_html = sub_menu_html.nil? ? "" : sub_menu_html + html = html.sub("{{header-data}}",sub_menu_html) + end + html.html_safe + render_social_share + render_popup_window(@page) end def render_site_title - site = current_site + site = @site title = site.title rescue "" - if site.title_always_on + if site.title_always_on || site.accessibility_mode if !params[:slug].nil? temp_title = params[:slug].sub("-#{params[:uid]}","") - temp_title = temp_title.gsub("-"," ") + temp_title = Nokogiri::HTML(temp_title.gsub("-"," ")).text title = "#{temp_title} | #{title}" elsif params[:target_action] == "index" temp_title = Page.find_by(:page_id => params[:page_id]).name @@ -60,16 +242,32 @@ module ApplicationHelper end def render_every_page_sharer - site = current_site + site = @site html = "
" site.every_page_sharer.each do |share| case share when "facebook" html = html + "
" + when "line" + line_share_text = I18n.t('site.share') + line_title = I18n.t('site.line_share_title') + line_share_url = "https://social-plugins.line.me/lineit/share" + line_share_img_src = "https://www.line-website.com/social-plugins/img/common/line_v3@3x.png" + html = html + " + + + + #{line_share_text} + + + + " when "twitter" html = html + "" when "google" - html = html + "
" + #html = html + "
" when "printer" html = html + "" end @@ -79,8 +277,8 @@ module ApplicationHelper end def render_google_analytics - html = current_site.google_analytics rescue "" - metas = current_site.meta_tags.split("\r\n") rescue [] + html = @site.google_analytics rescue "" + metas = @site.meta_tags.split("\r\n") rescue [] metas.each do |meta| m = meta.split(" ::: ") html = html + "" @@ -89,9 +287,10 @@ module ApplicationHelper end def render_footer - site = current_site - footer_file = File.join('../templates', "#{@key}", "/home/footer.html.erb") - footer_file_html = render :file => footer_file + site = @site + footer_file = File.join(Rails.root,'app/templates', "#{@key}", "/home/footer.html.erb") + footer_file_html = render(:file => footer_file, :formats => [:html]) + footer_file_html = footer_file_html.gsub("ruling-logo-icon",I18n.t(:ruling_digital)) footer = Nokogiri::HTML(footer_file_html, nil, "UTF-8") html = footer.to_s site_footer = site.footer @@ -99,8 +298,13 @@ module ApplicationHelper site_footer = site_footer + "Terms of use" end site_footer = site_footer.nil? ? "" : site_footer - html = html.gsub("{{footer-data}}",site_footer) - counter = Page.root.view_count.to_s rescue "" + if @custom_footer + html = html.gsub("{{footer-data}}",@custom_footer) + else + html = html.gsub("{{footer-data}}",site_footer) + end + #counter = Page.root.view_count.to_s rescue "" + counter = Impression.collection.count.to_s rescue "" counter = t(:visitors_count) + " : " + counter html = html.gsub("{{site-counter}}",counter) @@ -116,135 +320,307 @@ module ApplicationHelper end def get_last_updated - apps = ['bulletin', 'page_context', 'web_link'] - a = {} - apps.each do |module_app| - module_app_class = module_app.classify.constantize rescue nil - if !module_app_class.nil? - objects = module_app_class.order_by([:updated_at, :desc]).limit(1) - objects.each do |object| - a.merge!(object => object.updated_at) unless (object.archived rescue nil) - end - end + apps = ['bulletin', 'page_context', 'web_link'] + a = {} + apps.each do |module_app| + module_app_class = module_app.classify.constantize rescue nil + if !module_app_class.nil? + objects = module_app_class.order_by([:updated_at, :desc]).limit(1) + objects.each do |object| + a.merge!(object => object.updated_at) unless (object.archived rescue nil) + end + end end sorted_objects = a.sort {|a,b| b[1]<=>a[1]} return sorted_objects.first end + def render_popup_window(page=nil,editmode=false) + def render_edit_block(subpart,c) + "
#{c}
" + end + def get_popup_text(page,editmode) + site = @site + if editmode || site[:render_popup_only_on_root]!=true || (site[:render_popup_only_on_root] && !page.nil? && page.url=='/') + sub_parts = site.popup_subparts rescue [] + if !sub_parts.blank? #not nil or [] + sub_parts.map do |subpart| + next if !subpart.link_sub_part_id.nil? + link_subpart = site.popup_subparts.where(:link_sub_part_id=>subpart.id,:locale=>I18n.locale.to_s).first + subpart = link_subpart if !link_subpart.nil? + if subpart.kind == "module_widget" + OrbitHelper.set_current_widget subpart + OrbitHelper.set_widget_data_count subpart.data_count + OrbitHelper.set_widget_module_app subpart.module + OrbitHelper.set_widget_item_url subpart + OrbitHelper.render_meta_tags [] + OrbitHelper.set_widget_title subpart.title + OrbitHelper.set_widget_categories subpart.categories || ["all"] + OrbitHelper.set_widget_tags subpart.tags || ["all"] + custom_value = subpart.custom_string_field || subpart.custom_array_field || subpart.custom_data_field rescue nil + if !custom_value.nil? + OrbitHelper.set_widget_custom_value custom_value + else + OrbitHelper.set_widget_custom_value nil + end + c = render_widget_for_frontend(subpart.module,subpart.widget_method,subpart.widget_type,subpart.id.to_s) + if editmode + render_edit_block(subpart,c) + else + c + end + elsif subpart.kind == "none" + if editmode + render_edit_block(subpart,nil) + end + elsif subpart.kind == "text" + c = subpart.content + if editmode + render_edit_block(subpart,c) + else + if c.blank? + nil + else + "
#{c}
" + end + end + else + nil + end + end.compact.join("\n") + end + end + end + if page.nil? + page = @page + end + tmp = get_popup_text(page,editmode) + if editmode + tmp = (tmp||"") + " " + end + if !tmp.blank? + close_btn = editmode ? "" : " + Close + " + "
+
+ #{tmp} + #{close_btn} +
+
+ " + else + "" + end.html_safe + end + def render_social_share - site = current_site - share_file = File.join('../templates', "#{@key}", "/home/socialshare.html.erb") - share_file_html = render :file => share_file - sharer = Nokogiri::HTML(share_file_html, nil, "UTF-8") - html = sharer.to_s - site_sharer = site.social_share - site_sharer = site_sharer.nil? ? "" : site_sharer - html = html.gsub("{{social-share-data}}",site_sharer) - html.html_safe + site = @site + site_sharer = site.social_share.to_s.gsub(/).)*)-->/m,'') rescue "" + if !site_sharer.blank? + share_file = File.join(Rails.root,"app/templates/#{site.template}/home/socialshare.html.erb") + if Dir[share_file].length>0 + share_file_html = render(:file => share_file, :formats => [:html]) + html = share_file_html.encode("UTF-8", "UTF-8", invalid: :replace, replace: "") + html = html.gsub("{{social-share-data}}",site_sharer) + html.html_safe + else + pos = site[:social_share_position]==1 ? "left" : "right" + trans_hide_social_share_buttons = I18n.t('hide_social_share_buttons') + trans_show_social_share_buttons = I18n.t('show_social_share_buttons') + hide_sharer_button = "" + site_sharer = site_sharer.gsub(/).)*>(?:(?!<\/table>|).)*).)*>((?:(?!<\/table>|).)*)<\/tbody>(?:(?!<\/table>).)*<\/table>/m) do |tbody| + tmp = $1 + tmp = tmp.scan(/).)*)>((?:(?!<\/td>).)*)<\/td>/m).map{|v| "
#{v[1]}<\/span><\/div>"}.join + "
#{hide_sharer_button}#{tmp}<\/div>" + end + floating_class = site[:floating_window].blank? ? "" : " reactable" + hide_in_menu_class = site[:social_share_hide_in_menu] ? " mobile-diff" : "" + "
+
+ #{site_sharer} +
+
".html_safe + end + else + "" + end.html_safe end def render_orbit_bar - if is_mobile_layout_active - orbit_bar_file = File.join('../views', "orbit_bar", 'index_mobile.html.erb') + if $making_cache + return "{{render_orbit_bar}}".html_safe else - orbit_bar_file = File.join('../views', "orbit_bar", 'index.html.erb') + if @orbit_bar_not_display + orbit_bar_file = File.join(Rails.root,'app/views', "orbit_bar", 'no_orbit_bar.html.erb') + elsif is_mobile_layout_active + orbit_bar_file = File.join(Rails.root,'app/views', "orbit_bar", 'index_mobile.html.erb') + else + orbit_bar_file = File.join(Rails.root,'app/views', "orbit_bar", 'index.html.erb') + end + orbit_bar_file_html = render(:file => orbit_bar_file, :formats => [:html]) + orbit_bar_file_html.html_safe end - orbit_bar_file_html = render :file => orbit_bar_file - orbit_bar_file_html.html_safe end - def render_menu + def render_menu(tmp_mobile=nil) + @site ||= CurrentSite + @mobile = tmp_mobile.nil? ? $mobile : tmp_mobile + # cache_data = (@site.get_cache_menu_data(@mobile) rescue nil) + # if !cache_data.blank? + # return cache_data.html_safe + # end # menu_html = Rails.cache.fetch(['main_menu',request.original_fullpath, I18n.locale], race_condition_ttl: 2.seconds) do - # json_file = File.read(File.join(Rails.root, 'public', "menu.json")) - # @items = JSON.parse(json_file) - if $mobile.blank? - @pages = Page.root.sorted_published_child_pages - else - @pages = Page.root.sorted_published_child_pages_for_mobile + # json_file = File.read(File.join(Rails.root, 'public', "menu.json")) + # @items = JSON.parse(json_file) + root_page = @sub_home ? @sub_home : Page.root + def create_json(root_page) + can_access_levels = current_user.nil? ? [nil,"none"] : [nil,"none","users",*(current_user.member_profile.role_ids.collect{|role| role.to_s} rescue [])] + can_access_levels << "ip" if @need_process_ip + match_cond = {"menu_enabled_for"=>{"$in"=>[locale.to_s]}, + "access_level"=>{"$in"=>can_access_levels}, + "parent_page_id"=>{"$nin"=>[nil,""]} + } + if !@mobile.blank? + match_cond = match_cond.merge({"enabled_for_mobile"=>true}) + end + if !root_page.parent_page_id.blank? + match_cond = match_cond.merge({"url" => /^#{root_page.url}\//}) end - def create_json(pages) - item = {} - pages.each do |page| - next if !page.user_can_access?(current_user) - if page.child_page.size > 0 - if page.page_type == "page" - if $mobile.blank? - item["#{page.page_id}"] = {"name" => page.name, "url"=> "/#{locale.to_s}" + page.url, "children"=>create_json(page.sorted_published_child_pages), "target" => "_self"} - else - item["#{page.page_id}"] = {"name" => page.name, "url"=> "/#{locale.to_s}" + page.url, "children"=>create_json(page.sorted_published_child_pages_for_mobile), "target" => "_self"} - end - elsif page.page_type == "link" - target = get_target(page.external_url) - if $mobile.blank? - item["#{page.page_id}"] = {"name" => page.name, "url"=> page.external_url, "children"=>create_json(page.sorted_published_child_pages), "target" => target} - else - item["#{page.page_id}"] = {"name" => page.name, "url"=> page.external_url, "children"=>create_json(page.sorted_published_child_pages_for_mobile), "target" => target} - end - end - else - if page.page_type == "page" - item["#{page.page_id}"] = {"name" => page.name, "url"=> "/#{locale.to_s}" + page.url, "target" => "_self"} - elsif page.page_type == "link" - item["#{page.page_id}"] = {"name" => page.name, "url"=> page.external_url, "target" => get_target(page.external_url)} - end - end - end - item + project1 = {"name" => "$name.#{locale}", + "url" => {"$cond" => [{"$gt"=>["$external_url.#{locale}", ""]}, + "$external_url.#{locale}", + {"$concat" =>[{"$literal"=> (@custom_prefix_url ? @custom_prefix_url : "/#{locale.to_s}")},"$url"]}, + ]}, + "parent_page_id" => "$parent_page_id" + } + project2 = {"name" => "$name", + "url"=>"$url", + "target"=>{"$cond" => [{"$eq" => [{"$substr" => ["$url",0,4]}, "http"]}, + {"$literal"=> "_blank"}, + {"$literal"=> "_self"}, + ]}, + "parent_page_id" => "$parent_page_id" + } + if @process_ip_later + project1['access_level'] = "$access_level" + project1["is_ip"] = {"$eq" => ['$access_level','ip']} + project2['access_level'] = "$access_level" + project2["is_ip"] = {"$eq" => ['$access_level','ip']} end - @items = create_json(@pages) + return Page.collection.aggregate([ + {"$sort" => {"number" => 1}}, + {"$match" => match_cond}, + {"$project" => project1}, + {"$project" => project2}, + {"$group" => {"_id"=>"$parent_page_id","children"=>{"$push"=>"$$ROOT"}}} + ]).inject({}) do |r, s| + r.merge!({s['_id'] => s['children']}) + end + end + @need_process_ip = @site.need_process_ip + @has_privileged_page = @site.has_privileged_page + @process_ip_later = false + if @need_process_ip + @process_ip_later = OrbitHelper.current_user.nil? && @site.enable_cache unless @no_cache_for_this_request + unless @process_ip_later + @need_process_ip = @site.check_ip_is_privileged?((OrbitHelper.request.remote_ip rescue "")) + end + end + @items = create_json(root_page) + if @custom_menu + menu_file = File.open(@custom_menu) + else menu_file = File.open(File.join(Rails.root, 'app', 'templates', "#{@key}", "/home/menu.html.erb")) - doc = Nokogiri::HTML(menu_file, nil, "UTF-8") - menu_file.close + end + doc = Nokogiri::HTML(menu_file, nil, "UTF-8") + menu_file.close - temp = [] - @menus = [] - @menus_items = [] + 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 << doc.css("*[data-menu-level='0']") + temp << doc.css("*[data-menu-level='1']") + temp << doc.css("*[data-menu-level='2']") + @extra_script = doc.css("script").to_s + + 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[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| + t = Nokogiri::HTML(menu, nil, "UTF-8") + a = t.css("*[data-menu-link='true']") + a[0]["href"] = "href_here" + a[0]["target"] = "target_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 + current_path = request.original_fullpath + def create_menu(level,root_id) + html = "" + Array(@items[root_id]).each do |item| + next if @menus_items[level].nil? + li = @menus_items[level].gsub("href_here",(item["url"] || "")) + li = li.gsub("{{link_name}}",(item["name"] || "")) + li = li.gsub("target_here",(item["target"] || "")) + li = (current_path == item['url'] rescue @url == item['url']) ? li.gsub("{{active}}","active") : li.gsub("{{active}}","") - - temp.each_with_index do |menu,i| - t = Nokogiri::HTML(menu, nil, "UTF-8") - a = t.css("*[data-menu-link='true']") - a[0]["href"] = "href_here" - a[0]["target"] = "target_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| - next if @menus_items[level].nil? - li = @menus_items[level].gsub("href_here",(item["url"] || "")) - li = li.gsub("{{link_name}}",(item["name"] || "")) - li = li.gsub("target_here",(item["target"] || "")) - li = request.original_fullpath == item['url'] ? li.gsub("{{active}}","active") : li.gsub("{{active}}","") - - if item["children"] && !item["children"].empty? - li = li.gsub("{{level}}",create_menu(item["children"],level + 1)) - else - li = li.gsub("{{level}}","") - end - html = html + li + if @items[item['_id']] + li = li.gsub("{{level}}",create_menu(level + 1,item['_id'])) + else + li = li.gsub("{{level}}","") end - html = @menus[level].to_s.gsub("{{here}}",html) - html = html.to_s.gsub("{{class_level}}",level.to_s) - html + html = html + li end - h = create_menu(@items,0) - h.html_safe + html = @menus[level].to_s.gsub("{{here}}",html) + html = html.to_s.gsub("{{class_level}}",level.to_s) + html += @extra_script + html + end + def create_menu_process_ip_later(level,root_id) + html = "" + Array(@items[root_id]).each do |item| + next if @menus_items[level].nil? + li = @menus_items[level] + if item['is_ip'] + li = li.sub(' 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]) + 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.html_safe) - element = element.gsub("%7B%7B#{key}%7D%7D",value.to_s.html_safe) - element = render_link_to_edit(element, value) if key.eql?("url_to_edit") + element = element.gsub(/(%7B%7B|{{)#{::Regexp.escape(key)}(%7D%7D|}})/,value.to_s.html_safe) + element = render_link_to_edit(element, value) if key.eql?("url_to_edit") && !data_no_edit end end html_to_render = html_to_render + element @@ -298,36 +699,63 @@ module ApplicationHelper if params[:target_action] == "index" - filename = overridehtml.nil? ? params[:layout_type] : overridehtml - f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', params[:target_controller].singularize, "#{filename}.html.erb") + page = Page.where(:page_id=>params[:page_id]).first + @plugins = OrbitApp::Plugin::Registration.all + @plugins = @plugins.map{|plugin| plugin.module_app_name.underscore rescue nil}.compact + override_data = {} + if @plugins.include? page.module + plugin = OrbitApp::Plugin::Registration.find_by_module_app_name(page.module.classify) || OrbitApp::Plugin::Registration.find_by_module_app_name(page.module) + override_data = {'widget-title'=>plugin.name} + end + filename = overridehtml.nil? ? params[:layout_type].to_s.split('/').last : overridehtml + module_name = params[:target_controller].to_s.split('/').last.singularize + f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', module_name, "#{filename}.html.erb") if !File.exists?f - f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', params[:target_controller].singularize, "index.html.erb") + f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', module_name, "index.html.erb") if !File.exists?f return "
Maybe the administrator has changed the theme, please select the index page design again from the page settings.
".html_safe end end - file = File.open(f) - doc = Nokogiri::HTML(file, nil, "UTF-8") - file.close - controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new - begin - data = controller.send("#{params[:target_action]}")# rescue nil - rescue Exception => e - write_debug_file(e,params[:target_controller],params[:target_action]) if Site::DEBUG - end + f = File.join("#{@key}", 'modules', module_name, "#{filename}.html.erb") + begin + @ac = @ac || ActionController::Base.new() + @ac.prepend_view_path "app/templates" + doc = Nokogiri::HTML.parse(@ac.render_to_string(:file=>f, :formats => [:html])) + rescue => e + doc = Nokogiri::HTML.parse(e.to_s) + write_debug_file(e,params[:target_controller],params[:target_action]) + end + # file = File.open(f) + # doc = Nokogiri::HTML(file, nil, "UTF-8") + # file.close + data = action_data if !data.nil? + OrbitHelper.get_page_controller.redirect_to data['redirect_to'] and return if data['redirect_to'] + data["extras"] = data["extras"] || {} + data["extras"] = data["extras"].merge(override_data) wrap_elements = doc.css("*[data-list][data-level='0']") htmls = parsing_repeats_again(wrap_elements,data,1) - html = doc.to_s + html = doc.to_html htmls[0].each_with_index do |h,i| html = html.gsub(h,htmls[1][i]) end extras = data["extras"] || {} - extras["page-title"] = Page.find_by(:page_id => params[:page_id]).name rescue "" if !extras["page-title"] + unless extras["page-title"] + if params[:current_page_id] + extras["page-title"] = Page.find(params[:current_page_id]).name rescue "" + else + extras["page-title"] = Page.find_by(:page_id => params[:page_id]).name rescue "" + end + if extras["page-title"].blank? + extras["page-title"] = (I18n.t("module_name.#{OrbitHelper.this_module_app.key}") rescue "") + if extras["page-title"].class != String || extras["page-title"].include?("translation missing") + extras["page-title"] = "" + end + end + end extras.each do |key,value| value = value.nil? ? "" : value - html = html.gsub("{{#{key}}}",value.to_s.html_safe) - html = html.gsub("%7B%7B#{key}%7D%7D",value.to_s.html_safe) + html = html.gsub(/(%7B%7B|{{)#{::Regexp.escape(key)}(%7D%7D|}})/,value.to_s.html_safe) end total_pages = data['total_pages'].to_i rescue 1 if total_pages > 1 @@ -340,18 +768,14 @@ module ApplicationHelper return "
No content to show.
".html_safe end else - filename = overridehtml.nil? ? params[:target_action] : overridehtml - f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', params[:target_controller].singularize, "#{filename}.html.erb") + filename = overridehtml.nil? ? params[:target_action].to_s.split('/').last : overridehtml + module_name = params[:target_controller].to_s.split('/').last.singularize + f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', module_name, "#{filename}.html.erb") if File.exists?f file = File.open(f) doc = Nokogiri::HTML(file, nil, "UTF-8") file.close - controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new - begin - data = controller.send("#{params[:target_action]}")# rescue nil - rescue Exception => e - write_debug_file(e,params[:target_controller],params[:target_action]) if Site::DEBUG - end + data = action_data if data.nil? return "
No content to show.
".html_safe end @@ -362,6 +786,7 @@ module ApplicationHelper file.close doc.to_html.html_safe else + OrbitHelper.get_page_controller.redirect_to data['redirect_to'] and return if data['redirect_to'] unless data['impressionist'].blank? Thread.new do impression = data['impressionist'].impressions.create @@ -369,23 +794,23 @@ module ApplicationHelper impression.controller_name = params[:target_controller] impression.action_name = params[:target_action] impression.ip_address = request.remote_ip - impression.session_hash = request.session.id + #session id class: string in ruby 2.1, Rack::Session::SessionId in ruby 2.7. Rack::Session::SessionId cannot stored into db. + impression.session_hash = request.session.id.to_s impression.request_hash = @impressionist_hash impression.referrer = request.referrer impression.save end data['impressionist'].inc(view_count: 1) - data["data"]["view_count"] = data["impressionist"].view_count if data["data"].present? + data["data"]["view_count"] = (data["impressionist"].view_count+(@site.view_count_start rescue 0)) if data["data"].present? end wrap_elements = doc.css("*[data-list][data-level='0']") if wrap_elements.count == 0 - wrap_element_html = doc.to_s + wrap_element_html = doc.to_html el = wrap_element_html data.each do |key,value| - next if key.eql? 'impressionist' + next if key.eql? 'impressionist' value = value.nil? ? "" : value - el = el.gsub("{{#{key}}}",value.to_s.html_safe) - el = el.gsub("%7B%7B#{key}%7D%7D",value.to_s.html_safe) + el = el.gsub(/(%7B%7B|{{)#{::Regexp.escape(key)}(%7D%7D|}})/,value.to_s.html_safe) end el.html_safe else @@ -395,7 +820,7 @@ module ApplicationHelper not_array_key = key if data["#{key}"].kind_of?(Hash) end htmls = parsing_repeats_again(wrap_elements,data,1) - html = doc.to_s + html = doc.to_html htmls[0].each_with_index do |h,i| html = html.gsub(h,htmls[1][i]) end @@ -403,8 +828,7 @@ module ApplicationHelper extras.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) + html = html.gsub(/(%7B%7B|{{)#{::Regexp.escape(key)}(%7D%7D|}})/,value.to_s) end html = render_link_to_edit(html, data["url_to_edit"]) if !data["url_to_edit"].nil? total_pages = data['total_pages'].to_i rescue 1 @@ -422,113 +846,138 @@ module ApplicationHelper end end - def action_data(overrideaction=nil) - overrideaction = overrideaction.nil? ? params[:target_action] : overrideaction - controller = "#{params[:target_controller].capitalize}_controller".classify.constantize.new - begin - data = controller.send("#{overrideaction}")# rescue nil - rescue Exception => e - write_debug_file(e,params[:target_controller],overrideaction) if Site::DEBUG - end - data + def action_data(overrideaction=nil, extra_instance_variables=nil) + overrideaction = overrideaction.nil? ? params[:target_action].to_s : overrideaction + target_controller = params[:target_controller] + action_data_variable_name = "@#{target_controller}_#{overrideaction}_action_data" + tmp_value = instance_variable_get(action_data_variable_name) + if tmp_value + return tmp_value + end + controller_class = "#{target_controller.capitalize}_controller".classify.constantize + @index_controller = controller = controller_class.new + if extra_instance_variables + extra_instance_variables.each do |k, v| + controller.instance_variable_set(k, v) + end + end + begin + can_send_methods = ["index", "show"] + if controller_class.const_defined?(:FrontendMethods) + can_send_methods += controller_class.const_get(:FrontendMethods) + end + if can_send_methods.include?(overrideaction) + data = controller.send("#{overrideaction}")# rescue nil + else + data = nil + end + rescue Mongoid::Errors::DocumentNotFound => e + data = nil + rescue Exception => e + puts [e,e.backtrace] + write_debug_file(e,target_controller,overrideaction) if Site::DEBUG + end + instance_variable_set(action_data_variable_name, data) end def active_for_controllers(*controller_names) - (controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? 'active' : nil - end + (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 + def visible_for_controllers(*controller_names) + (controller_names.include?(controller.controller_name) || controller_names.include?(request.fullpath)) ? '' : 'hide' + end 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 + ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil + end - def link_back(custom_class=nil, params="") - case custom_class - when nil - link_to t('back'), get_go_back + params, :class => 'nav' - else - link_to t('back'), get_go_back + params, :class => custom_class - end - end + def link_back(custom_class=nil, params="") + case custom_class + when nil + link_to t('back'), get_go_back + params, :class => 'nav' + else + link_to t('back'), get_go_back + params, :class => custom_class + end + end - def switch_language(loc) - url = request.original_fullpath - - locale = url.include?("zh_cn") ? :zh_cn : I18n.locale + def switch_language(loc) + url = request.original_fullpath - 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 - url = url + "#{url.include?("?") ? "&" : "?"}locale=#{loc.to_s}" if url == request.original_fullpath and (!url.include?('/'+locale.to_s) or !url.include?(locale.to_s+'=')) - url - end + locale = url.include?("zh_cn") ? :zh_cn : I18n.locale - # 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 - "/#{params[:controller]}" - end - end + 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 + url = url + "#{url.include?("?") ? "&" : "?"}locale=#{loc.to_s}" if url == request.original_fullpath and (!url.include?('/'+locale.to_s) or !url.include?(locale.to_s+'=')) + url + end - def show_avatar(user) - image_tag(user.avatar.thumb.url) - end + # 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 + "/#{params[:controller]}" + end + end - def render_sitemap - items = action_data - def node(items, level) - class_name = nil - case level - when 0 - class_name = "sitemap-list level-1" - when 1 - class_name = "sitemap-list level-2" - when 2 - class_name = "sitemap-list level-3" - end - html = "
    " - items.each do |key,item| - if item["children"] && !item["children"].empty? - url = item["url"] - target = item["target"] - html = html + "
  • #{key}" - html = html + node(item["children"],level + 1) - html = html + "
  • " - else - target = item["target"] - url = item["url"] - html = html + "
  • #{key}
  • " - end - end - html = html + "
" - html - end - html = node(items,0) - html.html_safe - end + def show_avatar(member) + image_tag(member.avatar.thumb.url) + end - def show_attribute_value(value) - if value.kind_of? Hash - result = [] - value.each{|t| result.push(t.last)} - result.join "," - else - value - end - end + def render_sitemap + data = action_data(nil, {:@no_cache_for_this_request=>@no_cache_for_this_request}) + @items = data[:items] + root_page = data[:root_page] + def node(level,root_id) + class_name = nil + case level + when 0 + class_name = "sitemap-list level-1" + when 1 + class_name = "sitemap-list level-2" + when 2 + class_name = "sitemap-list level-3" + end - def create_pagination(total_pages) - file = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home', "pagination.html.erb") - html = "" - if File.exists?file + html = "
    " + Array(@items[root_id]).each do |item| + target = item["target"] + url = item["url"] + key = item['name'] + if @items[item['_id']] + html = html + "#{key}" + html = html + node(level + 1,item['_id']) + html = html + "" + else + html = html + "#{key}" + end + end + html = html + "
" + html + end + html = node(0,root_page.id) + html.html_safe + end + + def show_attribute_value(value) + if value.kind_of? Hash + result = [] + value.each{|t| result.push(t.last)} + result.join "," + else + value + end + end + + def create_pagination(total_pages) + file = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home', "pagination.html.erb") + html = "" + if File.exists?file file = File.open(file) doc = Nokogiri::HTML(file, nil, "UTF-8") file.close @@ -544,7 +993,7 @@ module ApplicationHelper first = first.gsub("{{pagination_active}}","") f_html = f_html + first end - start_number = current_page_number - 2 + start_number = current_page_number - 2 end_number = current_page_number + 2 if end_number > total_pages end_number = total_pages @@ -584,13 +1033,13 @@ module ApplicationHelper html = paginationobj.to_s end html - end + end - def get_target(link) - target = "_blank" - if !link.nil? && link.starts_with?("http") - link = link.split("?").first - temp_url = URI.parse(link) + def get_target(link) + target = "_blank" + if !link.nil? && link.starts_with?("http") + link = link.split("?").first + temp_url = URI.parse(URI.encode(link)) if temp_url.host.nil? target = "_self" end @@ -598,50 +1047,52 @@ module ApplicationHelper target = "_self" end target - end + end - def write_debug_file(e,controller_name,action_name) - url_dir_name = request.fullpath.split("?")[0] - url_dir_name = URI.decode(url_dir_name) - url_dir_name = (url_dir_name == "/" ? "home" : url_dir_name.sub("/","").gsub("/","_").gsub("-","_").gsub(" ","_")) - directory_name = "tmp/debug/#{url_dir_name}" - FileUtils.mkdir_p(directory_name) unless File.exists?(directory_name) - fn = "#{directory_name}/#{controller_name}_#{action_name}.html" - error_trace_spans = "" - e.backtrace.each do |bt| - error_trace_spans = error_trace_spans + "#{bt}
" - end - con = "#{controller_name.capitalize}_controller".classify.constantize - File.open(fn, "w"){ |file| - file.puts " - - - - Debug result - - -

Error Message

-
-

#{e.message}

-
-

Request Details

-
- Url : #{URI.decode(request.url)}
- Controller : #{con.to_s}
- Action : #{action_name.capitalize} -
-

Error Trace

-
- #{error_trace_spans} -
-

Params

-
- #{OrbitHelper.params} -
- - " + def write_debug_file(e,controller_name,action_name) + controller_name = controller_name.to_s.split('/').last + action_name = action_name.to_s.split('/').last + url_dir_name = request.fullpath.split("?")[0] + url_dir_name = URI.decode(url_dir_name) + url_dir_name = (url_dir_name == "/" ? "home" : url_dir_name.sub("/","").gsub("/","_").gsub("-","_").gsub(" ","_")) + directory_name = "tmp/debug/#{url_dir_name}" + FileUtils.mkdir_p(directory_name) unless File.exists?(directory_name) + fn = "#{directory_name}/#{controller_name}_#{action_name}.html" + error_trace_spans = "" + e.backtrace.each do |bt| + error_trace_spans = error_trace_spans + "#{bt}
" + end + con = "#{controller_name.capitalize}_controller".classify.constantize + File.open(fn, "w"){ |file| + file.puts " + + + + Debug result + + +

Error Message

+
+

#{e.message}

+
+

Request Details

+
+ Url : #{URI.decode(request.url)}
+ Controller : #{con.to_s}
+ Action : #{action_name.capitalize} +
+

Error Trace

+
+ #{error_trace_spans} +
+

Params

+
+ #{OrbitHelper.params} +
+ + " - } - end + } + end end