site_controller.rb
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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.where(title: domain_title).first
if lang_domain
domain = lang_domain.domain
if lang_domain.page_lang
lang = lang_domain.page_lang.code
else
lang = domain.default_lang.try(:code)
end
else
render plain: "Domain #{domain_title} Not Exist" and return
end
else
lang = domain.default_lang.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: "1. Can not found page #{@url} for domain #{domain_title} for lang #{I18n.locale}" and return
end
else
render plain: "2. Can not found page #{@url} for domain #{domain_title} for lang #{I18n.locale}" and return
end
end
session[:url] = @url
session[:current_page_id] = @page_data.page.id
end
end
end