diff --git a/app/assets/javascripts/calendar_frontend.js b/app/assets/javascripts/calendar_frontend.js index a3829e2..696cfea 100644 --- a/app/assets/javascripts/calendar_frontend.js +++ b/app/assets/javascripts/calendar_frontend.js @@ -31,7 +31,7 @@ var Calendar = function(dom,page_id,event_date=''){ c.calendar.fullCalendar({ editable: false, selectable: false, - events: "/xhr/calendars/events?page_id="+c.page_id, + events: "/xhr/calendars/events?page_id="+c.page_id+"&locale="+I18n.locale, header: false, default: dview, height: $("body").height() - 141, diff --git a/app/assets/javascripts/calendar_widget.js b/app/assets/javascripts/calendar_widget.js index 4b1b5d2..8e36a0a 100644 --- a/app/assets/javascripts/calendar_widget.js +++ b/app/assets/javascripts/calendar_widget.js @@ -317,7 +317,7 @@ usd_target = Math.round(first_target_day/1000), ued_target = Math.round(last_target_day/1000); $.ajax({ - url : (index_flag ? "/xhr/calendars/index_agenda" : "/xhr/calendars/agenda"), + url : (index_flag ? "/xhr/calendars/index_agenda" : "/xhr/calendars/agenda") + "?locale=" + I18n.locale, data : {"month_start" : usd,"unix_start" : usd_target, "unix_end" : ued_target, "subpart_id" : subpartid, "locale" : $('html').attr('lang')}, dataType : "json", type : "get" diff --git a/app/assets/javascripts/calendar_widget2.js b/app/assets/javascripts/calendar_widget2.js index 49e6dde..244a644 100644 --- a/app/assets/javascripts/calendar_widget2.js +++ b/app/assets/javascripts/calendar_widget2.js @@ -457,7 +457,7 @@ usd_target = Math.round(first_target_day/1000), ued_target = Math.round(last_target_day/1000); $.ajax({ - url : (index_flag ? "/xhr/calendars/index_agenda" : "/xhr/calendars/agenda"), + url : (index_flag ? "/xhr/calendars/index_agenda" : "/xhr/calendars/agenda") + "?locale=" + I18n.locale, data : {"month_start" : usd,"unix_start" : usd_target, "unix_end" : ued_target, "subpart_id" : subpartid, "locale" : $('html').attr('lang')}, dataType : "json", type : "get" diff --git a/app/controllers/admin/calendars_controller.rb b/app/controllers/admin/calendars_controller.rb index 3d6cda9..3792799 100644 --- a/app/controllers/admin/calendars_controller.rb +++ b/app/controllers/admin/calendars_controller.rb @@ -136,8 +136,6 @@ class Admin::CalendarsController < OrbitAdminController 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 - p1['title_translations'] = @event['title_translations'].merge({I18n.locale.to_s =>p1['title']}) - p1['note_translations'] = @event['title_translations'].merge({I18n.locale.to_s =>p1['note']}) if @event.update_attributes(p1) e = @event.to_json e = JSON.parse(e) diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 5929d3e..244bed0 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -44,11 +44,13 @@ class CalendarsController < ApplicationController 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' if !page.nil? 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.monthly_event(sdt,edt).convert_front+Event.recurring_event(sdt,edt) + events = Event.where("title_translations.#{locale}".to_sym.ne=>"").monthly_event(sdt,edt).convert_front+Event.recurring_event(sdt,edt) end end respond_to do |format| @@ -59,7 +61,9 @@ class CalendarsController < ApplicationController def index_agenda - I18n.with_locale(params[:locale]||I18n.locale) do + 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 @@ -70,10 +74,11 @@ class CalendarsController < ApplicationController if params[:unix_start].present? && params[:unix_end].present? 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=>"") if !calendar_types.blank? - events = Event.where(:calendar_type_id.in => calendar_types).agenda_events(agenda_start,agenda_end) + events = event.where(:calendar_type_id.in => calendar_types).agenda_events(agenda_start,agenda_end) else - events = Event.agenda_events(agenda_start,agenda_end) + events = event.agenda_events(agenda_start,agenda_end) end end render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true}) @@ -81,7 +86,9 @@ class CalendarsController < ApplicationController end def agenda - I18n.with_locale(params[:locale]||I18n.locale) do + 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) @@ -92,10 +99,11 @@ class CalendarsController < ApplicationController if params[:unix_start].present? && params[:unix_end].present? 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=>"") if !calendar_types.blank? - events = Event.where(:calendar_type_id.in => calendar_types).agenda_events(agenda_start,agenda_end) + events = event.where(:calendar_type_id.in => calendar_types).agenda_events(agenda_start,agenda_end) else - events = Event.agenda_events(agenda_start,agenda_end) + events = event.agenda_events(agenda_start,agenda_end) end end render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true}) diff --git a/app/models/event.rb b/app/models/event.rb index c2973a1..13a600e 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -25,12 +25,14 @@ class Event field :model_tags,type: Array,default: [] belongs_to :calendar_type field :url + field :url_translations,type: Hash,default: {} def get_module_url page = !self.model_page_id.blank? ? Page.find(self.model_page_id): Page.where(:module => self.module_key).where(:categories.in => Array(self.model_cat)+[[]],:tags.in=>Array(self.model_tags)+[[]]).first page.nil? ? '' : (page.url+'/'+eval(self.model_class).where(:id=>self.model_id).first.to_calendar_param) end def url_to_fronted - self.url.blank? ? (self.model_class.blank? ? '' : (self.get_module_url rescue '')) : self.url + tmp_url = self.url + tmp_url.blank? ? (self.model_class.blank? ? '' : (self.get_module_url rescue '')) : tmp_url end attr_accessor :agenda_start, :agenda_end, :get_agenda_events @@ -57,16 +59,25 @@ class Event end end end + before_save do + self['title'] = self.title + self['note'] = self.note + self['url'] = self.url + end ######################################## validates_presence_of :title, :message => "Please fill the title of the Event", :if => lambda { self['title_translations'].blank? } validates_presence_of :title_translations, :message => "Please fill the title of the Event", :if => lambda { self['title'].blank? } - def title - tp = self['title_translations'][I18n.locale] rescue nil - tp.blank? ? self['title'] : tp + def title(locale=I18n.locale) + tp = self['title_translations'][locale] rescue nil + tp.nil? ? self['title'] : tp end - def note - tp = self['note_translations'][I18n.locale] - tp.blank? ? self['note'] : tp + def note(locale=I18n.locale) + tp = self['note_translations'][locale] + tp.nil? ? self['note'] : tp + end + def url(locale=I18n.locale) + tp = self['url_translations'][locale] + tp.nil? ? self['url'] : tp end def self.with_categories(cat_ids=[]) if cat_ids.blank? diff --git a/app/views/admin/calendars/_form.html.erb b/app/views/admin/calendars/_form.html.erb index 8c05c6f..bc7c17f 100644 --- a/app/views/admin/calendars/_form.html.erb +++ b/app/views/admin/calendars/_form.html.erb @@ -51,24 +51,69 @@ <% end %> +<% locales = Site.first.in_use_locales rescue I18n.available_locales + locales = [I18n.locale] + (locales - [I18n.locale]) +%>
<%= f.label t("title"), :class=>"control-label" %>
-
<%= @event.title.to_s.html_safe %>
- <%= f.hidden_field :title %> +
+ <% locales.each_with_index do |locale, i| %> + <%= f.fields_for :title_translations, f.object do |f| %> +
" id="title_translations_<%= locale %>"> + <% title = f.object.title(locale) %> +
<%= title.to_s.html_safe %>
+ <%= hidden_field_tag "#{f.object_name}[#{locale}]", title %> +
+ <% end %> + <% end %> +
+
+ <% locales.each_with_index do |locale, i| %> + " href="#title_translations_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %> + <% end %> +
<%= f.label t("calendar.note"), :class=>"control-label" %>
-
<%= @event.note.to_s.html_safe %>
- <%= f.hidden_field :note %> +
+ <% locales.each_with_index do |locale, i| %> + <%= f.fields_for :note_translations, f.object do |f| %> +
" id="note_translations_<%= locale %>"> + <% note = f.object.note(locale) %> +
<%= note.to_s.html_safe %>
+ <%= hidden_field_tag "#{f.object_name}[#{locale}]", note %> +
+ <% end %> + <% end %> +
+
+ <% locales.each_with_index do |locale, i| %> + " href="#note_translations_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %> + <% end %> +
<%= f.label t("url"), :class=>"control-label" %>
- <%= f.text_field :url %> +
+ <% locales.each_with_index do |locale, i| %> + <%= f.fields_for :url_translations, f.object do |f| %> +
" id="note_translations_<%= locale %>"> + <% url = f.object.url(locale) %> + <%= text_field_tag "#{f.object_name}[#{locale}]", url %> +
+ <% end %> + <% end %> +
+
+ <% locales.each_with_index do |locale, i| %> + " href="#url_translations_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %> + <% end %> +
@@ -152,13 +197,12 @@ } function before_submit(ele){ var self = $(ele).parents('form') - var length = self.find('*[contenteditable="true"]').length - for (var i=0;i[nil,{}]).each do |e| + e.title_translations = locales.map{|l| [l.to_s, e[:title]]}.to_h + e.save + end + Event.where(:note_translations.in=>[nil,{}]).each do |e| + e.note_translations = locales.map{|l| [l.to_s, e[:note]]}.to_h + e.save + end + Event.where(:url_translations=>nil).each do |e| + e.url_translations = locales.map{|l| [l.to_s, e[:url].to_s]}.to_h + e.save + end + if update_flag + s = Site.first + s.tmp_flags << 'cf1' + s.save + end + puts "Calendar fix!" + end + end + end scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do get "/calendars/agenda" => "calendars#agenda"