Class: Caboose::Page

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

Constant Summary collapse

CONTENT_FORMAT_HTML =
1
CONTENT_FORMAT_TEXT =
2
CONTENT_FORMAT_RUBY =
3

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.crumb_trail(page) ⇒ Object


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'app/models/caboose/page.rb', line 269

def self.crumb_trail(page)    
  page_id = page.nil? || !page ? 1 : (page.is_a?(Integer) ? page : page.id)
  
  arr = []
  self.crumb_trail_helper(page_id, arr)
  arr.reverse!
  
  trail = arr.collect do |row|
    Caboose::StdClass.new({
      'href' => !row.uri.nil? && row.uri.length > 0 ? row.uri : '/',
      'text' => !row.menu_title.nil? && row.menu_title.length > 0 ? row.menu_title : row.title
    })
  end
  return trail
end

.crumb_trail_helper(page_id, arr) ⇒ Object


285
286
287
288
289
290
291
# File 'app/models/caboose/page.rb', line 285

def self.crumb_trail_helper(page_id, arr)
  return if page_id.nil? || page_id <= 0
  p = self.find_with_fields(page_id, [:parent_id, :title, :menu_title, :uri])   
  return if p.nil?
  arr << p
  self.crumb_trail_helper(p.parent_id, arr)
end

.find_with_fields(page_id, fields) ⇒ Object

def content

return "" if self.blocks.nil? || self.blocks.count == 0
self.blocks.collect { |b| b.render }.join("\n")

end


65
66
67
# File 'app/models/caboose/page.rb', line 65

def self.find_with_fields(page_id, fields)
  return self.where(:id => page_id).select(fields).first
end

.has_children(page_id) ⇒ Object


380
381
382
383
# File 'app/models/caboose/page.rb', line 380

def self.has_children(page_id)
  count = self.where(:parent_id => page_id).count
  return count > 0
end

.index_page(site_id) ⇒ Object


69
70
71
# File 'app/models/caboose/page.rb', line 69

def self.index_page(site_id)
  return self.where(:site_id => site_id, :parent_id => -1).first
end

.is_allowed(user, page_id, action) ⇒ Object


210
211
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/page.rb', line 210

def self.is_allowed(user, page_id, action)
  user = User.logged_out_user if user.nil?

  # Allow a user id to be sent instead of a user object
  user = User.find(user) if user.is_a?(Integer)
  user.role_ids = [Role.logged_out_role_id(user.site_id)] if user.role_ids.nil?

  t = PagePermission.table    
  reqs = nil
  user.role_ids.each do |role_id|
    if (reqs.nil?)
      reqs = t[:role_id].eq(role_id)
    else
      reqs.or(t[:role_id].eq(role_id))
    end
  end 
  var params = { :page_id => page_id, :action => action }
  params << reqs if !reqs.nil?
  count = PagePermission.where(params).count
  
  return true if count > 0
  return false
end

.is_child(parent_id, child_id) ⇒ Object


385
386
387
388
389
390
# File 'app/models/caboose/page.rb', line 385

def self.is_child(parent_id, child_id)
  pid = self.where(:id => child_id).limit(1).pluck(:parent_id)[0]
  return false if pid.nil? || pid <= 0
  return true if pid == parent_id
  return self.is_child(parent_id, pid)
end

.page_ids_with_permission(user, action) ⇒ Object


255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'app/models/caboose/page.rb', line 255

def self.page_ids_with_permission(user, action)
  if (user.is_a?(Integer))
    user = Caboose::User.find(user)
  end
  ids = []
  user.roles.each do |role|     
    ids + Caboose::PagePermission.where({
        :role_id => role.id,
        :action => action
      }).pluck(:page_id)
  end
  return ids.uniq
end

.page_with_uri(host_with_port, uri, get_closest_parent = true) ⇒ Object


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

def self.page_with_uri(host_with_port, uri, get_closest_parent = true)

  d = Caboose::Domain.where(:domain => host_with_port).first
  return false if d.nil? || d.under_construction == true
  site_id = d.site_id    
  
  uri = uri.to_s.gsub(/^(.*?)\?.*?$/, '\1')
  uri.chop! if uri.end_with?('/')
  uri[0] = '' if uri.starts_with?('/')
    
  return self.index_page(site_id) if uri.length == 0

  page = false
  parts = uri.split('/')
    
  # See where to start looking
  pagealias = parts[0].blank? ? '' : parts[0].downcase
  page_ids = self.where(:site_id => site_id, :alias => pagealias).limit(1).pluck(:id)
  page_id = !page_ids.nil? && page_ids.count > 0 ? page_ids[0] : false
  
  # Search for the page
  if page_id      
    page_id = self.page_with_uri_helper(parts, 1, page_id)
  else      
    parent_id = self.index_page(site_id).id
    page_id = self.page_with_uri_helper(parts, 0, parent_id)
  end
  
  return false if page_id.nil?
     
  page = self.find(page_id)
  
  if (!get_closest_parent) # // Look for an exact match
    return false if (page.uri.blank? ? '' : page.uri.downcase) != (uri.blank? ? '' : uri.downcase)
  end
  return page   
end

.page_with_uri_helper(parts, level, parent_id) ⇒ Object


155
156
157
158
159
160
161
# File 'app/models/caboose/page.rb', line 155

def self.page_with_uri_helper(parts, level, parent_id)
  return parent_id if level >= parts.count
  slug = parts[level].downcase
  page_ids = self.where(:parent_id => parent_id, :slug => slug).limit(1).pluck(:id)
  return parent_id if page_ids.nil? || page_ids.count == 0    
  return self.page_with_uri_helper(parts, level+1, page_ids[0])       
end

.pages_with_tag(parent_id, tag) ⇒ Object


417
418
419
# File 'app/models/caboose/page.rb', line 417

def self.pages_with_tag(parent_id, tag)
  self.includes(:page_tags).where(:hide => false, :parent_id => 1, :page_tags => { :tag => tag }).reorder('sort_order, title').all
end

.permissible_actions(user, page_id) ⇒ Object


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'app/models/caboose/page.rb', line 238

def self.permissible_actions(user, page_id)
  if (user.is_a?(Integer))
    user = Caboose::User.find(user)
  end
  actions = []
  user.roles.each do |role|
    acts = Caboose::PagePermission.where({
        :role_id => role.id, 
        :page_id => page_id
      }).pluq(:action)
    acts.each do |ac|
      actions << ac
    end
  end
  return actions
end

.roles_with_permission(page_id, action) ⇒ Object


234
235
236
# File 'app/models/caboose/page.rb', line 234

def self.roles_with_permission(page_id, action)
  return Role.roles_with_page_permission(page_id, action)
end

.slug(str) ⇒ Object


376
377
378
# File 'app/models/caboose/page.rb', line 376

def self.slug(str)
  return str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')
end

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

def self.subnav(page, use_redirect_urls = true, user = false)
  
  # Be nice and allow page ids to be sent
  if (page.is_a?(Integer)) 
    page = self.find_with_fields(page, [:title, :menu_title, :custom_sort_children])
  end
  
  block = Caboose::MenuBlock.new
  block.title = !page.menu_title.nil? && page.menu_title.length > 0 ? page.menu_title : page.title
  block.title_id = page.id
                 
  pages = self.select([:id, :title, :menu_title, :alias, :slug, :uri, :redirect_url, :sort_order]).where(:parent_id => page.id, :hide => false).reorder(:sort_order).all
  if (page.custom_sort_children)
    pages.sort! {|x,y| x.sort_order <=> y.sort_order }
  else
    pages.sort! {|x,y| x.order_title <=> y.order_title }
  end
    
  if (pages.nil? || pages.count == 0) # No children, go up a level  
    parent = self.find_with_fields(page.parent_id, [:title, :menu_title, :custom_sort_children])
    return block if parent.nil? # If we happen to be at the top page
    
    block.title = !parent.menu_title.nil? && parent.menu_title.length > 0 ? parent.menu_title : parent.title
    block.title_id = parent.id
    
    pages = self.select([
        :id, :title, :menu_title, :alias, :slug, :uri, :redirect_url, :sort_order
      ]).where(:parent_id => page.parent_id, :hide => false)
    if (parent.custom_sort_children)
      pages.sort! {|x,y| x.sort_order <=> y.sort_order }
    else
      pages.sort! {|x,y| x.order_title <=> y.order_title }    
    end
  end
  
  block.links = []
  pages.each do |p|
    link = Caboose::StdClass.new({
      'href' => !p.redirect_url.nil? && p.redirect_url.length > 0 ? p.redirect_url : p.uri,
      'text' => !p.menu_title.nil? && p.menu_title.length > 0 ? p.menu_title : p.title,
      'is_current' => p.id == page.id
    })
    if (!use_redirect_urls && self.is_allowed(user, p.id, 'edit'))
      link.href = row.uri
    end 
    block.links << link
  end    
  return block
end

.update_authorized_for_action(page_id, action, roles) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/models/caboose/page.rb', line 195

def self.update_authorized_for_action(page_id, action, roles)
  Caboose::PagePermission.where(:page_id => page_id, :action => action).destroy_all
  if roles
    roles.each do |role|
      role_id = role.is_a?(Integer) ? role : role.id
      Caboose::PagePermission.create({
        :page_id => page_id,
        :role_id => role_id,
        :action => action
      })
    end
  end
  return true
end

.update_child_perms(page_id) ⇒ Object


174
175
176
177
178
179
180
181
182
# File 'app/models/caboose/page.rb', line 174

def self.update_child_perms(page_id)
  page = self.find(page_id)
    
  viewer_ids   = Caboose::PagePermission.where(:page_id => page_id, :action => 'view'   ).all.collect{ |pp| pp.role_id }
  editor_ids   = Caboose::PagePermission.where(:page_id => page_id, :action => 'edit'   ).all.collect{ |pp| pp.role_id }
  approver_ids = Caboose::PagePermission.where(:page_id => page_id, :action => 'approve').all.collect{ |pp| pp.role_id }   
                            
  self.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids)
end

.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids) ⇒ Object


184
185
186
187
188
189
190
191
192
193
# File 'app/models/caboose/page.rb', line 184

def self.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids)
  
  self.update_authorized_for_action(page.id, 'view'     , viewer_ids)
  self.update_authorized_for_action(page.id, 'edit'     , editor_ids)
  self.update_authorized_for_action(page.id, 'approve'  , approver_ids)

  page.children.each do |kid|
    self.update_child_perms_helper(kid, viewer_ids, editor_ids, approver_ids)
  end
end

.update_uri(page) ⇒ Object


163
164
165
166
167
168
169
170
171
172
# File 'app/models/caboose/page.rb', line 163

def self.update_uri(page)
  #return if page.redirect_url && page.redirect_url.length > 0
  
  page.slug = self.slug(page.title) if page.slug.nil? || page.slug.strip.length == 0
  page.uri = page.alias && page.alias.strip.length > 0 ? page.alias : (page.parent ? "#{page.parent.uri}/#{page.slug}" : "#{page.slug}")
  page.uri[0] = '' if page.uri.starts_with?('/')
  page.save
  
  page.children.each { |p2| self.update_uri(p2) }     
end

.url(page_id) ⇒ Object


343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/models/caboose/page.rb', line 343

def self.url(page_id)
  arr = []
  self.url_helper(page_id, arr)
  arr.reverse!
  
  ru = arr.last.redirect_url
  return ru if ru && ru.strip.length > 0

  path = []      
  arr.each do |row|
    if row.alias && row.alias.strip.length > 0        
      path = [row.alias]
    elsif row.slug && row.slug.strip.length > 0
      path << row.slug
    end
  end
  return "/#{path.join('/')}"
end

.url_helper(page_id, arr) ⇒ Object


362
363
364
365
366
367
368
369
370
# File 'app/models/caboose/page.rb', line 362

def self.url_helper(page_id, arr)
  return if page_id <= 0
  
  p = self.find_with_fields(page_id, [:id, :parent_id, :title, :menu_title, :alias, :slug, :redirect_url])
  return if p.nil?

  arr << p
  self.url_helper(p.parent_id, arr)
end

Instance Method Details

#blockObject


52
53
54
# File 'app/models/caboose/page.rb', line 52

def block
  Caboose::Block.where("page_id = ? and parent_id is null", self.id).first
end

#custom_field_value(key) ⇒ Object


421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'app/models/caboose/page.rb', line 421

def custom_field_value(key)
  fv = Caboose::PageCustomFieldValue.where(:page_id => self.id, :key => key).first
  if fv.nil?
    f = Caboose::PageCustomField.where(:site_id => self.site_id, :key => key).first
    return nil if f.nil?
    fv = Caboose::PageCustomFieldValue.create(:page_id => self.id, :page_custom_field_id => f.id, :key => key, :value => f.default_value, :sort_order => f.sort_order)
  end
  f = fv.page_custom_field
  return fv.value if f.nil?
  case f.field_type
    when Caboose::PageCustomField::FIELD_TYPE_TEXT     then return fv.value
    when Caboose::PageCustomField::FIELD_TYPE_SELECT   then return fv.value
    when Caboose::PageCustomField::FIELD_TYPE_CHECKBOX then return fv.value == '1'
    when Caboose::PageCustomField::FIELD_TYPE_DATE     then return fv.value ? Date.strptime(fv.value, "%Y-%m-%d") : nil
    when Caboose::PageCustomField::FIELD_TYPE_DATETIME then return fv.value ? DateTime.strptime(fv.value, "%Y-%m-%d %H:%i:%s") : nil
  end  
  return fv.value
end

#duplicate(site_id, parent_id, duplicate_children = false, block_type_id = nil, child_block_type_id = nil) ⇒ Object


447
448
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
511
512
513
# File 'app/models/caboose/page.rb', line 447

def duplicate(site_id, parent_id, duplicate_children = false, block_type_id = nil, child_block_type_id = nil)
  
  if parent_id.to_i == -1
    p = Caboose::Page.index_page(site_id)
    p.children.destroy_all        
    #if self.site_id != site_id
    #  self.page_tags.destroy_all    
    #  self.page_custom_field_values.destroy_all          
    #  self.page_permissions.destroy_all      
    #  self.block.destroy
    #end
  else
    p = Caboose::Page.create(:site_id => site_id, :parent_id => parent_id)
  end

  p.title                = "Copy of " + self.title                 
  p.menu_title           = self.menu_title            
  p.alias                = self.alias                               
  p.redirect_url         = self.redirect_url
  p.hide                 = true               
  p.content_format       = self.content_format        
  p.custom_css           = self.custom_css            
  p.custom_js            = self.custom_js             
  p.linked_resources     = self.linked_resources      
  p.layout               = self.layout                
  p.sort_order           = self.sort_order            
  p.custom_sort_children = self.custom_sort_children  
  p.seo_title            = self.seo_title             
  p.meta_keywords        = self.meta_keywords         
  p.meta_description     = self.meta_description      
  p.meta_robots          = self.meta_robots           
  p.canonical_url        = self.canonical_url         
  p.fb_description       = self.fb_description        
  p.gp_description       = self.gp_description 

  i = 0
  begin
    par = Caboose::Page.where(:id => parent_id).first
    p.slug = Caboose::Page.slug(p.title + (i > 0 ? " #{i}" : ""))
    p.uri = par.parent_id == -1 ? p.slug : "#{par.uri}/#{p.slug}"
    i = i+1
  end while (Caboose::Page.where(:uri => p.uri, :site_id => site_id).count > 0 && i < 10)

  p.save
  
  self.page_tags.each{ |tag| Caboose::PageTag.create(:page_id => p.id, :tag => tag.tag) }
  
  self.page_custom_field_values.each do |v|
    f = v.page_custom_field.duplicate(site_id)
    v.duplicate(p.id, f.id)      
  end
  
  self.page_permissions.each do |pp|
    pp.role.duplicate(site_id)      
    r = Caboose::Role.where(:site_id => site_id, :name => pp.role.name).first
    Caboose::PagePermission.create(:page_id => p.id, :role_id => r.id, :action => pp.action)
  end

  self.block.duplicate_page_block(site_id, p.id, block_type_id)
      
  if duplicate_children && !p.is_child_of?(self.id)
    self.children.each do |p2|
      p2.duplicate(site_id, p.id, duplicate_children, child_block_type_id, child_block_type_id)
    end
  end
  return p
end

#head_titleObject


411
412
413
414
415
# File 'app/models/caboose/page.rb', line 411

def head_title
  str = ""
  str << "#{self.title} | " if !self.title.nil? && self.title.strip.length > 0
  str << self.site.description if self.site && self.site.description
end

#is_child_of?(parent_id) ⇒ Boolean

Returns:

  • (Boolean)

392
393
394
# File 'app/models/caboose/page.rb', line 392

def is_child_of?(parent_id)
  return Caboose::Page.is_child(parent_id, self.id)
end

#is_publishedObject


73
74
75
# File 'app/models/caboose/page.rb', line 73

def is_published
  Caboose::Block.where(:page_id => self.id).where('status != ?','published').count == 0 if !self.id.nil?
end

#linked_resources_mapObject


396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'app/models/caboose/page.rb', line 396

def linked_resources_map
  resources = { js: [], css: [] }
  return resources if self.linked_resources.nil?
  self.linked_resources.each_line do |r|
    r.chomp!
    case r
    when /\.js$/
      resources[:js] << r
    when /\.css$/
      resources[:css] << r
    end
  end
  return resources
end

#order_titleObject


45
46
47
48
49
50
# File 'app/models/caboose/page.rb', line 45

def order_title
  return "" + menu_title + title unless menu_title.nil? || title.nil?
  return menu_title unless menu_title.nil?
  return title unless title.nil?
  return ""
end

#publishObject


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

def publish
  if !self.id.nil?
    Caboose::Block.where(:page_id => self.id).where('status = ? OR status = ?','edited','added').all.each do |b|
      if b.new_value == 'EMPTY'
        b.value = nil
      elsif !b.new_value.blank?
        b.value = b.new_value
      end
      if b.new_media_id == 0
        b.media_id = nil
      elsif !b.new_media_id.blank?
        b.media_id = b.new_media_id
      end
      b.sort_order = b.new_sort_order if !b.new_sort_order.blank?
      b.parent_id = b.new_parent_id if !b.new_parent_id.blank?
      b.status = 'published'
      b.new_value = nil
      b.new_media_id = nil
      b.new_sort_order = nil
      b.new_parent_id = nil
      b.save
    end
    deleted_blocks = Caboose::Block.where(:page_id => self.id, :status => 'deleted').pluck(:id)
    dids = deleted_blocks.blank? ? 0 : deleted_blocks
    Caboose::Block.where("id in (?) or parent_id in (?)",dids,dids).destroy_all
    Caboose::Block.where(:page_id => self.id, :status => nil).update_all(:status => 'published')
    self.status = 'published'
    self.save
  end
end

#revertObject


108
109
110
111
112
113
114
115
# File 'app/models/caboose/page.rb', line 108

def revert
  if !self.id.nil?
    Caboose::Block.where(:page_id => self.id).where(:status => 'added').destroy_all
    Caboose::Block.where(:page_id => self.id).update_all("status = 'published', new_value = null, new_media_id = null, new_sort_order = sort_order, new_parent_id = null")
    self.status = 'published'
    self.save
  end
end

#top_level_blocksObject


56
57
58
# File 'app/models/caboose/page.rb', line 56

def top_level_blocks
  Caboose::Block.where("page_id = ? and parent_id is null", self.id).reorder(:sort_order).all
end

#urlObject


372
373
374
# File 'app/models/caboose/page.rb', line 372

def url
  return Caboose::Page.url(self.id)
end

#verify_custom_field_values_existObject


440
441
442
443
444
445
# File 'app/models/caboose/page.rb', line 440

def verify_custom_field_values_exist
  Caboose::PageCustomField.where(:site_id => self.site_id).all.each do |f|
    fv = Caboose::PageCustomFieldValue.where(:page_id => self.id, :page_custom_field_id => f.id).first                  
    Caboose::PageCustomFieldValue.create(:page_id => self.id, :page_custom_field_id => f.id, :key => f.key, :value => f.default_value, :sort_order => f.sort_order) if fv.nil?
  end
end