site_controller.rb 3.1 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

=begin
    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
=end

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

      domain_title = request.domain
      if request.subdomain.to_s.present?
        domain_title = "#{request.subdomain}.#{domain_title}"
      end

      domain = Domain.where(title: domain_title).first

      lang = nil

      if domain.nil?
        # try found alternative domain
        lang_domain = LanguageDomain.joins(:page_langs).where(title: domain_title,  kanjai_page_langs: {default_use: false}).first
        if lang_domain && lang_domain.page_langs.count > 0
          domain = lang_domain.domain
          lang = lang_domain.page_langs.where(default_use: false).first.code
        else
          render plain: "Domain #{domain_title} Not Exist" and return 
        end   
      else
        lang = domain.page_langs.where(default_use: true).first.try(:code)
      end

      if params && params[:locale].present? && request.original_fullpath.split('/')[1].length == 2
        I18n.locale = params[:locale]
      elsif lang
        I18n.locale = lang
      else
        I18n.locale = Rails.application.config.i18n.default_locale
      end


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


      if @page_data
        #$current_locale = @page_data.lang
      else
        if @url == '/'
          root_page = Page.joins(:domain).where(kanjai_domains: {id: domain.id}).where(root_page: true).first
          if root_page
            redirect_to root_page.menu_url(session[:scheme])
          else
            render plain: "Can not found page #{@url} for domain #{domain_title}" and return
          end
        else
          render plain: "Can not found page #{@url} for domain #{domain_title}" and return
        end
      end
      session[:url] = @url
    end



  end
end