pages_controller.rb
7.44 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
module Kanjai
class Admin::PagesController < AdminController
before_filter :get_page_list, :only => [:new, :create]
before_filter :get_page_list_without_current, :only => [:edit, :update]
def new
@page = Page.new
end
def create
@page = Page.new(permitted_params[:page])
if @page.save
if @page.default_private_page
Page.where('id != ?', @page.id).each do |item|
item.update_column(:default_private_page, false)
end
end
if @page.root_page
Page.where('id != ?', @page.id).each do |item|
item.update_column(:root_page, false)
end
end
page_data = @page.page_data.new(
lang: PageLang.default,
title: @page.title
)
page_data.save(:validate => false)
redirect_to edit_admin_page_url(@page, lang: PageLang.default)
else
render :action => :new
end
end
def edit
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
if @page_data.nil?
@page_data = @page.page_data.new(
lang: params[:lang]
)
@page_data.save(validate:false)
end
end
def update
@page = Page.find(params[:id])
if @page.update(permitted_params[:page])
if @page.default_private_page
Page.where('id != ?', @page.id).each do |item|
item.update_column(:default_private_page, false)
end
end
if @page.root_page
Page.where('id != ?', @page.id).each do |item|
item.update_column(:root_page, false)
end
end
redirect_to :action => :index
else
render :action => :edit, lang: params[:lang]
end
end
def destroy
@page = Page.find(params[:id])
@page.destroy
render :json => {:status => 'ok'}
end
def show_editor
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
@structure_id = params[:structure_id]
@obj = @page_data.page_contents.find_by_structure_id(@structure_id)
if @obj.nil?
@obj = PageContent.create({:page => @page, :page_datum => @page_data, :structure_id => @structure_id})
end
@type_contents = []
@type_contents << [I18n.t('admin.content_types.rte_editor'), 'rte_editor']
@type_contents << [I18n.t('admin.content_types.plugin'), 'plugin']
if page_template = @page_data.page.page_template
@type_contents += page_template.template_parts.where("part_type in ('content')").collect{|p| [p.name, p.code] }
end
@type_content = @obj.type_content
@obj.build_markers(@obj.type_content)
render :layout => false
end
def update_editor
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
@structure_id = params[:structure_id]
@obj = @page_data.page_contents.find_by_structure_id(@structure_id)
if @obj.nil?
@obj = PageContent.create({:page => @page, :page_datum => @page_data, :structure_id => @structure_id})
end
@obj.update(permitted_params[:page_content])
render :json => {:status => 'ok', :structure_id => @structure_id, :html => @obj.get_content}
end
def gallery
@page = Page.find(params[:id])
end
def meta
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
end
def meta_update
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
if @page_data.update(permitted_params[:page_datum])
redirect_to :action => :index
else
render :action => :meta, lang: params[:lang]
end
end
def get_gallery
@page = Page.find(params[:id])
@collection = @page.images
render :layout => false
end
def sort
@page = Page.find(params[:id])
case params[:direction]
when 'up'
@page.move_higher
when 'down'
@page.move_lower
end
redirect_to :action => :index
end
def structure
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
if @page_data.nil?
@page_data = @page.page_data.new(
lang: params[:lang]
)
@page_data.save(validate:false)
end
@page_data.template_content ||= []
end
def save_structure
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
content = JSON.parse(params[:content])
if @page_data.update_attribute(:template_content, content)
render :json => {:status => 'ok'}
else
render :json => {:status => 'error'}
end
end
def delete_content
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
if params[:structure_id]
obj = @page_data.page_contents.find_by_structure_id(params[:structure_id])
obj.destroy if obj
end
if params[:structure_ids]
params[:structure_ids].each do |structure_id|
obj = @page_data.page_contents.find_by_structure_id(structure_id)
obj.destroy if obj
end
end
render :nothing => true
end
def show_frontend_editor
@page = Page.find(params[:id])
@page_data = @page.page_data.find(params[:page_data_id])
@structure_id = params[:structure_id]
@obj = @page_data.page_contents.find_by_structure_id(@structure_id)
if @obj.nil?
@obj = PageContent.new
end
@type_content = @obj.type_content
@obj.build_markers(@obj.type_content)
render :layout => false
end
def update_frontend_editor
@page = Page.find(params[:id])
@page_data = @page.page_data.find(params[:page_data_id])
@structure_id = params[:structure_id]
@obj = @page_data.page_contents.find_by_structure_id(@structure_id)
if @obj.nil?
@obj = PageContent.create({:page => @page, :page_datum => @page_data, :structure_id => @structure_id})
end
@obj.update(permitted_params[:page_content])
render :json => {:status => 'ok', :structure_id => @structure_id, :html => @obj.get_content_frontend}
end
private
def permitted_params
params.permit(:page => [:parent_id, :page_template_id, :title, :private_flag, :default_private_page, :root_page,
:page_data_attributes => [:id, :title, :url, :meta_title, :meta_description, :meta_keywords]],
:page_content => [:type_content, :text_html, :controller_name, :action_name,
page_content_markers_attributes: [:id, :page_content_id, :marker, :text_value, :attachment_file_name]
],
:page_datum => [:id, :title, :url, :meta_title, :meta_description, :meta_keywords]
)
end
def get_page_list
@pages = []
Page.walk_tree do |page, level|
@pages << {title: page.lang_attributes(PageLang.default, :title), level: level, id: page.id}
end
end
def get_page_list_without_current
@pages = []
object = Page.find(params[:id])
Page.where('id <> ?', object.id).walk_tree do |page, level|
@pages << {title: page.lang_attributes(PageLang.default, :title), level: level, id: page.id}
end
end
end
end