Module: CalendarHelper

Defined in:
app/helpers/calendar_helper.rb

Constant Summary collapse

COMMON_YEAR_DAYS_IN_MONTH =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Instance Method Summary collapse

Instance Method Details

#days_in_month(month, year = Time.now.year) ⇒ Object



166
167
168
169
# File 'app/helpers/calendar_helper.rb', line 166

def days_in_month(month, year = Time.now.year)
 return 29 if month == 2 && Date.gregorian_leap?(year)
 COMMON_YEAR_DAYS_IN_MONTH[month]
end

#module_kit_buy_tickets(item, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/calendar_helper.rb', line 11

def module_kit_buy_tickets(item, options = {})
  begin
  event = CalendarEntry.sys(_sid).where(:id=>item).first

  options[:min_quantity] ||= 1
  options[:max_quantity] ||= 10

  if options[:max_quantity] > event.tickets_remaining
    options[:max_quantity] = events.tickets_remaining
    options[:show_ticket_warning] = true
  else
    options[:show_ticket_warning] = false
  end

  kit_render :partial=>"calendar/buy_tickets", :locals=>{:item=>item, :options=>options}
  rescue Exception => e
    e.message
  end
end

#module_kit_calendar(calendar_id, options = {}) ⇒ Object



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
# File 'app/helpers/calendar_helper.rb', line 84

def module_kit_calendar(calendar_id, options = {})

  begin
  year = options[:year]
  month = options[:month]

  mini = options[:mini]==true

  unless options.include?(:show_filters)
    options[:show_filters] = ! mini
    options[:show_days] = !mini
  end    
  options[:show_header] = true unless options.include?(:show_header)
  filter = options[:filter] || ''
  year ||= Time.now.year 
  month ||= Time.now.month

  month = params[:month].to_i if params[:month]
  year = params[:year].to_i if params[:year]

  params[:srid] = '' if params[:srid]=='0'
  params[:etid] = '' if params[:etid]=='0'

  filter = "(1=1) " if filter == ''
  if params[:subregion].not_blank? && params[:search_area]
    filter += " and subregions.name = \"#{params[:subregion]}\""
  end
  if params[:srid].not_blank? && params[:search_area]
    filter += " and locations.subregion_id = #{params[:srid]}"
  end
  if params[:entrytype].not_blank?
    filter += " and calendar_entry_types.name = \"#{params[:entrytype]}\""
  end
  if params[:etid].not_blank?
    filter += " and calendar_entries.calendar_entry_type_id = #{params[:etid]}"
  end

  unless can?(:moderate, :calendar)
    filter += " and calendar_entries.approved_at is not null "
  end

  date = Time.new(year, month, 1, 0, 0, 0, 0)
  cal = Calendar.find_by_name_or_id(calendar_id)
  return "[can't find calendar '#{calendar_id}']" unless cal

  entries = {}
  days_in_this_month = days_in_month(month)

  logger.debug "**** days in month #{month} #{days_in_this_month}"
  for i in 1..days_in_this_month do
    entries[i] = []
  end
  start_date = date
  end_date = Time.new(year,month,days_in_this_month, 23, 59, 0, 0)

  position = nil
  if params[:postcode] && params[:search_postcode]
    position = Geocoder.coordinates(params[:postcode]) 
    if params[:distance]
      distance = params[:distance].to_i
    end
  end

  cal.entries_between(start_date, end_date, filter, position, distance).each do |entry|
    for day in entry.start_date..entry.end_date
      next unless day.month == month
      if entry.no_days || (entry.on_day(day.wday)==1)
        entries[day.day] << entry
      end
    end
  end  
  kit_render :partial=>"calendar/month", :locals=>{:calendar=>cal, :date=>date, :entries=>entries, :options=>options, :position=>position}

  rescue Exception => e
    logger.debug e
    logger.debug e.backtrace.join('\n')
    return "[cannot display calendar]"
  end
end

#module_kit_calendar_entry(id, options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/calendar_helper.rb', line 70

def module_kit_calendar_entry(id, options)
  entry = CalendarEntry.sys(_sid).where(:id=>id).first
  if entry
    entry = nil if entry.approved_at == nil && !can?(:moderate, :calendar)
    kit_render :partial=>"calendar/calendar_entry", :locals=>{:entry=>entry, :options=>options}
  else
    if params[:edit]=="1"
      "[Calendar entry will appear here]"
    else
      "[Calendar entry #{params[:id]} not found]"
    end
  end
end

#module_kit_calendar_entry_add(calendar_id, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/calendar_helper.rb', line 43

def module_kit_calendar_entry_add(calendar_id, options = {})

  unless calendar_id.is_number?
    calendar_id = Calendar.find_by_name(calendar_id).id
  end

  options[:success] ||= "/"
  options[:edit_url] ||= params[:edit_url] || request.fullpath

  calendar_entry = @calendar_entry

  unless calendar_entry 
    calendar_entry = CalendarEntry.new
    calendar_entry.calendar_id = calendar_id
    calendar_entry.location = Location.new
    calendar_entry.location.country = "United Kingdom"
  end

  kit_render :partial=>"calendar/form", :locals=>{:calendar_entry=>calendar_entry, :choose_calendar=>false, :options=>options}
end

#module_kit_calendar_entry_load(id) ⇒ Object



64
65
66
67
68
# File 'app/helpers/calendar_helper.rb', line 64

def module_kit_calendar_entry_load(id)
  entry = CalendarEntry.sys(_sid).where(:id=>id).first
  entry = nil if entry && entry.approved_at == nil && !can?(:moderate, :calendar)
  return entry
end

#module_kit_list_tickets(item, options = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'app/helpers/calendar_helper.rb', line 3

def module_kit_list_tickets(item, options={})
  begin
  kit_render :partial=>"calendar/list_tickets", :locals=>{:item=>item, :options=>options}
  rescue Exception => e
    e.message
  end
end

#module_kit_ticket_form(model, description, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/calendar_helper.rb', line 31

def module_kit_ticket_form(model, description, options = {})

 options[:submit] ||= "Submit"
 options[:redirect] ||= "/"
 options[:label] ||= 'Tickets'
 begin
   kit_render :partial=>"calendar/sell_tickets", :locals=>{:description=>description, :options=>options,  :model=>model}
  rescue Exception => e
    e.message
  end
end

#regions_listObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/helpers/calendar_helper.rb', line 171

def regions_list
  last_region_id = nil 
  first = true
  op = "{ "
  at_least_one = false
  Subregion.order("regions.name, subregions.name").includes(:region).each do |sr| 
    at_least_one = true
    if sr.region_id != last_region_id 
      if !first
        op += "],\r\n"
      else 
        first = false
      end 
      last_region_id = sr.region_id
      op += "#{sr.region_id} : ["
    end
    op += "[ \"#{sr.name}\", #{sr.id} ], "
  end
  op += "]" if at_least_one
  op += "}"

  return op.html_safe
end