template_generator.rb 16.7 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
module Kanjai
  module TemplateGenerator
    def self.subpart_generate(session, domain, page_template, type, elements, hash_value = nil, page_content_id = nil)
      session ||= {}
      @doc = Nokogiri::HTML::DocumentFragment.parse ""

      @original_hash_value = hash_value
      @original_hash_value ||= {}

      @page_content_id = page_content_id
      @page_content = page_content_id.present? ? Kanjai::PageContent.find(page_content_id) : nil

      Nokogiri::HTML::Builder.with(@doc) do |subparts|
        @repeat_type_index = {}
        if type == "subMenuTemplate"
          type = "menu" 
          current_page = nil
          if session[:current_page_id]
            current_page = Kanjai::Page.find_by_id(session[:current_page_id])
            while current_page && current_page.parent && current_page.page_template == current_page.parent.page_template
              current_page = current_page.parent
            end
          end
          TemplateGenerator.method("#{type}_generator").call(session, domain, elements, subparts, @original_hash_value[0], true, current_page)
        elsif type == 'menu'
          TemplateGenerator.method("#{type}_generator").call(session, domain, elements, subparts, @original_hash_value[0], false )
        else
          TemplateGenerator.method("#{type}_generator").call(session, domain, elements, subparts, @original_hash_value[0] )
        end
      end

      @doc.css("img[@src]").each do |x|
        image_link = x['src']
        unless image_link.match(/^(http:|https:)/)
          x['src'] = page_template.template_content_url + image_link
        end
      end

      @doc.to_html
    end



    def self.menu_generator(session, domain, elements, subparts, hash_value, start_from_current_page, current_page = nil)
        elements.each do |item|
          case item[:name]
            when 'text'
              content = item[:source].to_s.strip
              if current_page
                content = content.gsub('###link###', current_page.menu_url(session[:scheme], lang = nil).to_s)
                     .gsub('###title###', current_page.lang_attributes(I18n.locale, :title).to_s)
              end
              subparts << content
            when 'repeat'
              #get template for normal state, active state and separator
              normal_state = item[:children].select{|item2| item2[:name] == 'normal' }.first
              active_state = item[:children].select{|item2| item2[:name] == 'active' }.first
              separator = item[:children].select{|item2| item2[:name] == 'separator' }.first
              next_level = item[:children].select{|item2| item2[:name] == 'level' }.first

              normal_state_template = active_state_template = separator_template = ''

              if normal_state
                doc_normal_state = Nokogiri::HTML::DocumentFragment.parse ""
                Nokogiri::HTML::Builder.with(doc_normal_state) do |repeat_part|
                  self.menu_generator(session, domain, normal_state[:children], repeat_part, nil, false)
                end
                normal_state_template = doc_normal_state.to_html
              end

              if active_state
                doc_active_state = Nokogiri::HTML::DocumentFragment.parse ""
                Nokogiri::HTML::Builder.with(doc_active_state) do |repeat_part|
                  self.menu_generator(session, domain, active_state[:children], repeat_part, nil, false)
                end
                active_state_template = doc_active_state.to_html
              end

              if separator
                doc_separator = Nokogiri::HTML::DocumentFragment.parse ""
                Nokogiri::HTML::Builder.with(doc_separator) do |repeat_part|
                  self.menu_generator(session, domain, separator[:children], repeat_part, nil, false)
                end
                separator_template = doc_separator.to_html
              end


              page_collection = Page.joins(:domain).where(kanjai_domains: {id: domain.id}).order(:position)

              if item[:attributes]["ids"].present?
                page_collection = page_collection.where('id in (?)', item[:attributes]["ids"])
              end


              unless item[:attributes]["root_id"].nil?
                root_id = item[:attributes]["root_id"].to_s

                root_id = nil if root_id.empty?

                page_collection = page_collection.where(:parent_id => root_id)
              end

              unless current_page.nil?
                if start_from_current_page                  
                  page_collection = page_collection.where(:id => current_page.id)
                else
                  page_collection = page_collection.where(:parent_id => current_page.id)
                end
              end

              begin
                if !!UserSession
                  if UserSession.current_user
                    page_collection = page_collection.where(:private_flag => [true, false])
                                      .where(show_public_only: false)
                  else
                    page_collection = page_collection.where(private_flag: false)
                  end
                end
              rescue
                page_collection = page_collection.where(private_flag: false)
              end



              page_collection.each do |page|
                is_current_page = session[:url] == page.menu_url(session[:scheme], I18n.locale).gsub("/#{I18n.locale}", "")

                if page.children.length > 0 and next_level
                  subparts << item[:source].to_s.gsub('###link###', page.menu_url(session[:scheme], lang = nil).to_s)
                                    .gsub('###title###', page.lang_attributes(I18n.locale, :title).to_s)
                                    .gsub('###active_class###', is_current_page ? 'active' : '' )

                  self.menu_generator(session, domain, next_level[:children], subparts, nil, false, page)
                else

                  if session[:url] == page.menu_url(session[:scheme], I18n.locale).gsub("/#{I18n.locale}", "")
                    if active_state_template.present?
                      subparts << active_state_template.gsub('###link###', page.menu_url(session[:scheme], lang = nil))
                                    .gsub('###title###', page.lang_attributes(I18n.locale, :title))
                    end
                  else
                    if normal_state_template.present?
                      subparts << normal_state_template.gsub('###link###', page.menu_url(session[:scheme], lang = nil).to_s)
                                                        .gsub('###title###', page.lang_attributes(I18n.locale, :title).to_s)
                    end
                  end

                  if separator_template.present? and page != page_collection.last
                    subparts << separator_template
                  end
                end


              end


            else
                attributes = item[:attributes]
                if current_page
                  attributes = replace_attributes_marker(attributes, {
                    '###link###' => current_page.menu_url(session[:scheme], lang = nil),
                    '###title###' => current_page.lang_attributes(I18n.locale, :title)
                  })
                end
                subparts.send(item[:name].to_s, attributes) do |next_subparts|
                  self.menu_generator(session, domain, item[:children], next_subparts, nil, start_from_current_page, current_page)
                end
          end

        end

    end

    def self.language_generator(session, domain, elements, subparts, hash_value)
      elements.each do |item|
        case item[:name]
          when 'text'
            lang = domain.page_langs.find_by_code(session[:current_locale])
            content = item[:source].to_s.strip
            content = content.gsub('###CURRENT_LANG_TITLE###', lang.title)
                        .gsub('###CURRENT_LANG_CODE###', lang.code)
            subparts << content
          when 'repeat'
            #get template for normal state, active state and separator
            item_state = item[:children].select{|item2| item2[:name] == 'item' }.first
            active_state = item[:children].select{|item2| item2[:name] == 'active' }.first

            if item_state
              doc_item_state = Nokogiri::HTML::DocumentFragment.parse ""
              Nokogiri::HTML::Builder.with(doc_item_state) do |repeat_part|
                self.language_generator(session, domain, item_state[:children], repeat_part, nil)
              end
              item_state_template = doc_item_state.to_html
            end

            if active_state
              doc_active_state = Nokogiri::HTML::DocumentFragment.parse ""
              Nokogiri::HTML::Builder.with(doc_active_state) do |repeat_part|
                self.language_generator(session, domain, active_state[:children], repeat_part, nil)
              end
              active_state_template = doc_active_state.to_html
            end


            page_data = PageDatum.where(:url => session[:url], :lang => session[:current_locale]).first
            if page_data
              page = page_data.page
            end
            domain.page_langs.all.each do |lang|
                if lang.code.to_s == session[:current_locale].to_s && active_state_template
                  content = active_state_template.gsub('###LANG_CODE###', lang.code)
                              .gsub('###LANG_TITLE###', lang.title)

                elsif item_state_template.present?
                  content = item_state_template.gsub('###LANG_CODE###', lang.code)
                                .gsub('###LANG_TITLE###', lang.title)

                end
                if content
                  if page
                    content = content.gsub('###PAGE_LINK###', page.menu_url(session[:scheme], lang.code))
                  else
                    content = content.gsub('###PAGE_LINK###', '/')
                  end

                  subparts << content

                end
            end
          else
            attributes = item[:attributes]
            subparts.send(item[:name], attributes) do |next_subparts|
              self.language_generator(session, domain, item[:children], next_subparts, nil)
            end
        end

      end
    end


    def self.content_generator(session, domain, elements, subparts, hash_value)
      elements.each do |item|
        case item[:name]
          when 'text'
            subparts << self.replace_text_marker(subparts.parent.name, item[:source].to_s.strip, hash_value)
          when 'repeat'
            repeat_id = item[:attributes]['id']
            @repeat_type_index[repeat_id] ||= 0
            scope = @page_content.page_content_markers.where(repeat_id: repeat_id)
            if hash_value && hash_value['PARENT_ITEM_ID'].present?
              scope = scope.where(parent_id: hash_value['PARENT_ITEM_ID'])
            end
            row_index = scope.pluck(:row_item).uniq.sort

            row_index.each_with_index do |row_index, position|
              value = scope.where(row_item: row_index).collect do |item| 
                marker_type =  item.get_marker_type(item.page_content.type_content)
                marker_value = item.text_value

                if marker_type.to_s == 'page'
                  page = Kanjai::Page.find_by(id: marker_value)
                  if page
                    marker_value = page.menu_url('https://')
                  end
                end

                [item.marker, marker_value]
              end.to_h
              value ||= {}
              hash_value ||= {}
              value = value.merge(hash_value)
              if(position == 0)
                value['###FIRST_ITEM_ACTIVE###'] = 'active show'
                value['###FIRST_ITEM_TRUE###'] = 'true'
                value['###FIRST_ITEM_FALSE###'] = 'false'
              else
                value['###FIRST_ITEM_ACTIVE###'] = ''
                value['###FIRST_ITEM_TRUE###'] = 'false'
                value['###FIRST_ITEM_FALSE###'] = 'true'
              end
              value['###REPEAT_NUMBER###'] = "#{repeat_id}_#{row_index.to_s}_#{hash_value['PARENT_ITEM_ID'] || 0}"
              value['###REPEAT_CYCLE_COUNT###'] = @repeat_type_index[repeat_id].to_s
              value['PARENT_ITEM_ID'] = scope.where(row_item: row_index).first.uuid.to_s

              value.each do |key, value|
                element = item[:children].find{|element| element[:name] ==  key}
                if element && element[:attributes] && element[:attributes]['type'] == 'page'
                  value[key] = 'page link'
                end
              end

              @repeat_type_index[repeat_id] += 1
              self.content_generator(session, domain, item[:children], subparts, value)

            end

          when 'language'
            code = item[:attributes]['code']
            if I18n.locale.to_s == code.to_s
              self.content_generator(session, domain, item[:children], subparts, hash_value)
            end
          when 'condition'
            condition_process = true

            if item[:name] == 'condition' && @page_content.present? && item[:attributes] && item[:attributes]['id']
              condition_process = hash_value[item[:attributes]['id']].to_i == 1 if item[:attributes]['type'].to_s.empty? || item[:attributes]['type'].to_s == 'enable'
              condition_process = hash_value[item[:attributes]['id']].to_i == 0 if item[:attributes]['type'].to_s == 'disable'
            end

            if condition_process
              self.content_generator(session, domain, item[:children], subparts, hash_value)
            end

          else
            attributes = self.replace_attributes_marker(item[:attributes], hash_value)
            if item[:name] == 'select'
              subparts.select(attributes) do |next_subparts|
                self.content_generator(session, domain, item[:children], next_subparts, hash_value)
              end
            elsif item[:name] == 'p'
              if item[:children].count > 0
                subparts << "<p #{attributes.to_a.collect{|item| "#{item.first}='#{item.last}'" }.join(' ')} >#{self.replace_text_marker('p', item[:children][0][:source].to_s.strip, hash_value).html_safe}</p>"
                #subparts.p(attributes, self.replace_text_marker('p', item[:children][0][:source], hash_value).html_safe)
              end
            else
              subparts.send(item[:name], attributes) do |next_subparts|
                self.content_generator(session, domain, item[:children], next_subparts, hash_value)
              end
            end
        end

      end

    end

    def self.system_generator(session, domain, elements, subparts, hash_value)
      str = ''
      controller_item = elements.select{|item| item[:name] == 'controller' }.first
      action_item = elements.select{|item| item[:name] == 'action' }.first
      if controller_item and action_item
        controller_name_item = controller_item[:children].first
        action_name_item = action_item[:children].first
        if controller_name_item and action_name_item
          controller_name = controller_name_item[:source]
          action_name = action_name_item[:source]

          controller = controller_name.constantize.new
          controller.send action_name
          controller_variables = controller.instance_variables
          hash = {}
          controller_variables.each do |variable|
            hash[variable.to_s[1..-1]] = controller.instance_variable_get(variable.to_s)
          end
          str = controller_name.constantize.render action_name.to_sym, assigns: hash

        end

      end


      subparts << str
    end


    def self.replace_text_marker(parent_node_name, text, hash_value)
      new_text = text.dup

      if !hash_value.nil? and !text.nil?
        hash_value.each do |key, value|
          if !key.nil? and !value.nil?
            work_value = value.dup
            work_value.gsub!(/\r\n/, "#newline#") if !['code', 'pre'].include?(parent_node_name.to_s.downcase)
            new_text.gsub!(key, work_value.to_s.html_safe)
          end
        end
      end
      new_text
    end

    def self.replace_attributes_marker(attributes, hash_value)

      new_attributes = attributes.deep_dup
      if new_attributes.keys.include?('checked')
        new_attributes['make-checked'] = new_attributes.delete('checked')
      end

      attributes.each do |key, text|
        if !hash_value.nil? and !text.nil?
          hash_value.each do |key2, value2|
            if !key2.nil? and !value2.nil?
              key = 'make-checked' if key == 'checked'
              new_attributes[key].gsub!(key2, value2)
              new_attributes[key].gsub!('###FORM_ACTION###', "/#{I18n.locale}/form/#{@page_content_id}")
            end
          end
        end

      end
      new_attributes
    end





  end
end