cirandas.net

ref: master

plugins/event/lib/event_plugin/event_block.rb


 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
class EventPlugin::EventBlock < Block
  attr_accessible :all_env_events, :limit, :future_only, :date_distance_limit, :display_as_calendar

  settings_items :all_env_events, :type => :boolean, :default => false
  settings_items :limit, :type => :integer, :default => 4
  settings_items :future_only, :type => :boolean, :default => true
  settings_items :date_distance_limit, :type => :integer, :default => 0
  settings_items :display_as_calendar, :type => :boolean, :default => false

  def self.description
    _('Events')
  end

  def help
    _('Show the profile events or all environment events.')
  end

  def events_source
    return environment if all_env_events
    if self.owner.kind_of? Environment
      environment.portal_community ? environment.portal_community : environment
    else
      self.owner
    end
  end

  def events(user = nil)
    events = events_source.events.order('start_date')
    events = user.nil? ? events.is_public : events.display_filter(user,nil)

    if future_only
      events = events.where('start_date >= ?', DateTime.now.beginning_of_day)
    end

    if date_distance_limit > 0
      events = events.by_range([
        DateTime.now.beginning_of_day - date_distance_limit,
        DateTime.now.beginning_of_day + date_distance_limit
      ])
    end

    event_list = []
    events.each do |event|
      event_list << event if event.display_to? user
      break if event_list.length >= limit
    end

    event_list
  end

  def cache_key language='en', user=nil
    last_event = self.events_source.events.published.order('updated_at DESC').first
    "#{super}-#{last_event.updated_at if last_event}"
  end

  def self.expire_on
    { :profile => [:article], :environment => [:article] }
  end

  def api_content(params = {})
    content = []
    events.each do |event|
      content << { title: event.title, id: event.id, date: event.start_date.to_i * 1000, view_url: event.view_url }
    end
    { events: content }
  end
end