Martin Edenhofer 11 лет назад
Родитель
Сommit
6b21e155e9

+ 37 - 27
app/assets/javascripts/app/controllers/agent_ticket_history.js.coffee

@@ -9,6 +9,7 @@ class App.TicketHistory extends App.ControllerModal
     @fetch(@ticket_id)
 
   fetch: (@ticket_id) ->
+
     # get data
     @ajax(
       id:    'ticket_history',
@@ -19,18 +20,43 @@ class App.TicketHistory extends App.ControllerModal
         # load collections
         App.Event.trigger 'loadAssets', data.assets
 
-        # load history collections
-        App.History.deleteAll()
-        App.Collection.load( type: 'History', data: data.history )
 
         # render page
-        @render()
+        @render(data.history)
     )
 
-  render: ->
+  render: ( items, orderClass = '' ) ->
+
+    for item in items
+      if item.object is 'Ticket'
+        ticket = App.Ticket.find( item.o_id )
+        item.link = '#ticket/zoom/' + ticket.id
+        item.title = ticket.title
+        item.object = 'Ticket'
+
+      else if item.object is 'Ticket::Article'
+        article = App.TicketArticle.find( item.o_id )
+        ticket  = App.Ticket.find( article.ticket_id )
+        item.link = '#ticket/zoom/' + ticket.id + '/' + article.id
+        item.title = article.subject || ticket.title
+        item.object = 'Article'
+
+      else if item.object is 'User'
+        user = App.User.find( item.o_id )
+        item.link = '#user/zoom/' + item.o_id
+        item.title = user.displayName()
+        item.object = 'User'
+
+      item.created_by = App.User.find( item.created_by_id )
+
+    # set cache
+    @historyListCache = items
 
     @html App.view('agent_ticket_history')(
-      objects: App.History.search()
+      items: items
+      orderClass: orderClass
+
+      @historyListCache
     )
 
     @modalShow()
@@ -43,25 +69,9 @@ class App.TicketHistory extends App.ControllerModal
 
   sortorder: (e) ->
     e.preventDefault()
-    isSorted = @el.find('.sorted')
-
-    if isSorted.length
-      @sortstate = 'notsorted'
-      @html App.view('agent_ticket_history')(
-        objects: App.History.search()
-        state:   @sortstate
-      )
-    else
-      @sortstate = 'sorted'
-      @html App.view('agent_ticket_history')(
-        objects: App.History.search().reverse()
-        state:   @sortstate
-      )
+    idDown = @el.find('[data-type="sortorder"]').hasClass('down')
 
-    @modalShow()
-
-    # enable user popups
-    @userPopups()
-
-    # show frontend times
-    @delay( @frontendTimeUpdate, 200, 'ui-time-update' )
+    if idDown
+      @render( @historyListCache, 'up' )
+    else
+      @render( @historyListCache.reverse(), 'down' )

+ 0 - 20
app/assets/javascripts/app/models/history.js.coffee

@@ -1,20 +0,0 @@
-class App.History extends App.Model
-  @configure 'History', 'name'
-  @extend Spine.Model.Ajax
-  @url: @apiPath + '/histories'
-
-  @_fillUp: (data) ->
-
-    # add user
-    data.created_by = App.User.find( data.created_by_id )
-
-    # add possible actions
-    if data.history_attribute_id
-      data.attribute = App.HistoryAttribute.find( data.history_attribute_id )
-    if data.history_type_id
-      data.type      = App.HistoryType.find( data.history_type_id )
-    if data.history_object_id
-      data.object    = App.HistoryObject.find( data.history_object_id )
-
-    return data
-

+ 0 - 4
app/assets/javascripts/app/models/history_attributes.js.coffee

@@ -1,4 +0,0 @@
-class App.HistoryAttribute extends App.Model
-  @configure 'HistoryAttribute', 'name'
-  @extend Spine.Model.Ajax
-  @url: @apiPath + '/history_attributes'

+ 0 - 4
app/assets/javascripts/app/models/history_object.js.coffee

@@ -1,4 +0,0 @@
-class App.HistoryObject extends App.Model
-  @configure 'HistoryObject', 'name'
-  @extend Spine.Model.Ajax
-  @url: @apiPath + '/history_objects'

+ 0 - 4
app/assets/javascripts/app/models/history_type.js.coffee

@@ -1,4 +0,0 @@
-class App.HistoryType extends App.Model
-  @configure 'HistoryType', 'name'
-  @extend Spine.Model.Ajax
-  @url: @apiPath + '/history_types'

+ 18 - 22
app/assets/javascripts/app/views/agent_ticket_history.jst.eco

@@ -3,44 +3,40 @@
     <div class="modal-header">
       <a href="#" class="close">&times;</a>
       <h2 class="modal-title"><%- @T( 'History' ) %></h2>
-      <a href="#" data-type="sortorder" id="sortorder" class="<%= @state %>"><%- @T( 'Change order' ) %></a>
+      <a href="#" data-type="sortorder" class="<%= @orderClass %>"><%- @T( 'Change order' ) %></a>
     </div>
     <div class="modal-body">
 
 <% open = false %>
-<% for object in @objects: %>
-  <% object['history_object_display'] = object['history_object'] %>
-  <% if object['history_object'] is 'Ticket::Article': %>
-    <% object['history_object_display'] = 'Article' %>
-  <% end %>
-  <% if lasttime isnt object['created_at'] || last_user isnt object['created_by_id']: %>
+<% for item in @items: %>
+  <% if lasttime isnt item['created_at'] || last_user isnt item['created_by_id']: %>
     <% if open: %>
       </ul>
       <hr>
     <% end %>
     <% open = true %>
-    <% last_user = object['created_by_id'] %>
-    <% lasttime = object['created_at'] %>
-    <span class="user-popover" data-id="<%= object.created_by.id %>"><%= object.created_by.displayName() %></span> -
-    <span class="humanTimeFromNow" data-time="<%- object.created_at %>">?</span>
+    <% last_user = item['created_by_id'] %>
+    <% lasttime = item['created_at'] %>
+    <span class="user-popover" data-id="<%= item.created_by.id %>"><%= item.created_by.displayName() %></span> -
+    <span class="humanTimeFromNow" data-time="<%- item.created_at %>">?</span>
     <ul>
   <% end %>
     <li>
-    <% if ( object['history_type'] is 'notification' || object['history_type'] is 'email' ): %>
-      <%= object['history_type'] %>
-      <% if object['value_from']: %>
-        "<%= object['value_from'] %>" <%- @T( 'sent to' ) %> 
+    <% if ( item['type'] is 'notification' || item['type'] is 'email' ): %>
+      <%= item['type'] %>
+      <% if item['value_from']: %>
+        "<%= item['value_from'] %>" <%- @T( 'sent to' ) %> 
       <% end %>
-      <% if object['value_to']: %>
-        "<%= object['value_to'] %>"
+      <% if item['value_to']: %>
+        "<%= item['value_to'] %>"
       <% end %>
     <% else: %>
-      <%= object['history_type'] %> <%= object['history_object_display'] %> <% if object['history_attribute']: %>"<%= object['history_attribute'] %>"<% end %>
-      <% if object['value_from']: %>
-          <%- @T( 'from' ) %> "<%= object['value_from'] %>"
+      <%= item['type'] %> <%= item['object'] %> <% if item['attribute']: %>"<%= item['attribute'] %>"<% end %>
+      <% if item['value_from']: %>
+          <%- @T( 'from' ) %> "<%= item['value_from'] %>"
       <% end %>
-      <% if object['value_to']: %>
-          <%- @T( 'to' ) %> "<%= object['value_to'] %>"
+      <% if item['value_to']: %>
+          <%- @T( 'to' ) %> "<%= item['value_to'] %>"
       <% end %>
     <% end %>
     </li>

+ 2 - 44
app/controllers/tickets_controller.rb

@@ -130,53 +130,11 @@ class TicketsController < ApplicationController
     return if !ticket_permission( ticket )
 
     # get history of ticket
-    history = ticket.history_get
+    history = ticket.history_get(true)
 
-    # get related assets
-    assets = ticket.assets({})
-    history_list = []
-    history.each do |item|
-
-      assets = item.assets(assets)
-
-      item_tmp = item.attributes
-      if item['history_object'] == 'Ticket::Article'
-        item_temp['type'] = 'Article ' + item['type'].to_s
-      else
-        item_tmp['type'] = 'Ticket ' + item['type'].to_s
-      end
-      item_tmp['history_type'] = item.history_type.name
-      item_tmp['history_object'] = item.history_object.name
-      if item.history_attribute
-       item_tmp['history_attribute'] = item.history_attribute.name
-      end
-      item_tmp.delete( 'history_attribute_id' )
-      item_tmp.delete( 'history_object_id' )
-      item_tmp.delete( 'history_type_id' )
-      item_tmp.delete( 'o_id' )
-      item_tmp.delete( 'updated_at' )
-      if item_tmp['id_to'] == nil && item_tmp['id_from'] == nil
-        item_tmp.delete( 'id_to' )
-        item_tmp.delete( 'id_from' )
-      end
-      if item_tmp['value_to'] == nil && item_tmp['value_from'] == nil
-        item_tmp.delete( 'value_to' )
-        item_tmp.delete( 'value_from' )
-      end
-      if item_tmp['related_history_object_id'] == nil
-        item_tmp.delete( 'related_history_object_id' )
-      end
-      if item_tmp['related_o_id'] == nil
-        item_tmp.delete( 'related_o_id' )
-      end
-      history_list.push item_tmp
-    end
 
     # return result
-    render :json => {
-      :assets   => assets,
-      :history  => history_list,
-    }
+    render :json => history
   end
 
   # GET /api/v1/ticket_merge_list/1

+ 141 - 8
app/models/application_model.rb

@@ -19,8 +19,14 @@ class ApplicationModel < ActiveRecord::Base
   after_update  :activity_stream_update
   after_destroy :activity_stream_destroy
 
+  after_create  :history_create
+  after_update  :history_update
+  after_destroy :history_destroy
+
   # create instance accessor
-  class << self; attr_accessor :activity_stream_support_config end
+  class << self
+    attr_accessor :activity_stream_support_config, :history_support_config
+  end
 
   @@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::Priority', 'Group', 'User' ]
 
@@ -418,7 +424,7 @@ class OwnModel < ApplicationModel
 
 =begin
 
-serve methode to configure activity stream support for this model
+serve methode to configure and enable activity stream support for this model
 
 class Model < ApplicationModel
   activity_stream_support :role => 'Admin'
@@ -432,7 +438,7 @@ end
 
 =begin
 
-log object create activity stream
+log object create activity stream, if configured - will be executed automatically
 
   model = Model.find(123)
   model.activity_stream_create
@@ -445,7 +451,7 @@ log object create activity stream
 
 =begin
 
-log object update activity stream
+log object update activity stream, if configured - will be executed automatically
 
   model = Model.find(123)
   model.activity_stream_update
@@ -453,12 +459,13 @@ log object update activity stream
 =end
 
   def activity_stream_update
+    return if !self.changed?
     activity_stream_log( 'updated', self['updated_by_id'] )
   end
 
 =begin
 
-delete object activity stream
+delete object activity stream, will be executed automatically
 
   model = Model.find(123)
   model.activity_stream_destroy
@@ -471,15 +478,141 @@ delete object activity stream
 
 =begin
 
-destory object dependencies, will be executed automatically
+serve methode to configure and enable history support for this model
+
+class Model < ApplicationModel
+  history_support
+end
+
+
+class Model < ApplicationModel
+  history_support :ignore_attributes => { :article_count => true }
+end
 
 =end
 
-  def destroy_dependencies
+  def self.history_support(data = {})
+    @history_support_config = data
+  end
+
+=begin
+
+log object create history, if configured - will be executed automatically
+
+  model = Model.find(123)
+  model.history_create
+
+=end
+
+  def history_create
+    return if !self.class.history_support_config
+#    puts self.changes.inspect
+    self.history_log( 'created', self.created_by_id )
+
+  end
+
+=begin
+
+log object update history with all updated attributes, if configured - will be executed automatically
+
+  model = Model.find(123)
+  model.history_update
+
+=end
+
+  def history_update
+    return if !self.class.history_support_config
 
-    # delete history
+    return if !self.changed?
+
+    # return if it's no update
+    return if self.new_record?
+
+    # new record also triggers update, so ignore new records
+    changes = self.changes
+    return if changes['id'] && !changes['id'][0]
+    #puts 'updated' + self.changes.inspect
+
+    # TODO: Swop it to config file later
+    ignore_attributes = {
+      :created_at               => true,
+      :updated_at               => true,
+      :created_by_id            => true,
+      :updated_by_id            => true,
+      :article_count            => true,
+      :create_article_type_id   => true,
+      :create_article_sender_id => true,
+    }
+
+    changes.each {|key, value|
+
+      # do not log created_at and updated_at attributes
+      next if ignore_attributes[key.to_sym] == true
+
+      # get attribute name
+      attribute_name = key.to_s
+      if attribute_name[-3,3] == '_id'
+        attribute_name = attribute_name[ 0, attribute_name.length-3 ]
+      end
+      value_id = []
+      if key.to_s[-3,3] == '_id'
+        value_id[0] = value[0]
+        value_id[1] = value[1]
+
+        if self.respond_to?( attribute_name )
+          relation_class = self.send(attribute_name).class
+          if relation_class
+            relation_model = relation_class.lookup( :id => value_id[0] )
+            if relation_model
+              if relation_model['name']
+                value[0] = relation_model['name']
+              elsif relation_model.respond_to?('fullname')
+                value[0] = relation_model.send('fullname')
+              end
+            end
+            relation_model = relation_class.lookup( :id => value_id[1] )
+            if relation_model
+              if relation_model['name']
+                value[1] = relation_model['name']
+              elsif relation_model.respond_to?('fullname')
+                value[1] = relation_model.send('fullname')
+              end
+            end
+          end
+        end
+      end
+      data = {
+        :history_attribute      => attribute_name,
+        :value_from             => value[0],
+        :value_to               => value[1],
+        :id_from                => value_id[0],
+        :id_to                  => value_id[1],
+      }
+      #puts "HIST NEW " + data.inspect
+      self.history_log( 'updated', self.updated_by_id, data )
+    }
+  end
+
+=begin
+
+delete object history, will be executed automatically
+
+  model = Model.find(123)
+  model.history_destroy
+
+=end
+
+  def history_destroy
     History.remove( self.class.to_s, self.id )
+  end
 
+=begin
+
+destory object dependencies, will be executed automatically
+
+=end
+
+  def destroy_dependencies
   end
 
 end

+ 21 - 4
app/models/application_model/history_log_base.rb

@@ -7,7 +7,7 @@ module ApplicationModel::HistoryLogBase
 create history entry for this object
 
   organization = Organization.find(123)
-  result = organization.history_create( 'created', user_id )
+  result = organization.history_log( 'created', user_id )
 
 returns
 
@@ -15,7 +15,7 @@ returns
 
 =end
 
-  def history_create (type, user_id, data = {})
+  def history_log (type, user_id, data = {})
     data[:o_id]                   = self['id']
     data[:history_type]           = type
     data[:history_object]         = self.class.name
@@ -57,8 +57,25 @@ returns
 
 =end
 
-  def history_get
-    History.list( self.class.name, self['id'] )
+  def history_get(fulldata = false)
+    list = History.list( self.class.name, self['id'] )
+    return list if !fulldata
+
+    # get related objects
+    assets = {}
+    list.each {|item|
+      record = Kernel.const_get( item['object'] ).find( item['o_id'] )
+      assets = record.assets(assets)
+
+      if item['related_object']
+        record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
+        assets = record.assets(assets)
+      end
+    }
+    return {
+      :history => list,
+      :assets  => assets,
+    }
   end
 
 end

+ 2 - 0
app/models/group.rb

@@ -5,5 +5,7 @@ class Group < ApplicationModel
   belongs_to               :email_address
   belongs_to               :signature
   validates                :name, :presence => true
+
   activity_stream_support  :role => 'Admin'
+  history_support
 end

Некоторые файлы не были показаны из-за большого количества измененных файлов