DataObjects/Core_event_audit.php
[Pman.Core] / DataObjects / Events.php
index 2386636..8f84ff9 100644 (file)
@@ -44,7 +44,7 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
         $pg = HTML_FlexyFramework::get()->page;
         $au = $pg->getAuthUser();
          
-        $this->person_name = $au ? $au->name : '';
+        $this->person_name = $au && !empty($au->name) ? $au->name : '';
         $this->person_id = $au ? $au->id : '';
         $this->ipaddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'cli';
         $this->action = $act;
@@ -59,4 +59,42 @@ class Pman_Core_DataObjects_Events extends DB_DataObject
         $rem[] = $remarks;
         $this->remarks = implode(' : ', $rem);
     }
+    
+    /**
+     * Generate an audit for this field.
+     *
+     * @param {DB_DataObject} new data
+     * @param {DB_DataObject} old data
+     * 
+     * @return {int} number of entries logged.
+     */
+    
+    function audit($new, $old = false)
+    {
+        if ($old == $new) {
+            return 0; // they are the same...
+        }
+         
+        $ret = 0;
+        foreach(array_keys($new->table()) as $k) {
+            // should we JSON serialize this?
+            $n = empty($new->$k) ? '' : $new->$k;
+            $o = empty($old->$k) || empty($old->$k) ? '' : $old->$k;
+            if ($n == $o) {
+                continue;
+            }
+            $x = DB_DataObject::factory('core_event_audit');
+            $x->setFrom(array(
+                'event_id' => $this->id,
+                'name' => $k,
+                'old_audit_id' => $old ? $x->findLast($this, $k) : 0,
+                'newvalue' => $n
+
+            ));
+            $x->insert();
+            $ret++;
+        }
+        return $ret;
+    }
+     
 }