calendar/app/controllers/calendars_controller.rb

183 lines
6.8 KiB
Ruby

class CalendarsController < ApplicationController
# GET /events
# GET /events.json
include ActionView::Helpers::AssetTagHelper
def index
page = Page.where(page_id: OrbitHelper.params[:page_id]).first
style_file = page.layout=='index3' ? '/assets/calendar_widget2' : '/assets/calendar_widget1'
cat_ids = page.categories
if cat_ids.include?('all')
cat_ids = []
end
@calendar_types = cat_ids.blank? ? CalendarType.all : CalendarType.where(:id.in => cat_ids)
@calendar_setting = CalendarSetting.first
{
"modes_info" => @calendar_setting.get_modes_info.map.with_index{|(trans, mode),i| {trans: trans, mode: mode, active_class: (i ==
2 ? 'active' : '')}},
"week_title" => @calendar_setting.week_title.map{|t| {'week_title'=> t}}.rotate(@calendar_setting.sunday_first ? 0 : 1),
"extras" => {
"page_id" => OrbitHelper.params[:page_id],
'widget_title' => page.name,
'widget_title_class' => page.name.blank? ? 'center' : '',
'calendar_title' => get_calendar_title,
'style_tag' => stylesheet_link_tag(style_file),
'default_column' => widget_default_column
}
}
end
def show
params = OrbitHelper.params
event = Event.find(params[:uid])
{
"page_id" => params[:page_id],
"event_date" => event.start.strftime("%Y-%m-%d")
}
end
def widget
part = OrbitHelper.get_current_widget
@calendar_setting = CalendarSetting.first
cat_ids = part.custom_array_field
@calendar_types = cat_ids.blank? ? CalendarType.all : CalendarType.where(:id.in => cat_ids)
extra_before_html = render_to_string(partial: 'calendars/calendar_variable', locals: {:@calendar_setting=>@calendar_setting,:@calendar_types=>@calendar_types})
{
"modes_info" => @calendar_setting.get_modes_info.map.with_index{|(trans, mode),i| {trans: trans, mode: mode, active_class: (i ==
2 ? 'active' : '')}},
"week_title" => @calendar_setting.week_title.map{|t| {'week_title'=> t}}.rotate(@calendar_setting.sunday_first ? 0 : 1),
"extras" => {
"subpart-id" => part.id.to_s,
"more_url" => OrbitHelper.widget_more_url,
'widget_title' => part.title,
'widget_title_class' => part.title.blank? ? 'center' : '',
'calendar_title' => get_calendar_title,
'default_column' => widget_default_column,
'extra_before_html' => extra_before_html
}
}
end
def events
if !(valid_timestamp?(params[:start]) && valid_timestamp?(params[:end]))
return render json: {}, status: :unprocessable_entity
end
page = Page.find_by(:page_id => params[:page_id]) rescue nil
events =[]
locale = params[:locale]||I18n.locale
locale = 'zh_tw' if locale == 'zh_cn'
if !page.nil?
calendar_types = page.categories
if calendar_types.include?('all')
calendar_types = []
end
tags = page.tags
if tags.include?('all')
tags = []
end
if params[:start].present? && params[:end].present?
sdt = Time.at(params[:start].to_i).utc
edt = Time.at(params[:end].to_i).utc
events = Event.with_categories(calendar_types).with_tags(tags).where("title_translations.#{locale}".to_sym.ne=>"").agenda_events(sdt,edt)
end
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: events.to_json }
end
end
def index_agenda
if !(valid_timestamp?(params[:unix_start]) && valid_timestamp?(params[:unix_end]))
return render json: {}, status: :unprocessable_entity
end
locale = params[:locale]||I18n.locale
locale = 'zh_tw' if locale == 'zh_cn'
I18n.with_locale(locale) do
if !params[:subpart_id].nil?
subpartid = params[:subpart_id]
page = Page.where(page_id: subpartid).first
calendar_types = page.categories rescue []
if calendar_types.include?('all')
calendar_types = []
end
tags = page.tags rescue []
if tags.include?('all')
tags = []
end
else
calendar_types = []
tags = []
end
if params[:unix_start].present? && params[:unix_end].present?
agenda_start = Time.at(params[:unix_start].to_i).utc
agenda_end = Time.at(params[:unix_end].to_i).utc
event = Event.where("title_translations.#{locale}".to_sym.ne=>"")
events = event.with_categories(calendar_types).with_tags(tags).agenda_events(agenda_start,agenda_end).sort_by{|e| e[:start]}
end
render json: {"events" => events,"calendar_title"=>get_calendar_title(params[:month_start] ? Time.at(params[:month_start].to_i).utc : Time.at(params[:unix_start].to_i))}.to_json({"frontend" => true})
end
end
def agenda
if !(valid_timestamp?(params[:unix_start]) && valid_timestamp?(params[:unix_end]))
return render json: {}, status: :unprocessable_entity
end
locale = params[:locale]||I18n.locale
locale = 'zh_tw' if locale == 'zh_cn'
I18n.with_locale(locale) do
if !params[:subpart_id].nil?
subpartid = params[:subpart_id]
widget = SubPart.find(subpartid)
calendar_types = widget.custom_array_field
tags = widget.tags
if tags.include?('all')
tags = []
end
else
calendar_types = []
tags = []
end
if params[:unix_start].present? && params[:unix_end].present?
agenda_start = Time.at(params[:unix_start].to_i).utc
agenda_end = Time.at(params[:unix_end].to_i).utc
event = Event.where("title_translations.#{locale}".to_sym.ne=>"")
events = event.with_categories(calendar_types).with_tags(tags).agenda_events(agenda_start,agenda_end).sort_by{|e| e[:start]}
end
render json: {"events" => events,"calendar_title"=>get_calendar_title(params[:month_start] ? Time.at(params[:month_start].to_i).utc : Time.at(params[:unix_start].to_i))}.to_json({"frontend" => true})
end
end
def get_calendar_title(now_date=nil)
now_date = Time.now.utc if now_date.nil?
month_name = I18n.locale.to_s=='zh_tw' ? now_date.month : I18n.t("calendar.month_name.#{now_date.month}")
I18n.t("calendar.calendar_title",year: now_date.year,month: month_name)
end
def widget_default_column
(1..5).collect do |i|
"<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>"
end.join("\n")
end
private
def valid_timestamp?(number)
begin
# Ensure the input is numeric and within a practical range
number = Integer(number) rescue Float(number)
min_timestamp = Time.new(1900, 1, 1).to_i
max_timestamp = Time.new(3000, 12, 31).to_i
number >= min_timestamp && number <= max_timestamp
rescue ArgumentError, TypeError
false
end
end
end