Merge branch 'develop' into feature/ui2

This commit is contained in:
Martin Edenhofer 2013-08-06 22:09:13 +02:00
commit 9be9546410
11 changed files with 256 additions and 155 deletions

View file

@ -110,32 +110,13 @@ class TicketsController < ApplicationController
# GET /api/tickets_customer # GET /api/tickets_customer
def ticket_customer def ticket_customer
# get closed/open states
ticket_state_list_open = Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['new','open', 'pending reminder', 'pending action'] )
)
ticket_state_list_closed = Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['closed'] )
)
# get tickets
tickets_open = Ticket.where(
:customer_id => params[:customer_id],
:ticket_state_id => ticket_state_list_open
).limit(15).order('created_at DESC')
tickets_closed = Ticket.where(
:customer_id => params[:customer_id],
:ticket_state_id => ticket_state_list_closed
).limit(15).order('created_at DESC')
# return result # return result
result = Ticket.list_by_customer(
:customer_id => params[:customer_id],
:limit => 15,
)
render :json => { render :json => {
:tickets => { :tickets => result
:open => tickets_open,
:closed => tickets_closed
}
# :users => users,
} }
end end
@ -212,12 +193,11 @@ class TicketsController < ApplicationController
# GET /api/ticket_merge_list/1 # GET /api/ticket_merge_list/1
def ticket_merge_list def ticket_merge_list
# get closed/open states
ticket_states = Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['new','open', 'pending reminder', 'pending action', 'closed'] )
)
ticket = Ticket.find( params[:ticket_id] ) ticket = Ticket.find( params[:ticket_id] )
ticket_list = Ticket.where( :customer_id => ticket.customer_id, :ticket_state_id => ticket_states ) ticket_list = Ticket.where(
:customer_id => ticket.customer_id,
:ticket_state_id => Ticket::State.by_category( 'open' )
)
.where( 'id != ?', [ ticket.id ] ) .where( 'id != ?', [ ticket.id ] )
.order('created_at DESC') .order('created_at DESC')
.limit(6) .limit(6)

View file

@ -5,28 +5,7 @@ class TranslationsController < ApplicationController
# GET /translations/:lang # GET /translations/:lang
def load def load
translations = Translation.where( :locale => params[:locale] ) render :json => Translation.list( params[:locale] )
list = []
translations.each { |item|
data = [
item.id,
item.source,
item.target,
]
list.push data
}
timestamp_map_default = 'yyyy-mm-dd HH:MM'
timestamp_map = {
:de => 'dd.mm.yyyy HH:MM',
}
timestamp = timestamp_map[ params[:locale].to_sym ] || timestamp_map_default
render :json => {
:list => list,
:timestampFormat => timestamp,
}
end end
# GET /translations # GET /translations

View file

@ -14,25 +14,17 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
return if !record.customer_id return if !record.customer_id
# open ticket count # open ticket count
ticket_state_open_ids = Cache.get( 'ticket::state_ids::open' ) ticket_state_open = Ticket::State.by_category( 'open' )
if !ticket_state_open_ids
ticket_state_open_ids = self.state_ids( ['new','open', 'pending reminder', 'pending action'] )
Cache.write( 'ticket::state_ids::open', ticket_state_open_ids, { :expires_in => 1.hour } )
end
tickets_open = Ticket.where( tickets_open = Ticket.where(
:customer_id => record.customer_id, :customer_id => record.customer_id,
:ticket_state_id => ticket_state_open_ids, :ticket_state_id => ticket_state_open,
).count() ).count()
# closed ticket count # closed ticket count
ticket_state_closed_ids = Cache.get( 'ticket::state_ids::closed' ) ticket_state_closed = Ticket::State.by_category( 'closed' )
if !ticket_state_closed_ids
ticket_state_closed_ids = self.state_ids( ['closed'] )
Cache.write( 'ticket::state_ids::closed', ticket_state_closed_ids, { :expires_in => 1.hour } )
end
tickets_closed = Ticket.where( tickets_closed = Ticket.where(
:customer_id => record.customer_id, :customer_id => record.customer_id,
:ticket_state_id => ticket_state_closed_ids, :ticket_state_id => ticket_state_closed,
).count() ).count()
# check if update is needed # check if update is needed
@ -51,17 +43,5 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer
end end
end end
def state_ids(ticket_state_types)
ticket_state_types = Ticket::StateType.where(
:name => ticket_state_types,
)
ticket_states = Ticket::State.where( :state_type_id => ticket_state_types )
ticket_state_ids = []
ticket_states.each {|ticket_state|
ticket_state_ids.push ticket_state.id
}
ticket_state_ids
end
end end

View file

@ -0,0 +1,69 @@
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
class Observer::User::Geo < ActiveRecord::Observer
observe 'user'
def before_create(record)
check_geo(record)
end
def before_update(record)
check_geo(record)
end
# check if geo need to be updated
def check_geo(record)
location = ['street', 'zip', 'city', 'country']
# check if geo update is needed based on old/new location
if record.id
current = User.where( :id => record.id ).first
return if !current
current_location = {}
location.each { |item|
current_location[item] = current[item]
}
end
# get full address
next_location = {}
location.each { |item|
next_location[item] = record[item]
}
# return if address hasn't changed and geo data is already available
return if ( current_location == next_location ) && record.preferences['lat'] && record.preferences['lng']
# geo update
self.geo_update(record)
end
# update geo data of user
def geo_update(record)
address = ''
location = ['street', 'zip', 'city', 'country']
location.each { |item|
if record[item] && record[item] != ''
address = address + ',' + record[item]
end
}
# return if no address is given
return if address == ''
# load adapter
adapter = Setting.get('geo_backend')
return if !adapter
adapter_module = Object.const_get(adapter)
# db lookup
latlng = adapter_module.geocode(address)
return if !latlng
# store data
record.preferences['lat'] = latlng[0]
record.preferences['lng'] = latlng[1]
end
end

View file

@ -520,11 +520,8 @@ class Ticket < ApplicationModel
end end
def self.escalation_calculation_rebuild def self.escalation_calculation_rebuild
ticket_state_list_open = Ticket::State.where( ticket_state_list_open = Ticket::State.by_category( 'open' )
:state_type_id => Ticket::StateType.where(
:name => ['new','open', 'pending reminder', 'pending action']
)
)
tickets = Ticket.where( :ticket_state_id => ticket_state_list_open ) tickets = Ticket.where( :ticket_state_id => ticket_state_list_open )
tickets.each {|ticket| tickets.each {|ticket|
ticket.escalation_calculation ticket.escalation_calculation
@ -581,9 +578,7 @@ class Ticket < ApplicationModel
# set escalation off if ticket is already closed # set escalation off if ticket is already closed
ticket_state = Ticket::State.lookup( :id => self.ticket_state_id ) ticket_state = Ticket::State.lookup( :id => self.ticket_state_id )
ticket_state_type = Ticket::StateType.lookup( :id => ticket_state.state_type_id ) if ticket_state.ignore_escalation?
ignore_escalation = ['removed', 'closed', 'merged', 'pending action']
if ignore_escalation.include?( ticket_state_type.name )
self.escalation_time = nil self.escalation_time = nil
# self.first_response_escal_date = nil # self.first_response_escal_date = nil
# self.close_time_escal_date = nil # self.close_time_escal_date = nil
@ -693,6 +688,47 @@ class Ticket < ApplicationModel
self.save self.save
end end
=begin
list tickets by customer groupd in state categroie open and closed
result = Ticket.list_by_customer(
:customer_id => 123,
:limit => 15, # optional, default 15
)
returns
result = {
:open => tickets_open,
:closed => tickets_closed,
}
=end
def self.list_by_customer(data)
# get closed/open states
ticket_state_list_open = Ticket::State.by_category( 'open' )
ticket_state_list_closed = Ticket::State.by_category( 'closed' )
# get tickets
tickets_open = Ticket.where(
:customer_id => data[:customer_id],
:ticket_state_id => ticket_state_list_open
).limit( data[:limit] || 15 ).order('created_at DESC')
tickets_closed = Ticket.where(
:customer_id => data[:customer_id],
:ticket_state_id => ticket_state_list_closed
).limit( data[:limit] || 15 ).order('created_at DESC')
return {
:open => tickets_open,
:closed => tickets_closed,
}
end
private private
def number_generate def number_generate

View file

@ -3,4 +3,49 @@
class Ticket::State < ApplicationModel class Ticket::State < ApplicationModel
belongs_to :state_type, :class_name => 'Ticket::StateType' belongs_to :state_type, :class_name => 'Ticket::StateType'
validates :name, :presence => true validates :name, :presence => true
=begin
list tickets by customer
states = Ticket::State.by_category('open') # open|closed
returns:
state objects
=end
def self.by_category(category)
if category == 'open'
return Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['new', 'open', 'pending reminder', 'pending action'] )
)
elsif category == 'closed'
return Ticket::State.where(
:state_type_id => Ticket::StateType.where( :name => ['closed'] )
)
end
raise "Unknown category '#{category}'"
end
=begin
check if state is ignored for escalation
ticket_state = Ticket::State.lookup( :name => 'state name' )
result = ticket_state.ignore_escalation?
returns:
true/false
=end
def ignore_escalation?
ignore_escalation = ['removed', 'closed', 'merged']
return true if ignore_escalation.include?( self.name )
return false
end
end end

View file

@ -3,6 +3,29 @@
class Translation < ApplicationModel class Translation < ApplicationModel
before_create :set_initial before_create :set_initial
def self.list(locale)
translations = Translation.where( :locale => locale )
list = []
translations.each { |item|
data = [
item.id,
item.source,
item.target,
]
list.push data
}
timestamp_map_default = 'yyyy-mm-dd HH:MM'
timestamp_map = {
:de => 'dd.mm.yyyy HH:MM',
}
timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default
return {
:list => list,
:timestampFormat => timestamp,
}
end
def self.translate(locale, string) def self.translate(locale, string)
# translate string # translate string

View file

@ -4,10 +4,8 @@ require 'digest/sha2'
require 'organization' require 'organization'
class User < ApplicationModel class User < ApplicationModel
include Gmaps before_create :check_name, :check_email, :check_login, :check_image, :check_password
before_update :check_password, :check_image, :check_email, :check_login_update
before_create :check_name, :check_email, :check_login, :check_image, :check_geo, :check_password
before_update :check_password, :check_image, :check_geo, :check_email, :check_login_update
after_create :notify_clients_after_create after_create :notify_clients_after_create
after_update :notify_clients_after_update after_update :notify_clients_after_update
after_destroy :notify_clients_after_destroy after_destroy :notify_clients_after_destroy
@ -380,73 +378,12 @@ class User < ApplicationModel
return user return user
end end
# update all users geo data
def self.geo_update_all
User.all.each { |user|
user.geo_update
user.save
}
end
# update geo data of one user
def geo_update
address = ''
location = ['street', 'zip', 'city', 'country']
location.each { |item|
if self[item] && self[item] != ''
address = address + ',' + self[item]
end
}
# return if no address is given
return if address == ''
# dp lookup
latlng = Gmaps.geocode(address)
if latlng
self.preferences['lat'] = latlng[0]
self.preferences['lng'] = latlng[1]
end
end
def update_last_login def update_last_login
self.last_login = Time.now self.last_login = Time.now
self.save self.save
end end
private private
def check_geo
# geo update if no user exists
if !self.id
self.geo_update
return
end
location = ['street', 'zip', 'city', 'country']
# get current user data
current = User.where( :id => self.id ).first
return if !current
# check if geo update is needed
current_location = {}
location.each { |item|
current_location[item] = current[item]
}
# get full address
next_location = {}
location.each { |item|
next_location[item] = self[item]
}
# return if address hasn't changed and geo data is already available
return if ( current_location == next_location ) && ( self.preferences['lat'] && self.preferences['lng'] )
# geo update
self.geo_update
end
def check_name def check_name

View file

@ -40,9 +40,11 @@ module Zammad
'observer::_ticket::_article::_communicate_facebook', 'observer::_ticket::_article::_communicate_facebook',
'observer::_ticket::_article::_communicate_twitter', 'observer::_ticket::_article::_communicate_twitter',
'observer::_ticket::_notification', 'observer::_ticket::_notification',
'observer::_tag::_ticket_history',
'observer::_ticket::_reset_new_state', 'observer::_ticket::_reset_new_state',
'observer::_ticket::_escalation_calculation' 'observer::_ticket::_escalation_calculation',
'observer::_tag::_ticket_history',
'observer::_user::_geo'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

View file

@ -0,0 +1,29 @@
class UpdateGeo < ActiveRecord::Migration
def up
Setting.create_if_not_exists(
:title => 'Geo Location Backend',
:name => 'geo_backend',
:area => 'System::Geo',
:description => 'Defines the backend for geo location lookups.',
:options => {
:form => [
{
:display => '',
:null => true,
:name => 'geo_backend',
:tag => 'select',
:options => {
'' => '-',
'Gmaps' => 'Google Maps',
},
},
],
},
:state => 'Gmaps',
:frontend => true
)
end
def down
end
end

View file

@ -133,7 +133,28 @@ Setting.create_if_not_exists(
:state => 'DB', :state => 'DB',
:frontend => false :frontend => false
) )
Setting.create_if_not_exists(
:title => 'Geo Location Backend',
:name => 'geo_backend',
:area => 'System::Geo',
:description => 'Defines the backend for geo location lookups.',
:options => {
:form => [
{
:display => '',
:null => true,
:name => 'geo_backend',
:tag => 'select',
:options => {
'' => '-',
'Gmaps' => 'Google Maps',
},
},
],
},
:state => 'Gmaps',
:frontend => true
)
Setting.create_if_not_exists( Setting.create_if_not_exists(
:title => 'New User Accounts', :title => 'New User Accounts',