Class: Caboose::CalendarsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/calendars_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_json, #admin_json_single, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/caboose/calendars_controller.rb', line 105

def admin_add
  return unless user_is_allowed('calendars', 'edit')
  
  resp = StdClass.new      
  calendar = Calendar.new
  calendar.name = params[:name]
  calendar.site_id = @site.id
  
  if calendar.name.nil? || calendar.name.strip.length == 0
    resp.error = "Please enter a calendar name."
  else
    calendar.save
    resp.redirect = "/admin/calendars/#{calendar.id}"
  end
  render :json => resp
end

#admin_deleteObject


123
124
125
126
127
128
# File 'app/controllers/caboose/calendars_controller.rb', line 123

def admin_delete
  return unless user_is_allowed('calendars', 'delete')
  Calendar.find(params[:id]).destroy      
  resp = StdClass.new({ 'redirect' => "/admin/calendars" })                  
  render :json => resp
end

#admin_editObject


74
75
76
77
78
79
80
81
82
# File 'app/controllers/caboose/calendars_controller.rb', line 74

def admin_edit
  return unless user_is_allowed('calendars', 'edit')
  @calendar = Calendar.find(params[:id])
  
  @d = params[:d] ? DateTime.iso8601(params[:d]) : DateTime.now
  @d = @d - (@d.strftime('%-d').to_i-1)      
  
  render :layout => 'caboose/admin'
end

#admin_indexObject


65
66
67
68
69
70
71
# File 'app/controllers/caboose/calendars_controller.rb', line 65

def admin_index
  return if !user_is_allowed('calendars', 'view')
  render :file => 'caboose/extras/error_invalid_site' and return if @site.nil?
              
  @calendars = Calendar.where(:site_id => @site.id).reorder(:name).all
  render :layout => 'caboose/admin'      
end

#admin_updateObject


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/caboose/calendars_controller.rb', line 85

def admin_update
  return unless user_is_allowed('calendars', 'edit')
  
  resp = StdClass.new({'attributes' => {}})
  calendar = Calendar.find(params[:id])
  
  save = true      
  params.each do |name, value|
    case name
      when 'name'         then calendar.name         = value
      when 'description'  then calendar.description  = value
      when 'color'        then calendar.color        = value
    end
  end

  resp.success = save && calendar.save
  render :json => resp
end

#before_actionObject


7
8
9
# File 'app/controllers/caboose/calendars_controller.rb', line 7

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end

#feedObject


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
# File 'app/controllers/caboose/calendars_controller.rb', line 12

def feed
  cal = Caboose::Calendar.find(params[:id])
  if cal
    query = []                                                                                                                             
    args = []
    d1 = Date.parse(params[:start])
    d2 = Date.parse(params[:end])
    query << "(( cast(begin_date as date) >= ? and cast(begin_date as date) <= ?) or ( cast(end_date as date) >= ? and cast(end_date as date) <= ? )) or ( cast(begin_date as date) <= ? and cast(end_date as date) >= ? )"
    args << d1
    args << d2
    args << d1
    args << d2
    args << d1
    args << d2
    where = [query.join(' and ')]
    where2 = params[:admin] == 'true' && params[:published] == 'false' ? '(published is false)' : '(published is true)'
    args.each { |arg| where << arg }
    events = Caboose::CalendarEvent.where(where2).where("calendar_id = ?", cal.id).where(where).reorder(:begin_date).all
    render :json => events.collect { |e|
      begin_date = e.all_day ? Date.parse(e.begin_date.strftime("%Y-%m-%d")) : e.begin_date
      end_date = e.all_day ? Date.parse(e.end_date.strftime("%Y-%m-%d")).next : e.end_date
      {
        'id'     => e.id,
        'title'  => (e.published ? e.name : "#{e.name} (DRAFT)"),
        'start'  => begin_date.strftime('%Y-%m-%d %H:%M:%S'),
        'end'    => end_date.strftime('%Y-%m-%d %H:%M:%S'),
        'url'    => (params[:admin] == 'true' ? "/admin/calendars/#{cal.id}/events/#{e.id}" : "/calendar-events/#{e.id}"),
        'allDay' => e.all_day
      }
    }
  else
    return nil
  end
end

#jsonObject


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/caboose/calendars_controller.rb', line 48

def json
  event = Caboose::CalendarEvent.find(params[:id])
  e = {
    'id'     => event.id,
    'name'  => event.name,
    'begin_date'  => event.begin_date.strftime('%Y-%m-%d %H:%M:%S'),
    'end_date'    => event.end_date.strftime('%Y-%m-%d %H:%M:%S'),
    'location'    => event.location,
    'description' => event.description,
    'all_day' => event.all_day
  }
  if event
    render :json => e
  end
end