2014-06-22 22:06:12 +02:00
|
|
|
class EventsController < ApplicationController
|
2014-05-25 23:59:03 +02:00
|
|
|
before_action :set_event, only: [:show, :edit, :update, :cancel, :destroy]
|
|
|
|
before_action :check_secret, only: [:edit, :update, :cancel, :destroy]
|
|
|
|
before_filter :set_mailer_host
|
2014-01-01 21:26:35 +01:00
|
|
|
|
2013-12-28 23:45:13 +01:00
|
|
|
def index
|
2014-07-27 01:50:22 +02:00
|
|
|
# So we can use the search form from the central calendar page
|
|
|
|
@search = Lug.search params[:q]
|
|
|
|
|
2014-07-01 23:00:05 +02:00
|
|
|
@events = Event.moderated
|
2014-07-01 15:50:39 +02:00
|
|
|
if params[:region] && params[:region].present? && params[:region] != 'all'
|
|
|
|
@events = @events.region params[:region]
|
2014-01-01 22:38:56 +01:00
|
|
|
end
|
2014-07-01 15:50:39 +02:00
|
|
|
@events = @events.tag(params[:tag]) if params[:tag]
|
2014-01-01 22:38:56 +01:00
|
|
|
|
2013-12-29 20:42:00 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
2014-06-27 00:52:47 +02:00
|
|
|
if params[:year]
|
2013-12-29 20:42:00 +01:00
|
|
|
# Whole year calendar
|
2014-06-27 00:52:47 +02:00
|
|
|
@events = @events.year params[:year].to_i
|
2013-12-29 20:42:00 +01:00
|
|
|
else
|
2014-06-27 00:52:47 +02:00
|
|
|
if params[:start_date]
|
|
|
|
start_date = Date.parse params[:start_date]
|
|
|
|
else
|
|
|
|
start_date = Date.today
|
|
|
|
end
|
|
|
|
@events = @events.month start_date.change day: 1
|
2013-12-29 20:42:00 +01:00
|
|
|
end
|
2014-07-18 13:20:11 +02:00
|
|
|
|
2014-07-20 17:19:19 +02:00
|
|
|
set_meta_tags description: "#{t('layouts.application.subtitle')} - #{start_date ? t('date.month_names')[start_date.month] : ''} #{start_date ? start_date.year.to_s : params[:year]} - #{t '.nb_events', count: @events.size}",
|
2014-07-18 13:20:11 +02:00
|
|
|
keywords: @events.pluck(:tags)
|
|
|
|
.join(' ')
|
|
|
|
.split
|
|
|
|
.group_by { |i| i }
|
|
|
|
.reject { |k, v| v.size < 2 }
|
|
|
|
.collect { |k, v| k },
|
|
|
|
DC: {
|
|
|
|
title: t('layouts.application.title'),
|
|
|
|
subject: t('layouts.application.subtitle'),
|
|
|
|
date: start_date.to_s
|
|
|
|
}
|
2013-12-29 20:42:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
format.rss {
|
2014-05-10 11:38:39 +02:00
|
|
|
@events = @events.future_30.includes(:related_city)
|
2014-01-02 00:28:11 +01:00
|
|
|
@events = @events.limit params[:daylimit] if params[:daylimit]
|
2013-12-29 20:42:00 +01:00
|
|
|
}
|
2014-01-04 11:34:50 +01:00
|
|
|
|
|
|
|
format.ics {
|
|
|
|
@events = @events.where('start_time > ?', 360.days.ago).order :id
|
|
|
|
}
|
2014-07-19 09:03:08 +02:00
|
|
|
|
|
|
|
format.xml {
|
|
|
|
@events = @events.order :id
|
|
|
|
}
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-22 22:06:12 +02:00
|
|
|
# GET /users/new
|
|
|
|
def new
|
|
|
|
@event = Event.new
|
2014-07-16 22:29:07 +02:00
|
|
|
@event.start_time ||= Time.now.change(min: 0) + 1.day + 1.hour
|
|
|
|
@event.end_time ||= Time.now.change(min: 0) + 1.day + 2.hour
|
2014-06-22 22:06:12 +02:00
|
|
|
end
|
|
|
|
|
2014-01-01 21:26:35 +01:00
|
|
|
# POST /events
|
|
|
|
# POST /events.json
|
|
|
|
def create
|
2014-07-01 15:50:39 +02:00
|
|
|
@event = Event.new event_params
|
2014-01-01 21:26:35 +01:00
|
|
|
|
2014-01-10 16:35:58 +01:00
|
|
|
if params[:visu]
|
2014-05-31 23:13:43 +02:00
|
|
|
@event.valid?
|
2014-01-10 16:35:58 +01:00
|
|
|
render action: :new
|
2014-01-01 21:26:35 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @event.save
|
2014-05-25 23:59:03 +02:00
|
|
|
# Send an event creation mail to its author
|
|
|
|
EventMailer.create(@event).deliver
|
2014-06-21 18:53:05 +02:00
|
|
|
# Send a mail to moderators
|
|
|
|
ModerationMailer.create(@event).deliver
|
2014-05-25 23:59:03 +02:00
|
|
|
|
2014-06-26 21:55:23 +02:00
|
|
|
format.html { redirect_to :root, notice: t('.ok') }
|
2014-01-01 21:26:35 +01:00
|
|
|
format.json { render action: 'show', status: :created, location: @event }
|
|
|
|
else
|
|
|
|
format.html { render action: 'new' }
|
|
|
|
format.json { render json: @event.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PATCH/PUT /events/1
|
|
|
|
# PATCH/PUT /events/1.json
|
|
|
|
def update
|
2014-07-20 21:46:28 +02:00
|
|
|
olderEvent = Event.new @event.attributes
|
2014-01-06 11:22:39 +01:00
|
|
|
if params[:visu]
|
2014-05-25 23:59:03 +02:00
|
|
|
@event.attributes = event_params
|
2014-05-31 23:13:43 +02:00
|
|
|
@event.valid?
|
2014-01-10 16:35:58 +01:00
|
|
|
render action: :edit
|
2014-01-06 11:22:39 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-01-01 21:26:35 +01:00
|
|
|
respond_to do |format|
|
2014-07-01 15:50:39 +02:00
|
|
|
if @event.update event_params
|
2014-07-01 21:55:38 +02:00
|
|
|
# Send an update mail to moderators
|
2014-07-20 21:46:28 +02:00
|
|
|
ModerationMailer.update(olderEvent, @event, nil).deliver
|
2014-07-01 21:55:38 +02:00
|
|
|
|
2014-07-01 21:11:39 +02:00
|
|
|
format.html { redirect_to :root, notice: t('.ok') }
|
2014-01-01 21:26:35 +01:00
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
|
|
|
format.html { render action: 'edit' }
|
|
|
|
format.json { render json: @event.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-22 22:06:12 +02:00
|
|
|
# DELETE /events/1
|
|
|
|
# DELETE /events/1.json
|
|
|
|
def destroy
|
|
|
|
@event.destroy
|
|
|
|
respond_to do |format|
|
2014-07-01 15:50:39 +02:00
|
|
|
format.html { redirect_to events_url, notice: t('.ok') }
|
2014-06-22 22:06:12 +02:00
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-28 23:45:13 +01:00
|
|
|
private
|
2014-01-01 21:26:35 +01:00
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_event
|
2014-06-26 21:31:17 +02:00
|
|
|
if params[:secret].present?
|
|
|
|
@event = Event.where secret: params[:secret]
|
2014-05-25 23:59:03 +02:00
|
|
|
else
|
2014-07-01 23:00:05 +02:00
|
|
|
@event = Event.moderated
|
2014-05-25 23:59:03 +02:00
|
|
|
end
|
2014-06-26 21:31:17 +02:00
|
|
|
@event = @event.find params[:id]
|
2014-07-18 13:20:11 +02:00
|
|
|
|
|
|
|
set_meta_tags title: @event.title,
|
|
|
|
description: @event.description,
|
|
|
|
keywords: @event.tags,
|
|
|
|
DC: {
|
|
|
|
title: @event.title,
|
|
|
|
date: @event.start_time.to_s
|
|
|
|
},
|
|
|
|
geo: {
|
|
|
|
region: @event.related_region,
|
|
|
|
placename: @event.city,
|
|
|
|
position: "#{@event.related_city.try :latitude};#{@event.related_city.try :longitude}"
|
|
|
|
}
|
2014-01-01 21:26:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Never trust parameters from the scary internet, only allow the white list through.
|
|
|
|
def event_params
|
2014-01-06 11:22:39 +01:00
|
|
|
params.require(:event)
|
2014-07-01 15:50:39 +02:00
|
|
|
.permit :title, :start_time, :end_time, :description, :city, :region, :locality, :url, :contact, :submitter, :tags
|
2014-01-01 21:26:35 +01:00
|
|
|
end
|
2014-05-25 23:59:03 +02:00
|
|
|
|
|
|
|
def check_secret
|
2014-06-26 21:55:23 +02:00
|
|
|
if params[:secret] != @event.secret
|
|
|
|
redirect_to :root, notice: t(:forbidden, scope: [:events, :edit])
|
2014-05-25 23:59:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Useful to manage absolute url in mails
|
|
|
|
def set_mailer_host
|
|
|
|
ActionMailer::Base.default_url_options[:host] = request.host_with_port
|
|
|
|
end
|
2013-12-28 23:45:13 +01:00
|
|
|
end
|