event.rb
2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Event < Article
  def self.type_name
    _('Event')
  end
  settings_items :address, :type => :string
  def link=(value)
    self.setting[:link] = maybe_add_http(value)
  end
  def link
    maybe_add_http(self.setting[:link])
  end
  xss_terminate :only => [ :link ], :on => 'validation'
  xss_terminate :only => [ :body, :link, :address ], :with => 'white_list', :on => 'validation'
  def initialize(*args)
    super(*args)
    self.start_date ||= Date.today
  end
  validates_presence_of :title, :start_date
  validates_each :start_date do |event,field,value|
    if event.end_date && event.start_date && event.start_date > event.end_date
      event.errors.add(:start_date, _('%{fn} cannot come before end date.').fix_i18n)
    end
  end
  named_scope :by_day, lambda { |date|
    {:conditions => ['start_date = :date AND end_date IS NULL OR (start_date <= :date AND end_date >= :date)', {:date => date}]}
  }
  include WhiteListFilter
  filter_iframes :body, :link, :address, :whitelist => lambda { profile && profile.environment && profile.environment.trusted_sites_for_iframe }
  def self.description
    _('A calendar event.')
  end
  def self.short_description
    _('Event')
  end
  def self.icon_name(article = nil)
    'event'
  end
  named_scope :by_range, lambda { |range| {
    :conditions => [
      'start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day',
      { :start_day => range.first, :end_day => range.last }
    ]
  }}
  def self.date_range(year, month)
    if year.nil? || month.nil?
      today = Date.today
      year = today.year
      month = today.month
    else
      year = year.to_i
      month = month.to_i
    end
    first_day = Date.new(year, month, 1)
    last_day = first_day + 1.month - 1.day
    first_day..last_day
  end
  def date_range
    start_date..(end_date||start_date)
  end
  # FIXME this shouldn't be needed
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include ActionController::UrlWriter
  include DatesHelper
  def to_html(options = {})
    result = ''
    html = Builder::XmlMarkup.new(:target => result)
    html.div(:class => 'event-info' ) {
      html.ul(:class => 'event-data' ) {
        html.li(:class => 'event-dates' ) {
          html.span _('When:')
          html.text! show_period(start_date, end_date)
        }
        html.li {
          html.span _('URL:')
          html.a(self.link || "", 'href' => self.link || "")
        }
        html.li {
          html.span _('Address:')
          html.text! self.address || ""
        }
      }
      if self.body
        html.div('_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____', :class => 'event-description') 
      end
    }
    if self.body
      result.sub!('_____XXXX_DESCRIPTION_GOES_HERE_XXXX_____', self.body)
    end
    result
  end
  def event?
    true
  end
  def tiny_mce?
    true
  end
  def notifiable?
    true
  end
  include Noosfero::TranslatableContent
  include MaybeAddHttp
end