page_content.rb 5.02 KB
require 'kanjai/template_generator'

module Kanjai
  class PageContent < ActiveRecord::Base
    belongs_to :page

    belongs_to :page_datum

    has_many :page_content_markers, dependent: :destroy
    accepts_nested_attributes_for :page_content_markers

    def self.edit_template(content_type)
      case content_type
        when 'rte_editor'
          return ['kanjai/admin/pages/content_types/rte_editor', {}]
        when 'plugin'
          return ['kanjai/admin/pages/content_types/plugin', {}]
        else
          template_part = TemplatePart.find_by_code(content_type)
          if (template_part.part_type == 'content')
            return ['kanjai/admin/pages/content_types/content', {template_part: template_part}]
          end
      end
    end

    def build_markers(type_content)
      if type_content != 'rte_editor' and type_content != self.type_content


        self.update_column(:type_content, type_content)

        self.page_content_markers.each do |item|
          item.destroy
        end

        page_template = self.page.page_template
        if page_template
          subpart = TemplatePart.find_by_code(type_content)
          if subpart
            #find repeat block
            repeat_element = subpart.field_options.select{|item| item[:name] == 'repeat' }.first
            count_repeat = nil
            if repeat_element and repeat_element[:attributes]['count'].present?
              count_repeat = repeat_element[:attributes]['count'].to_i
            end


            subpart.field_options.each do |item|
              if item[:name] != 'repeat'

                marker_obj = self.page_content_markers.find_by_marker(item[:name])
                unless marker_obj
                  if item[:attributes]['repeat'].to_s == 'true'
                    if count_repeat.to_i > 0
                      (1..count_repeat).each do |index|
                        self.page_content_markers.create({
                                                             marker: item[:name],
                                                             row_item: index
                                                         })
                      end
                    end
                  else

                    self.page_content_markers.create({
                                                         marker: item[:name]
                                                     })
                  end
                end
              end

            end
          end

        end



      end
    end

    def build_markers_rows(type_content)
      row_item = nil
      if type_content != 'rte_editor'
        page_template = self.page.page_template
        if page_template
          subpart = TemplatePart.find_by_code(type_content)
          if subpart
            #find current max row_item
            row_item = self.page_content_markers.collect(&:row_item).max
            row_item = 0 if row_item.nil?

            row_item += 1

            subpart.field_options.each do |item|
              if item[:name] != 'repeat'

                  if item[:attributes]['repeat'].to_s == 'true'
                        self.page_content_markers.create({
                                                             marker: item[:name],
                                                             row_item: row_item
                                                         })
                  end

              end
            end
          end
        end

      end

      return row_item
    end

    def delete_markers_row(row_item)
      self.page_content_markers.where(row_item: row_item).delete_all
    end

    def get_content
      if self.type_content == 'rte_editor'
        I18n.t('admin.content_types.rte_editor')
      elsif self.type_content == 'plugin'
        "#{I18n.t('admin.content_types.plugin')}: #{controller_name}.#{action_name}"
      else
        self.type_content
      end
    end

    def get_content_frontend
      if self.type_content == 'rte_editor'
        self.text_html.to_s
      elsif self.type_content == 'plugin'
        controller = self.controller_name.constantize.new
        controller.send self.action_name
        controller_variables = controller.instance_variables
        hash = {}
        controller_variables.each do |variable|
          hash[variable.to_s[1..-1]] = controller.instance_variable_get(variable.to_s)
        end
        self.controller_name.constantize.render self.action_name.to_sym, assigns: hash
      else
        page_template = self.page.page_template
        subpart = TemplatePart.find_by_code(self.type_content)
        html = Kanjai::TemplateGenerator.subpart_generate(page_template, 'content', subpart.elements, self.marker_hash, self.id)
        html
      end
    end

    def get_simple_content
      subpart = TemplatePart.find_by_code(self.type_content)
      subpart.source
    end


    def marker_hash
      result = {}
      self.page_content_markers.order(:row_item).each do |item|
        result[item.row_item] ||= {}
        result[item.row_item][item.marker] = item.text_value
      end
      result
    end

  end
end