lib/modules/language_methods.rb
#TODO: add check to see if type of field exists.
add check to see if type of field exists.
add check to see if type of field exists .
def replace_variables type_of_field, language, loop_sources, loop_item=nil, images_urls=nil
        return self.send(type_of_field.to_s) if !loop_item
        result = self.send(type_of_field.to_s)
        vars = result.gsub(/#\[[^\]]*\]/) 
        vars.each do |match|
          result = result.gsub(match, Sanitize.clean(match))
        end
        if self.is_a?(QuestionField)
          obj_type_var = "question"
        else
          obj_type_var = "section"
        end
        self.send(obj_type_var).send("extras").each do |extra|
          item = extra.loop_item_type.root.loop_source_id.present? ? loop_sources[extra.loop_item_type.root.loop_source.id.to_s] : nil
          if item
            item_to_use = item.loop_item_type == extra.loop_item_type ? item.loop_item_name : item.ancestors.find_by_loop_item_type_id(extra.loop_item_type_id).loop_item_name
            the_item_extra = item_to_use.item_extras.find_by_extra_id(extra.id)
            if the_item_extra
              the_field = ( the_item_extra.item_extra_fields.find_by_language(language) || the_item_extra.item_extra_fields.find_by_is_default_language(true) || the_item_extra.item_extra_fields.first)
              if extra.image? 
                the_value = "<img src='#{the_field.value}' alt='#{extra.name}::#{item_to_use.item_name(language)}' width='200px'/>"
                unless images_urls.nil?
                  images_urls << the_field.value
                end
              elsif extra.link? 
                the_value = "<a href='#{the_field.value}' alt='#{extra.name}::#{item_to_use.item_name(language)}'>#{item_to_use.item_name(language)}</a>"
              else
                the_value = the_field.value 
              end
              new_result = result.gsub("#[#{Sanitize.clean(extra.loop_item_type.name)}::#{Sanitize.clean(extra.name)}]", the_value)
            else
              new_result = result.gsub("#[#{Sanitize.clean(extra.loop_item_type.name)}::#{Sanitize.clean(extra.name)}]", "")
            end
            result = new_result.present? ? new_result : result
          end
        end
        result
      end
def replace_variables type_of_field , language , loop_sources , loop_item = nil , images_urls = nil return self . send ( type_of_field . to_s ) if ! loop_item result = self . send ( type_of_field . to_s ) vars = result . gsub ( / # \[ [^ \] ]* \] / ) vars . each do | match | result = result . gsub ( match , Sanitize . clean ( match ) ) end if self . is_a? ( QuestionField ) obj_type_var = "question" else obj_type_var = "section" end self . send ( obj_type_var ) . send ( "extras" ) . each do | extra | item = extra . loop_item_type . root . loop_source_id . present? ? loop_sources [ extra . loop_item_type . root . loop_source . id . to_s ] : nil if item item_to_use = item . loop_item_type == extra . loop_item_type ? item . loop_item_name : item . ancestors . find_by_loop_item_type_id ( extra . loop_item_type_id ) . loop_item_name the_item_extra = item_to_use . item_extras . find_by_extra_id ( extra . id ) if the_item_extra the_field = ( the_item_extra . item_extra_fields . find_by_language ( language ) || the_item_extra . item_extra_fields . find_by_is_default_language ( true ) || the_item_extra . item_extra_fields . first ) if extra . image? the_value = "<img src='#{the_field.value}' alt='#{extra.name}::#{item_to_use.item_name(language)}' width='200px'/>" unless images_urls . nil? images_urls << the_field . value end elsif extra . link? the_value = "<a href='#{the_field.value}' alt='#{extra.name}::#{item_to_use.item_name(language)}'>#{item_to_use.item_name(language)}</a>" else the_value = the_field . value end new_result = result . gsub ( "#[#{Sanitize.clean(extra.loop_item_type.name)}::#{Sanitize.clean(extra.name)}]" , the_value ) else new_result = result . gsub ( "#[#{Sanitize.clean(extra.loop_item_type.name)}::#{Sanitize.clean(extra.name)}]" , "" ) end result = new_result . present? ? new_result : result end end result end
TODO: add check to see if type of field exists.
TODO : add check to see if type of field exists .
#Sanitize the difference occurrences of variables in the field #using a loop in case there are multiple variables in the same field #look for occurrences of #[SomeVar] #either in language, or the default, or the first... #Image #URL #TEXT
type_of_field
language
loop_sources
loop_item
images_urls