pages_helper.rb 8.36 KB
module Kanjai
  module Admin::PagesHelper
    def get_html_by_json(page_data, client_view = false)
      json_data = page_data.template_content
      html = ''
      json_data.each do |row|
        row['attributes'] ||= {}
        class_name = 'row-fluid row'
        class_name += ' row-element-block' if row['attributes']['type'] == 'block'
        class_name += ' row-element-row' if row['attributes']['type'] == 'row'

        html_attributes = {}
        html_attributes[:class] = class_name

        html_attributes['data-status'] = row['attributes']['status'].to_s == 'enabled' || row['attributes']['status'].to_s.empty? ? 'enabled' : 'disabled'

        row_html = ''
        if client_view == false and row['attributes']['type'] == 'block'
          html_attributes['data-id'] = row['attributes']['id']
          page_content = page_data.page_contents.where(:structure_id => row['attributes']['id']).first
          row_html = '<div class="col-html">' + (page_content.nil? ? '' : page_content.get_content) + '</div>'
        end

        html += ActionController::Base.helpers.content_tag(:div, '', html_attributes) do
          ActionController::Base.helpers.concat(row_html.html_safe)
          row["cells"].each do |cell|
            page_content = page_data.page_contents.where(:structure_id => cell['id']).first
            html = page_content.nil? ? '' : page_content.get_content

            cell_class_name = ''


            ActionController::Base.helpers.concat(ActionController::Base.helpers.content_tag(:div, html.html_safe, :class => "col-md-#{cell['size']} col #{cell_class_name}", 'data-size' => cell['size'], 'data-offset' => cell['offset'], 'data-id' => cell['id'], 'data-page_data_id' => page_data.id , 'data-class' => cell['className'], 'data-status' => cell['status'].to_s == 'enabled' || cell['status'].to_s.empty? ? 'enabled' : 'disabled' ))
          end
        end
      end

      html
    end


    def get_html_by_json_client(page_data)
      json_data = page_data.template_content
      html = ''
      #row_template = File.open("#{Rails.root}/app/view/kanjai/pages/templates/row.html.erb").read
      #cell_template = File.open("#{Rails.root}/app/view/kanjai/pages/templates/cell.html.erb").read

      json_data.each do |row|
        row['attributes'] ||= {}


        if row['attributes']['type'] == 'block'
          if row['attributes']['status'].to_s == 'enabled' || row['attributes']['status'].to_s.empty?
            #show direct content
            structure_id = row['attributes']['id']
            page_content = page_data.page_contents.where(:structure_id => structure_id).first

            if page_content
              html_attributes = {}
              html_attributes[:class] = 'col'
              html_attributes['data-id'] = row['attributes']['id']
              html_attributes['data-page_data_id'] = page_data.id

              if page_content.type_content.split('-').include?('wrapper')
                html += (page_content.get_simple_content).html_safe
              else
                cells = page_content.nil? ? '' : page_content.get_content_frontend

                data_attributes = [
                    "data-id=#{row['attributes']['id']}",                
                    "data-page_data_id=#{page_data.id}"
                ]
                html += ApplicationController.render(file: 'kanjai/pages/templates/block', assigns: {content: cells.html_safe, data_attributes: data_attributes.join(' ')}, layout: false).html_safe

                #html += ActionController::Base.helpers.content_tag(:div, '', html_attributes) do
                #  ActionController::Base.helpers.concat((page_content.nil? ? '' : page_content.get_content_frontend).html_safe)
                #end
              end
            end
          end

        else
=begin
          class_name = 'row-fluid clearfix'
          html_attributes = {}
          html_attributes[:class] = class_name

          html += ActionController::Base.helpers.content_tag(:div, '', html_attributes) do
            cells = ''
            row["cells"].each do |cell|
              page_content = page_data.page_contents.where(:structure_id => cell['id']).first
              html = page_content.nil? ? '' : page_content.get_content_frontend
              cell_class_name = cell['className']

              data_attributes = [
                "data-size='#{cell['size']}'",
                "data-offset='#{cell['offset']}'",
                "data-id='#{cell['id']}'",                
                "data-page_data_id='#{page_data.id}'",    
                "data-class='#{cell['className']}'",                                                            
              ]

              cells += ApplicationController.render  assigns: {content: html.html_safe, cell: cell, class_name: cell_class_name, data_attributes: data_attributes.join(' ')}, inline: template
              ActionController::Base.helpers.concat(ActionController::Base.helpers.content_tag(:div, html.html_safe, :class => "col-md-#{cell['size']} col #{cell_class_name}", 'data-size' => cell['size'], 'data-offset' => cell['offset'], 'data-id' => cell['id'], 'data-page_data_id' => page_data.id , 'data-class' => cell['className']))
            end
          end
=end
            if row['attributes']['status'].to_s == 'enabled' || row['attributes']['status'].to_s.empty?
              cells = ''
              row["cells"].each do |cell|
                if cell['status'].to_s == 'enabled' || cell['status'].to_s.empty?
                  page_content = page_data.page_contents.where(:structure_id => cell['id']).first
                  html_content = page_content.nil? ? '' : page_content.get_content_frontend
                  cell_class_name = cell['className']

                  data_attributes = [
                    "data-size=#{cell['size']}",
                    "data-offset=#{cell['offset']}",
                    "data-id=#{cell['id']}",                
                    "data-page_data_id=#{page_data.id}",    
                    "data-class=#{cell['className']}",                                                            
                  ]

                  cells += ApplicationController.render file: 'kanjai/pages/templates/cell', assigns: {content: html_content.html_safe, cell: cell, class_name: cell_class_name, data_attributes: data_attributes.join(' ')}, layout: false
                end
              end

              html += ApplicationController.render(file: 'kanjai/pages/templates/row', assigns: {content: cells.html_safe}, layout: false).html_safe
            end

        end


      end


      #html = '<div class="container">' + html.to_s + '</div>'

      html.to_s
    end

    def get_html_by_json_client_url(url, locale)
      page_data = PageDatum.where(:url => url, lang: locale).first
      get_html_by_json_client(page_data)
    end

    def make_navigation(parent_id, level)
      html = ''
      if level > 0
        html += ActionController::Base.helpers.content_tag(:ul, class: 'nav navbar-nav') do
          Page.where(:parent_id => parent_id).order(:position).each do |page|
            if page.children.count > 0
              ActionController::Base.helpers.concat(
                  ActionController::Base.helpers.content_tag(:li) do
                    ActionController::Base.helpers.concat(
                        ActionController::Base.helpers.link_to (page.lang_attributes(PageLang.default(page.domain), :title) + '<span class="caret"></span>').html_safe, page.menu_url(session[:scheme], lang = nil), :class => 'dropdown-toggle', 'data-toggle' => "dropdown", 'role' => "button", 'aria-haspopup' => "true", 'aria-expanded' => "false"
                    )
                    ActionController::Base.helpers.concat(
                        ActionController::Base.helpers.content_tag(:ul) do
                          ActionController::Base.helpers.concat(make_navigation(page.id, level - 1))
                        end
                    )
                  end
              )


            else
              ActionController::Base.helpers.concat(
                  ActionController::Base.helpers.content_tag(:li) do
                    ActionController::Base.helpers.concat(
                        ActionController::Base.helpers.link_to page.lang_attributes(PageLang.default(page.domain), :title), page.menu_url(session[:scheme], lang = nil)
                    )
                  end
              )
            end

          end
        end
      end

      html.html_safe
    end

  end
end