calendar/app/controllers/admin/calendars_controller.rb

196 lines
5.7 KiB
Ruby

class Admin::CalendarsController < OrbitAdminController
# GET /events
# GET /events.json
def index
@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)
@monthly_events = Event.monthly_event(sdt,edt).with_categories(filters("category"))
.with_tags(filters("tag")).convert_front
@re = Event.with_categories(filters("category")).with_tags(filters("tag")).recurring_event(sdt,edt)
allevents = @monthly_events.inject(@re, :<<)
events = allevents.to_json
events = JSON.parse(events)
events.each_with_index do |e,i|
e["can_edit"] = can_edit_or_delete_event?(allevents[i])
@events << e
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
# 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).to_s
agenda_end = Time.at(params[:unix_end].to_i).to_s
@events = Event.agenda_events(agenda_start,agenda_end)
# 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 = params[:endDate]
@start_d_t = params[:startDate]
@all_day = false;
@recurring = false;
if params
case params[:allDay]
when "true"
@all_day = true
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])
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
# 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.to_json
e = JSON.parse(e)
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