calendar/app/controllers/calendars_controller.rb

157 lines
5.2 KiB
Ruby
Raw Normal View History

2014-12-16 11:40:15 +00:00
class CalendarsController < ApplicationController
# GET /events
# GET /events.json
include ActionView::Helpers::AssetTagHelper
2014-12-16 11:40:15 +00:00
def index
page = Page.where(page_id: OrbitHelper.params[:page_id]).first
2020-08-15 13:51:32 +00:00
style_file = page.layout=='index3' ? '/assets/calendar_widget2' : '/assets/calendar_widget1'
@calendar_setting = CalendarSetting.first
2014-12-16 11:40:15 +00:00
{
"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}},
2014-12-16 11:40:15 +00:00
"extras" => {
"page_id" => OrbitHelper.params[:page_id],
'widget_title' => page.name,
2021-09-06 05:14:27 +00:00
'widget_title_class' => page.name.blank? ? 'center' : '',
'calendar_title' => get_calendar_title,
2021-09-06 05:14:27 +00:00
'style_tag' => stylesheet_link_tag(style_file),
'default_column' => widget_default_column
2014-12-16 11:40:15 +00:00
}
}
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
2015-06-01 08:03:45 +00:00
def widget
part = OrbitHelper.get_current_widget
@calendar_setting = CalendarSetting.first
2022-07-13 07:00:21 +00:00
begin
extra_before_html = render_to_string(partial: 'calendars/calendar_variable', locals: {:@calendar_setting=>@calendar_setting})
rescue => e
extra_before_html = ""
puts e, e.backtrace
end
2015-06-01 08:03:45 +00:00
{
"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}},
2015-06-03 09:38:31 +00:00
"extras" => {
"subpart-id" => part.id.to_s,
"more_url" => OrbitHelper.widget_more_url,
'widget_title' => part.title,
2021-09-06 05:14:27 +00:00
'widget_title_class' => part.title.blank? ? 'center' : '',
'calendar_title' => get_calendar_title,
2022-07-13 07:00:21 +00:00
'default_column' => widget_default_column,
'extra_before_html' => extra_before_html
2015-06-03 09:38:31 +00:00
}
2015-06-01 08:03:45 +00:00
}
end
2014-12-16 11:40:15 +00:00
def events
page = Page.find_by(:page_id => params[:page_id]) rescue nil
events =[]
locale = params[:locale]||I18n.locale
locale = 'zh_tw' if locale == 'zh_cn'
2014-12-16 11:40:15 +00:00
if !page.nil?
calendar_types = page.categories
if calendar_types.include?('all')
calendar_types = []
end
2014-12-16 11:40:15 +00:00
if params[:start].present? && params[:end].present?
2021-08-26 10:57:42 +00:00
sdt = Time.at(params[:start].to_i).utc
edt = Time.at(params[:end].to_i).utc
events = Event.with_categories(calendar_types).where("title_translations.#{locale}".to_sym.ne=>"").monthly_event(sdt,edt).convert_front+Event.recurring_event(sdt,edt)
2014-12-16 11:40:15 +00:00
end
2020-08-08 12:33:26 +00:00
end
2014-12-16 11:40:15 +00:00
respond_to do |format|
format.html # index.html.erb
format.json { render json: events.to_json }
end
end
def index_agenda
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
2022-07-11 03:03:14 +00:00
if calendar_types.include?('all')
calendar_types = []
end
else
calendar_types = []
end
if params[:unix_start].present? && params[:unix_end].present?
2021-08-26 10:57:42 +00:00
agenda_start = Time.at(params[:unix_start].to_i).utc.to_s
agenda_end = Time.at(params[:unix_end].to_i).utc.to_s
event = Event.where("title_translations.#{locale}".to_sym.ne=>"")
events = event.with_categories(calendar_types).agenda_events(agenda_start,agenda_end)
end
render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true})
end
end
def agenda
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
2014-12-16 11:40:15 +00:00
else
calendar_types = []
2014-12-16 11:40:15 +00:00
end
if params[:unix_start].present? && params[:unix_end].present?
2021-08-26 10:57:42 +00:00
agenda_start = Time.at(params[:unix_start].to_i).utc.to_s
agenda_end = Time.at(params[:unix_end].to_i).utc.to_s
event = Event.where("title_translations.#{locale}".to_sym.ne=>"")
events = event.with_categories(calendar_types).agenda_events(agenda_start,agenda_end)
end
render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true})
end
end
def get_calendar_title(now_date=nil)
2021-08-26 10:57:42 +00:00
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)
2014-12-16 11:40:15 +00:00
end
2021-09-06 05:14:27 +00:00
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
end