page_content.rb
7.81 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
199
200
201
202
203
204
205
206
207
208
209
210
211
require 'kanjai/template_generator'
module Kanjai
class PageContent < ActiveRecord::Base
belongs_to :page
belongs_to :page_datum
has_many :page_content_markers, dependent: :destroy
accepts_nested_attributes_for :page_content_markers
def self.edit_template(page, content_type)
case content_type
when 'rte_editor'
return ['kanjai/admin/pages/content_types/rte_editor', {}]
when 'plugin'
return ['kanjai/admin/pages/content_types/plugin', {}]
else
template_part = page.page_template.template_parts.find_by_code(content_type)
if (template_part.part_type == 'content')
return ['kanjai/admin/pages/content_types/content', {template_part: template_part}]
end
end
end
def build_markers(type_content, build_always = false)
if type_content != 'rte_editor' and (type_content != self.type_content || build_always)
self.update_column(:type_content, type_content)
self.page_content_markers.each do |item|
item.destroy
end
page_template = self.page.page_template
if page_template
subpart = page_template.template_parts.find_by_code(type_content)
if subpart
#find repeat block
repeat_element = subpart.field_options.select{|item| item[:name] == 'repeat' }.first
count_repeat = nil
if repeat_element and repeat_element[:attributes]['count'].present?
count_repeat = repeat_element[:attributes]['count'].to_i
end
subpart.field_options.each do |item|
if item[:name] != 'repeat'
if item[:attributes]['repeatItemId'] == ""
marker_obj = self.page_content_markers.find_by_marker(item[:name])
unless marker_obj
if item[:attributes]['repeat'].to_s == 'true'
if count_repeat.to_i > 0
(1..count_repeat).each do |index|
self.page_content_markers.create({
marker: item[:name],
row_item: index,
marker_name: item[:itemName]
})
end
end
else
self.page_content_markers.create({
marker: item[:name],
marker_name: item[:itemName]
})
end
end
end
end
end
subpart.conditions.select{|item| item["repeat_id"] == "" }.each do |cond|
self.page_content_markers.create({
marker: cond["name"],
marker_name: cond["name"],
condition: true
})
end
end
end
end
end
def build_markers_rows(type_content, repeat_id, parent_item_index)
row_item = nil
if type_content != 'rte_editor'
page_template = self.page.page_template
if page_template
subpart = page_template.template_parts.find_by_code(type_content)
if subpart
#find current max row_item
row_item = self.page_content_markers.where(parent_id: parent_item_index, repeat_id: repeat_id).collect(&:row_item).max
row_item = 0 if row_item.nil?
row_item += 1
subpart.field_options.each do |item|
if item[:name] != 'repeat'
if item[:attributes]['repeatItemId'] == repeat_id
self.page_content_markers.create({
marker: item[:name],
row_item: row_item,
marker_name: item[:itemName],
parent_id: parent_item_index,
repeat_id: repeat_id,
condition: false
})
end
end
end
subpart.conditions.select{|item| item["repeat_id"] == repeat_id }.each do |cond|
self.page_content_markers.create({
marker: cond["name"],
row_item: row_item,
marker_name: cond["name"],
parent_id: parent_item_index,
repeat_id: repeat_id,
condition: true
})
end
end
end
end
return row_item
end
def delete_markers_row(row_item, repeat_id, parent_item_index)
template_part = page.page_template.template_parts.find_by_code(type_content)
self.page_content_markers.where(row_item: row_item, repeat_id: repeat_id, parent_id: parent_item_index).delete_all
repeat_element = template_part.field_options.select{|item| item[:name] == 'repeat' && item[:repeatItemId].to_s == repeat_id}.first
if repeat_element
ids = self.page_content_markers.where(repeat_id: repeat_element[:id], parent_id: row_item).pluck(:row_item).uniq
ids.each do |index|
delete_markers_row(index, repeat_element[:id], row_item)
end
end
end
def get_content
if self.type_content == 'rte_editor'
I18n.t('admin.content_types.rte_editor')
elsif self.type_content == 'plugin'
"#{I18n.t('admin.content_types.plugin')}: #{controller_name}.#{action_name}"
else
subpart = page.page_template.template_parts.find_by_code(self.type_content)
subpart ? subpart.name : self.type_content
end
end
def get_content_frontend
if self.type_content == 'rte_editor'
self.text_html.to_s
elsif self.type_content == 'plugin'
controller = self.controller_name.constantize.new
controller.send self.action_name
controller_variables = controller.instance_variables
hash = {}
controller_variables.each do |variable|
hash[variable.to_s[1..-1]] = controller.instance_variable_get(variable.to_s)
end
self.controller_name.constantize.render self.action_name.to_sym, assigns: hash
else
page_template = self.page.page_template
subpart = page_template.template_parts.find_by_code(self.type_content)
if subpart
html = Kanjai::TemplateGenerator.subpart_generate(nil, self.page.domain, page_template, 'content', subpart.elements, self.marker_hash, self.id)
else
html = ''
end
html.gsub("#newline#", '<br/>')
end
end
def get_simple_content
subpart = page.page_template.template_parts.find_by_code(self.type_content)
subpart.source
end
def marker_hash
result = {}
self.page_content_markers.order(:row_item).each do |item|
result[item.row_item] ||= {}
result[item.row_item][item.marker] = item.text_value
end
result
end
def formatted_conditions(repeat_id)
(conditions || [])[repeat_id]
end
end
end