pages_controller.rb
10.7 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
module Kanjai
class Admin::PagesController < AdminController
def new
@page = Page.new
@domain = Domain.find(params[:domain_id])
@page.domain = @domain
get_page_list(@domain)
end
def create
@page = Page.new(permitted_params[:page])
get_page_list(@page.domain)
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(@page.domain),
title: @page.title
)
page_data.save(:validate => false)
redirect_to edit_admin_page_url(@page, lang: PageLang.default(@page.domain) )
else
render :action => :new
end
end
def edit
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
get_page_list_without_current(@page.domain)
@step = 'general'
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])
@page_data = @page.page_data.find_by_lang(params[:lang])
@step = 'general'
get_page_list_without_current(@page.domain)
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 => :edit, lang: params[:lang]
else
render :action => :edit, lang: params[:lang]
end
end
def destroy
@page = Page.find(params[:id])
@page.destroy
render :json => {:status => 'ok'}
end
def clone
@page = Page.find(params[:id])
@page.clone
redirect_to action: :index, domain_id: @page.domain.id
end
def show_editor
prepare_edit_content
render :layout => false
end
def duplicate_block
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
if(params[:structure_ids].present?)
new_ids = []
params[:structure_ids].split(',').each do |structure_id|
next_structure_id = @page_data.page_contents.pluck(:structure_id).max.to_i + 1
@obj = @page_data.page_contents.find_by_structure_id(structure_id)
if @obj
@new_obj = @obj.dup
@new_obj.structure_id = next_structure_id
@new_obj.save(validate: false)
@obj.page_content_markers.each do |item|
new_item = item.dup
new_item.page_content = @new_obj
new_item.save(validate: false)
end
new_ids << next_structure_id
else
new_ids << ''
end
end
render json: {status: 'ok', ids: new_ids}
else
@structure_id = params[:structure_id]
if @structure_id.to_i > 0
@obj = @page_data.page_contents.find_by_structure_id(@structure_id)
if @obj
Page.transaction do
next_structure_id = @page_data.page_contents.pluck(:structure_id).max.to_i + 1
@new_obj = @obj.dup
@new_obj.structure_id = next_structure_id
@new_obj.save(validate: false)
@obj.page_content_markers.each do |item|
new_item = item.dup
new_item.page_content = @new_obj
new_item.save(validate: false)
end
render json: {status: 'ok', id: next_structure_id}
end
else
render json: {status: 'ok', id: ''}
end
else
render json: {status: 'ok', id: ''}
end
end
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
params[:page_content] ||= {}
params[:page_content][:conditions] ||= []
@obj.update(permitted_params[:page_content])
if params[:frontend_part].to_i == 1
render :json => {:status => 'ok', :structure_id => @structure_id, frontentd: 1, :html => @obj.get_content_frontend}
else
render :json => {:status => 'ok', :structure_id => @structure_id, :html => @obj.get_content}
end
end
def gallery
@page = Page.find(params[:id])
@s3_direct_post = S3_BUCKET.presigned_post(key: "#{@page.get_image_file_path}${filename}", success_action_status: '201', acl: 'public-read')
end
def meta_update
@page = Page.find(params[:id])
@page_data = @page.page_data.find_by_lang(params[:lang])
@step = 'meta'
if @page_data.update(permitted_params[:page_datum])
redirect_to edit_admin_page_url(@page, lang: params[:lang]) + '#meta'
else
render :action => :edit, lang: params[:lang]
end
end
def sort
sort_order(params[:sort], nil)
render json: {status: 'ok'}
end
def activate
@page = Page.find(params[:id])
@page.status = 'active'
@page.save(validate: false)
redirect_to :action => :index
end
def deactivate
@page = Page.find(params[:id])
@page.status = 'block'
@page.save(validate: false)
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 body: nil
end
def show_frontend_editor
prepare_edit_content
@frontend = true
render layout: 'kanjai/frontend_content'
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, :domain_id, :page_template_id, :title, :private_flag, :default_private_page, :root_page, :show_public_only,
: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, :form_subject, :form_body, :from_mail, :to_mail, :form_answer_text, :_destroy],
conditions: []
],
:page_datum => [:id, :title, :url, :meta_title, :meta_description, :meta_keywords]
)
end
def get_page_list(domain)
@pages = []
domain.pages.walk_tree do |page, level|
@pages << {title: page.lang_attributes(PageLang.default(domain), :title), level: level, id: page.id}
end
end
def get_page_list_without_current(domain)
@pages = []
object = Page.find(params[:id])
domain.pages.where('id <> ?', object.id).walk_tree do |page, level|
@pages << {title: page.lang_attributes(PageLang.default(domain), :title), level: level, id: page.id}
end
end
def prepare_edit_content
@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 = []
@main_field = []
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] }
page_template.template_parts.where("part_type in ('content')").each do |item|
item.field_options.each do |el|
@main_field << el[:name] if el[:main].to_i == 1
end
end
end
@type_contents.sort!{|x,y| x[0] <=> y[0]}
@type_contents.unshift([I18n.t('admin.content_types.plugin'), 'plugin'])
@type_contents.unshift([I18n.t('admin.content_types.rte_editor'), 'rte_editor'])
@type_content = @obj.type_content
@obj.build_markers(@obj.type_content)
end
def sort_order(collection, parent)
collection.each do |order, data|
p order
page = Kanjai::Page.where(id: data[:id]).first
if page
page.update_columns(position: order.to_i + 1, parent_id: parent)
if data[:children]
sort_order(data[:children], page.id)
end
end
end
end
end
end