page_content.rb 7.81 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(page, 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 = page.page_template.template_parts.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, build_always = false)
      if type_content != 'rte_editor' and (type_content != self.type_content || build_always)


        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 = page_template.template_parts.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'
                if item[:attributes]['repeatItemId'] == ""

                  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,
                                                               marker_name: item[:itemName]
                                                           })
                        end
                      end
                    else

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

            end


            subpart.conditions.select{|item| item["repeat_id"] == "" }.each do |cond|
                self.page_content_markers.create({
                                                     marker: cond["name"],
                                                     marker_name: cond["name"],
                                                     condition: true
                                                 })
            end


          end
        end
      end
    end

    def build_markers_rows(type_content, repeat_id, parent_item_index)
      row_item = nil
      if type_content != 'rte_editor'
        page_template = self.page.page_template
        if page_template
          subpart = page_template.template_parts.find_by_code(type_content)
          if subpart
            #find current max row_item
            row_item = self.page_content_markers.where(parent_id: parent_item_index, repeat_id: repeat_id).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]['repeatItemId'] == repeat_id
                        self.page_content_markers.create({
                                                             marker: item[:name],
                                                             row_item: row_item,
                                                             marker_name: item[:itemName],
                                                             parent_id: parent_item_index,
                                                             repeat_id: repeat_id,
                                                             condition: false
                                                         })
                  end

              end
            end

            subpart.conditions.select{|item| item["repeat_id"] == repeat_id }.each do |cond|
                self.page_content_markers.create({
                                                     marker: cond["name"],
                                                     row_item: row_item,
                                                     marker_name: cond["name"],
                                                     parent_id: parent_item_index,
                                                     repeat_id: repeat_id,
                                                     condition: true
                                                 })
            end


          end
        end

      end

      return row_item
    end

    def delete_markers_row(row_item, repeat_id, parent_item_index)
      template_part = page.page_template.template_parts.find_by_code(type_content)
      self.page_content_markers.where(row_item: row_item, repeat_id: repeat_id, parent_id: parent_item_index).delete_all

      repeat_element = template_part.field_options.select{|item| item[:name] == 'repeat' && item[:repeatItemId].to_s ==  repeat_id}.first
      if repeat_element
        ids = self.page_content_markers.where(repeat_id: repeat_element[:id], parent_id: row_item).pluck(:row_item).uniq
        ids.each do |index|
          delete_markers_row(index, repeat_element[:id], row_item)
        end
      end

    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
        subpart = page.page_template.template_parts.find_by_code(self.type_content)
        subpart ? subpart.name : 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 = page_template.template_parts.find_by_code(self.type_content)
        if subpart
          html = Kanjai::TemplateGenerator.subpart_generate(nil, self.page.domain, page_template, 'content', subpart.elements, self.marker_hash, self.id)
        else
          html = ''
        end
        html.gsub("#newline#", '<br/>')
      end
    end

    def get_simple_content
      subpart = page.page_template.template_parts.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

    def formatted_conditions(repeat_id)
      (conditions || [])[repeat_id]
    end

  end
end