Commit 6a4b9d99982a74cfcb40f480b2a25e2353e198d5
1 parent
20f39774
check generate page url, add new type for template element
Showing
7 changed files
with
120 additions
and
20 deletions
| ... | ... | @@ -74,21 +74,19 @@ module Kanjai |
| 74 | 74 | |
| 75 | 75 | #@page_data = PageDatum.where(:url => @url).first |
| 76 | 76 | #$current_locale = PageLang.where(default_use: true).first.code |
| 77 | - @page_data = PageDatum.joins(page: :domain).where(kanjai_domains: {id: domain.id}).where(:url => @url, lang: I18n.locale).first | |
| 78 | - session[:current_locale] = I18n.locale | |
| 79 | - if @page_data.nil? | |
| 80 | - mas = @url.split('/') | |
| 81 | - if mas.length > 1 | |
| 82 | - @url = '/' + mas[1] + '/:path' | |
| 83 | - @page_data = PageDatum.joins(page: :domain).where(kanjai_domains: {id: domain.id}).where(:url => @url, lang: I18n.locale).first | |
| 84 | - end | |
| 85 | - end | |
| 77 | + @page_data = Kanjai::CurrentPage.new(domain: domain, lang: I18n.locale, url: @url).call | |
| 86 | 78 | |
| 79 | + session[:current_locale] = I18n.locale | |
| 80 | + #if @page_data.nil? | |
| 81 | + # mas = @url.split('/') | |
| 82 | + # if mas.length > 1 | |
| 83 | + # @url = '/' + mas[1] + '/:path' | |
| 84 | + # @page_data = PageDatum.joins(page: :domain).where(kanjai_domains: {id: domain.id}).where(:url => @url, lang: I18n.locale).first | |
| 85 | + # end | |
| 86 | + #end | |
| 87 | 87 | |
| 88 | - if @page_data | |
| 89 | - #$current_locale = @page_data.lang | |
| 90 | - else | |
| 91 | - if @url == '/' | |
| 88 | + if @page_data.nil? | |
| 89 | + if @url == '/' || @url == '' | |
| 92 | 90 | root_page = Page.joins(:domain).where(kanjai_domains: {id: domain.id}).where(root_page: true).first |
| 93 | 91 | if root_page |
| 94 | 92 | redirect_to root_page.menu_url(session[:scheme]) | ... | ... |
| ... | ... | @@ -55,17 +55,38 @@ module Kanjai |
| 55 | 55 | |
| 56 | 56 | if /(http|https):\/\//.match url |
| 57 | 57 | url |
| 58 | + elsif url[0] == '/' | |
| 59 | + url == '/' ? "/#{lang}" : "/#{lang}#{url}" | |
| 60 | + else | |
| 61 | + current_page = self | |
| 62 | + segments = [] | |
| 63 | + loop do | |
| 64 | + current_page_url = current_page.lang_attributes(lang, :url) | |
| 65 | + segments << current_page_url.gsub(/^\//, '').gsub(/\/$/, '') | |
| 66 | + break if current_page_url[0] == '/' | |
| 67 | + break if current_page.parent.nil? | |
| 68 | + current_page = current_page.parent | |
| 69 | + end | |
| 70 | + | |
| 71 | + url = segments.reverse.join('/') | |
| 72 | + url = "/#{url}" unless url[0] == '/' | |
| 73 | + "/#{lang}#{url}" | |
| 74 | + end | |
| 75 | + end | |
| 76 | + | |
| 77 | + def generate_url_old(scheme, url, lang) | |
| 78 | + lang = I18n.locale.to_s if lang.nil? | |
| 79 | + | |
| 80 | + if /(http|https):\/\//.match url | |
| 81 | + url | |
| 58 | 82 | else |
| 59 | 83 | if /\//.match url |
| 60 | 84 | if url == '/' |
| 61 | - #scheme + ADMIN_CONFIG['domain_name'] + '/' + lang | |
| 62 | 85 | '/' + lang |
| 63 | 86 | else |
| 64 | - #scheme + ADMIN_CONFIG['domain_name'] + '/' + lang + url | |
| 65 | 87 | '/' + lang + url |
| 66 | 88 | end |
| 67 | 89 | else |
| 68 | - #scheme + ADMIN_CONFIG['domain_name'] + '/' + lang + '/' + url | |
| 69 | 90 | '/' + lang + '/' + url |
| 70 | 91 | end |
| 71 | 92 | end | ... | ... |
| ... | ... | @@ -187,7 +187,7 @@ module Kanjai |
| 187 | 187 | page_template = self.page.page_template |
| 188 | 188 | subpart = page_template.template_parts.find_by_code(self.type_content) |
| 189 | 189 | if subpart |
| 190 | - html = Kanjai::TemplateGenerator.subpart_generate(session, self.page.domain, page_template, 'content', subpart.elements, self.marker_hash, self.id) | |
| 190 | + html = Kanjai::TemplateGenerator.subpart_generate(session, self.page.domain, page_template, 'content', subpart.elements, self.marker_hash(subpart), self.id) | |
| 191 | 191 | else |
| 192 | 192 | html = '' |
| 193 | 193 | end |
| ... | ... | @@ -201,11 +201,19 @@ module Kanjai |
| 201 | 201 | end |
| 202 | 202 | |
| 203 | 203 | |
| 204 | - def marker_hash | |
| 204 | + def marker_hash(subpart) | |
| 205 | 205 | result = {} |
| 206 | 206 | self.page_content_markers.order(:row_item).each do |item| |
| 207 | 207 | result[item.row_item] ||= {} |
| 208 | - result[item.row_item][item.marker] = item.text_value | |
| 208 | + element = subpart.field_options.find{|element| element[:name] == item.marker} | |
| 209 | + marker_value = item.text_value | |
| 210 | + if element && element[:attributes] && element[:attributes]['type'] == 'page' | |
| 211 | + page = Kanjai::Page.find_by(id: marker_value) | |
| 212 | + if page | |
| 213 | + marker_value = page.menu_url('https://') | |
| 214 | + end | |
| 215 | + end | |
| 216 | + result[item.row_item][item.marker] = marker_value | |
| 209 | 217 | end |
| 210 | 218 | result |
| 211 | 219 | end | ... | ... |
| ... | ... | @@ -55,6 +55,10 @@ |
| 55 | 55 | </div> |
| 56 | 56 | <% end %> |
| 57 | 57 | |
| 58 | + <% if form2.object.get_marker_type(@type_content) == 'page' %> | |
| 59 | + <%= form2.select :text_value, @pages.collect{|p| ["-" * p[:level]*2 + p[:title], p[:id], :style => "padding-left:#{p[:level]*10}px;"] }, {:include_blank => true}, :class => "form-control" %> | |
| 60 | + <% end %> | |
| 61 | + | |
| 58 | 62 | </div> |
| 59 | 63 | |
| 60 | 64 | <% end %> | ... | ... |
lib/kanjai/current_page.rb
0 → 100644
| 1 | +module Kanjai | |
| 2 | + class CurrentPage | |
| 3 | + | |
| 4 | + attr_reader :domain, :lang, :url | |
| 5 | + | |
| 6 | + def initialize(domain:, lang:, url:) | |
| 7 | + @domain = domain | |
| 8 | + @lang = lang | |
| 9 | + @url = url | |
| 10 | + end | |
| 11 | + | |
| 12 | + def call | |
| 13 | + mas = url.split('/').reject { |c| c.empty? } | |
| 14 | + root_page = nil | |
| 15 | + page_datum = nil | |
| 16 | + | |
| 17 | + loop do | |
| 18 | + break if mas.count == 0 | |
| 19 | + | |
| 20 | + check_length = mas.count | |
| 21 | + | |
| 22 | + loop do | |
| 23 | + break if check_length <= 0 | |
| 24 | + | |
| 25 | + work_url = mas[0...check_length].join('/') | |
| 26 | + work_url = '/' if work_url.to_s.empty? | |
| 27 | + sheck_scope = scope | |
| 28 | + if root_page | |
| 29 | + page_ids = [].tap do |n| | |
| 30 | + root_page.walk_tree do |page, level| | |
| 31 | + n << page.id | |
| 32 | + end | |
| 33 | + end | |
| 34 | + sheck_scope = sheck_scope.where(kanjai_pages: {id: page_ids}) | |
| 35 | + end | |
| 36 | + | |
| 37 | + page_datum = sheck_scope.find_by(url: work_url) | |
| 38 | + page_datum = sheck_scope.find_by(url: "/#{work_url}") if page_datum.nil? | |
| 39 | + page_datum = sheck_scope.find_by(url: "#{work_url}/") if page_datum.nil? | |
| 40 | + page_datum = sheck_scope.find_by(url: "/#{work_url}/") if page_datum.nil? | |
| 41 | + | |
| 42 | + if page_datum | |
| 43 | + root_page = page_datum.page | |
| 44 | + mas = mas.last(mas.count - check_length) | |
| 45 | + break | |
| 46 | + else | |
| 47 | + check_length -= 1 | |
| 48 | + end | |
| 49 | + end | |
| 50 | + break if page_datum.nil? | |
| 51 | + end | |
| 52 | + | |
| 53 | + if page_datum.nil? && url == '/' | |
| 54 | + page_datum = scope.find_by(url: url) | |
| 55 | + end | |
| 56 | + | |
| 57 | + page_datum | |
| 58 | + end | |
| 59 | + | |
| 60 | + private | |
| 61 | + | |
| 62 | + def scope | |
| 63 | + @scope ||= PageDatum.joins(page: :domain).where(lang: lang, kanjai_domains: {id: domain.id}) | |
| 64 | + end | |
| 65 | + | |
| 66 | + end | |
| 67 | +end | |
| \ No newline at end of file | ... | ... |