/app/helpers/application_helper.rb
Ruby | 369 lines | 288 code | 43 blank | 38 comment | 38 complexity | c3f0bc50c2e78aef211b5d82138e0e74 MD5 | raw file
- require 'util'
- require 'dual_list'
- require 'uri'
- # Methods added to this helper will be available to all templates in the application.
- module ApplicationHelper
- include Util
- include DualList
- def current_account
- current_user.school_account
- end
- def arrow_loader
- %q{
- <div align="center">
- <div class="loader" align="center" style="margin-top:80px;margin-bottom:80px;border:1px solid black;padding-bottom:50px;padding-top:50px;border-radius:3px;background:url('/images/bg-mac_blue.gif') #aef;width:450px;">
- <div class="inner_loader" align="center" style="border:2px dotted #395;padding:15px;width:200px;height:80px;border-radius:5px;background:white">
- <br/><img src="/images/ajax-loader_arrows.gif">
- <br/>
- <h4>please wait...</h4>
- </div>
- </div>
- </div>
- }.html_safe
- end
- def special_chars_div(chars, text_field)
- html = %Q{<div class="special_chars">}
- chars.each_with_index do |c, idx|
- html += %Q{<input type="button" name="char_#{idx}" value="#{c}" onclick="add_char2form(this.form, '#{text_field}', '#{c}')" />}
- end
- html += "</div>"
- html.html_safe
- end
- def begin_panel(header=nil)
- html = '<div class="panel">'
- html += %Q{ <h1 class="heading">#{header}</h1> } if header
- html += ' <div class="body">'
- html.html_safe
- end
- def end_panel
- "</div></div>".html_safe
- end
- def sound_button(sound_url)
- sound_url = static_url() + sound_url if sound_url =~ /^\//
- %Q{ <a href="#{sound_url}" onclick="play(this.href); return false;"><img border="0" src="#{static_url()}/cms_images/hear.gif" /></a> }.html_safe
- end
- def alert(message)
- return ('<div class="alert-outer"><div class="alert-inner">' + message + '</div></div>').html_safe
- end
- # Returns protocol and host portion of URL
- # Example: http://static.123teachme.com
- # options num parameter specifies host
- # 0 -> static (default)
- # 1 -> static1
- # 2 -> static2
- # 3 -> static3
- def static_url(num=0)
- return 'http://static.123teachme.com'.html_safe if num==0 || num > 3
- return "http://static#{num}.123teachme.com".html_safe
- end
- # Returns protocol and host portion of URL for the Content Delivery Network
- def cdn_url(protocol="http")
- return "#{protocol}://d1a9cyuca1q7uc.cloudfront.net".html_safe
- end
- def random_links(num=1)
- link = '<a href="http://www.123TeachMe.com/">Spanish Immersion Programs</a>'
- return link.html_safe
- end
- # converts any text to a number between 1 (inclusive) and max (inclusive)
- # This is often used to consistently map a
- # record in the database to a particular page (URL)
- # In that case, the URL is passed in as the text, and
- # the max id is passed in as max
- def simple_hash(text, max)
- sum = 0
- text.each_byte do |c|
- sum += c.to_i
- end
- return sum%max + 1
- end
- ######## QUERY STRING MANIPULATION ###############
- # given a hash of parameters, it returns a query string
- # with all of the parameters in the proper order (alphabetized)
- def params2query(params)
- end
- # given a request uri, it parses the query parameters,
- # sorts them, and returns the request uri in proper form
- # (with query parameters in alphabetical order)
- def sort_uri(old_uri, remove_param=nil)
- return nil if !old_uri
- uri = old_uri.clone
- # remove any params if specified to be removed
- if remove_param
- uri.gsub!(/#{remove_param}=([^&]*)(\?|&)/, '')
- end
- # remove empty params
- uri.gsub!(/&[^=]+=&/, '&')
- uri.gsub!(/&[^=]+=$/, '')
- path, query = uri.split('?')
- return path.html_safe if !query
- pairs = query.split('&')
- return uri.html_safe if pairs.length < 2
- key_vals = []
- pairs.each do |pair|
- key_vals << pair.split('=')
- end
- key_vals.sort! {|x,y| x[0] <=> y[0] }
- new_uri = path + '?'
- count = 0
- key_vals.each do |x|
- next if !x[0] || !x[1]
- new_uri += '&' if count>0
- new_uri += x[0] + '=' + x[1]
- count+=1
- end
- return new_uri.html_safe
- end
- def header(str)
- @page_heading = str
- return ''
- end
- def header_and_title(str)
- @page_heading = str
- @page_title = str
- return ''
- end
- def required
- '<span style="color:#f00;">*</span>'.html_safe
- end
- def stars(num)
- #('<img src="/images/' + num.round.to_i.to_s + '_star.gif" />').html_safe
- ('<img src="http://static.123teachme.com/images/schools/' + num.round.to_i.to_s + '_star.png" />').html_safe
- end
- # the old way of generating stars (no longer used)
- def old_stars(num)
- num_stars = num.round
- html = '<nobr>'
- stars_left = 5 - num_stars
- for k in 0...num_stars
- html += '<img src="/images/star_on.gif" />'
- end
- for k in 0...stars_left
- html += '<img src="/images/star_off.gif" />'
- end
- html += '</nobr>'
- html.html_safe
- end
- def golden_stars(num)
- num_stars = num.round
- html = '<nobr>'
- stars_left = 5 - num_stars
- for k in 0...num_stars
- html += '<img src="http://static.123teachme.com/images/school_list/gold_star.png" />'
- end
- for k in 0...stars_left
- html += '<img src="http://static.123teachme.com/images/school_list/star.png" />'
- end
- html += '</nobr>'
- html.html_safe
- end
- # if text is longer than length, then it shortens it and appends an ellipsis
- def snippet(text, length)
- return text if !text || text.length <= length
- return text[0..length] + '...'
- end
- def strip_html(text)
- txt = text.gsub(/ /, ' ')
- txt.gsub(/<[^>]*>/, '')
- end
- def caution(message)
- ('<div class="caution"><table><tr><td><img src="/images/caution.png"></td><td>' + message + '</td></tr></table></div>').html_safe
- end
- def paging_link(text, page_number)
- link = ''
- begin
- new_uri = request.fullpath.gsub("site/search", "search.php")
- # if page is present, then replace, otherwise add it
- p_idx = new_uri.index(/page=\d+/)
- page_param = "page=#{page_number}"
- if p_idx
- url = new_uri.gsub(/page=\d+/, page_param)
- else
- amp_or_q = new_uri.index('?') ? '&' : '?'
- url = new_uri + amp_or_q + page_param
- end
- # remove page=1, since this is implied
- if url=~/page=1$/
- url.gsub!(/(\?|&)page=1/, '')
- else
- url.gsub!(/page=1&/,'')
- end
- # sort query parameters
- url = sort_uri(url)
- rescue => e
- return link = "<a href=\"#{request.fullpath}\">Error</a>".html_safe
- end
- link = "<a href=\"#{url}\">#{text}</a>".html_safe
- end
- def old_paging_link
- new_uri = request.fullpath
- amp_or_q = new_uri.index('?') ? '&' : '?'
- # if page_number is not nil, then replace current page number
- if page_number
- new_uri.gsub!(/search\[page\]=\d*/, '')
- new_uri.gsub!(/search%5Bpage%5D=\d*/, '')
- new_uri.gsub!(/search%5bpage%5d=\d*/, '')
- page_str = 'search[page]=' + page_number.to_s
- else
- page_str = 'z=1'
- end
- # if order is not nil, then replace current order param
- order_str = ''
- if order
- new_uri.gsub!(/search\[order\]=[^&]*/, '')
- new_uri.gsub!(/search%5Border%5D=[^&]*/, '')
- new_uri.gsub!(/search%5border%5d=[^&]*/, '')
- order_str = '&search[order]=' + order.to_s
- end
- link = '<a href="' + new_uri
- link += amp_or_q + page_str + order_str + '">' + text.to_s + '</a>'
- # remove extra param dividers
- link.gsub!(/&&+/,'&')
- #link.gsub('&&&','&')
- #link.gsub('&&','&')
- link.gsub!('?&','?')
- link.html_safe
- end
- def url_encode(text)
- text.gsub(' ', '+')
- end
- def bookmark_url
- buri = @bookmark_uri || request.fullpath
- buri = 'http://www.123TeachMe.com' + buri if !buri.index('http')
- URI.escape(buri, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).html_safe
- end
- def bookmark_title
- title = @bookmark_title || @page_title || '123TeachMe - Learn Spanish Online'
- URI.escape(title, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).html_safe
- end
- #place advertisement or fb like box
- def place_ad_or_fb
- ads=[]
- #NuLengua Ad
- ads[0]={img:"http://static.123TeachMe.com/images/advertisements/NuLenguaAd1.png",url:"http://www.nulengua.com",
- width:"300",height:"200"}
- #SpanishImmersion Ad
- ads[1]={img:"http://static.123TeachMe.com/images/advertisements/SpanishImmersionTMAd4.png",url:"http://www.123TeachMe.com/language_schools/",
- width:"300",height:"200"}
- #facebook is turned off for now because it doesn't seem to be producing any traffic
- #code may be removed later
- fb_as_alternate=false
- fb_percentage=0
- use_fb=(fb_as_alternate)? ((1+rand(100)<fb_percentage)? true : false) : false
- ad_out=""
- if use_fb
- ad_out='<fb:like-box href="http://www.facebook.com/123TeachMe" width="300" connections="4" stream="0" header="1"></fb:like-box>'
- else
- ad=ads.sample #pick randomly from list of ads
- ad_out='<div style="width:'+ad[:width]+'px;height:'+ad[:width]+'px">' +
- '<a href="'+ad[:url]+'">' +
- '<img src="'+ad[:img]+'" border="0">' +
- '</a></div>'
- end
- return ad_out.html_safe
- end
- def show_referred(school)
- if school.present?
- school.stats.extra1Counter if school.stats
- else
- String.new
- end
- end
- def link_to_add_fields(name, f, association)
- new_object = f.object.class.reflect_on_association(association).klass.new
- fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
- render("/schools/"+association.to_s.singularize + "_fields", :fo => builder)
- end
- link_to_function(name, ("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
- end
- def link_to_remove_fields(name, f)
- f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
- end
- def thumbnail_video(video,school)
- if video.present? && video.video_url.present?
- video_ko_url=video.video_url
- @m=video_ko_url.split('=')
- @vu=@m.last
- w='http://i.ytimg.com/vi/'+@vu+'/default.jpg'
- return image_tag(w, :class=>"resize_image", :id=> school.id )
- end
- end
- def photos_and_videos_present?(school)
- flags = school.photos.map { |photo| photo.image.exists? || school.videos.map(&:video_url).reject!{|v| v.blank?}.present? }
- if flags.uniq.include?(true)
- return true
- else
- return false
- end
- end
- def banner_url(school)
- if school.banner.url=="/banners/original/missing.png"
- banner_url="http://static.123teachme.com/images/schools/banner-default.jpg"
- else
- banner_url=school.banner.url
- end
- end
- def logo_url(school)
- if school.photo.url=="/photos/original/missing.png"
- logo_url="http://static.123teachme.com/images/schools/logo-default.jpg"
- else
- logo_url=school.photo.url
- end
- end
- def pricing_method(a,school)
- level_text ="#{a.level.humanize}"
- price =a.search_price(school.country)
- if price.try(:monthly)==false || price.nil?
- month=""
- else
- month=" / month"
- end
- if a.level =="basic" && school.account_level != nil
- level_text.concat(" - Free ")
- else
- level_text.concat(" - $#{number_with_precision(price.try(:price), :precision => 2)} #{month} ")
- end
- end
- end