diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 7bef119a1..3c29ae9a1 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -110,32 +110,13 @@ class TicketsController < ApplicationController # GET /api/tickets_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 + result = Ticket.list_by_customer( + :customer_id => params[:customer_id], + :limit => 15, + ) render :json => { - :tickets => { - :open => tickets_open, - :closed => tickets_closed - } - # :users => users, + :tickets => result } end @@ -212,12 +193,11 @@ class TicketsController < ApplicationController # GET /api/ticket_merge_list/1 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_list = Ticket.where( + :customer_id => ticket.customer_id, + :ticket_state_id => Ticket::State.by_category( 'open' ) ) - ticket = Ticket.find( params[:ticket_id] ) - ticket_list = Ticket.where( :customer_id => ticket.customer_id, :ticket_state_id => ticket_states ) .where( 'id != ?', [ ticket.id ] ) .order('created_at DESC') .limit(6) diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 9f72640a5..b33b0155a 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -5,28 +5,7 @@ class TranslationsController < ApplicationController # GET /translations/:lang def load - translations = Translation.where( :locale => 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, - } + render :json => Translation.list( params[:locale] ) end # GET /translations @@ -53,4 +32,4 @@ class TranslationsController < ApplicationController def destroy model_destory_render(Translation, params) end -end +end \ No newline at end of file diff --git a/app/models/observer/ticket/user_ticket_counter.rb b/app/models/observer/ticket/user_ticket_counter.rb index 69495aca1..e3accb90d 100644 --- a/app/models/observer/ticket/user_ticket_counter.rb +++ b/app/models/observer/ticket/user_ticket_counter.rb @@ -14,25 +14,17 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer return if !record.customer_id # open ticket count - ticket_state_open_ids = Cache.get( 'ticket::state_ids::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( + ticket_state_open = Ticket::State.by_category( 'open' ) + tickets_open = Ticket.where( :customer_id => record.customer_id, - :ticket_state_id => ticket_state_open_ids, + :ticket_state_id => ticket_state_open, ).count() # closed ticket count - ticket_state_closed_ids = Cache.get( 'ticket::state_ids::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( + ticket_state_closed = Ticket::State.by_category( 'closed' ) + tickets_closed = Ticket.where( :customer_id => record.customer_id, - :ticket_state_id => ticket_state_closed_ids, + :ticket_state_id => ticket_state_closed, ).count() # check if update is needed @@ -51,17 +43,5 @@ class Observer::Ticket::UserTicketCounter < ActiveRecord::Observer 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 diff --git a/app/models/observer/user/geo.rb b/app/models/observer/user/geo.rb new file mode 100644 index 000000000..0d6053204 --- /dev/null +++ b/app/models/observer/user/geo.rb @@ -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 \ No newline at end of file diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 0ea4101be..2b29a0611 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -520,11 +520,8 @@ class Ticket < ApplicationModel end def self.escalation_calculation_rebuild - ticket_state_list_open = Ticket::State.where( - :state_type_id => Ticket::StateType.where( - :name => ['new','open', 'pending reminder', 'pending action'] - ) - ) + ticket_state_list_open = Ticket::State.by_category( 'open' ) + tickets = Ticket.where( :ticket_state_id => ticket_state_list_open ) tickets.each {|ticket| ticket.escalation_calculation @@ -580,10 +577,8 @@ class Ticket < ApplicationModel def escalation_calculation # set escalation off if ticket is already closed - ticket_state = Ticket::State.lookup( :id => self.ticket_state_id ) - ticket_state_type = Ticket::StateType.lookup( :id => ticket_state.state_type_id ) - ignore_escalation = ['removed', 'closed', 'merged', 'pending action'] - if ignore_escalation.include?( ticket_state_type.name ) + ticket_state = Ticket::State.lookup( :id => self.ticket_state_id ) + if ticket_state.ignore_escalation? self.escalation_time = nil # self.first_response_escal_date = nil # self.close_time_escal_date = nil @@ -693,6 +688,47 @@ class Ticket < ApplicationModel self.save 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 def number_generate diff --git a/app/models/ticket/state.rb b/app/models/ticket/state.rb index 6ab1aef3d..258ef5150 100644 --- a/app/models/ticket/state.rb +++ b/app/models/ticket/state.rb @@ -3,4 +3,49 @@ class Ticket::State < ApplicationModel belongs_to :state_type, :class_name => 'Ticket::StateType' 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 diff --git a/app/models/translation.rb b/app/models/translation.rb index b62a195bd..62f661c3c 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -3,6 +3,29 @@ class Translation < ApplicationModel 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) # translate string diff --git a/app/models/user.rb b/app/models/user.rb index e5121b29a..393e47d7a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,10 +4,8 @@ require 'digest/sha2' require 'organization' class User < ApplicationModel - include Gmaps - - 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 + before_create :check_name, :check_email, :check_login, :check_image, :check_password + before_update :check_password, :check_image, :check_email, :check_login_update after_create :notify_clients_after_create after_update :notify_clients_after_update after_destroy :notify_clients_after_destroy @@ -380,73 +378,12 @@ class User < ApplicationModel return user 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 self.last_login = Time.now self.save end 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 diff --git a/config/application.rb b/config/application.rb index 83aacad44..5054a5295 100644 --- a/config/application.rb +++ b/config/application.rb @@ -40,9 +40,11 @@ module Zammad 'observer::_ticket::_article::_communicate_facebook', 'observer::_ticket::_article::_communicate_twitter', 'observer::_ticket::_notification', - 'observer::_tag::_ticket_history', '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. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] diff --git a/db/migrate/20130806000001_update_geo.rb b/db/migrate/20130806000001_update_geo.rb new file mode 100644 index 000000000..dd3bad9ca --- /dev/null +++ b/db/migrate/20130806000001_update_geo.rb @@ -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 + diff --git a/db/seeds.rb b/db/seeds.rb index 7590c9a42..ed4f5e25b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -133,7 +133,28 @@ Setting.create_if_not_exists( :state => 'DB', :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( :title => 'New User Accounts',