events.rb 5.76 KB
Newer Older
Tyler Lemburg's avatar
Tyler Lemburg committed
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
require 'rest-client'
require 'models/event'
require 'models/event_type'
require 'models/location'
require 'models/resource'

before '/:service_space_url_name/admin/events*' do
	unless @user.has_permission?(Permission::MANAGE_EVENTS, @space)
		raise Sinatra::NotFound
	end
end

get '/:service_space_url_name/admin/events/?' do
	@breadcrumbs << {:text => 'Admin Events'}
	page = params[:page]
	page = page.to_i >= 1 ? page.to_i : 1
	page_size = 10
	tab = ['upcoming', 'past'].include?(params[:tab]) ? params[:tab] : 'upcoming'

	case tab
	when 'past'
		where_clause = 'start_time < ?', Time.now
		order_clause = {:start_time => :desc}
	else
		where_clause = 'start_time >= ?', Time.now
		order_clause = {:start_time => :asc}
	end

	iterator = Event.includes(:event_signups).where(:service_space_id => @space.id).where(where_clause)

	erb :'admin/events', :layout => :fixed, :locals => {
		:events => iterator.order(order_clause).limit(page_size).offset((page-1)*page_size).all,
		:total_pages => (iterator.count.to_f / page_size).ceil,
		:page => page,
		:tab => tab
	}
end

get '/:service_space_url_name/admin/events/:event_id/signup_list/?' do
Tyler Lemburg's avatar
Tyler Lemburg committed
40
	@breadcrumbs << {:text => 'Admin Events', :href => @space.admin_events_href} << {text: 'Signup List'}
Tyler Lemburg's avatar
Tyler Lemburg committed
41
42
43
44
45
46
47
48
49
50
51
52
53
	event = Event.includes(:event_signups).find_by(:id => params[:event_id], :service_space_id => @space.id)
	if event.nil?
		# that event does not exist
		flash(:danger, 'Not Found', 'That event does not exist')
		redirect @space.admin_events_href
	end

	erb :'admin/signup_list', :layout => :fixed, :locals => {
		:event => event
	}
end

get '/:service_space_url_name/admin/events/create/?' do
Tyler Lemburg's avatar
Tyler Lemburg committed
54
	@breadcrumbs << {:text => 'Admin Events', :href => @space.admin_events_href} << {text: 'Create Event'}
Tyler Lemburg's avatar
Tyler Lemburg committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
	erb :'admin/new_event', :layout => :fixed, :locals => {
		:event => Event.new,
		:types => EventType.where(:service_space_id => @space.id).all,
		:locations => Location.where(:service_space_id => @space.id).all,
		:resources => Resource.where(:service_space_id => @space.id, :is_reservable => true).all,
		:on_unl_events => false,
		:on_main_calendar => false
	}
end

post '/:service_space_url_name/admin/events/create/?' do
	if params[:location] == 'new'
		# this is a new location, we must create it!
		location = Location.create(params[:new_location].merge({
			:service_space_id => @space.id
		}))
		params[:location] = location.id
	end

	event = Event.new
75
	event.service_space_id = @space.id
Tyler Lemburg's avatar
Tyler Lemburg committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
	event.set_image_data(params)
	event.set_data(params)

	if params.has_key?('reserve_resource') && params['reserve_resource'] == 'on'
		# we need to create a reservation for the resource on the appropriate time
		Reservation.create(
			:resource_id => params[:resource],
			:event_id => event.id,
			:start_time => event.start_time,
			:end_time => event.end_time,
			:is_training => true,
			:user_id => nil
		)
	end

	# notify that it worked
	flash(:success, 'Event Created', "Your #{event.type.description}: #{event.title} has been created.")
	redirect @space.admin_events_href
end

get '/:service_space_url_name/admin/events/:event_id/edit/?' do
Tyler Lemburg's avatar
Tyler Lemburg committed
97
	@breadcrumbs << {:text => 'Admin Events', :href => @space.admin_events_href} << {text: 'Edit Event'}
Tyler Lemburg's avatar
Tyler Lemburg committed
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	event = Event.includes(:event_type, :location, :reservation => :resource).find_by(:id => params[:event_id], :service_space_id => @space.id)
	if event.nil?
		# that event does not exist
		flash(:danger, 'Not Found', 'That event does not exist')
		redirect @space.admin_events_href
	end

	erb :'admin/new_event', :layout => :fixed, :locals => {
		:event => event,
		:types => EventType.where(:service_space_id => @space.id).all,
		:locations => Location.where(:service_space_id => @space.id).all,
		:resources => Resource.where(:service_space_id => @space.id, :is_reservable => true).all
	}
end

post '/:service_space_url_name/admin/events/:event_id/edit/?' do
	event = Event.find_by(:id => params[:event_id], :service_space_id => @space.id)
	if event.nil?
		# that event does not exist
		flash(:danger, 'Not Found', 'That event does not exist')
		redirect @space.admin_events_href
	end

	if params[:location] == 'new'
		# this is a new location, we must create it!
		location = Location.create(params[:new_location].merge({
			:service_space_id => @space.id
		}))
		params[:location] = location.id
	end

	if params.checked?('remove_image')
		event.remove_image_data
	else
		event.set_image_data(params)
	end
	event.set_data(params)
	event.service_space_id = @space.id

	# check the resource reservation for this
	checked = params.checked?('reserve_resource')
	if event.has_reservation && checked
		# update the reservation
		event.reservation.update(
			:resource_id => params[:resource],
			:event_id => event.id,
			:start_time => event.start_time,
			:end_time => event.end_time,
			:is_training => true,
			:user_id => nil
		)
	elsif event.has_reservation && !checked
		# remove the reservation
		event.reservation.delete
	elsif !event.has_reservation && checked
		# create the reservation
		Reservation.create(
			:resource_id => params[:resource],
			:event_id => event.id,
			:start_time => event.start_time,
			:end_time => event.end_time,
			:is_training => true,
			:user_id => nil
		)
	end

	# notify that it worked
	flash(:success, 'Event Updated', "Your #{event.type.description}: #{event.title} has been updated.")
	redirect @space.admin_events_href
end

post '/:service_space_url_name/admin/events/:event_id/delete/?' do
	event = Event.find_by(:id => params[:event_id], :service_space_id => @space.id)
	if event.nil?
		# that event does not exist
		flash(:danger, 'Not Found', 'That event does not exist')
		redirect @space.admin_events_href
	end

	event.destroy

	flash(:success, 'Event Deleted', "Your event #{event.title} has been deleted. All signups on this event have also been removed, and if a reservation was attached, it also has been removed.")
	redirect @space.admin_events_href
end