orbit4-5/app/helpers/pages_helper.rb

92 lines
3.7 KiB
Ruby

module PagesHelper
def parse_pp_tags(file)
opened_file = File.open(file)
doc = Nokogiri::HTML(opened_file, nil, "UTF-8")
opened_file.close
pps = doc.css("*[data-pp]")
pps.each do |pp|
pp.content = "<p class='add-ps'></p>"
end
doc.to_html
end
def render_widget_for_frontend(controller_name, widget_method, widget_file)
def widget_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 = widget_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.html_safe)
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
controller_name = controller_name.downcase.singularize
f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', "#{controller_name}", "_#{widget_file}.html.erb");
if !File.exists?f
f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'widgets', "#{controller_name}", "_#{widget_file}.html.erb");
if !File.exists?f
f = File.join(Rails.root, 'app', 'templates', "#{@key}", 'modules', "#{controller_name}", "_widget.html.erb");
if File.exists?f
f = File.join('../templates', "#{@key}", 'modules', "#{controller_name}", "_widget.html.erb");
else
return "<div class='well'>Maybe the administrator has changed the theme, please select the widget design again from the page settings.</div>".html_safe
end
else
f = File.join('../templates', "#{@key}", 'widgets', "#{controller_name}", "_#{widget_file}.html.erb");
end
else
f = File.join('../templates', "#{@key}", 'modules', "#{controller_name}", "_#{widget_file}.html.erb");
end
s = render_to_string(f)
doc = Nokogiri::HTML(s, nil, "UTF-8")
wrap_elements = doc.css("*[data-repeat]")
controller = "#{controller_name.capitalize.pluralize}_controller".classify.constantize.new
OrbitHelper.set_current_widget_module controller_name
data = controller.send("#{widget_method}") rescue nil
if !data.nil?
wrap_elements = doc.css("*[data-list][data-level='0']")
htmls = widget_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
extras = data['extras'] || {}
extras["widget-title"] = OrbitHelper.widget_title
extras.each do |key,value|
html = html.gsub("{{#{key}}}",value.to_s.html_safe)
html = html.gsub("%7B%7B#{key}%7D%7D",value.to_s)
end
html.html_safe
else
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
end
end
end