pages_controller.rb
4.75 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
module Kanjai
class PagesController < SiteController
include Admin::PagesHelper
def show
#expires_in 10.minutes, :public => true
if @page_data
if @page_data.page.private_flag == true
if !!UserSession
unless UserSession.current_user
render :template => 'kanjai/shared/401', status: 401, layout: false and return
end
end
end
#look - exist template or not for page
if @page_data.page.page_template
if current_admin_user or stale?(@page_data, public: true)
#get page content
layer = @page_data.page.page_template.get_html_content
content_for_render = setTemplateContent(@page_data.page.page_template, layer, @page_data)
render :text => content_for_render and return
end
#render :html => content_for_render.html_safe
else
page_content = get_html_by_json_client(@page_data)
render :html => page_content.html_safe and return
end
else
render :template => 'kanjai/shared/404', status: 404, layout: false and returm
end
end
private
def setTemplateContent(page_template, layer, page_data)
layer.scan(/(<element name="([\w\W]+?)" type="([\w\W]+?)"><\/element>)/).each do |item|
content = item[0]
name = item[1]
type = item[2]
code = name.parameterize
template_part = page_template.template_parts.find_by_code(code)
if template_part
html = TemplateGenerator::subpart_generate(page_template, template_part.part_type, template_part.elements)
layer.gsub!(content, html)
end
end
layer.scan(/(<element name="([\w\W]+?)"><\/element>)/).each do |item|
content = item[0]
name = item[1]
code = name.parameterize
template_part = page_template.template_parts.find_by_code(code)
if template_part
html = TemplateGenerator::subpart_generate(page_template, template_part.part_type, template_part.elements)
layer.gsub!(content, html)
end
end
#try find in static elements
page_template.page_content_markers.where(lang: I18n.locale).each do |marker|
layer.gsub!(marker.marker, marker.text_value)
end
markers = PageTemplate.get_marker(layer)
markers.each do |marker|
case marker
when '###CONTENT###'
page_content = get_html_by_json_client(page_data)
layer.gsub!(marker, page_content)
when '###META_TITLE###'
layer.gsub!(marker, page_data.meta_title.to_s)
when '###META_DESCRIPTION###'
layer.gsub!(marker, page_data.meta_description.to_s)
when '###META_KEYWORDS###'
layer.gsub!(marker, page_data.meta_keywords.to_s)
when '###BODY_CONTENT_EDIT_CLASS_NAME###'
body_class_name = ''
body_class_name = 'editable-content' if current_admin_user
layer.gsub!(marker, body_class_name)
when '###VARIABLE_FOR_EDIT_CONTENT###'
content = ''
if current_admin_user
content = show_frontend_editor_admin_page_url(@page_data.page)
end
layer.gsub!(marker, content)
when '###CSRF_PARAM###'
layer.gsub!(marker, Rack::Utils.escape_html(request_forgery_protection_token))
when '###CSRF_TOKEN###'
layer.gsub!(marker, Rack::Utils.escape_html(form_authenticity_token))
when '###DOMAIN_NAME###'
layer.gsub!(marker, $scheme + ADMIN_CONFIG['domain_name'])
when '###INCLUDE_JS_FILE###'
js_array = []
if current_admin_user
js_array << "<script src='#{$scheme}#{ADMIN_CONFIG['domain_name']}#{ActionController::Base.helpers.asset_path('kanjai/frontend.js')}'></script>"
css_path = "#{$scheme}#{ADMIN_CONFIG['domain_name']}#{ActionController::Base.helpers.asset_path('kanjai/frontend.css')}"
js_array << "<script type='text/javascript'>$('<link>', {rel: 'stylesheet',type: 'text/css',href: '#{css_path}'}).appendTo('head');</script>"
else
js_array << "<script src='#{$scheme}#{ADMIN_CONFIG['domain_name']}#{ActionController::Base.helpers.asset_path('kanjai/frontend_not_login.js')}'></script>"
end
layer.gsub!(marker, js_array.join(''))
else
#if PageMarker.public_instance_methods.include?("process_#{marker.gsub('###', '')}".to_sym)
# page_marker = PageMarker.new
# content = page_marker.method("process_#{marker.gsub('###', '')}").call(page_data)
# layer.gsub!(marker, content)
#end
end
end
return layer
end
end
end