Class: PandaCms::BulkEditor

Inherits:
Object
  • Object
show all
Defined in:
app/lib/panda_cms/bulk_editor.rb

Overview

Bulk editor for site content in JSON format

Class Method Summary collapse

Class Method Details

.exportString

Export all site content to a JSON string

Returns:

  • (String)

    The JSON data



14
15
16
17
# File 'app/lib/panda_cms/bulk_editor.rb', line 14

def self.export
  data = extract_current_data
  JSON.pretty_generate(data)
end

.import(json_data) ⇒ Hash

Import site content from a JSON string

Parameters:

  • json_data (String)

    The JSON data to import

Returns:

  • (Hash)

    A hash of debug information



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
123
# File 'app/lib/panda_cms/bulk_editor.rb', line 25

def self.import(json_data)
  # See if we can parse the JSON
  new_data = JSON.parse(json_data)
  current_data = extract_current_data

  debug = {
    success: [],
    error: [],
    warning: []
  }

  # Make sure templates are up to date
  PandaCms::Template.generate_missing_blocks

  # Run through the new data and compare it to the current data
  new_data["pages"].each do |path, page_data|
    if current_data["pages"][path].nil?
      begin
        page = PandaCms::Page.create!(
          path: path,
          title: page_data["title"],
          template: PandaCms::Template.find_by(name: page_data["template"]),
          parent: PandaCms::Page.find_by(path: page_data["parent"])
        )
      rescue => e
        debug[:error] << "Failed to create page '#{path}': #{e.message}"
        next
      end

      if !page
        debug[:error] << "Unhandled: page '#{path}' does not exist in the current data and cannot be created"
        next
      else
        debug[:success] << "Created page '#{path}' with title '#{page_data["title"]}'"
      end
    else
      page = PandaCms::Page.find_by(path: path)

      if page_data["title"] != current_data["pages"][path]["title"]
        page.update(title: page_data["title"])
        debug[:success] << "Updated: page '#{path}' title from '#{current_data["pages"][path]["title"]}' to '#{page_data["title"]}'"
      end

      if page_data["template"] != current_data["pages"][path]["template"]
        # TODO: Handle page template changes
        debug[:error] << "Page '#{path}' template is '#{current_data["pages"][path]["template"]}' and cannot be changed to '#{page_data["template"]}' without manual intervention"
      end
    end

    page_data["contents"].each do |key, block_data|
      content = block_data["content"]

      if current_data.dig("pages", path, "contents", key).nil?
        raise "Unknown page 1" if page.nil?
        block = PandaCms::Block.find_or_create_by(key: key, template: page.template) do |block_meta|
          block_meta.name = key.titleize
        end

        if !block
          debug[:error] << "Error creating block '#{key.titleize}' on page '#{page.title}'"
          next
        end

        block_content = PandaCms::BlockContent.find_or_create_by(block: block, page: page)
        # block_content.content = HTMLEntities.new.encode(content, :named)
        block_content.content = content

        begin
          block_content.save!

          if block_content.content != content
            debug[:error] << "Failed to save content for '#{block.name}' on page '#{page.title}'"
          else
            debug[:success] << "Created '#{block.name}' content on page '#{page.title}'"
          end
        rescue => e
          debug[:error] << "Failed to create '#{block.name}' content on page '#{page.title}': #{e.message}"
        end
      elsif content != current_data["pages"][path]["contents"][key]["content"]
        # Content has changed
        raise "Unknown page 2" if page.nil?
        block = PandaCms::Block.find_by(key: key, template: page.template)
        if PandaCms::BlockContent.find_by(page: page, block: block)&.update(content: content)
          debug[:success] << "Updated '#{key.titleize}' content on page '#{page.title}'"
        else
          debug[:error] << "Failed to update '#{key.titleize}' content on page '#{page.title}'"
        end
      end
    end
  end

  new_data["menus"].each do |menu_data|
  end

  new_data["templates"].each do |template_data|
  end

  debug
end