Module: Zena::Use::Calendar::ViewMethods

Defined in:
lib/zena/use/calendar.rb

Instance Method Summary collapse

Instance Method Details

#cal_assign_cell(node, role, remove_used, target_zip = nil, date = nil, template_url = nil) ⇒ Object

display a calendar cell to assign ‘node_a’ to ‘node_b’ with A (target_zip) … B (source_zip) —> reference_to A, B, C, D

<r:calendar assign='reference' to='main' />


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/zena/use/calendar.rb', line 123

def cal_assign_cell(node, role, remove_used, target_zip=nil, date=nil, template_url=nil)
  date         ||= Time.parse(params[:date])
  target_zip   ||= params[:s]
  template_url ||= params[:t_url]
  state = node.linked_node ? (node.linked_node.zip ==  target_zip.to_i ? 'on' : 'used') : 'free'
  title = node.linked_node ? node.linked_node.title : _('free')
  hour  = date.strftime('%H')
  full_dom_id = "#{node.zip}_#{target_zip}_#{date.to_i}"
  res = "<li id='#{full_dom_id}' class='hour_#{hour} #{state}'>"

  if state == 'used' && remove_used.nil?
    res << title
  else
    date_format = "%Y-%m-%dT%H"
    opts = {:url => "/nodes/#{node.zip}?node[rel][#{role}][date]=#{date.strftime(date_format)}&node[rel][#{role}][other_id]=#{state == 'free' ? target_zip : ''}&s=#{target_zip}&dom_id=#{full_dom_id}&t_url=#{CGI.escape(template_url)}&date=#{date.strftime(date_format)}", :method => :put}
    if state == 'used' && remove_used == 'warn'
      opts[:confirm] = _("Delete relation '%{role}' between '%{source}' and '%{target}' ?") % {:role => role, :source => node.title, :target => node.linked_node.title}
    end
    res << link_to_remote(title, opts)
  end
  res << "</li>"
  res
end

#cal_class(utc_date, local_ref, tz, events = nil) ⇒ Object

Get day class. The first parameter is an UTC Date. The second is a local Time.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zena/use/calendar.rb', line 54

def cal_class(utc_date, local_ref, tz, events=nil)
  date = tz.utc_to_local(utc_date.to_time)
  @cal_today ||= tz.utc_to_local(Time.now).strftime(DAY_FORMAT)
  case date.wday
  when 6
    s = "sat"
  when 0
    s = "sun"
  else
    s = ""
  end
  s +=  'other' if date.mon != local_ref.mon
  s = s == '' ? [] : [s]
  s <<  'today' if date.strftime(DAY_FORMAT) == @cal_today
  s <<  'ref'   if date.strftime(DAY_FORMAT) == local_ref.strftime(DAY_FORMAT)
  s <<  'events' if events
  s.join(' ')
end

#cal_day_names(size, week_start_day) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zena/use/calendar.rb', line 7

def cal_day_names(size, week_start_day)
  if size == :tiny
    day_names = Date::ABBR_DAYNAMES
  else
    day_names = Date::DAYNAMES
  end

  res = ""
  0.upto(6) do |i|
    j = (i+week_start_day) % 7
    if j == 0
      html_class = " class='sun'"
    elsif j == 6
      html_class = " class='sat'"
    end
    res << "<th#{html_class}>#{_(day_names[j])}</th>"
  end
  res
end

#cal_start_end(utc_date, type, tz, week_start_day) ⇒ Object

find start and end dates for a calendar showing a specified date



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zena/use/calendar.rb', line 28

def cal_start_end(utc_date, type, tz, week_start_day)
  # We need to compute start/end in local tz
  date = tz.utc_to_local(utc_date)

  case type
  when :week
    # week
    start_date  = date
    end_date    = date
  else
    # month
    # From 2000-10-01 00:00
    start_date  = Time.utc(date.year, date.mon, 1)
    # To   2000-11-01 00:00
    end_date    = start_date.advance(:months => 1)
  end

  start_date = start_date.advance(:days => -((start_date.wday + 7 - week_start_day) % 7))
  # end_date.wday - 1 because at 00:00 this is considered to be the next day but we do not
  # show this day.
  end_date = end_date.advance(:days => (6 + week_start_day - (end_date.wday - 1)) % 7)
  # convert back to UTC
  [tz.local_to_utc(start_date.to_time), tz.local_to_utc(end_date)]
end

#cal_weeks(date_attr, list, start_date, end_date, tz, hours = nil) ⇒ Object

Yield block for every week between ‘start_date’ and ‘end_date’ with a hash of days => events.



74
75
76
77
78
79
80
81
82
83
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
# File 'lib/zena/use/calendar.rb', line 74

def cal_weeks(date_attr, list, start_date, end_date, tz, hours = nil)
  # build event hash
  cal_hash = {}
  if hours
    # hours should contain 0 and should be sorted
    # [0,12] ==> 0  => dates from 00:00 to 11:59
    #            12 => dates from 12:00 to 23:59

    (list || []).each do |n|
      # d is an UTC date
      utc_d = n.send(date_attr) rescue nil
      next unless utc_d && utc_d.kind_of?(Time)
      d = tz.utc_to_local(utc_d)
      hours.reverse_each do |h|
        if d.hour >= h
          # too bad Time does not have an hour= method, we could have written d.hour = h
          # d = d - (d.hour - h) * 3600
          # # we need this to properly display hour class in ajax return ?
          # n.send("#{date_attr}=", d)

          # d = local date
          h_list = cal_hash[d.strftime('%Y-%m-%d %H')] ||= []
          h_list << n
          break
        end
      end
    end

  else
    (list || []).each do |n|
      utc_d = n.send(date_attr)
      next unless utc_d
      d = tz.utc_to_local(utc_d)
      h_list = cal_hash[d.strftime('%Y-%m-%d 00')] ||= []
      h_list << n
    end
  end

  # Date#step includes the last date [first, last] but we need [first, last[
  start_date.to_datetime.step(end_date.to_datetime.advance(:seconds => -1),7) do |week|
    # each week (UTC Date)
    yield(week, cal_hash)
  end
end