content_function.rb 6.62 KB
require 'cgi'
module Kanjai
    module ContentFunction
      def get_page_url
        url = ''
        req_url = request.original_fullpath if req_url.blank?
        req_url = req_url.split('.')[0]

        if req_url.split('?').length > 1
          req_url = req_url.split('?')[0]
        end


        req_url.split('/').each {|item|
          unless item.empty?
            if /^[\d]+$/ === item
              url += '/'
              url += ':d'
            elsif item.length == 2
            #I18n.available_locales.collect{|locale| locale.to_s }.include?(item) 

            else
              url += '/'
              url += item
            end
          end
        }
        url = '/' if url.empty?

        return url
      end


=begin
      def get_page_content(req_url = '', opts = nil)
        lang = I18n.locale.to_s
        content = {}
        if @page


          @page.contents.find(:all,:conditions => ['lang = ? and (hide = ? or hide is null)', lang, false], :order => 'position').each{|item|
            if ContentPage.type?(item.name)
              if(ContentPage.content_element(item.name)[:type] == 'plugin')
                content[ContentPage.content_element(item.name)[:page_part]] ||= ''
                content[ContentPage.content_element(item.name)[:page_part]] = content[ContentPage.content_element(item.name)[:page_part]] + (render_cell ContentPage.content_element(item.name)[:cells], ContentPage.content_element(item.name)[:action], {:content_id => item.id, :lang => lang, :host => request.host, :scheme => request.protocol.to_s.downcase, :params => params})
              else
                content_part = ContentPage.content_element(item.name)[:page_part]
                content_part = 'default' if content_part.nil?
                content[content_part] ||= ''
                content[content_part] = content[content_part] + CGI.unescapeHTML(render_to_string :template => 'admin/contents/templates/' + item.name,:layout=>false,:locals=>{:content => item, :opts => opts})
              end
            end
          }


        end

        content
      end

      def get_another_page_by_id(id)
        lang = I18n.locale.to_s
        if id.to_i > 0
          page = Page.find_by_id(id.to_i)
          if page
            return page.page_datas.find_by_lang(lang)
          end
        end
      end

      def get_anather_page(url)
        lang = I18n.locale.to_s

        page = PageData.find_by_url(url).try(:page)

        page.page_datas.find_by_lang(lang)

      end

      def get_another_page_content_by_id(id, opts = nil)
        lang = I18n.locale.to_s
        content = ''

        if id.to_i > 0
          @another_page = Page.find_by_id(id.to_i)
          if @another_page
            @another_page.contents.find(:all,:conditions => ['lang = ?', lang],:order => 'position').each{|item|
              if ContentPage.type?(item.name)
                if(ContentPage.content_element(item.name)[:type] == 'plugin')
                  content = content + (render_cell ContentPage.content_element(item.name)[:cells], ContentPage.content_element(item.name)[:action], {:content_id => item.id, :lang => lang, :host => request.host, :scheme => request.protocol.to_s.downcase, :params => params} )
                else
                  content = content + CGI.unescapeHTML( (render_to_string :template => 'admin/contents/templates/' + item.name,:layout=>false,:locals=>{:content => item, :opts => opts}).to_str)
                end
              end
            }

            @page_data = @another_page.page_datas.find_by_lang(lang)
            if @page_data
              content = ("<div  style='#{ @page_data.background_image_file_name.present? ? 'background:url(' + @page_data.background_image.expiring_url(ADMIN_CONFIG['expiring_time'],:original) + ') no-repeat top center' : '' }'>" + content + "</div>").html_safe
            end

          end
        end
        content
      end

      def get_another_page_content(url, opts = nil)
        lang = I18n.locale.to_s
        content = ''


        @anather_page = PageData.find_by_url(url).try(:page)
        if @anather_page
          @anather_page.contents.find(:all,:conditions => ['lang = ?', lang],:order => 'position').each{|item|
            if item.hide.nil? or item.hide == false
              if ContentPage.type?(item.name)
                if(ContentPage.content_element(item.name)[:type] == 'plugin')
                  content = content + (render_cell ContentPage.content_element(item.name)[:cells], ContentPage.content_element(item.name)[:action], {:content_id => item.id, :lang => lang, :host => request.host, :scheme => request.protocol.to_s.downcase, :params => params} )
                else
                  content = content + CGI.unescapeHTML( (render_to_string :template => 'admin/contents/templates/' + item.name,:layout=>false,:locals=>{:content => item, :opts => opts}).to_str)
                end
              end
            end
          }

          @page_data = @anather_page.page_datas.find_by_lang(lang)
          if @page_data
            content = ("<div  style='#{ @page_data.background_image_file_name.present? ? 'background:url(' + @page_data.background_image.expiring_url(ADMIN_CONFIG['expiring_time'],:original) + ') no-repeat top center' : '' }'>" + content + "</div>").html_safe
          end

        end
        content
      end


      def get_another_page_content2(url, opts = nil)
        lang = I18n.locale.to_s
        content = {}

        @anather_page = PageData.find_by_url(url).try(:page)
        if @anather_page
          @anather_page.contents.find(:all,:conditions => ['lang = ?', lang],:order => 'position').each{|item|
            if ContentPage.type?(item.name)
              if(ContentPage.content_element(item.name)[:type] == 'plugin')
                content[ContentPage.content_element(item.name)[:page_part]] ||= ''
                content[ContentPage.content_element(item.name)[:page_part]] = content[ContentPage.content_element(item.name)[:page_part]] + (render_cell ContentPage.content_element(item.name)[:cells], ContentPage.content_element(item.name)[:action], {:content_id => item.id, :lang => lang, :host => request.host, :scheme => request.protocol.to_s.downcase, :params => params} )
              else
                content_part = ContentPage.content_element(item.name)[:page_part]
                content_part = 'default' if content_part.nil?
                content[content_part] ||= ''
                content[content_part] = content[content_part] + CGI.unescapeHTML(render_to_string :template => 'admin/contents/templates/' + item.name,:layout=>false,:locals=>{:content => item, :opts => opts})
              end
            end
          }

        end
        content
      end
=end

    end

end