calendar/app/controllers/admin/calendars_controller.rb

219 lines
6.4 KiB
Ruby

class Admin::CalendarsController < OrbitAdminController
# GET /events
# GET /events.json
def index
@calendar_types = CalendarType.all
@calendar_setting = CalendarSetting.first
@events = []
tags = @module_app.tags
categories = CalendarType.all
@filter_fields = {
:category=>categories.map{|c| {:title=>(c.title.blank? ? " " : c.title), :id=>c.id}},
:tags=>tags.map{|tag| {:title=>(tag.name.blank? ? " " : tag.name), :id=>tag.id}}
}
if params[:start].present? && params[:end].present?
sdt = Time.at(params[:start].to_i)
edt = Time.at(params[:end].to_i)
allevents = Event.with_categories(filters("category"))
.with_tags(filters("tag")).agenda_events(sdt,edt, params[:display_hide] == 'true')
allevents.each do |e|
event_json = e.as_json
event_json["can_edit"] = can_edit_or_delete_event?(e)
@events << event_json
end
end
if request.xhr?
respond_to do |format|
format.html { render :partial => "index" }
format.json { render json: @events.to_json }
end
else
respond_to do |format|
format.html { render "index" }
format.json { render json: @events.to_json }
end
end
end
def get_single_event
@events = []
if params[:start].present? && params[:end].present?
sdt = Time.at(params[:start].to_i)
edt = Time.at(params[:end].to_i)
allevents = Event.where(:id=>params[:id]).agenda_events(sdt,edt, params[:display_hide] == 'true')
allevents.each do |e|
event_json = e.as_json
event_json["can_edit"] = can_edit_or_delete_event?(e)
@events << event_json
end
end
render :json => @events.to_json
end
# GET /events/1
# GET /events/1.json
def show
@event = Event.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @event }
end
end
def agenda
agenda_start = Time.at(params[:unix_start].to_i).utc
agenda_end = Time.at(params[:unix_end].to_i).utc
@events = Event.agenda_events(agenda_start,agenda_end).sort_by{|e| e[:start]}
# re = Event.recurring_event(Time.at(params[:unix_start].to_i), Time.at(params[:unix_end].to_i))
# @events = @events.inject(re, :<<)
render :json=>@events.to_json
end
# GET /events/new
# GET /events/new.json
def new
@event = Event.new(start: params[:startDate],end: params[:endDate])
categories = user_authenticated_categories rescue []
if categories.first == "all"
@categories = CalendarType.all
else
@categories = CalendarType.where(:category_id.in => categories) rescue []
end
@end_d_t = @event.end
@start_d_t = @event.start
@all_day = false;
@recurring = false;
if params
case params[:allDay]
when "true"
@all_day = true
@event.end = @event.end - 1.day
@end_d_t = @event.end
when "false"
@all_day = false
case params[:recurring]
when "true"
@recurring = true
when "false"
@recurring = false
end
end
end
render :layout => false
end
# GET /events/1/edit
def edit
@event = Event.find(params[:id])
if params[:hide]
hide_mode = (params[:hide] == 'true')
start = params[:start]
if hide_mode
@event.hide_start << start
else
@event.hide_start.delete(start)
end
@event.save
render :json => {:success=>true}.to_json
else
categories = user_authenticated_categories rescue []
if categories.first == "all"
@categories = CalendarType.all
else
@categories = CalendarType.where(:category_id.in => categories) rescue []
end
@end_d_t = @event.end.strftime("%Y/%m/%d %H:%M").to_s
@start_d_t = @event.start.strftime("%Y/%m/%d %H:%M").to_s
@all_day = @event.all_day
@recurring = @event.recurring
render :layout => false
end
end
# POST /events
# POST /events.json
def create
p = event_page_params
p["create_user_id"] = current_user.id
p["update_user_id"] = current_user.id
@event = Event.new(p)
if @event.present? && @event.save
event_json = @event.as_json
event_json["can_edit"] = true
render json: event_json.to_json
else
respond_to do |format|
format.html { render action: "new" }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# PUT /events/1
# PUT /events/1.json
def update
@event = Event.find(params[:id])
p1 = event_page_params
p1["update_user_id"] = current_user.id
bulletin = @event.bulletin
if !bulletin.nil?
bulletin.update_attributes(calendar_start_date: p1[:start],calendar_end_date: p1[:end],calendar_all_day: p1[:all_day],calendar_type_id: p1[:calendar_type_id])
end
if @event.update_attributes(p1)
e = @event.as_json({}, true)
e["can_edit"] = true
render json: e.to_json
else
respond_to do |format|
format.html { render action: "edit" }
format.json { render json: @event.errors, status: :unprocessable_entity }
#format.js
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
@event = Event.find(params[:id])
bulletin = @event.bulletin
if !bulletin.nil?
bulletin.update_attributes(event_id: nil,add_to_calendar: '0')
end
@event.destroy
render :json => {"success"=>true}.to_json
# respond_to do |format|
# format.html { redirect_to events }
# format.json { head :no_content }
# end
end
def calendar_setting
@calendar_setting = CalendarSetting.first
end
def update_calendar_setting
@calendar_setting = CalendarSetting.first
@calendar_setting.update_attributes(params.require(:calendar_setting).permit!)
redirect_to calendar_setting_admin_calendars_path
end
private
def event_page_params
params.require(:event).permit!
end
def can_edit_or_delete_event?(obj)
create_user = obj.create_user_id.to_s rescue nil
if @user_authenticated_categories.first == "all"
return true
elsif @current_user_is_sub_manager && !create_user.nil?
create_user == current_user.id.to_s
else
@user_authenticated_categories.include?obj.calendar_type.category_id rescue (current_user.is_manager?(@module_app) rescue false)
end
end
end