Class: Caboose::BlocksController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/blocks_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_json, #admin_json_single, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_createObject

POST /admin/pages/:page_id/blocks POST /admin/pages/:page_id/blocks/:id



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
# File 'app/controllers/caboose/blocks_controller.rb', line 186

def admin_create
  return unless user_is_allowed('pages', 'add')

  resp = Caboose::StdClass.new({
      'error' => nil,
      'redirect' => nil
  })

  b = Block.new
  b.page_id = params[:page_id].to_i
  b.parent_id = params[:id] ? params[:id] : nil
  b.block_type_id = params[:block_type_id]
                  
  if !params[:index].nil?      
    b.sort_order = params[:index].to_i
    
    i = 1
    b.parent.children.where('sort_order >= ?', b.sort_order).reorder(:sort_order).all.each do |b3|
      b3.sort_order = b.sort_order + i
      b3.save
      i = i + 1                  
    end
    
  elsif params[:before_id]
    b2 = Block.find(params[:before_id].to_i)
    b.sort_order = b2.sort_order
    
    i = 1
    b2.parent.children.where('sort_order >= ?', b.sort_order).reorder(:sort_order).all.each do |b3|
      b3.sort_order = b.sort_order + i
      b3.save
      i = i + 1                  
    end
  
  elsif params[:after_id]
    b2 = Block.find(params[:after_id].to_i)
    b.sort_order = b2.sort_order + 1
    
    i = 1
    b2.parent.children.where('sort_order >= ?', b.sort_order).reorder(:sort_order).all.each do |b3|
      b3.sort_order = b.sort_order + i
      b3.save
      i = i + 1                  
    end
    
  elsif params[:id]
    b.sort_order = Block.where(:parent_id => params[:id]).count        
  end                  
  
  # Save the block
  b.save
  
  # Ensure that all the children are created for the block
  b.create_children
  
  # Set the global values if necessary
  if b.block_type.is_global        
    b.get_global_value(@site.id)
  end

  # Send back the response
  #resp.block = b
  resp.redirect = "/admin/pages/#{b.page_id}/blocks/#{b.id}/edit"
  render :json => resp
end

#admin_deleteObject

DELETE /admin/pages/:page_id/blocks/:id



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/controllers/caboose/blocks_controller.rb', line 337

def admin_delete
  return unless user_is_allowed('pages', 'delete')
  
  resp = StdClass.new
  b = Block.find(params[:id])
  parent_id = b.parent_id
  if b.parent_id
    resp.redirect = "/admin/pages/#{b.page_id}/blocks/#{b.parent_id}/edit"
  else
    resp.close = true
  end
  b.destroy
  
  if parent_id
    i = 0
    Block.where(:parent_id => parent_id).reorder(:sort_order).all.each do |b2|
      b2.sort_order = i
      b2.save
      i = i + 1
    end
  end

  render :json => resp
end

#admin_editObject

GET /admin/pages/:page_id/blocks/:id/edit



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/caboose/blocks_controller.rb', line 157

def admin_edit
  return unless user_is_allowed('pages', 'edit')
  @page = Page.find(params[:page_id])
  @block = Block.find(params[:id])
  @block.create_children
  @modal = true

  @document_domain = request.host
  @document_domain.gsub('http://', '')
  @document_domain.gsub('https://', '')

  begin        
    render "caboose/blocks/admin_edit_#{@block.block_type.full_name}", :layout => 'caboose/modal'
  rescue
    render :layout => 'caboose/modal'
  end
end

#admin_edit_advancedObject

GET /admin/pages/:page_id/blocks/:id/advanced



176
177
178
179
180
181
182
# File 'app/controllers/caboose/blocks_controller.rb', line 176

def admin_edit_advanced
  return unless user_is_allowed('pages', 'edit')
  @page = Page.find(params[:page_id])
  @block = Block.find(params[:id])
  @block.create_children      
  render :layout => 'caboose/modal'      
end

#admin_indexObject

GET /admin/pages/:page_id/blocks



13
14
15
16
17
# File 'app/controllers/caboose/blocks_controller.rb', line 13

def admin_index
  return if !user_is_allowed('pages', 'view')
  blocks = Block.where(:page_id => params[:page_id]).reorder(:sort_order)
  render :json => blocks      
end

#admin_move_downObject

PUT /admin/pages/:page_id/blocks/:id/move-down



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'app/controllers/caboose/blocks_controller.rb', line 401

def admin_move_down
  return unless user_is_allowed('pages', 'delete')
  
  resp = StdClass.new
  b = Block.find(params[:id])
  b2 = Block.where("parent_id = ? and sort_order = ?", b.parent_id, b.sort_order + 1).first
  if b2.nil?
    resp.error = "The block is already at the bottom."
  else        
    b2.sort_order = b.sort_order
    b2.save
    b.sort_order = b.sort_order + 1
    b.save
    resp.success = "The block has been moved down successfully."
  end

  render :json => resp
end

#admin_move_upObject

PUT /admin/pages/:page_id/blocks/:id/move-up



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'app/controllers/caboose/blocks_controller.rb', line 363

def admin_move_up
  return unless user_is_allowed('pages', 'delete')
  
  resp = StdClass.new
  b = Block.find(params[:id])
  changed = b.move_up
  if !changed
    resp.error = "The block is already at the top."
  else
    resp.success = "The block has been moved up successfully."
  end
    
  #if b.sort_order == 0
  #  resp.error = "The block is already at the top."
  #else
  #  b2 = nil
  #  
  #  new_sort_order = b.sort_order - 1
  #  while new_sort_order > 0 do
  #    b2 = Block.where("parent_id = ? and sort_order = ?", b.parent_id, new_sort_order).first
  #    break if b2
  #    new_sort_order = new_sort_order - 1
  #  end
  #  if b2
  #    b2.sort_order = new_sort_order + 1
  #    b2.save
  #  else
  #    new_sort_order = 1
  #  end
  #  b.sort_order = new_sort_order
  #  b.save
  #  resp.success = "The block has been moved up successfully."
  #end

  render :json => resp
end

#admin_newObject

GET /admin/pages/:page_id/blocks/:id/new GET /admin/pages/:page_id/blocks/new



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
# File 'app/controllers/caboose/blocks_controller.rb', line 21

def admin_new
  return unless user_is_allowed('pages', 'add')
              
  if params[:id]
    block_type_id = params[:block_type_id]
    block_type_id = Block.find(params[:id]).block_type.default_child_block_type_id if block_type_id.nil?                 
    if block_type_id                                  
      b = Block.new
      b.page_id = params[:page_id].to_i
      b.parent_id = params[:id]
      b.block_type_id = block_type_id
      b.sort_order = Block.where(:parent_id => params[:id]).count              
      b.save
      b.create_children        
      redirect_to "/admin/pages/#{b.page_id}/blocks/#{b.id}/edit"
      return
    end
  end

  @page = Page.find(params[:page_id])
  @block = params[:id] ? Block.find(params[:id]) : Block.new(:page_id => params[:page_id])
  @after_id = params[:after_id] ? params[:after_id] : nil
  @before_id = params[:before_id] ? params[:before_id] : nil
  render :layout => 'caboose/modal'                
end

#admin_renderObject

GET /admin/pages/:page_id/blocks/:id/render



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/caboose/blocks_controller.rb', line 80

def admin_render
  return unless user_is_allowed('pages', 'edit')      
  b = Block.find(params[:id])      
  bt = b.block_type
  if bt.nil?
    bt = BlockType.where(:name => 'richtext').first 
    b.block_type_id = bt.id
    b.save
  end
  #b.create_children
  html = b.render(b, {
    :empty_text => params[:empty_text],
    :editing => true
  })
  render :json => html            
end

#admin_render_allObject

GET /admin/pages/:page_id/blocks/render



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/caboose/blocks_controller.rb', line 98

def admin_render_all
  return unless user_is_allowed('pages', 'edit')
  p = Page.find(params[:page_id])
  blocks = Block.where("page_id = ? and parent_id is null", p.id).reorder(:sort_order).collect do |b|
    #bt = b.block_type
    #if bt.nil?
    #  bt = BlockType.where(:name => 'richtext').first 
    #  b.block_type_id = bt.id
    #  b.save
    #end
    #b.create_children
    {
      :id => b.id,
      :block_type_id => b.block_type.id,
      :sort_order => b.sort_order,
      :html => b.render(b, {
        :empty_text => params[:empty_text],
        :editing => true
      })        
    }
  end
  render :json => blocks
end

#admin_render_second_levelObject

GET /admin/pages/:page_id/blocks/render-second-level



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
# File 'app/controllers/caboose/blocks_controller.rb', line 123

def admin_render_second_level
  return unless user_is_allowed('pages', 'edit')
  p = Page.find(params[:page_id])
  @p = p
  blocks = p.block.children.collect do |b|
    {           
      :id => b.id,
      :block_type_id => b.block_type.id,
      :sort_order => b.sort_order,
      :html => b.render(b, {
        :view => nil,
        :controller_view_content => nil,
        :modal => false,
        :editing => true,
        :empty_text => params[:empty_text],            
        :css => '|CABOOSE_CSS|',                     
        :js => '|CABOOSE_JAVASCRIPT|',
        :csrf_meta_tags => '|CABOOSE_CSRF|',
        :csrf_meta_tags2 => '|CABOOSE_CSRF|',    
        :logged_in_user => @logged_in_user,
        :site => @site,
        :page => p
      })
    }
  end
  render :json => blocks
  #render :layout => false
  
  #respond_to do |format|        
  #  format.json
  #end
end

#admin_showObject

GET /admin/pages/:page_id/blocks/:id



48
49
50
51
52
# File 'app/controllers/caboose/blocks_controller.rb', line 48

def admin_show
  return unless user_is_allowed('pages', 'edit')      
  block = Block.find(params[:id])
  render :json => block      
end

#admin_treeObject

GET /admin/pages/:page_id/blocks/tree GET /admin/pages/:page_id/blocks/:id/tree



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/caboose/blocks_controller.rb', line 56

def admin_tree
  return unless user_is_allowed('pages', 'edit')      
  
  blocks = []
  if params[:id]
    b = Block.find(params[:id])
    blocks << { 'id' => b.id, 'children' => admin_tree_helper(b), 'field_type' => b.block_type.field_type }
  else
    Block.where("parent_id is null and page_id = ?", params[:page_id]).reorder(:sort_order).all.each do |b|      
      blocks << { 'id' => b.id, 'allow_child_blocks' => b.block_type.allow_child_blocks, 'children' => admin_tree_helper(b), 'field_type' => b.block_type.field_type }
    end
  end
  render :json => blocks
end

#admin_tree_helper(b) ⇒ Object



71
72
73
74
75
76
77
# File 'app/controllers/caboose/blocks_controller.rb', line 71

def admin_tree_helper(b)
  arr = []
  b.children.each do |b2|
    arr << { 'id' => b2.id, 'allow_child_blocks' => b2.block_type.allow_child_blocks, 'children' => admin_tree_helper(b2), 'field_type' => b2.block_type.field_type }
  end
  return arr
end

#admin_updateObject

PUT /admin/pages/:page_id/blocks/:id



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
# File 'app/controllers/caboose/blocks_controller.rb', line 253

def admin_update
  return unless user_is_allowed('pages', 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  b = Block.find(params[:id])

  save = true
  params.each do |k,v|
    case k
      #when 'page_id'       then b.page_id       = v
      when 'parent_id'     then 
        b.parent_id  = v
        b.sort_order = Block.where(:parent_id => v).count
      when 'block_type_id' then b.block_type_id = v
      when 'sort_order'    then b.sort_order    = v
      when 'constrain'     then b.constrain     = v
      when 'name'          then b.name          = v
      when 'value'         then
        
        if b.block_type.is_global
          if b.block_type.field_type == 'checkbox_multiple'
            b.value = Block.parse_checkbox_multiple_value(b, v)
          else                    
            b.value = v
          end                
          b.update_global_value(b.value, @site.id)              
        else              
          if Caboose::parse_richtext_blocks == true && b.block_type.field_type == 'richtext' && (b.name.nil? || b.name.strip.length == 0) && (b.block_type.name != 'richtext2')                
            b = RichTextBlockParser.parse(b, v, request.host_with_port)
          else
            if b.block_type.field_type == 'checkbox_multiple'
              b.value = Block.parse_checkbox_multiple_value(b, v)
            else                    
              b.value = v
            end
          end
        end
    end
  end
  
  # Trigger the page cache to be updated
  pc = PageCache.where(:page_id => b.page_id).first
  if pc
    pc.refresh = true
    pc.save
    PageCacher.delay.refresh
  else
    PageCacher.delay.cache(b.page_id)
  end
            
  resp.success = save && b.save
  b.create_children
  render :json => resp      
end

#admin_update_fileObject

POST /admin/pages/:page_id/blocks/:id/file



323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/controllers/caboose/blocks_controller.rb', line 323

def admin_update_file
  return unless user_is_allowed('pages', 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  b = Block.find(params[:id])
  b.file = params[:value]
  b.save
  resp.success = true      
  resp.attributes = { 'value' => { 'value' => b.file.url }}
  
  render :json => resp
end

#admin_update_imageObject

POST /admin/pages/:page_id/blocks/:id/image



309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/controllers/caboose/blocks_controller.rb', line 309

def admin_update_image
  return unless user_is_allowed('pages', 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  b = Block.find(params[:id])
  b.image = params[:value]
  b.save
  resp.success = true 
  resp.attributes = { 'value' => { 'value' => b.image.url(:tiny) }}
  
  render :json => resp
end