Permalink
Browse files

Fixes #1589 - No localised time displayed in trigger (autoreply/slack…

…/...) notifications.
  • Loading branch information...
znuny-robo committed Feb 10, 2019
1 parent 487f36a commit 63214c9327f9d39589a6ebefe38676be84e324a9
@@ -0,0 +1,35 @@
class App.SettingsAreaItemDefaultTimezone extends App.SettingsAreaItem
result: {}

render: =>
@fetchTimezones()

localRender: (data) =>
options = {}
for timezone, offset of data.timezones
if !offset.toString().match(/(\+|\-)/)
offset = "+#{offset}"
options[timezone] = "#{timezone} (GMT#{offset})"
configure_attributes = [
{ name: 'timezone_default', display: '', tag: 'searchable_select', null: false, class: 'input', options: options, default: @setting.state_current.value },
]

@html App.view(@template)(
setting: @setting
)

new App.ControllerForm(
el: @el.find('.form-item')
model: { configure_attributes: configure_attributes, className: '' }
autofocus: false
)

fetchTimezones: =>
@ajax(
id: 'calendar_timezones'
type: 'GET'
url: "#{@apiPath}/calendars/timezones"
success: (data) =>
@result = data
@localRender(data)
)
@@ -340,6 +340,7 @@ class Base extends App.WizardFullScreen
@params = @formParam(e.target)
@params.logo = @logoPreview.attr('src')
@params.locale_default = App.i18n.detectBrowserLocale()
@params.timezone_default = App.i18n.detectBrowserTimezone()

store = (logoResizeDataUrl) =>
@params.logo_resize = logoResizeDataUrl
@@ -0,0 +1,36 @@
class DefaultTimezone extends App.Controller
constructor: ->
super

check = =>
timezone = App.i18n.detectBrowserTimezone()
return if !timezone

# check system timezone_default
if _.isEmpty(@Config('timezone_default')) && @permissionCheck('admin.system')
App.Setting.fetchFull(
=> @updateSetting(timezone)
force: false
)

# prepare user based timezone
# check current user timezone
#preferences = App.Session.get('preferences')
#return if !preferences
#return if !_.isEmpty(preferences.timezone)
#@ajax(
# id: "i18n-set-user-timezone"
# type: 'PUT'
# url: "#{App.Config.get('api_path')}/users/preferences"
# data: JSON.stringify(timezone: timezone)
# processData: true
#)

App.Event.bind('auth:login', (session) =>
@delay(check, 8500, 'default_timezone')
)

updateSetting: (timezone) ->
App.Setting.set('timezone_default', timezone)

App.Config.set('default_timezone', DefaultTimezone, 'Widgets')
@@ -103,6 +103,17 @@ class App.i18n

window.navigator.userLanguage || window.navigator.language || 'en-us'

@detectBrowserTimezone: ->
return if !window.Intl
return if !window.Intl.DateTimeFormat
DateTimeFormat = Intl.DateTimeFormat()
return if !DateTimeFormat
return if !DateTimeFormat.resolvedOptions
resolvedOptions = DateTimeFormat.resolvedOptions()
return if !resolvedOptions
return if !resolvedOptions.timeZone
resolvedOptions.timeZone

class _i18nSingleton extends Spine.Module
@include App.LogInclude

@@ -1,7 +1,8 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class CalendarsController < ApplicationController
prepend_before_action { authentication_check(permission: 'admin.calendar') }
prepend_before_action -> { authentication_check(permission: 'admin.calendar') }, only: %i[init index show create update destroy]
prepend_before_action -> { authentication_check(permission: 'admin') }, only: %i[timezones]

def init
assets = {}
@@ -41,4 +42,10 @@ def destroy
model_destroy_render(Calendar, params)
end

def timezones
render json: {
timezones: Calendar.timezones
}
end

end
@@ -148,6 +148,11 @@ def base
settings[:locale_default] = params[:locale_default]
end

# add timezone_default
if params[:timezone_default].present?
settings[:timezone_default] = params[:timezone_default]
end

if messages.present?
render json: {
result: 'invalid',
@@ -1543,10 +1543,13 @@ def send_sms_notification(value, article, perform_origin)
end

objects = build_notification_template_objects(article)
body = NotificationFactory::Renderer.new(objects, 'en-en', value['body'], false)
.render
.html2text
.tr(' ', ' ') # convert non-breaking space to simple space
body = NotificationFactory::Renderer.new(
objects: objects,
locale: 'en-en',
timezone: Setting.get('timezone_default'),
template: value['body'],
escape: false
).render.html2text.tr(' ', ' ') # convert non-breaking space to simple space

# attributes content_type is not needed for SMS
article = Ticket::Article.create(
@@ -242,7 +242,7 @@ def human_changes(user, record)

# only show allowed attributes
attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
#puts "AL #{attribute_list.inspect}"

user_related_changes = {}
@item[:changes].each do |key, value|

@@ -84,7 +84,8 @@ def perform

result = NotificationFactory::Slack.template(
template: template,
locale: user[:preferences][:locale],
locale: user[:preferences][:locale] || Setting.get('locale_default'),
timezone: user[:preferences][:timezone] || Setting.get('timezone_default'),
objects: {
ticket: ticket,
article: article,
@@ -215,6 +215,90 @@ def self.translate(locale, string)

=begin
translate timestampes in ruby context, e. g. for notifications
translated = Translation.timestamp('de-de', 'Europe/Berlin', '2018-10-10T10:00:00Z0')
or
translated = Translation.timestamp('de-de', 'Europe/Berlin', Time.zone.parse('2018-10-10T10:00:00Z0'))
=end

def self.timestamp(locale, timezone, timestamp)

if timestamp.class == String
begin
timestamp_parsed = Time.zone.parse(timestamp)
return timestamp.to_s if !timestamp_parsed

timestamp = timestamp_parsed
rescue
return timestamp.to_s
end
end

record = Translation.where(locale: locale, source: 'timestamp', format: 'time').pluck(:target).first
return timestamp.to_s if !record

begin
timestamp = timestamp.in_time_zone(timezone)
rescue
return timestamp.to_s
end
record.sub!('dd', format('%02d', timestamp.day))
record.sub!('d', timestamp.day.to_s)
record.sub!('mm', format('%02d', timestamp.month))
record.sub!('m', timestamp.month.to_s)
record.sub!('yyyy', timestamp.year.to_s)
record.sub!('yy', timestamp.year.to_s.last(2))
record.sub!('SS', format('%02d', timestamp.sec.to_s))
record.sub!('MM', format('%02d', timestamp.min.to_s))
record.sub!('HH', format('%02d', timestamp.hour.to_s))
"#{record} (#{timezone})"
end

=begin
translate date in ruby context, e. g. for notifications
translated = Translation.date('de-de', '2018-10-10')
or
translated = Translation.date('de-de', Date.parse('2018-10-10'))
=end

def self.date(locale, date)

if date.class == String
begin
date_parsed = Date.parse(date)
return date.to_s if !date_parsed

date = date_parsed
rescue
return date.to_s
end
end

return date.to_s if date.class != Date

record = Translation.where(locale: locale, source: 'date', format: 'time').pluck(:target).first
return date.to_s if !record

record.sub!('dd', format('%02d', date.day))
record.sub!('d', date.day.to_s)
record.sub!('mm', format('%02d', date.month))
record.sub!('m', date.month.to_s)
record.sub!('yyyy', date.year.to_s)
record.sub!('yy', date.year.to_s.last(2))
record
end

=begin
load translations from local
all:
@@ -2,11 +2,12 @@
api_path = Rails.configuration.api_path

# calendars
match api_path + '/calendars_init', to: 'calendars#init', via: :get
match api_path + '/calendars', to: 'calendars#index', via: :get
match api_path + '/calendars/:id', to: 'calendars#show', via: :get
match api_path + '/calendars', to: 'calendars#create', via: :post
match api_path + '/calendars/:id', to: 'calendars#update', via: :put
match api_path + '/calendars/:id', to: 'calendars#destroy', via: :delete
match api_path + '/calendars_init', to: 'calendars#init', via: :get
match api_path + '/calendars/timezones', to: 'calendars#timezones', via: :get
match api_path + '/calendars', to: 'calendars#index', via: :get
match api_path + '/calendars/:id', to: 'calendars#show', via: :get
match api_path + '/calendars', to: 'calendars#create', via: :post
match api_path + '/calendars/:id', to: 'calendars#update', via: :put
match api_path + '/calendars/:id', to: 'calendars#destroy', via: :delete

end
@@ -0,0 +1,27 @@
class SettingTimezoneDefault < ActiveRecord::Migration[5.1]
def up
# return if it's a new setup
return if !Setting.find_by(name: 'system_init_done')

Setting.create_if_not_exists(
title: 'Timezone',
name: 'timezone_default',
area: 'System::Branding',
description: 'Defines the system default timezone.',
options: {
form: [
{
name: 'timezone_default',
}
],
},
state: '',
preferences: {
prio: 9,
controller: 'SettingsAreaItemDefaultTimezone',
permission: ['admin.system'],
},
frontend: true
)
end
end
@@ -176,6 +176,26 @@
},
frontend: true
)
Setting.create_if_not_exists(
title: 'Timezone',
name: 'timezone_default',
area: 'System::Branding',
description: 'Defines the system default timezone.',
options: {
form: [
{
name: 'timezone_default',
}
],
},
state: '',
preferences: {
prio: 9,
controller: 'SettingsAreaItemDefaultTimezone',
permission: ['admin.system'],
},
frontend: true
)
Setting.create_or_update(
title: 'Pretty Date',
name: 'pretty_date_format',
Oops, something went wrong.

0 comments on commit 63214c9

Please sign in to comment.