2014-06-30 10:08:48 +00:00
# encoding: utf-8
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
2014-08-15 09:47:14 +00:00
site = current_site
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
2014-06-30 09:11:41 +00:00
def render_site_title
2014-08-15 09:47:14 +00:00
site = current_site
2014-06-30 09:11:41 +00:00
title = site . title rescue " "
if site . title_always_on
if ! params [ :slug ] . nil?
temp_title = params [ :slug ] . sub ( " - #{ params [ :uid ] } " , " " )
temp_title = temp_title . gsub ( " - " , " " )
title = " #{ temp_title } | #{ title } "
2014-07-16 09:12:23 +00:00
elsif params [ :target_action ] == " index "
temp_title = Page . find_by ( :page_id = > params [ :page_id ] ) . name
title = " #{ temp_title } | #{ title } "
2014-06-30 09:11:41 +00:00
end
end
title
end
2014-07-07 08:18:52 +00:00
def render_google_analytics
2014-08-15 09:47:14 +00:00
current_site . google_analytics . html_safe rescue " "
2014-07-07 08:18:52 +00:00
end
2014-05-22 11:19:25 +00:00
def render_footer
2014-08-15 09:47:14 +00:00
site = current_site
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-07-11 13:06:23 +00:00
if $mobile . blank?
@pages = Page . root . sorted_published_child_pages
else
@pages = Page . root . sorted_published_child_pages_for_mobile
end
2014-04-01 07:10:21 +00:00
def create_json ( pages )
item = { }
pages . each do | page |
if page . child_page . size > 0
2014-06-09 10:58:44 +00:00
if page . page_type == " page "
2014-07-11 13:06:23 +00:00
if $mobile . blank?
item [ " #{ page . name } " ] = { " url " = > " / #{ locale . to_s } " + page . url , " children " = > create_json ( page . sorted_published_child_pages ) , " target " = > " _self " }
else
item [ " #{ page . name } " ] = { " url " = > " / #{ locale . to_s } " + page . url , " children " = > create_json ( page . sorted_published_child_pages_for_mobile ) , " target " = > " _self " }
end
2014-06-09 10:58:44 +00:00
elsif page . page_type == " link "
2014-07-16 10:48:23 +00:00
target = get_target ( page . external_url )
2014-07-11 13:06:23 +00:00
if $mobile . blank?
2014-07-16 10:48:23 +00:00
item [ " #{ page . name } " ] = { " url " = > page . external_url , " children " = > create_json ( page . sorted_published_child_pages ) , " target " = > target }
2014-07-11 13:06:23 +00:00
else
2014-07-16 10:48:23 +00:00
item [ " #{ page . name } " ] = { " url " = > page . external_url , " children " = > create_json ( page . sorted_published_child_pages_for_mobile ) , " target " = > target }
2014-07-11 13:06:23 +00:00
end
2014-06-09 10:58:44 +00:00
end
2014-04-01 07:10:21 +00:00
else
2014-06-09 10:58:44 +00:00
if page . page_type == " page "
item [ " #{ page . name } " ] = { " url " = > " / #{ locale . to_s } " + page . url , " target " = > " _self " }
elsif page . page_type == " link "
2014-07-16 10:48:23 +00:00
item [ " #{ page . name } " ] = { " url " = > page . external_url , " target " = > get_target ( page . external_url ) }
2014-06-09 10:58:44 +00:00
end
2014-04-01 07:10:21 +00:00
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 "
2014-06-09 10:58:44 +00:00
a [ 0 ] [ " target " ] = " target_here "
2014-03-12 04:42:59 +00:00
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-06-09 10:58:44 +00:00
li = li . gsub ( " target_here " , item [ " target " ] )
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-08-05 06:45:52 +00:00
def render_link_to_edit ( html , url_to_edit )
if html . scan ( " {{link_to_edit}} " ) . length == 0
html = url_to_edit . blank? ? html : html + " <p class='admin-edit text-right'><a class='btn btn-primary' href=' #{ url_to_edit } '><i class='icon-edit'></i> #{ t ( :edit ) } </a></p> "
else
html = url_to_edit . blank? ? html . gsub ( " {{link_to_edit}} " , " " ) : html . gsub ( " {{link_to_edit}} " , " <p class='admin-edit text-right'><a class='btn btn-primary' href=' #{ url_to_edit } '><i class='icon-edit'></i> #{ t ( :edit ) } </a></p> " )
end
return html
end
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 } '] " )
2014-08-05 06:45:52 +00:00
if d [ data_name ]
d [ data_name ] . each_with_index do | item , i |
2014-05-22 11:19:25 +00:00
element = el . inner_html
if wrap_elements . count > 0
2014-08-05 06:45:52 +00:00
htmls = parsing_repeats_again ( wrap_elements , d [ data_name ] [ i ] , level + 1 )
2014-05-22 11:19:25 +00:00
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 )
2014-08-05 07:53:08 +00:00
element = render_link_to_edit ( element , value ) if key . eql? ( " url_to_edit " )
2014-05-22 11:19:25 +00:00
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-06-06 08:30:56 +00:00
f = File . join ( Rails . root , 'app' , 'templates' , " #{ @key } " , 'modules' , params [ :target_controller ] . singularize , " #{ params [ :layout_type ] } .html.erb " )
if ! File . exists? f
f = File . join ( Rails . root , 'app' , 'templates' , " #{ @key } " , 'modules' , params [ :target_controller ] . singularize , " index.html.erb " )
2014-07-16 09:12:23 +00:00
if ! File . exists? f
return " <div class='well'>Maybe the administrator has changed the theme, please select the index page design again from the page settings.</div> " . html_safe
end
2014-06-06 08:30:56 +00:00
end
file = File . open ( f )
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-07-16 07:23:27 +00:00
data = controller . send ( " #{ params [ :target_action ] } " ) rescue nil
2014-07-16 07:12:41 +00:00
if ! data . nil?
wrap_elements = doc . css ( " *[data-list][data-level='0'] " )
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
2014-07-16 09:12:23 +00:00
extras = data [ " extras " ] || { }
extras [ " page-title " ] = Page . find_by ( :page_id = > params [ :page_id ] ) . name rescue " " if ! extras [ " page-title " ]
extras . 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 )
2014-05-22 11:19:25 +00:00
end
2014-07-16 09:12:23 +00:00
2014-07-16 07:12:41 +00:00
total_pages = data [ 'total_pages' ] . to_i rescue 1
if total_pages > 1
html = html . gsub ( " {{pagination_goes_here}} " , create_pagination ( total_pages ) )
else
html = html . gsub ( " {{pagination_goes_here}} " , " " ) ;
end
html . html_safe
2014-06-04 12:36:12 +00:00
else
2014-07-16 09:12:23 +00:00
return " <div class='well'>It seems we have a problem with the module at this point of time, we will try to fix it as soon as possible. Sorry for the inconvenience!! :( </div> " . html_safe
2014-06-04 12:36:12 +00:00
end
2014-06-18 09:57:36 +00:00
else
f = File . join ( Rails . root , 'app' , 'templates' , " #{ @key } " , 'modules' , params [ :target_controller ] . singularize , " #{ params [ :target_action ] } .html.erb " )
if File . exists? f
file = File . open ( f )
2014-05-05 06:04:50 +00:00
doc = Nokogiri :: HTML ( file , nil , " UTF-8 " )
file . close
2014-06-18 09:57:36 +00:00
controller = " #{ params [ :target_controller ] . capitalize } _controller " . classify . constantize . new
2014-07-16 07:21:04 +00:00
data = controller . send ( " #{ params [ :target_action ] } " ) rescue nil
if data . nil?
2014-07-16 09:12:23 +00:00
return " <div class='well'>It seems we have a problem with the module at this point of time, we will try to fix it as soon as possible. Sorry for the inconvenience!! :( </div> " . html_safe
2014-07-16 07:21:04 +00:00
end
2014-06-18 09:57:36 +00:00
2014-07-16 07:21:04 +00:00
if data . blank? || data . empty?
2014-06-18 09:57:36 +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-06-18 09:57:36 +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
2014-05-22 11:19:25 +00:00
end
2014-06-18 09:57:36 +00:00
wrap_elements = doc . css ( " *[data-list][data-level='0'] " )
if wrap_elements . count == 0
wrap_element_html = doc . to_s
el = wrap_element_html
data . each do | key , value |
next if key . eql? 'impressionist'
2014-05-22 11:19:25 +00:00
value = value . nil? ? " " : value
2014-06-18 09:57:36 +00:00
el = el . gsub ( " {{ #{ key } }} " , value . to_s )
el = el . gsub ( " %7B%7B #{ key } %7D%7D " , value . to_s )
end
el . html_safe
else
keys = data . keys
not_array_key = nil
data . keys . each do | key |
not_array_key = key if data [ " #{ key } " ] . kind_of? ( Hash )
end
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
2014-07-16 09:12:23 +00:00
extras = data [ " #{ not_array_key } " ] || { }
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 )
2014-05-22 11:19:25 +00:00
end
2014-08-05 06:45:52 +00:00
html = render_link_to_edit ( html , data [ " url_to_edit " ] ) if ! data [ " url_to_edit " ] . nil?
2014-06-18 09:57:36 +00:00
html . html_safe
2014-05-22 11:19:25 +00:00
end
2014-05-02 10:19:57 +00:00
end
2014-07-16 09:12:23 +00:00
else
return " <div class='well'>There is a problem with the design. We will try to fix it as soon as possible. Sorry for the inconvenience!! :(</div> " . html_safe
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
2014-07-09 02:05:02 +00:00
locale = url . include? ( " zh_cn " ) ? :zh_cn : I18n . 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-08-08 03:46:21 +00:00
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 + '=' ) )
2014-05-24 09:12:19 +00:00
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
2014-10-01 08:12:38 +00:00
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 = " <ul class=' #{ class_name } '> "
2014-05-22 11:19:25 +00:00
items . each do | key , item |
if item [ " children " ] && ! item [ " children " ] . empty?
url = item [ " url " ]
2014-06-09 10:58:44 +00:00
target = item [ " target " ]
html = html + " <li><a href=' #{ url } ' target=' #{ target } '> #{ key } </a> "
2014-10-01 08:12:38 +00:00
html = html + node ( item [ " children " ] , level + 1 )
2014-05-22 11:19:25 +00:00
html = html + " </li> "
else
2014-06-09 10:58:44 +00:00
target = item [ " target " ]
2014-05-22 11:19:25 +00:00
url = item [ " url " ]
2014-06-09 10:58:44 +00:00
html = html + " <li><a href=' #{ url } ' target=' #{ target } '> #{ key } </a></li> "
2014-05-22 11:19:25 +00:00
end
end
html = html + " </ul> "
html
end
2014-10-01 08:12:38 +00:00
html = node ( items , 0 )
2014-05-22 11:19:25 +00:00
html . html_safe
end
2014-06-25 05:27:34 +00:00
def show_attribute_value ( value )
if value . kind_of? Hash
result = [ ]
value . each { | t | result . push ( t . last ) }
result . join " , "
else
value
end
end
2014-06-11 09:52:29 +00:00
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
paginationobj = doc . css ( " *[data-pagination='true'] " ) . first
in_html = first = last = nextpage = prevpage = paginationobj . inner_html
f_html = " "
current_page_number = OrbitHelper . page_number
if current_page_number > 1
first = first . gsub ( " %7B%7Bpagination_link%7D%7D " , " ?page_no=1 " )
first = first . gsub ( " {{page_number}} " , " First " )
first = first . gsub ( " {{pagination_active}} " , " " )
f_html = f_html + first
end
start_number = current_page_number - 2
end_number = current_page_number + 2
if end_number > total_pages
end_number = total_pages
start_number = total_pages - 4
end
if start_number < 1
start_number = 1
end_number = 5
end
2014-06-18 06:46:47 +00:00
end_number = end_number > total_pages ? total_pages : end_number
2014-06-11 09:52:29 +00:00
( start_number .. end_number ) . each do | i |
h = in_html
h = h . gsub ( " %7B%7Bpagination_link%7D%7D " , " ?page_no= #{ i . to_s } " )
h = h . gsub ( " {{page_number}} " , i . to_s )
h = h . gsub ( " {{pagination_active}} " , ( i == current_page_number ? " active " : " " ) )
f_html = f_html + h
end
if current_page_number > 1
prevpage = prevpage . gsub ( " %7B%7Bpagination_link%7D%7D " , " ?page_no= #{ current_page_number - 1 } " )
prevpage = prevpage . gsub ( " {{page_number}} " , " « " )
prevpage = prevpage . gsub ( " {{pagination_active}} " , " " )
f_html = f_html + prevpage
end
if current_page_number < total_pages
nextpage = nextpage . gsub ( " %7B%7Bpagination_link%7D%7D " , " ?page_no= #{ current_page_number + 1 } " )
nextpage = nextpage . gsub ( " {{page_number}} " , " » " )
nextpage = nextpage . gsub ( " {{pagination_active}} " , " " )
f_html = f_html + nextpage
last = last . gsub ( " %7B%7Bpagination_link%7D%7D " , " ?page_no= #{ total_pages } " )
last = last . gsub ( " {{page_number}} " , " Last " )
last = last . gsub ( " {{pagination_active}} " , " " )
f_html = f_html + last
end
paginationobj . inner_html = f_html
html = paginationobj . to_s
end
html
end
2014-07-16 10:48:23 +00:00
def get_target ( link )
temp_url = URI . parse ( link )
target = " _blank "
if temp_url . host . nil?
target = " _self "
end
target
end
2014-03-12 04:42:59 +00:00
end