Class: Fabulator::Radiant::Actions::Page

Inherits:
Action
  • Object
show all
Defined in:
lib/fabulator/radiant/actions/page.rb

Instance Method Summary collapse

Instance Method Details

#run(context, autovivify = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fabulator/radiant/actions/page.rb', line 19

def run(context, autovivify = false)
  @context.with(context) do |ctx|
    ## collect page parts
    parents = self.parent(ctx)
    if parents.nil? || parents.empty? # default is root page
      parents = self.select(ctx)
    end

    if parents.nil? || parents.empty? # default is root page
      parents = [ ctx.root.anon_node('/') ]
    end

    parents = parents.collect{ |pp| pp.to([ Fabulator::RADIANT_NS, 'page' ], ctx) } - [ nil ]
    # TODO: remove any non-found pages (404 pages, for example)

    return [] if parents.empty?

    s = self.slug(ctx).first

    return [] if s.nil? && !self.has_select?

    using_children = false

    if s.nil?
      s = parents.first
      
    else
      using_children = true
      s = s.to_s
    end

    ctx.set_scoped_info('radiant/page/parts', { })
    self.run_actions(ctx)

    ## update or create page - requires parent to exist
    page_parts_info = ctx.get_scoped_info('radiant/page/parts')

    parents.each do |page_id|
      page = ::Page.find(page_id.value)
      child = nil
      if using_children
        child = page.children.select{ |c| c.slug == s }.first
        if child.nil?
          child = page.class.new_with_defaults()

          child.slug = s
          child.parent_id = page.id
        end
      else
        child = page
      end

      begin
        child.status = Status[self.status(ctx).first.to_s]
      rescue
        # ignore status changes if status doesn't exist
      end

      begin
        child.description = self.description(ctx).first.to_s
      rescue
      end

      begin
        child.title = self.title(ctx).first.to_s
      rescue
      end

      begin
        child.breadcrumb = self.breadcrumb(ctx).first.to_s || child.title
      rescue
        child.breadcrumb = child.title
      end

      if child.breadcrumb.nil? || child.breadcrumb == ''
        child.breadcrumb = child.title
      end

      child.class_name = child.class.name

      page_parts_info.each_pair do |part_name, part_info|
        part = child.part(part_name)
        if part.nil?
          part = ::PagePart.new(:name => part_name)
        end
        if part_info[:filter]
          part.filter_id = part_info[:filter]
        end
        part.content = part_info[:content]
        if !child.has_part?(part_name)
          child.parts.concat part
        end
      end
      child.save!
      child.parts.each do |part|
        if part.content.nil?
          part.content = ''
          part.save
        end
      end
    end
  end
  [] # we return nothing
end