site_controller.rb 1.66 KB
module Kanjai
  class SiteController < ApplicationController
    layout false

    #protect_from_forgery

    before_action :define_locale
    before_action :get_page

    include Kanjai::ContentFunction

    private

    def default_url_options(options={})
      if params && params[:locale] || @current_controller == 'pages'
        { :locale => I18n.locale }
      else
        {}
      end

    end


    def define_locale
      if params && params[:locale].present? && request.original_fullpath.split('/')[1].length == 2
        I18n.locale = params[:locale]
      elsif Kanjai::PageLang.where(default_use: true).first
        I18n.locale = Kanjai::PageLang.where(default_use: true).first.code
      else
        I18n.locale = Rails.application.config.i18n.default_locale
      end
    end


    def get_page
      @url = get_page_url
      $original_fullpath = request.original_fullpath
      $original_format = request.format

      #@page_data = PageDatum.where(:url => @url).first
      #$current_locale = PageLang.where(default_use: true).first.code
      @page_data = PageDatum.where(:url => @url, lang: I18n.locale).first
      $current_locale = I18n.locale
      if @page_data.nil?
        mas = @url.split('/')
        if mas.length > 1
          @url = '/' + mas[1] + '/:path'
          @page_data = PageDatum.where(:url => @url, lang: I18n.locale).first
        end
      end


      if @page_data
        #$current_locale = @page_data.lang
      else
        if @url == '/'
          root_page = Page.where(root_page: true).first
          if root_page
            redirect_to root_page.menu_url($scheme)
          end
        end
      end
      $url = @url
    end



  end
end