]> git.openstreetmap.org Git - rails.git/blob - app/helpers/banner_helper.rb
Add end dates to banners
[rails.git] / app / helpers / banner_helper.rb
1 module BannerHelper
2
3   def all_banners()
4     {
5       :sotmus2016 => {
6         :id => 'sotmus2016',
7         :alt => 'State of the Map US 2016',
8         :link => 'http://stateofthemap.us/',
9         :img => 'banners/sotmus-2016.jpg',
10         :enddate => '2016-jul-23'
11       },
12       :sotm2016 => {
13         :id => 'sotm2016',
14         :alt => 'State of the Map 2016',
15         :link => 'http://2016.stateofthemap.org/',
16         :img => 'banners/sotm-2016.jpg',
17         :enddate => '2016-sep-23'
18       }
19     }
20   end
21
22   def active_banners()
23     all_banners().reject do |k,v|
24       enddate = v[:enddate]
25       parsed = (enddate and Date.parse enddate rescue nil)
26       parsed.is_a?(Date) and parsed.past?
27     end
28   end
29
30   # returns the least recently seen banner that is not hidden
31   def next_banner()
32     banners = active_banners()
33     bannerKey = nil
34     cookieKey = nil
35     queuePos = 9999
36
37     banners.each do |k, v|
38       ckey = cookie_id(v[:id]).to_sym
39       cval = cookies[ckey] || 0
40       next if cval == 'hide'
41
42       # rotate all banner queue positions
43       index = cval.to_i
44       if index > 0
45         cookies[ckey] = index - 1
46       end
47
48       # pick banner with mininum queue position
49       if index <= queuePos
50         bannerKey = k
51         cookieKey = ckey
52         queuePos = index
53       end
54     end
55
56     unless bannerKey.nil?
57       cookies[cookieKey] = banners.length   # bump to end of queue
58       banners[bannerKey]
59     end
60   end
61
62   def cookie_id(key)
63     "_osm_banner_#{key}"
64   end
65
66 end