pages_helper.rb
7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
module Kanjai
module Admin::PagesHelper
require 'open-uri'
def get_html_by_json(page_data, client_view = false)
json_data = page_data.template_content
html = ''
json_data.each do |row|
row['attributes'] ||= {}
class_name = 'row-fluid row'
class_name += ' row-element-block' if row['attributes']['type'] == 'block'
class_name += ' row-element-row' if row['attributes']['type'] == 'row'
html_attributes = {}
html_attributes[:class] = class_name
html_attributes['data-status'] = row['attributes']['status'].to_s == 'enabled' || row['attributes']['status'].to_s.empty? ? 'enabled' : 'disabled'
row_html = ''
if client_view == false and row['attributes']['type'] == 'block'
html_attributes['data-id'] = row['attributes']['id']
page_content = page_data.page_contents.where(:structure_id => row['attributes']['id']).first
row_html = '<div style="width:100%;" class="col-html">' + (page_content.nil? ? '' : wrap_iframe(page_content)) + '</div>'
end
if client_view == false and row['attributes']['type'] == 'row'
html_attributes['data-id'] = row['attributes']['id']
end
html_attributes['data-class'] = row['attributes']['className'];
html += ActionController::Base.helpers.content_tag(:div, '', html_attributes) do
ActionController::Base.helpers.concat(row_html.html_safe)
row["cells"].each do |cell|
page_content = page_data.page_contents.where(:structure_id => cell['id']).first
html = page_content.nil? ? '' : wrap_iframe(page_content)
cell_class_name = ''
ActionController::Base.helpers.concat(ActionController::Base.helpers.content_tag(:div, html.html_safe, :class => "col-md-#{cell['size']} col #{cell_class_name}", 'data-size' => cell['size'], 'data-offset' => cell['offset'], 'data-id' => cell['id'], 'data-page_data_id' => page_data.id , 'data-class' => cell['className'], 'data-status' => cell['status'].to_s == 'enabled' || cell['status'].to_s.empty? ? 'enabled' : 'disabled' ))
end
end
end
html
end
def get_html_by_json_client(page_data, session = nil)
json_data = page_data.template_content
html = ''
#row_template = File.open("#{Rails.root}/app/view/kanjai/pages/templates/row.html.erb").read
#cell_template = File.open("#{Rails.root}/app/view/kanjai/pages/templates/cell.html.erb").read
json_data.each do |row|
row['attributes'] ||= {}
if row['attributes']['type'] == 'block'
if row['attributes']['status'].to_s == 'enabled' || row['attributes']['status'].to_s.empty?
#show direct content
structure_id = row['attributes']['id']
page_content = page_data.page_contents.where(:structure_id => structure_id).first
if page_content
html_attributes = {}
html_attributes[:class] = 'col'
html_attributes['data-id'] = row['attributes']['id']
html_attributes['data-page_data_id'] = page_data.id
if page_content.type_content.split('-').include?('wrapper')
html += (page_content.get_simple_content).html_safe
else
cells = page_content.nil? ? '' : page_content.get_content_frontend(session)
data_attributes = [
"data-id=#{row['attributes']['id']}",
"data-page_data_id=#{page_data.id}"
]
html += ApplicationController.render(file: 'kanjai/pages/templates/block', assigns: {content: cells.html_safe, data_attributes: data_attributes.join(' '), row: row}, layout: false).html_safe
end
end
end
else
if row['attributes']['status'].to_s == 'enabled' || row['attributes']['status'].to_s.empty?
cells = ''
row["cells"].each do |cell|
if cell['status'].to_s == 'enabled' || cell['status'].to_s.empty?
page_content = page_data.page_contents.where(:structure_id => cell['id']).first
html_content = page_content.nil? ? '' : page_content.get_content_frontend(session)
cell_class_name = cell['className']
data_attributes = [
"data-size=#{cell['size']}",
"data-offset=#{cell['offset']}",
"data-id=#{cell['id']}",
"data-page_data_id=#{page_data.id}",
"data-class=#{cell['className']}",
]
cells += ApplicationController.render file: 'kanjai/pages/templates/cell', assigns: {content: html_content.html_safe, cell: cell, class_name: cell_class_name, data_attributes: data_attributes.join(' ')}, layout: false
end
end
html += ApplicationController.render(file: 'kanjai/pages/templates/row', assigns: {content: cells.html_safe, row: row}, layout: false).html_safe
end
end
end
#html = '<div class="container">' + html.to_s + '</div>'
html.to_s
end
def get_html_by_json_client_url(url, locale, session = nil)
page_data = PageDatum.where(:url => url, lang: locale).first
get_html_by_json_client(page_data, session)
end
def make_navigation(parent_id, level)
html = ''
if level > 0
html += ActionController::Base.helpers.content_tag(:ul, class: 'nav navbar-nav') do
Page.where(:parent_id => parent_id).order(:position).each do |page|
if page.children.count > 0
ActionController::Base.helpers.concat(
ActionController::Base.helpers.content_tag(:li) do
ActionController::Base.helpers.concat(
ActionController::Base.helpers.link_to (page.lang_attributes(PageLang.default(page.domain), :title) + '<span class="caret"></span>').html_safe, page.menu_url(session[:scheme], lang = nil), :class => 'dropdown-toggle', 'data-toggle' => "dropdown", 'role' => "button", 'aria-haspopup' => "true", 'aria-expanded' => "false"
)
ActionController::Base.helpers.concat(
ActionController::Base.helpers.content_tag(:ul) do
ActionController::Base.helpers.concat(make_navigation(page.id, level - 1))
end
)
end
)
else
ActionController::Base.helpers.concat(
ActionController::Base.helpers.content_tag(:li) do
ActionController::Base.helpers.concat(
ActionController::Base.helpers.link_to page.lang_attributes(PageLang.default(page.domain), :title), page.menu_url(session[:scheme], lang = nil)
)
end
)
end
end
end
end
html.html_safe
end
def wrap_iframe(page_content)
content = page_content.get_content_frontend
layer = page_content.page_datum.page.page_template.get_html_content
css_files = [].tap do |n|
doc = Nokogiri::HTML(layer, nil, 'UTF-8')
doc.css("link").each do |x|
if x['rel'] == 'stylesheet'
css_link = x['href']
n << css_link
end
end
end
styles = css_files.collect do |file|
#begin
"<link rel='stylesheet' href='#{file}'>"
#rescue
#end
end
#css_files = css_files.collect{|link| "<link rel='stylesheet' href='#{link}' />".html_safe }.join('')
content += "#{styles.join(' ')}".html_safe
#<link rel="stylesheet" href="./assets/css/libs.bundle.css" />
"<iframe frameBorder='0' src='data:text/html;charset=utf-8,#{ERB::Util.url_encode(content)}'></iframe>"
end
end
end