PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/app/helpers/application_helper.rb

https://bitbucket.org/kapilnakhwa/demo-teachme
Ruby | 369 lines | 288 code | 43 blank | 38 comment | 38 complexity | c3f0bc50c2e78aef211b5d82138e0e74 MD5 | raw file
  1. require 'util'
  2. require 'dual_list'
  3. require 'uri'
  4. # Methods added to this helper will be available to all templates in the application.
  5. module ApplicationHelper
  6. include Util
  7. include DualList
  8. def current_account
  9. current_user.school_account
  10. end
  11. def arrow_loader
  12. %q{
  13. <div align="center">
  14. <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;">
  15. <div class="inner_loader" align="center" style="border:2px dotted #395;padding:15px;width:200px;height:80px;border-radius:5px;background:white">
  16. <br/><img src="/images/ajax-loader_arrows.gif">
  17. <br/>
  18. <h4>please wait...</h4>
  19. </div>
  20. </div>
  21. </div>
  22. }.html_safe
  23. end
  24. def special_chars_div(chars, text_field)
  25. html = %Q{<div class="special_chars">}
  26. chars.each_with_index do |c, idx|
  27. html += %Q{<input type="button" name="char_#{idx}" value="#{c}" onclick="add_char2form(this.form, '#{text_field}', '#{c}')" />}
  28. end
  29. html += "</div>"
  30. html.html_safe
  31. end
  32. def begin_panel(header=nil)
  33. html = '<div class="panel">'
  34. html += %Q{ <h1 class="heading">#{header}</h1> } if header
  35. html += ' <div class="body">'
  36. html.html_safe
  37. end
  38. def end_panel
  39. "</div></div>".html_safe
  40. end
  41. def sound_button(sound_url)
  42. sound_url = static_url() + sound_url if sound_url =~ /^\//
  43. %Q{ <a href="#{sound_url}" onclick="play(this.href); return false;"><img border="0" src="#{static_url()}/cms_images/hear.gif" /></a> }.html_safe
  44. end
  45. def alert(message)
  46. return ('<div class="alert-outer"><div class="alert-inner">' + message + '</div></div>').html_safe
  47. end
  48. # Returns protocol and host portion of URL
  49. # Example: http://static.123teachme.com
  50. # options num parameter specifies host
  51. # 0 -> static (default)
  52. # 1 -> static1
  53. # 2 -> static2
  54. # 3 -> static3
  55. def static_url(num=0)
  56. return 'http://static.123teachme.com'.html_safe if num==0 || num > 3
  57. return "http://static#{num}.123teachme.com".html_safe
  58. end
  59. # Returns protocol and host portion of URL for the Content Delivery Network
  60. def cdn_url(protocol="http")
  61. return "#{protocol}://d1a9cyuca1q7uc.cloudfront.net".html_safe
  62. end
  63. def random_links(num=1)
  64. link = '<a href="http://www.123TeachMe.com/">Spanish Immersion Programs</a>'
  65. return link.html_safe
  66. end
  67. # converts any text to a number between 1 (inclusive) and max (inclusive)
  68. # This is often used to consistently map a
  69. # record in the database to a particular page (URL)
  70. # In that case, the URL is passed in as the text, and
  71. # the max id is passed in as max
  72. def simple_hash(text, max)
  73. sum = 0
  74. text.each_byte do |c|
  75. sum += c.to_i
  76. end
  77. return sum%max + 1
  78. end
  79. ######## QUERY STRING MANIPULATION ###############
  80. # given a hash of parameters, it returns a query string
  81. # with all of the parameters in the proper order (alphabetized)
  82. def params2query(params)
  83. end
  84. # given a request uri, it parses the query parameters,
  85. # sorts them, and returns the request uri in proper form
  86. # (with query parameters in alphabetical order)
  87. def sort_uri(old_uri, remove_param=nil)
  88. return nil if !old_uri
  89. uri = old_uri.clone
  90. # remove any params if specified to be removed
  91. if remove_param
  92. uri.gsub!(/#{remove_param}=([^&]*)(\?|&)/, '')
  93. end
  94. # remove empty params
  95. uri.gsub!(/&[^=]+=&/, '&')
  96. uri.gsub!(/&[^=]+=$/, '')
  97. path, query = uri.split('?')
  98. return path.html_safe if !query
  99. pairs = query.split('&')
  100. return uri.html_safe if pairs.length < 2
  101. key_vals = []
  102. pairs.each do |pair|
  103. key_vals << pair.split('=')
  104. end
  105. key_vals.sort! {|x,y| x[0] <=> y[0] }
  106. new_uri = path + '?'
  107. count = 0
  108. key_vals.each do |x|
  109. next if !x[0] || !x[1]
  110. new_uri += '&' if count>0
  111. new_uri += x[0] + '=' + x[1]
  112. count+=1
  113. end
  114. return new_uri.html_safe
  115. end
  116. def header(str)
  117. @page_heading = str
  118. return ''
  119. end
  120. def header_and_title(str)
  121. @page_heading = str
  122. @page_title = str
  123. return ''
  124. end
  125. def required
  126. '<span style="color:#f00;">*</span>'.html_safe
  127. end
  128. def stars(num)
  129. #('<img src="/images/' + num.round.to_i.to_s + '_star.gif" />').html_safe
  130. ('<img src="http://static.123teachme.com/images/schools/' + num.round.to_i.to_s + '_star.png" />').html_safe
  131. end
  132. # the old way of generating stars (no longer used)
  133. def old_stars(num)
  134. num_stars = num.round
  135. html = '<nobr>'
  136. stars_left = 5 - num_stars
  137. for k in 0...num_stars
  138. html += '<img src="/images/star_on.gif" />'
  139. end
  140. for k in 0...stars_left
  141. html += '<img src="/images/star_off.gif" />'
  142. end
  143. html += '</nobr>'
  144. html.html_safe
  145. end
  146. def golden_stars(num)
  147. num_stars = num.round
  148. html = '<nobr>'
  149. stars_left = 5 - num_stars
  150. for k in 0...num_stars
  151. html += '<img src="http://static.123teachme.com/images/school_list/gold_star.png" />'
  152. end
  153. for k in 0...stars_left
  154. html += '<img src="http://static.123teachme.com/images/school_list/star.png" />'
  155. end
  156. html += '</nobr>'
  157. html.html_safe
  158. end
  159. # if text is longer than length, then it shortens it and appends an ellipsis
  160. def snippet(text, length)
  161. return text if !text || text.length <= length
  162. return text[0..length] + '...'
  163. end
  164. def strip_html(text)
  165. txt = text.gsub(/&nbsp;/, ' ')
  166. txt.gsub(/<[^>]*>/, '')
  167. end
  168. def caution(message)
  169. ('<div class="caution"><table><tr><td><img src="/images/caution.png"></td><td>' + message + '</td></tr></table></div>').html_safe
  170. end
  171. def paging_link(text, page_number)
  172. link = ''
  173. begin
  174. new_uri = request.fullpath.gsub("site/search", "search.php")
  175. # if page is present, then replace, otherwise add it
  176. p_idx = new_uri.index(/page=\d+/)
  177. page_param = "page=#{page_number}"
  178. if p_idx
  179. url = new_uri.gsub(/page=\d+/, page_param)
  180. else
  181. amp_or_q = new_uri.index('?') ? '&' : '?'
  182. url = new_uri + amp_or_q + page_param
  183. end
  184. # remove page=1, since this is implied
  185. if url=~/page=1$/
  186. url.gsub!(/(\?|&)page=1/, '')
  187. else
  188. url.gsub!(/page=1&/,'')
  189. end
  190. # sort query parameters
  191. url = sort_uri(url)
  192. rescue => e
  193. return link = "<a href=\"#{request.fullpath}\">Error</a>".html_safe
  194. end
  195. link = "<a href=\"#{url}\">#{text}</a>".html_safe
  196. end
  197. def old_paging_link
  198. new_uri = request.fullpath
  199. amp_or_q = new_uri.index('?') ? '&' : '?'
  200. # if page_number is not nil, then replace current page number
  201. if page_number
  202. new_uri.gsub!(/search\[page\]=\d*/, '')
  203. new_uri.gsub!(/search%5Bpage%5D=\d*/, '')
  204. new_uri.gsub!(/search%5bpage%5d=\d*/, '')
  205. page_str = 'search[page]=' + page_number.to_s
  206. else
  207. page_str = 'z=1'
  208. end
  209. # if order is not nil, then replace current order param
  210. order_str = ''
  211. if order
  212. new_uri.gsub!(/search\[order\]=[^&]*/, '')
  213. new_uri.gsub!(/search%5Border%5D=[^&]*/, '')
  214. new_uri.gsub!(/search%5border%5d=[^&]*/, '')
  215. order_str = '&search[order]=' + order.to_s
  216. end
  217. link = '<a href="' + new_uri
  218. link += amp_or_q + page_str + order_str + '">' + text.to_s + '</a>'
  219. # remove extra param dividers
  220. link.gsub!(/&&+/,'&')
  221. #link.gsub('&&&','&')
  222. #link.gsub('&&','&')
  223. link.gsub!('?&','?')
  224. link.html_safe
  225. end
  226. def url_encode(text)
  227. text.gsub(' ', '+')
  228. end
  229. def bookmark_url
  230. buri = @bookmark_uri || request.fullpath
  231. buri = 'http://www.123TeachMe.com' + buri if !buri.index('http')
  232. URI.escape(buri, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).html_safe
  233. end
  234. def bookmark_title
  235. title = @bookmark_title || @page_title || '123TeachMe - Learn Spanish Online'
  236. URI.escape(title, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).html_safe
  237. end
  238. #place advertisement or fb like box
  239. def place_ad_or_fb
  240. ads=[]
  241. #NuLengua Ad
  242. ads[0]={img:"http://static.123TeachMe.com/images/advertisements/NuLenguaAd1.png",url:"http://www.nulengua.com",
  243. width:"300",height:"200"}
  244. #SpanishImmersion Ad
  245. ads[1]={img:"http://static.123TeachMe.com/images/advertisements/SpanishImmersionTMAd4.png",url:"http://www.123TeachMe.com/language_schools/",
  246. width:"300",height:"200"}
  247. #facebook is turned off for now because it doesn't seem to be producing any traffic
  248. #code may be removed later
  249. fb_as_alternate=false
  250. fb_percentage=0
  251. use_fb=(fb_as_alternate)? ((1+rand(100)<fb_percentage)? true : false) : false
  252. ad_out=""
  253. if use_fb
  254. ad_out='<fb:like-box href="http://www.facebook.com/123TeachMe" width="300" connections="4" stream="0" header="1"></fb:like-box>'
  255. else
  256. ad=ads.sample #pick randomly from list of ads
  257. ad_out='<div style="width:'+ad[:width]+'px;height:'+ad[:width]+'px">' +
  258. '<a href="'+ad[:url]+'">' +
  259. '<img src="'+ad[:img]+'" border="0">' +
  260. '</a></div>'
  261. end
  262. return ad_out.html_safe
  263. end
  264. def show_referred(school)
  265. if school.present?
  266. school.stats.extra1Counter if school.stats
  267. else
  268. String.new
  269. end
  270. end
  271. def link_to_add_fields(name, f, association)
  272. new_object = f.object.class.reflect_on_association(association).klass.new
  273. fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
  274. render("/schools/"+association.to_s.singularize + "_fields", :fo => builder)
  275. end
  276. link_to_function(name, ("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
  277. end
  278. def link_to_remove_fields(name, f)
  279. f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
  280. end
  281. def thumbnail_video(video,school)
  282. if video.present? && video.video_url.present?
  283. video_ko_url=video.video_url
  284. @m=video_ko_url.split('=')
  285. @vu=@m.last
  286. w='http://i.ytimg.com/vi/'+@vu+'/default.jpg'
  287. return image_tag(w, :class=>"resize_image", :id=> school.id )
  288. end
  289. end
  290. def photos_and_videos_present?(school)
  291. flags = school.photos.map { |photo| photo.image.exists? || school.videos.map(&:video_url).reject!{|v| v.blank?}.present? }
  292. if flags.uniq.include?(true)
  293. return true
  294. else
  295. return false
  296. end
  297. end
  298. def banner_url(school)
  299. if school.banner.url=="/banners/original/missing.png"
  300. banner_url="http://static.123teachme.com/images/schools/banner-default.jpg"
  301. else
  302. banner_url=school.banner.url
  303. end
  304. end
  305. def logo_url(school)
  306. if school.photo.url=="/photos/original/missing.png"
  307. logo_url="http://static.123teachme.com/images/schools/logo-default.jpg"
  308. else
  309. logo_url=school.photo.url
  310. end
  311. end
  312. def pricing_method(a,school)
  313. level_text ="#{a.level.humanize}"
  314. price =a.search_price(school.country)
  315. if price.try(:monthly)==false || price.nil?
  316. month=""
  317. else
  318. month=" / month"
  319. end
  320. if a.level =="basic" && school.account_level != nil
  321. level_text.concat(" - Free ")
  322. else
  323. level_text.concat(" - $#{number_with_precision(price.try(:price), :precision => 2)} #{month} ")
  324. end
  325. end
  326. end