Class: Caboose::Block

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/block.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cached_ancestor_helper(b, content_id, footer_id) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'app/models/caboose/block.rb', line 78

def self.cached_ancestor_helper(b, content_id, footer_id)
  if b.block_type && b.name.nil? && !b.block_type.is_dynamic && (b.parent_id == content_id || b.parent_id == footer_id)
    return b.id
  elsif b.parent
    return cached_ancestor_helper(b.parent, content_id, footer_id)
  else
    return nil
  end
end

.descendant_helper(block) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'app/models/caboose/block.rb', line 92

def self.descendant_helper(block)
  return true if block.block_type.nil? || block.block_type.is_dynamic
  is_dynamic = false
  block.children.where("name is null OR name ILIKE ?","%_column").each do |b|
    is_dynamic = Caboose::Block.descendant_helper(b)
    return true if is_dynamic == true
  end
  return is_dynamic
end

.parse_checkbox_multiple_value(b, arr) ⇒ Object

Parses the value given for a checkbox multiple block



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'app/models/caboose/block.rb', line 542

def self.parse_checkbox_multiple_value(b, arr)
  current_value = b.new_value.blank? ? (b.value ? b.value.split('|') : []) : (b.new_value ? b.new_value.split('|') : [])
  v = arr[0]
  checked = arr[1].to_i == 1
  if v == 'all'
    if checked && b.block_type && !b.block_type.options.blank?
      # Caboose.log(b.block_type.options)
      return b.block_type.options.split("\n").join('|')
    else
      return ''
    end
  else
    if checked && !current_value.include?(v)
      current_value << v      
    elsif !checked && current_value.include?(v)
      current_value.delete(v)
    end
    return current_value.join('|')
  end
end

.unique_file_upload_name(str) ⇒ Object



651
652
653
654
655
656
657
658
659
660
# File 'app/models/caboose/block.rb', line 651

def self.unique_file_upload_name(str)
  base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')
  str = "#{base}"
  i = 1
  while Caboose::Block.where("file_upload_name = ?", str).exists? && i < 100 do
    str = "#{base}-#{i}"
    i = i + 1
  end
  return str              
end

.unique_image_upload_name(str) ⇒ Object



662
663
664
665
666
667
668
669
670
671
# File 'app/models/caboose/block.rb', line 662

def self.unique_image_upload_name(str)
  base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')
  str = "#{base}"
  i = 1
  while Caboose::Block.where("image_upload_name = ?", str).exists? && i < 100 do
    str = "#{base}-#{i}"
    i = i + 1
  end
  return str              
end

Instance Method Details

#block_message(block, ex) ⇒ Object



367
368
369
370
371
372
373
374
375
# File 'app/models/caboose/block.rb', line 367

def block_message(block, ex)    
  msg = block ? (block.block_type ? "Error with #{block.block_type.name} block (block_type_id #{block.block_type.id}, block_id #{block.id})\n" : "Error with block (block_id #{block.id})\n") : ''
  if ex.is_a?(String)
    Caboose.log("#{msg}#{ex}")
  else
    Caboose.log("#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}")
  end
  return msg
end

#cached_ancestorObject



66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/caboose/block.rb', line 66

def cached_ancestor
  begin
    page_or_post = self.page ? self.page : self.post
    content = page_or_post.block.child('content')
    index_page = Caboose::Page.index_page(page_or_post.site_id)
    footer = index_page.block.child('footer')
    return Caboose::Block.cached_ancestor_helper(self, content.id, footer.id)
  rescue
   return nil
  end
end

#caste_valueObject



51
52
53
54
55
56
57
58
59
# File 'app/models/caboose/block.rb', line 51

def caste_value
  if self.block_type_id.nil?
    bt = Caboose::BlockType.where(:field_type => 'text').first
    if bt.nil?
      bt = Caboose::BlockType.create(:name => 'text', :description => 'Text', :field_type => 'text', :default => '', :width => 800, :height => 400, :fixed_placeholder => false)
    end      
    self.block_type_id = bt.id
  end
end

#child(name) ⇒ Object



197
198
199
# File 'app/models/caboose/block.rb', line 197

def child(name)
  Caboose::Block.where("parent_id = ? and name = ?", self.id, name).first
end

#child_value(name) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/caboose/block.rb', line 142

def child_value(name)
  b = self.child(name)
  return nil if b.nil?
  if b.block_type.field_type == 'image'
    return b.media.image if b.media
    return b.image
  end
  if b.block_type.field_type == 'file'
    return b.media.file if b.media
    return b.file
  end
  return b.value
end

#constrain_allObject



622
623
624
625
626
627
# File 'app/models/caboose/block.rb', line 622

def constrain_all         
  self.children.each do |b|
    return false if b.full_width == true
  end
  return true
end

#create_children(block_type_override: nil, status: 'published') ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/caboose/block.rb', line 212

def create_children(block_type_override: nil, status: 'published')
  bt = block_type_override ? block_type_override : block_type
  bt.children.each do |bt2|
    bt_id = bt2.id      
    #if bt2.parent_id
    #  new_bt_id = Caboose::BlockType.where(:name => bt2.field_type).first.id 
    #end      
    if self.child(bt2.name).nil?
      b = Caboose::Block.create(
        :post_id => self.post_id,
        :page_id => self.page_id,
        :parent_id => self.id, 
        :block_type_id => bt_id,
        :name => bt2.name,
        :value => bt2.default,
        :status => status
      )
      b.create_children(block_type_override: bt2, status: status)
    end
  end
end

#cv(editing, name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/caboose/block.rb', line 156

def cv(editing, name)
  editing = defined?(editing) ? editing : false
  b = self.child(name)
  return nil if b.nil?
  if b.block_type.field_type == 'image' && !editing
    return b.media.image if b.media
    return b.image
  end
  if b.block_type.field_type == 'image' && editing
    mid = b.new_media_id.blank? ? b.media_id : b.new_media_id
    return Caboose::Media.find(mid).image if Caboose::Media.where(:id => mid).exists?
    return (mid != 0 ? b.image : nil)
  end
  if b.block_type.field_type == 'file' && !editing
    return b.media.file if b.media
    return b.file
  end
  if b.block_type.field_type == 'file' && editing
    mid = b.new_media_id.blank? ? b.media_id : b.new_media_id
    return Caboose::Media.find(mid).file if Caboose::Media.where(:id => mid).exists?
    return (mid != 0 ? b.file : nil)
  end
  return (editing && !b.new_value.blank?) ? (b.new_value == 'EMPTY' ? nil : b.new_value) : b.value
end

#duplicate_block(site_id, page_id, post_id, new_block_type_id = nil, new_parent_id = nil) ⇒ Object

Assumes that we start the duplicate process at the top level block



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'app/models/caboose/block.rb', line 737

def duplicate_block(site_id, page_id, post_id, new_block_type_id = nil, new_parent_id = nil)      
  b = Caboose::Block.create(
    :page_id            => page_id,          
    :post_id            => post_id,         
    :parent_id          => new_parent_id,
    :media_id           => self.media_id,
    :new_media_id       => self.new_media_id,
    :block_type_id      => self.block_type_id,    
    :sort_order         => (self.new_sort_order.blank? ? (self.sort_order) : (self.new_sort_order)),
    :new_sort_order     => (self.new_sort_order.blank? ? (self.sort_order) : (self.new_sort_order)),
    :constrain          => self.constrain,
    :full_width         => self.full_width,
    :name               => self.name,
    :value              => self.value,
    :new_value          => self.new_value,
    :status             => 'added'
  )

  if b.name.nil?
 #     Caboose.log("moving block #{b.id} down, parent_id is #{b.parent_id}")
    b.reorganize
  end

  self.filtered_children(true).each do |b2|
    if b2.name 
      # The block is part of the block type, so we have to find the corresponding child block in the new block type        
      bt = Caboose::BlockType.where(:parent_id => self.block_type_id, :name => b2.name).first
      if bt
        b2.duplicate_block(site_id, page_id, post_id, bt.id, b.id)
      else
        # Don't duplicate it because the corresponding child block doesn't exist in the new block type
      end
    else
      # The block is a child block that isn't part of the block type definition
      b2.duplicate_block(site_id, page_id, post_id, b2.block_type_id, b.id)
    end
  end
  return b.id
end

#duplicate_page_block(site_id, page_id, new_block_type_id = nil, new_parent_id = nil) ⇒ Object

Assumes that we start the duplicate process at the top level block



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
# File 'app/models/caboose/block.rb', line 706

def duplicate_page_block(site_id, page_id, new_block_type_id = nil, new_parent_id = nil)        
  b = Caboose::Block.create(
    :page_id            => page_id,          
    :post_id            => nil,         
    :parent_id          => new_parent_id,
    :media_id           => self.media_id,
    :block_type_id      => new_block_type_id,    
    :sort_order         => self.sort_order,
    :constrain          => self.constrain,
    :full_width         => self.full_width,
    :name               => self.name,
    :value              => self.value
  )    
  self.children.each do |b2|
    if b2.name 
      # The block is part of the block type, so we have to find the corresponding child block in the new block type        
      bt = Caboose::BlockType.where(:parent_id => new_block_type_id, :name => b2.name).first
      if bt
        b2.duplicate_page_block(site_id, page_id, bt.id, b.id)
      else
        # Don't duplicate it because the corresponding child block doesn't exist in the new block type
      end
    else
      # The block is a child block that isn't part of the block type definition
      b2.duplicate_page_block(site_id, page_id, b2.block_type_id, b.id)
    end
  end
end

#filtered_children(editing, sort_by_id = false) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'app/models/caboose/block.rb', line 201

def filtered_children(editing, sort_by_id = false)
  blocks = []
  if editing
    sortby = sort_by_id ? 'block_type_id' : 'new_sort_order,id'
    blocks = Caboose::Block.where("status != ?","deleted").where("new_parent_id = ? or (parent_id = ? and new_parent_id is null)", self.id, self.id).order(sortby)
  else
    blocks = Caboose::Block.where(:parent_id => self.id).order('sort_order,id')
  end
  return blocks
end

#full_nameObject



61
62
63
64
# File 'app/models/caboose/block.rb', line 61

def full_name    
  return self.name if parent_id.nil?
  return "#{parent.full_name}_#{self.name}"
end

#get_global_value(site_id) ⇒ Object

Returns the master block for this global block



509
510
511
512
513
514
515
516
517
518
519
# File 'app/models/caboose/block.rb', line 509

def get_global_value(site_id)
  return if !self.block_type.is_global    
  return if !Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).exists?        
  b = Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).reorder("blocks.id").first    
  self.value = b.value
  self.save
  
  self.children.each do |b2|
    b2.get_global_value(site_id) if b2.block_type.is_global
  end    
end

#has_dynamic_descendantObject



88
89
90
# File 'app/models/caboose/block.rb', line 88

def has_dynamic_descendant
  return Caboose::Block.descendant_helper(self)
end

#js_hashObject



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
# File 'app/models/caboose/block.rb', line 458

def js_hash
  kids = self.children.collect { |b| b.js_hash }
  bt = self.block_type    
  return {
    'id'             => self.id,     
    'post_id'        => self.post_id,      
    'page_id'        => self.page_id,      
    'parent_id'      => self.parent_id,
    'parent_title'   => self.parent ? self.parent.title : '',
    'block_type_id'  => self.block_type_id,    
    'sort_order'     => self.sort_order,
    'name'           => self.name,
    'value'          => self.value,
    'children'       => kids,      
    'block_type'     => {      
      'id'                              => bt.id,                            
      'parent_id'                       => bt.parent_id,                     
      'name'                            => bt.name,
      'description'                     => bt.description,
      'render_function'                 => bt.render_function,
      'use_render_function'             => bt.use_render_function,
      'use_render_function_for_layout'  => bt.use_render_function_for_layout,
      'allow_child_blocks'              => bt.allow_child_blocks,
      'field_type'                      => bt.field_type,
      'default'                         => bt.default,
      'width'                           => bt.width,
      'height'                          => bt.height,
      'fixed_placeholder'               => bt.fixed_placeholder,
      'options'                         => bt.options,
      'options_function'                => bt.options_function,
      'options_url'                     => bt.options_url
    },
    'file' => {
      'url' => self.file.url
    },
    'image' => {
      'tiny_url'  => self.image.url(:tiny),
      'thumb_url' => self.image.url(:thumb),
      'large_url' => self.image.url(:large),
    }
  }
end

#log_helper(prefix = '') ⇒ Object



501
502
503
504
505
506
# File 'app/models/caboose/block.rb', line 501

def log_helper(prefix = '')
  puts "#{prefix}#{self.id}"
  self.children.each do |b|
    b.log_helper("#{prefix}-")
  end
end

#migrate_mediaObject



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'app/models/caboose/block.rb', line 673

def migrate_media
  
  if self.block_type.field_type == 'image' && !self.image_file_name.nil? && self.media_id.nil?
    
    site_id = self.page_id ? self.page.site_id : self.post.site_id
    cat = Caboose::MediaCategory.top_category(site_id)      
    m = self.media
    m = Caboose::Media.create(:media_category_id => cat.id, :original_name => self.image_file_name, :name => Caboose::Media.upload_name(self.image_file_name)) if m.nil?      
    m.image = URI.parse(self.image.url(:original))
    m.processed = true
    m.save
    
    self.media_id = m.id
    self.save
  
  elsif self.block_type.field_type == 'file' && !self.file_file_name.nil? && self.media_id.nil?
    
    site_id = self.page_id ? self.page.site_id : self.post.site_id
    cat = Caboose::MediaCategory.top_category(site_id)
    m = self.media      
    m = Caboose::Media.create(:media_category_id => cat.id, :original_name => self.file_file_name, :name => Caboose::Media.upload_name(self.file_file_name)) if m.nil?        
    m.file = URI.parse(self.file.url)
    m.processed = true
    m.save
    
    self.media_id = m.id
    self.save
    
  end
  
end


777
778
779
780
781
782
783
784
# File 'app/models/caboose/block.rb', line 777

def modal_js_block_names
  arr = []
  arr << self.block_type.name if self.block_type.use_js_for_modal
  self.filtered_children(true).each do |b2|
    self.modal_js_controllers_helper(b2, arr)
  end
  return arr
end


786
787
788
789
790
791
792
# File 'app/models/caboose/block.rb', line 786

def modal_js_controllers_helper(b, arr)
  bt = b.block_type
  arr << bt.name if bt.use_js_for_modal
  b.filtered_children(true).each do |b2|
    self.modal_js_controllers_helper(b2, arr)
  end                          
end

#move_downObject

Move a block down



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'app/models/caboose/block.rb', line 591

def move_down
  sibs = self.siblings
  sibs.each_with_index do |b2, i|      
    b2.new_sort_order = i
    b2.status = 'edited' if b2.status == 'published'
    b2.save
  end
  changed = false
  sibs.each_with_index do |b2, i|      
    if i < (sibs.count-1) && b2.id == self.id                
      sibs[i+1].new_sort_order = i
      sibs[i+1].save        
      b2.new_sort_order = i + 1
      b2.status = 'edited' if b2.status == 'published'
      b2.save
      changed = true
    end      
  end
  return changed            
end

#move_upObject

Move a block up



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'app/models/caboose/block.rb', line 569

def move_up    
  sibs = self.siblings
  sibs.each_with_index do |b2, i|      
    b2.new_sort_order = i
    b2.status = 'edited' if b2.status == 'published'
    b2.save
  end
  changed = false
  sibs.each_with_index do |b2, i|      
    if i > 0 && b2.id == self.id                
      sibs[i-1].new_sort_order = i
      sibs[i-1].save        
      b2.new_sort_order = i - 1
      b2.status = 'edited' if b2.status == 'published'
      b2.save
      changed = true
    end      
  end
  return changed            
end

#partial(name, options) ⇒ Object



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
# File 'app/models/caboose/block.rb', line 384

def partial(name, options)
  defaults = {
    :view => nil,
    :controller_view_content => nil,
    :modal => false,
    :empty_text => '',
    :editing => false,
    :css => nil,
    :js => nil,
    :csrf_meta_tags => nil,
    :csrf_meta_tags2 => nil,    
    :logged_in_user => nil,
    :is_cache => false
  }
  options2 = nil        
  if options.is_a?(Hash)
    options2 = defaults.merge(options)
  else
    options2 = {
      :view                    => options.view                    ? options.view                    : nil,
      :controller_view_content => options.controller_view_content ? options.controller_view_content : nil,
      :modal                   => options.modal                   ? options.modal                   : nil,
      :empty_text              => options.empty_text              ? options.empty_text              : nil,
      :editing                 => options.editing                 ? options.editing                 : nil,
      :css                     => options.css                     ? options.css                     : nil,
      :js                      => options.js                      ? options.js                      : nil,
      :csrf_meta_tags          => options.csrf_meta_tags          ? options.csrf_meta_tags          : nil,
      :csrf_meta_tags2         => options.csrf_meta_tags2         ? options.csrf_meta_tags2         : nil,
      :logged_in_user          => options.logged_in_user          ? options.logged_in_user          : nil,
      :is_cache                => options.is_cache                ? options.is_cache                : nil
    }
  end
  options2[:block] = self
  
  view = options2[:view]     
  view = ActionView::Base.new(ActionController::Base.view_paths) if view.nil?        
  site = options[:site]

  begin
    str = view.render(:partial => "../../app/views/caboose/blocks/#{site.name}/#{name}", :locals => options2)
  rescue ActionView::MissingTemplate => ex      
    begin
      str = view.render(:partial => "../../app/views/caboose/blocks/default_theme/#{name}", :locals => options2)
    rescue ActionView::MissingTemplate => ex      
      begin
        str = view.render(:partial => "caboose/blocks/#{name}", :locals => options2)  
      rescue Exception => ex      
        Caboose.log("Partial caboose/blocks/#{name} doesn't exist.")
        str = "<p class='note error'>#{self.partial_message(name, ex)}</p>"
      end
    end
  end
    
  return str
end

#partial_message(name, ex) ⇒ Object



440
441
442
443
444
445
446
447
448
# File 'app/models/caboose/block.rb', line 440

def partial_message(name, ex)    
  msg = "Error with partial #{name}:\n"
  if ex.is_a?(String)
    Caboose.log("#{msg}#{ex}")
  else
    Caboose.log("#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}")
  end
  return msg
end

#render(block, options) ⇒ Object



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
# File 'app/models/caboose/block.rb', line 234

def render(block, options)             
  if block && block.is_a?(String)
    b = self.child(block)        
    if b.nil?
      self.create_children
      b = self.child(block)
      if b.nil?        
        Caboose.log("No block exists with name \"#{block}\".")
        return false
      end
    end
    block = b
  end        
  str = ""

  defaults = {
    :view => nil,
    :controller_view_content => nil,
    :modal => false,
    :empty_text => '',
    :editing => false,
    :css => nil,
    :js => nil,
    :csrf_meta_tags => nil,
    :csrf_meta_tags2 => nil,    
    :logged_in_user => nil,
    :is_cache => false
  }    

  options2 = nil
  if options.is_a?(Hash)
    options2 = defaults.merge(options)
  else
    options2 = {
      :view                    => options.view                    ? options.view                    : nil,
      :controller_view_content => options.controller_view_content ? options.controller_view_content : nil,
      :modal                   => options.modal                   ? options.modal                   : nil,
      :empty_text              => options.empty_text              ? options.empty_text              : nil,
      :editing                 => options.editing                 ? options.editing                 : nil,
      :css                     => options.css                     ? options.css                     : nil,
      :js                      => options.js                      ? options.js                      : nil,
      :csrf_meta_tags          => options.csrf_meta_tags          ? options.csrf_meta_tags          : nil,
      :csrf_meta_tags2         => options.csrf_meta_tags2         ? options.csrf_meta_tags2         : nil,
      :logged_in_user          => options.logged_in_user          ? options.logged_in_user          : nil,
      :is_cache                => options.is_cache                ? options.is_cache                : nil
    }
  end
  options2[:block] = block

  pop = block.page ? block.page : block.post
  return block.cached_value if block && pop && pop.site && pop.site.use_caching && block.use_cache && !block.cached_value.blank? && block.block_type && !block.block_type.is_dynamic && !options2[:is_cache] && (options2[:editing] == false || (pop && pop.is_published))

  view = options2[:view]     
  view = ActionView::Base.new(ActionController::Base.view_paths) if view.nil?

  btsm_rf = Caboose::BlockTypeSiteMembership.where(:site_id => options2[:site].id, :block_type_id => block.block_type.id).where('custom_html IS NOT NULL and custom_html != ?', '').first
  bt_rf = btsm_rf.nil? ? (block.block_type.use_render_function && block.block_type.render_function ? block.block_type.render_function : nil) : btsm_rf
  rf = btsm_rf.nil? ? bt_rf : btsm_rf.custom_html
    
  if !rf.blank?
    options2[:caboose_js] = rf.match(/<% content_for :js do %>(.*?)<% end %>/m)
    options2[:caboose_css] = rf.match(/<% content_for :css do %>(.*?)<% end %>/m)
    rf = rf.gsub(/<% content_for :js do %>(.*?)<% end %>/m, '')
    rf = rf.gsub(/<% content_for :css do %>(.*?)<% end %>/m, '')
    options2[:render_function] = rf
    begin
      str = Timeout::timeout(3) { view.render(:partial => "caboose/blocks/render_function", :locals => options2) }
    rescue Exception => ex
      msg = block ? (block.block_type ? "Error with #{block.block_type.name} block (block_type_id #{block.block_type.id}, block_id #{block.id})\n" : "Error with block (block_id #{block.id})\n") : ''             
      Caboose.log("#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}")
      str = "<p class='note error'>#{msg}</p>"
      if block && block.block_type
        block.block_type.latest_error = "#{msg}#{ex.message}\n#{ex.backtrace.join("\n")}"
        block.block_type.save
      end
    end         
  else
    
    full_name = block.block_type.full_name
    full_name = "lksdjflskfjslkfjlskdfjlkjsdf" if full_name.nil? || full_name.length == 0
    
    # Check the local site first      
    site = options2[:site]
    if site.nil?
      self.block_message(block, "Error: site variable is nil.")
    end
    
    arr = [
      "../../app/views/caboose/blocks/#{site.name}/#{full_name}",
      "../../app/views/caboose/blocks/#{site.name}/#{block.block_type.name}",
      "../../app/views/caboose/blocks/#{site.name}/#{block.block_type.field_type}",
      "../../app/views/caboose/blocks/default_theme/#{full_name}",
      "../../app/views/caboose/blocks/default_theme/#{block.block_type.name}",
      "../../app/views/caboose/blocks/#{full_name}",
      "../../app/views/caboose/blocks/#{block.block_type.name}",
      "../../app/views/caboose/blocks/#{block.block_type.field_type}",
      "caboose/blocks/#{full_name}",                
      "caboose/blocks/#{block.block_type.name}",                
      "caboose/blocks/#{block.block_type.field_type}"                
    ]

    if options2[:editing] == true
      if !block.new_value.blank? && block.new_value != 'EMPTY'
        block.value = block.new_value 
      elsif block.new_value == 'EMPTY'
        block.value = nil
      end
      block.media_id = block.new_media_id if !block.new_media_id.nil?
      if block.status != 'deleted'
        str = self.render_helper(view, options2, block, full_name, arr, 0)
      end
    else
      if block.status != 'added'
        str = self.render_helper(view, options2, block, full_name, arr, 0)
      end
    end

  end    
  return str
end

#render_from_function(options) ⇒ Object



377
378
379
380
381
382
# File 'app/models/caboose/block.rb', line 377

def render_from_function(options)
  self.create_children
  locals = OpenStruct.new(options)
  erb = ERB.new(block_type.render_function)
  return erb.result(locals.instance_eval { binding })
end

#render_helper(view, options, block, full_name, arr, i) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
# File 'app/models/caboose/block.rb', line 355

def render_helper(view, options, block, full_name, arr, i)
  return "<p class='note error'>Could not find block view anywhere.</p>" if i > arr.count
  begin
    str = view.render(:partial => arr[i], :locals => options)
    rescue ActionView::MissingTemplate => ex     
      str = render_helper(view, options, block, full_name, arr, i+1)
    rescue Exception => ex
      str = "<p class='note error'>#{self.block_message(block, ex)}</p>"
  end
  return str      
end

#rendered_child_value(name, options) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/caboose/block.rb', line 181

def rendered_child_value(name, options)
  b = self.child(name)
  return nil if b.nil?
  if b.block_type.field_type == 'image'
    return b.media.image if b.media
    return b.image
  end
  if b.block_type.field_type == 'file'
    return b.media.file if b.media
    return b.file
  end
  return "" if b.value.nil? || b.value.strip.length == 0
  view = options && options[:view] ? options[:view] : ActionView::Base.new(ActionController::Base.view_paths)    
  return view.render(:inline => b.value, :locals => options)                    
end

#reorganizeObject

fix sort orders



613
614
615
616
617
618
619
620
# File 'app/models/caboose/block.rb', line 613

def reorganize
  sibs = self.siblings
  sibs.each_with_index do |b2, i|      
    b2.new_sort_order = i
    b2.status = 'edited' if b2.status == 'published'
    b2.save
  end     
end

#siblings(min_sort_order = 0) ⇒ Object

block siblings (in editing mode)



564
565
566
# File 'app/models/caboose/block.rb', line 564

def siblings(min_sort_order=0)
  Caboose::Block.where(:name => nil).where('new_sort_order >= ?',min_sort_order).where("(new_parent_id is null and parent_id = ? and status != ?) OR (new_parent_id = ? and status != ?)",(self.new_parent_id.blank? ? self.parent_id : self.new_parent_id),'deleted',(self.new_parent_id.blank? ? self.parent_id : self.new_parent_id),'deleted').order(:new_sort_order, :id).all
end

#titleObject



450
451
452
453
454
455
456
# File 'app/models/caboose/block.rb', line 450

def title    
  str = "#{self.block_type.name}"
  if self.name && self.name.strip.length > 0
    str << " (#{self.name})"
  end
  return str
end

#unique_file_upload_name(str) ⇒ Object



629
630
631
632
633
634
635
636
637
638
# File 'app/models/caboose/block.rb', line 629

def unique_file_upload_name(str)
  base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')
  str = "#{base}"
  i = 1
  while Caboose::Block.where("id <> ? and file_upload_name = ?", self.id, str).exists? && i < 100 do
    str = "#{base}-#{i}"
    i = i + 1
  end
  return str              
end

#unique_image_upload_name(str) ⇒ Object



640
641
642
643
644
645
646
647
648
649
# File 'app/models/caboose/block.rb', line 640

def unique_image_upload_name(str)
  base = str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')
  str = "#{base}"
  i = 1
  while Caboose::Block.where("id <> ? and image_upload_name = ?", self.id, str).exists? && i < 100 do
    str = "#{base}-#{i}"
    i = i + 1
  end
  return str              
end

#update_cacheObject



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
# File 'app/models/caboose/block.rb', line 102

def update_cache
  par = self.parent
  hdd = self.has_dynamic_descendant
  if par && self.block_type && self.name.blank? && !self.block_type.is_dynamic && !hdd
    opts = {
      :view => nil,
      :controller_view_content => nil,
      :modal => false,
      :empty_text => '',
      :editing => false,
      :css => nil,
      :js => nil,
      :csrf_meta_tags => nil,
      :csrf_meta_tags2 => nil,    
      :logged_in_user => nil,
      :page => self.page,
      :post => self.post,
      :site => self.page ? self.page.site : self.post.site,
      :is_cache => true,
      :protocol => "https://"
    }
    str = par.render(self, opts)
    host = Rails.env.production? ? "https://#{ActionController::Base.asset_host}" : ""
    self.cached_value = str.blank? ? '' : str.gsub('"/assets/',"\"#{host}/assets/")
    self.use_cache = true
  elsif self.block_type && !self.block_type.is_dynamic && self.children && self.children.count > 0 && hdd # Try caching the children
    self.cached_value = nil
    self.cached_css = nil
    self.cached_js = nil
    self.children.where(:name => nil).each do |child|
      child.delay(:queue => 'caching', :priority => 6).update_cache
    end
  elsif hdd
    self.cached_value = nil
    self.cached_css = nil
    self.cached_js = nil
  end
  self.save
end

#update_global_value(value, site_id) ⇒ Object

Updates all the global blocks for the given site



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'app/models/caboose/block.rb', line 522

def update_global_value(value, site_id)
  return if !self.block_type.is_global    
  return if !Caboose::Block.includes(:page).where("blocks.id <> ? and block_type_id = ? and pages.site_id = ?", self.id, self.block_type_id, site_id).exists?        
  
  sql = nil
  if self.page_id
    sql = ["update blocks set value = ? where id in (
        select B.id from blocks B left join pages P on B.page_id = P.id
        where B.id <> ? and B.block_type_id = ? and P.site_id = ?
      )", value, self.id, self.block_type_id, site_id]
  elsif self.post_id      
    sql = ["update blocks set value = ? where id in (
      select B.id from blocks B left join posts P on B.post_id = P.id
      where B.id <> ? and B.block_type_id = ? and P.site_id = ?
    )", value, self.id, self.block_type_id, site_id]
  end
  ActiveRecord::Base.connection.execute(ActiveRecord::Base.send(:sanitize_sql_array, sql))            
end