Class: Caboose::BlocksController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #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, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_block_infoObject



666
667
668
669
670
671
672
673
674
675
676
# File 'app/controllers/caboose/blocks_controller.rb', line 666

def admin_block_info
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  resp = StdClass.new
  b = Block.find(params[:id])
  bt = b.block_type if b
  resp.block_name = b.name
  resp.bt_name = bt.name
  resp.use_js_for_modal = bt.use_js_for_modal
  resp.field_type = bt.field_type
  render :json => resp
end

#admin_createObject



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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'app/controllers/caboose/blocks_controller.rb', line 335

def admin_create
  return unless user_is_allowed("#{page_or_post}s", 'edit')

  resp = Caboose::StdClass.new

  b = Block.new
  b.status = 'added'    
  if params[:page_id]
    b.page_id = params[:page_id].to_i
  else
    b.post_id = params[:post_id].to_i
  end
  b.parent_id = params[:id] ? params[:id] : nil
  b.block_type_id = params[:block_type_id]
                  
  if !params[:index].nil?      
    b.new_sort_order = params[:index].to_i
    
    i = 1
    b.parent.filtered_children(true).where('new_sort_order >= ?', b.new_sort_order).order('new_sort_order,id').all.each do |b3|
      b3.new_sort_order = b.new_sort_order + i
      b3.status = 'edited' if b3.status == 'published'
      b3.save
      i = i + 1                  
    end
    
  elsif params[:before_id] && !params[:before_id].blank?
    b2 = Block.find(params[:before_id].to_i)
    b.new_sort_order = b2.new_sort_order
    
    i = 1
    b2.parent.filtered_children(true).where('new_sort_order >= ?', b.new_sort_order).order('new_sort_order,id').all.each do |b3|
      b3.new_sort_order = b.new_sort_order + i
      b3.status = 'edited' if b3.status == 'published'
      b3.save
      i = i + 1                  
    end
  
  elsif params[:after_id] && !params[:after_id].blank?
    b2 = Block.find(params[:after_id].to_i)
    b.new_sort_order = b2.new_sort_order + 1

    i = 1
    b2.parent.filtered_children(true).where('new_sort_order >= ?', b.new_sort_order).order('new_sort_order,id').all.each do |b3|
      b3.new_sort_order = b.new_sort_order + i
      b3.status = 'edited' if b3.status == 'published'
      b3.save
      i = i + 1                  
    end
    
  elsif params[:id]
    b.new_sort_order = Block.where(:name => nil).where("(new_parent_id is null and parent_id = ? and status != ?) OR (new_parent_id = ? and status != ?)",params[:id],'deleted',params[:id],'deleted').count        
  end                  
  
  # Save the block
  b.save

  # if b.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end

  if !b.block_type.default.blank?
    b.value = b.block_type.default
    b.save
  end
  
  # Ensure that all the children are created for the block
  b.create_children(status: 'added')

  # Default child block count
  if !params[:child_count].blank? && params[:child_count].to_i > 0
    (1..params[:child_count].to_i).each_with_index do |cc, ind|
      b1 = Block.new
      if params[:page_id]
        b1.page_id = params[:page_id].to_i
      else
        b1.post_id = params[:post_id].to_i
      end
      b1.parent_id = b.id
      b1.sort_order = ind
      b1.new_sort_order = ind
 #     b1.new_parent_id = b.id
      b1.status = 'added'
      b1.block_type_id = b.block_type.default_child_block_type_id
      b1.save
      b1.create_children(status: 'added')
      bw = b1.child('width')
      if bw
        bw.value = 'auto' # (100.0 / params[:child_count].to_f).to_i.to_s + '%'
        bw.save
      end
    end
  end

  # 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.success = true
  resp.new_id = b.id
  resp.parent_id = b.parent_id
  resp.redirect = params[:page_id] ? "/admin/pages/#{b.page_id}/blocks/#{b.id}" : "/admin/posts/#{b.post_id}/blocks/#{b.id}"              
  render :json => resp
end

#admin_deleteObject



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'app/controllers/caboose/blocks_controller.rb', line 573

def admin_delete
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  
  resp = StdClass.new
  b = Block.find(params[:id])
#  sibs = b.siblings
  parent_id = b.new_parent_id.blank? ? b.parent_id : b.new_parent_id
  if b.parent_id
    if params[:page_id]
      resp.redirect = "/admin/pages/#{b.page_id}/blocks/#{b.parent_id}/edit"
    else
      resp.redirect = "/admin/posts/#{b.post_id}/blocks/#{b.parent_id}/edit"
    end        
  else
    resp.close = true
  end

  # if b.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end


  child_ids = b.filtered_children(true).pluck(:id)
  cids = child_ids.blank? ? 0 : child_ids

  
  bid1 = Block.where("id = ? or id in (?)",b.id,cids).where('status = ? or status = ?','edited','published')
  # Caboose.log("marking as deleted: #{bid1.pluck(:id)}")
  bid1.update_all(:status => 'deleted')
  bid2 = Block.where("id = ? or id in (?)",b.id,cids).where(:status => 'added')
  bid2.destroy_all
  # Caboose.log("destroying: #{bid2.pluck(:id)}")

  
  if parent_id
    # Caboose.log("reorganizing blocks under #{parent_id}")
    # i = 0
    # Block.where(:parent_id => parent_id).where('status != ?', 'deleted').order(:new_sort_order).all.each do |b2|
    #   b2.new_sort_order = i
    #   b2.status = 'edited' if b2.status == 'published'
    #   b2.save
    #   i = i + 1
    # end
    sibs = Caboose::Block.where(:name => nil).where("(new_parent_id is null and parent_id = ? and status != ?) OR (new_parent_id = ? and status != ?)",parent_id,'deleted',parent_id,'deleted').order(:new_sort_order, :id).all
    sibs.each_with_index do |b2, i|      
      b2.new_sort_order = i
      b2.status = 'edited' if b2.status == 'published'
      b2.save
    end  

  end

  render :json => resp
end

#admin_duplicateObject



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'app/controllers/caboose/blocks_controller.rb', line 634

def admin_duplicate
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  resp = StdClass.new
  b = Block.find(params[:id])
  # if b.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end
  resp.new_id = b.duplicate_block(@site.id, params[:page_id], params[:post_id], b.block_type_id, (b.new_parent_id.blank? ? b.parent_id : b.new_parent_id))
  resp.success = true
  render :json => resp
end

#admin_editObject



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

def admin_edit
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  @page = Page.find(params[:page_id]) if params[:page_id]
  @post = Post.find(params[:post_id]) if params[:post_id]
  @block = Block.find(params[:id])
  @block.create_children
  @modal = true

  @document_domain = request.host
  @document_domain.gsub('http://', '')
  @document_domain.gsub('https://', '')
        
  full_name = @block.block_type.full_name
  begin
    if full_name != 'image'  
      render "caboose/blocks/admin_edit_#{@block.block_type.full_name}", :layout => 'caboose/modal'
    else
      render :layout => 'caboose/modal'
    end
  rescue ActionView::MissingTemplate => ex        
    begin        
      render "caboose/blocks/admin_edit_#{@block.block_type.field_type}", :layout => 'caboose/modal'
    rescue ActionView::MissingTemplate => ex        
      render :layout => 'caboose/modal'          
    end
  end
end

#admin_edit_advancedObject



314
315
316
317
318
319
320
321
# File 'app/controllers/caboose/blocks_controller.rb', line 314

def admin_edit_advanced
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  @page = Page.find(params[:page_id]) if params[:page_id]
  @post = Post.find(params[:post_id]) if params[:post_id]
  @block = Block.find(params[:id])
  @block.create_children      
  render :layout => 'caboose/modal'      
end

#admin_indexObject



23
24
25
26
27
28
29
# File 'app/controllers/caboose/blocks_controller.rb', line 23

def admin_index
  return if !user_is_allowed("#{page_or_post}s", 'view')
  #h = params[:post_id] ? { :post_id => params[:post_id] } : { :page_id => params[:page_id] }
  #blocks = Block.where(h).reorder(:sort_order)
  #render :json => blocks
  render :json => @p.block      
end

#admin_moveObject



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'app/controllers/caboose/blocks_controller.rb', line 693

def admin_move
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  resp = StdClass.new
  b = Block.find(params[:id])
  if !params[:before_id].blank?
    b2 = Block.find(params[:before_id].to_i)
    b.new_sort_order = (b2.new_sort_order.blank? ? b2.sort_order : b2.new_sort_order)
    i = 1
    sibs = b2.siblings(b.new_sort_order)
#    Caboose.log("updating #{sibs.count} siblings")
    sibs.each do |b3|
      b3.new_sort_order = b.new_sort_order + i
      b3.status = 'edited' if b3.status == 'published'
      b3.save
      i = i + 1                  
    end
  elsif !params[:after_id].blank?
    b2 = Block.find(params[:after_id].to_i)
    b.new_sort_order = (b2.new_sort_order.blank? ? (b2.sort_order + 1) : (b2.new_sort_order + 1))
    i = 1
    sibs = b2.siblings(b.new_sort_order)
 #   Caboose.log("updating #{sibs.count} siblings")
    sibs.each do |b3|
      b3.new_sort_order = b.new_sort_order + i
      b3.status = 'edited' if b3.status == 'published'
      b3.save
      i = i + 1
    end
  elsif params[:parent_id]
    b.new_sort_order = b.siblings.count        
  end
  b.new_parent_id = params[:parent_id]  # if params[:parent_id] != b.parent_id.to_s
  b.status = 'edited' if b.status == 'published'
  # if b.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end
  resp.success = true
  b.save
  render :json => resp
end

#admin_move_downObject



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'app/controllers/caboose/blocks_controller.rb', line 763

def admin_move_down
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  
  resp = StdClass.new
  b = Block.find(params[:id])
  changed = b.move_down
  if !changed
    resp.error = "The block is already at the bottom."
  else
    resp.success = "The block has been moved down successfully."
  end
  # if b.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end

  render :json => resp
end

#admin_move_upObject



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'app/controllers/caboose/blocks_controller.rb', line 740

def admin_move_up
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  
  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.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end
  render :json => resp
end

#admin_newObject



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

def admin_new
  return unless user_is_allowed("#{page_or_post}s", '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
      if params[:page_id]
        b.page_id = params[:page_id].to_i
      else
        b.post_id = params[:post_id].to_i
      end
      b.parent_id = params[:id]
      b.block_type_id = block_type_id
      b.status = 'added'
      b.sort_order = Block.where(:parent_id => params[:id]).count              
      b.save
      b.create_children(status: 'added')
      if params[:page_id]
        redirect_to "/admin/pages/#{b.page_id}/blocks/#{b.id}/edit"
      else
        redirect_to "/admin/posts/#{b.post_id}/blocks/#{b.id}/edit"
      end
      return
    end
  end

  @page = Page.find(params[:page_id]) if params[:page_id]
  if @page
    @page.status = 'edited'
    @page.save
  end
  @post = Post.find(params[:post_id]) if params[:post_id] 
  if @post
    @post.status = 'edited'
    @post.save
  end     
  @block = params[:id] ? Block.find(params[:id]) : (params[:page_id] ? Block.new(:page_id => params[:page_id]) : Block.new(:post_id => params[:post_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_parent_blockObject



680
681
682
683
684
685
686
687
688
689
# File 'app/controllers/caboose/blocks_controller.rb', line 680

def admin_parent_block
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  resp = StdClass.new
  b = Block.find(params[:id])
  par = Block.where("name is null or name like '%column%'").where(:id => (b.new_parent_id.blank? ? b.parent_id : b.new_parent_id)).first
  resp.parent_id = par ? par.id : nil
  gp = par ? Block.where("name is null or name like '%column%'").where(:id => (par.new_parent_id.blank? ? par.parent_id : par.new_parent_id)).first : nil
  resp.grandparent_id = gp ? gp.id : nil
  render :json => resp
end

#admin_remove_mediaObject



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/caboose/blocks_controller.rb', line 175

def admin_remove_media
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  resp = Caboose::StdClass.new   
  b = Block.find(params[:id])      
  b.new_media_id = 0
  b.image_file_name = nil
  b.image_file_size = nil
  b.image_content_type = nil
  b.image_updated_at = nil
  b.status = 'edited' if b.status == 'published'
  resp.success = b.save
  render :json => resp      
end

#admin_renderObject



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

def admin_render
  return unless user_is_allowed("#{page_or_post}s", '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      
  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 => params[:page_id] ? @p : nil,
    :post => params[:post_id] ? @p : nil,            
    :request => request,
    :is_new => (params[:is_new].blank? ? false : true)
  })
  render :inline => html
end

#admin_render_allObject



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

def admin_render_all
  return unless user_is_allowed("#{page_or_post}s", 'edit')            
  blocks = Block.where("#{page_or_post}_id = ? and parent_id is null", @p.id).reorder(:sort_order).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 => params[:page_id] ? @p : nil,
        :post => params[:post_id] ? @p : nil,            
        :request => request
      })        
    }
  end
  render :json => blocks
end

#admin_render_second_levelObject



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

def admin_render_second_level
  return unless user_is_allowed("#{page_or_post}s", 'edit')      
  view = ActionView::Base.new(ActionController::Base.view_paths)      
  blocks = @p.block.filtered_children(true,true).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] ? params[:empy_text] : '[Empty, click to edit]',            
        :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 => params[:page_id] ? @p : nil,
        :post => params[:post_id] ? @p : nil,            
        :request => request,
        :params => params
      })
    }
  end
  render :json => blocks      
end

#admin_showObject



325
326
327
328
329
# File 'app/controllers/caboose/blocks_controller.rb', line 325

def admin_show
  return unless user_is_allowed("#{page_or_post}s", 'edit')      
  block = Block.find(params[:id])
  render :json => block      
end

#admin_treeObject



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

def admin_tree
  return unless user_is_allowed("#{page_or_post}s", 'edit')      
  blocks = []
  if params[:id]
    b = Block.find(params[:id])
    b.create_children(status: 'added') # if Caboose::Block.where(:parent_id => b.id).count == 0
    bt = b.block_type
    blocks << { 
      'id'                 => b.id,
      'parent_id'          => (b.new_parent_id.blank? ? b.parent_id : b.new_parent_id),
      'page_id'            => b.page_id,          
      'post_id'            => b.post_id,          
      'name'               => b.name,
      'value'              => (b.new_value.blank? ? b.value : (b.new_value == 'EMPTY' ? nil : b.new_value)),
      'constrain'          => b.constrain,
      'full_width'         => b.full_width,
      'block_type'         => bt,
      'children'           => admin_tree_helper(b,1),
      'crumbtrail'         => self.crumbtrail(b)          
      #'block_type_id'      => bt.id,           
      #'field_type'         => bt.field_type,
      #'allow_child_blocks' => bt.allow_child_blocks,
      #'use_js_for_modal'   => bt.use_js_for_modal,                              
    }
  else
    q = !params[:page_id].blank? ? ["parent_id is null and page_id = ?", params[:page_id]] : ["parent_id is null and post_id = ?", params[:post_id]] 
    Block.where(q).reorder(:new_sort_order).all.each do |b|
      bt = b.block_type
      blocks << { 
        'id'                 => b.id,
        'parent_id'          => (b.new_parent_id.blank? ? b.parent_id : b.new_parent_id),
        'page_id'            => b.page_id,            
        'post_id'            => b.post_id,                        
        'name'               => b.name, 
        'value'              => (b.new_value.blank? ? b.value : (b.new_value == 'EMPTY' ? nil : b.new_value)),
        'constrain'          => b.constrain,
        'full_width'         => b.full_width,
        'block_type'         => bt,
        'children'           => admin_tree_helper(b,1)            
        #'block_type_id'      => bt.id,
        #'field_type'         => bt.field_type,
        #'allow_child_blocks' => bt.allow_child_blocks,
        #'use_js_for_modal'   => bt.use_js_for_modal,                                    
      }
    end        
  end
  render :json => blocks
end

#admin_tree_helper(b, level) ⇒ Object



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

def admin_tree_helper(b,level)
  arr = []
  if level < 3 && b.block_type
    sort_by_id = b.block_type.default_child_block_type_id.nil? ? true : false
    b.filtered_children(true,sort_by_id).each do |b2|
      bt = b2.block_type
      arr << {
        'id'                 => b2.id,
        'parent_id'          => (b2.new_parent_id.blank? ? b2.parent_id : b2.new_parent_id),
        'page_id'            => b2.page_id,
        'post_id'            => b2.post_id,
        'name'               => b2.name,
        'value'              => (b2.new_value.blank? ? b2.value : (b2.new_value == 'EMPTY' ? nil : b2.new_value)),
        'constrain'          => b2.constrain,
        'full_width'         => b2.full_width,
        'block_type'         => bt,
        'children'           => admin_tree_helper(b2,level+1)          
        #'block_type_id'      => bt.id,          
        #'field_type'         => bt.field_type,
        #'allow_child_blocks' => bt.allow_child_blocks,
        #'use_js_for_modal'   => bt.use_js_for_modal,          
      }
    end
  end
  return arr
end

#admin_updateObject



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'app/controllers/caboose/blocks_controller.rb', line 449

def admin_update
  return unless user_is_allowed("#{page_or_post}s", '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.new_sort_order = Block.where("(parent_id = ? and status = ?) or (new_parent_id = ? and (status = ? or status = ?))",v,'published',v,'edited','added').count
        b.status = 'edited' if b.status == 'published'
      when 'block_type_id' then b.block_type_id = v
      when 'sort_order'    then 
        b.new_sort_order    = v
        b.status = 'edited' if b.status == 'published'
      when 'constrain'     then b.constrain     = v
      when 'full_width'    then b.full_width    = v
      when 'media_id'      then 
        b.new_media_id      = v
        b.status = 'edited' if b.status == 'published'
      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'
              pv = Block.parse_checkbox_multiple_value(b, v)
              b.new_value = (pv.blank? ? 'EMPTY' : pv)
              b.status = 'edited' if b.status == 'published'
            else                    
              b.new_value = (v.blank? ? 'EMPTY' : v)
              b.status = 'edited' if b.status == 'published'
            end
          end
        end
    end
  end

  # if b.page
  #   b.page.status = 'edited'
  #   b.page.save
  # elsif b.post
  #   b.post.status = 'edited'
  #   b.post.save
  # end
            
  resp.success = save && b.save
  b.create_children(status: 'added')
  render :json => resp      
end

#admin_update_fileObject



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'app/controllers/caboose/blocks_controller.rb', line 551

def admin_update_file
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  b = Block.find(params[:id])
  b.file = params[:value]
  
  str = params[:value].original_filename
  arr = str.split('.')
  arr.pop
  str = arr.join('.')      
  b.file_upload_name = b.unique_file_upload_name(str)
  
  b.save
  resp.success = true      
  resp.attributes = { 'value' => { 'value' => b.file.url }}
  
  render :json => resp
end

#admin_update_imageObject



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'app/controllers/caboose/blocks_controller.rb', line 529

def admin_update_image
  return unless user_is_allowed("#{page_or_post}s", 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  b = Block.find(params[:id])
  b.image = params[:value]
  
  str = params[:value].original_filename
  arr = str.split('.')
  arr.pop
  str = arr.join('.')      
  b.image_upload_name = b.unique_image_upload_name(str)      
  
  b.save
  resp.success = true 
  resp.attributes = { 'value' => { 'value' => b.image.url(:tiny) }}
  
  render :json => resp
end

#admin_update_valueObject



514
515
516
517
518
519
520
521
522
523
524
525
# File 'app/controllers/caboose/blocks_controller.rb', line 514

def admin_update_value
  return unless user_is_allowed('pages', 'edit')
  resp = StdClass.new({'attributes' => {}})
  val = params[:value]
  b = Block.find(params[:id])
  if b
    b.new_value = val.blank? ? 'EMPTY' : val.gsub(/<span><span contenteditable="false"([A-z0-9;\-\(\),><#&\/\.\?↵ =":]*)<\/span><\/span>/,'').gsub('<span></span>','').gsub('<span style=""></span>','')
    b.status = 'edited' if b.status == 'published'
    resp.success = b.save
  end
  render :json => resp
end

#before_blocks_actionObject



9
10
11
# File 'app/controllers/caboose/blocks_controller.rb', line 9

def before_blocks_action
  @p = params[:page_id] ? Page.find(params[:page_id]) : Post.find(params[:post_id])
end

#crumbtrail(block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/caboose/blocks_controller.rb', line 132

def crumbtrail(block)
  crumbs = []      
  b = block
  while b
    bt = b.block_type
    crumbs << {
      :block_id => b.id,
      :text => bt.description
    }        
    b = (b.new_parent_id.blank? ? b.parent : Caboose::Block.find(b.new_parent_id))
  end
  return crumbs.reverse
end

#page_or_postObject



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

def page_or_post
  return params[:page_id] ? 'page' : 'post'
end