pages_helper.rb 7.92 KB
module Kanjai
  module Admin::PagesHelper
    require 'open-uri'

    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 style="width:100%;" class="col-html">' + (page_content.nil? ? '' : wrap_iframe(page_content)) + '</div>'
        end

        if client_view == false and row['attributes']['type'] == 'row'
          html_attributes['data-id'] = row['attributes']['id']
        end

        html_attributes['data-class'] = row['attributes']['className'];

        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? ? '' : wrap_iframe(page_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, session = nil)
      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(session)

                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(' '), row: row}, layout: false).html_safe

              end
            end
          end

        else

            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(session)
                  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, row: row}, 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, session = nil)
      page_data = PageDatum.where(:url => url, lang: locale).first
      get_html_by_json_client(page_data, session)
    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

    def wrap_iframe(page_content)
      content = page_content.get_content_frontend
      layer = page_content.page_datum.page.page_template.get_html_content

      css_files = [].tap do |n|
        doc = Nokogiri::HTML(layer, nil, 'UTF-8')
        doc.css("link").each do |x|
          if x['rel'] == 'stylesheet'
            css_link = x['href']
            n << css_link
          end
        end
      end
      styles = css_files.collect do |file|  
        #begin
          "<link rel='stylesheet' href='#{file}'>"
        #rescue

        #end
      end

      #css_files = css_files.collect{|link| "<link rel='stylesheet' href='#{link}' />".html_safe  }.join('')
      content += "#{styles.join(' ')}".html_safe

    #<link rel="stylesheet" href="./assets/css/libs.bundle.css" />

      "<iframe frameBorder='0' src='data:text/html;charset=utf-8,#{ERB::Util.url_encode(content)}'></iframe>"
    end


  end


end