Change url to localize field.

Fix title, note, url localize data missing.
Update form page to enable input localize data.
This commit is contained in:
BoHung Chiu 2022-06-26 19:21:51 +08:00
parent 36e1725fab
commit 6ce0270d0b
8 changed files with 118 additions and 31 deletions

View File

@ -31,7 +31,7 @@ var Calendar = function(dom,page_id,event_date=''){
c.calendar.fullCalendar({ c.calendar.fullCalendar({
editable: false, editable: false,
selectable: 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, header: false,
default: dview, default: dview,
height: $("body").height() - 141, height: $("body").height() - 141,

View File

@ -317,7 +317,7 @@
usd_target = Math.round(first_target_day/1000), usd_target = Math.round(first_target_day/1000),
ued_target = Math.round(last_target_day/1000); ued_target = Math.round(last_target_day/1000);
$.ajax({ $.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')}, data : {"month_start" : usd,"unix_start" : usd_target, "unix_end" : ued_target, "subpart_id" : subpartid, "locale" : $('html').attr('lang')},
dataType : "json", dataType : "json",
type : "get" type : "get"

View File

@ -457,7 +457,7 @@
usd_target = Math.round(first_target_day/1000), usd_target = Math.round(first_target_day/1000),
ued_target = Math.round(last_target_day/1000); ued_target = Math.round(last_target_day/1000);
$.ajax({ $.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')}, data : {"month_start" : usd,"unix_start" : usd_target, "unix_end" : ued_target, "subpart_id" : subpartid, "locale" : $('html').attr('lang')},
dataType : "json", dataType : "json",
type : "get" type : "get"

View File

@ -136,8 +136,6 @@ class Admin::CalendarsController < OrbitAdminController
if !bulletin.nil? 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]) 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 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) if @event.update_attributes(p1)
e = @event.to_json e = @event.to_json
e = JSON.parse(e) e = JSON.parse(e)

View File

@ -44,11 +44,13 @@ class CalendarsController < ApplicationController
def events def events
page = Page.find_by(:page_id => params[:page_id]) rescue nil page = Page.find_by(:page_id => params[:page_id]) rescue nil
events =[] events =[]
locale = params[:locale]||I18n.locale
locale = 'zh_tw' if locale == 'zh_cn'
if !page.nil? if !page.nil?
if params[:start].present? && params[:end].present? if params[:start].present? && params[:end].present?
sdt = Time.at(params[:start].to_i).utc sdt = Time.at(params[:start].to_i).utc
edt = Time.at(params[:end].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
end end
respond_to do |format| respond_to do |format|
@ -59,7 +61,9 @@ class CalendarsController < ApplicationController
def index_agenda 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? if !params[:subpart_id].nil?
subpartid = params[:subpart_id] subpartid = params[:subpart_id]
page = Page.where(page_id: subpartid).first page = Page.where(page_id: subpartid).first
@ -70,10 +74,11 @@ class CalendarsController < ApplicationController
if params[:unix_start].present? && params[:unix_end].present? if params[:unix_start].present? && params[:unix_end].present?
agenda_start = Time.at(params[:unix_start].to_i).utc.to_s agenda_start = Time.at(params[:unix_start].to_i).utc.to_s
agenda_end = Time.at(params[:unix_end].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? 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 else
events = Event.agenda_events(agenda_start,agenda_end) events = event.agenda_events(agenda_start,agenda_end)
end end
end end
render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true}) 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 end
def agenda 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? if !params[:subpart_id].nil?
subpartid = params[:subpart_id] subpartid = params[:subpart_id]
widget = SubPart.find(subpartid) widget = SubPart.find(subpartid)
@ -92,10 +99,11 @@ class CalendarsController < ApplicationController
if params[:unix_start].present? && params[:unix_end].present? if params[:unix_start].present? && params[:unix_end].present?
agenda_start = Time.at(params[:unix_start].to_i).utc.to_s agenda_start = Time.at(params[:unix_start].to_i).utc.to_s
agenda_end = Time.at(params[:unix_end].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? 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 else
events = Event.agenda_events(agenda_start,agenda_end) events = event.agenda_events(agenda_start,agenda_end)
end end
end end
render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true}) render json: {"events" => events,"calendar_title"=>get_calendar_title(Time.at(params[:month_start].to_i).utc)}.to_json({"frontend" => true})

View File

@ -25,12 +25,14 @@ class Event
field :model_tags,type: Array,default: [] field :model_tags,type: Array,default: []
belongs_to :calendar_type belongs_to :calendar_type
field :url field :url
field :url_translations,type: Hash,default: {}
def get_module_url 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 = !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) page.nil? ? '' : (page.url+'/'+eval(self.model_class).where(:id=>self.model_id).first.to_calendar_param)
end end
def url_to_fronted 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 end
attr_accessor :agenda_start, :agenda_end, :get_agenda_events attr_accessor :agenda_start, :agenda_end, :get_agenda_events
@ -57,16 +59,25 @@ class Event
end end
end 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, :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? } validates_presence_of :title_translations, :message => "Please fill the title of the Event", :if => lambda { self['title'].blank? }
def title def title(locale=I18n.locale)
tp = self['title_translations'][I18n.locale] rescue nil tp = self['title_translations'][locale] rescue nil
tp.blank? ? self['title'] : tp tp.nil? ? self['title'] : tp
end end
def note def note(locale=I18n.locale)
tp = self['note_translations'][I18n.locale] tp = self['note_translations'][locale]
tp.blank? ? self['note'] : tp tp.nil? ? self['note'] : tp
end
def url(locale=I18n.locale)
tp = self['url_translations'][locale]
tp.nil? ? self['url'] : tp
end end
def self.with_categories(cat_ids=[]) def self.with_categories(cat_ids=[])
if cat_ids.blank? if cat_ids.blank?

View File

@ -51,24 +51,69 @@
</ul> </ul>
</div> </div>
<% end %> <% end %>
<% locales = Site.first.in_use_locales rescue I18n.available_locales
locales = [I18n.locale] + (locales - [I18n.locale])
%>
<div class="control-group"> <div class="control-group">
<%= f.label t("title"), :class=>"control-label" %> <%= f.label t("title"), :class=>"control-label" %>
<div class="controls"> <div class="controls">
<div data-ph="<%= t('calendar.press_enter') %>" contenteditable="true" class="calendar-comment"><%= @event.title.to_s.html_safe %></div> <div class="tab-content" style="overflow: hidden;">
<%= f.hidden_field :title %> <% locales.each_with_index do |locale, i| %>
<%= f.fields_for :title_translations, f.object do |f| %>
<div class="tab-pane fade <%= ( i == 0 ) ? "active in" : '' %>" id="title_translations_<%= locale %>">
<% title = f.object.title(locale) %>
<div data-ph="<%= t('calendar.press_enter') %>" contenteditable="true" class="calendar-comment"><%= title.to_s.html_safe %></div>
<%= hidden_field_tag "#{f.object_name}[#{locale}]", title %>
</div>
<% end %>
<% end %>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<% locales.each_with_index do |locale, i| %>
<a class="btn <%= ( i == 0 ) ? "active" : '' %>" href="#title_translations_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %></a>
<% end %>
</div>
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<%= f.label t("calendar.note"), :class=>"control-label" %> <%= f.label t("calendar.note"), :class=>"control-label" %>
<div class="controls"> <div class="controls">
<div data-ph="<%= t('calendar.press_enter') %>" contenteditable="true" class="calendar-comment"><%= @event.note.to_s.html_safe %></div> <div class="tab-content" style="overflow: hidden;">
<%= f.hidden_field :note %> <% locales.each_with_index do |locale, i| %>
<%= f.fields_for :note_translations, f.object do |f| %>
<div class="tab-pane fade <%= ( i == 0 ) ? "active in" : '' %>" id="note_translations_<%= locale %>">
<% note = f.object.note(locale) %>
<div data-ph="<%= t('calendar.press_enter') %>" contenteditable="true" class="calendar-comment"><%= note.to_s.html_safe %></div>
<%= hidden_field_tag "#{f.object_name}[#{locale}]", note %>
</div>
<% end %>
<% end %>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<% locales.each_with_index do |locale, i| %>
<a class="btn <%= ( i == 0 ) ? "active" : '' %>" href="#note_translations_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %></a>
<% end %>
</div>
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
<%= f.label t("url"), :class=>"control-label" %> <%= f.label t("url"), :class=>"control-label" %>
<div class="controls"> <div class="controls">
<%= f.text_field :url %> <div class="tab-content" style="overflow: hidden;">
<% locales.each_with_index do |locale, i| %>
<%= f.fields_for :url_translations, f.object do |f| %>
<div class="tab-pane fade <%= ( i == 0 ) ? "active in" : '' %>" id="note_translations_<%= locale %>">
<% url = f.object.url(locale) %>
<%= text_field_tag "#{f.object_name}[#{locale}]", url %>
</div>
<% end %>
<% end %>
</div>
<div class="btn-group" data-toggle="buttons-radio">
<% locales.each_with_index do |locale, i| %>
<a class="btn <%= ( i == 0 ) ? "active" : '' %>" href="#url_translations_<%= locale %>" data-toggle="tab"><%= t(locale.to_s) %></a>
<% end %>
</div>
</div> </div>
</div> </div>
<div class="control-group"> <div class="control-group">
@ -152,13 +197,12 @@
} }
function before_submit(ele){ function before_submit(ele){
var self = $(ele).parents('form') var self = $(ele).parents('form')
var length = self.find('*[contenteditable="true"]').length var content_editables = self.find('*[contenteditable="true"]');
for (var i=0;i<length;i++){ for (var i=0,length = content_editables.length; i<length; i++){
var tp1 = self.find('*[contenteditable="true"]').eq(i) var tp1 = content_editables.eq(i);
var tp2 = tp1.parents('.controls').eq(0).find('input[type="hidden"]') tp1.siblings('input[type="hidden"]').val(tp1.html());
tp2.val(tp1.html())
} }
self.submit() self.submit();
} }
$('body').ready(function(){ $('body').ready(function(){
if ($('.default_picker,.time_picker,.date_picker').find('input').length!=0){ if ($('.default_picker,.time_picker,.date_picker').find('input').length!=0){

View File

@ -1,7 +1,33 @@
Rails.application.routes.draw do Rails.application.routes.draw do
locales = Site.first.in_use_locales rescue I18n.available_locales locales = Site.first.in_use_locales rescue I18n.available_locales
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
s = Site.first
update_flag = s.respond_to?(:tmp_flags)
need_update = !update_flag || !(s.tmp_flags.include?('cf1'))
if need_update
Event.where(:title_translations.in=>[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 scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do namespace :admin do
get "/calendars/agenda" => "calendars#agenda" get "/calendars/agenda" => "calendars#agenda"