show names in CallEcho logger
authorChristian Weiske <cweiske@cweiske.de>
Fri, 10 Aug 2012 16:16:02 +0000 (18:16 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 10 Aug 2012 16:16:02 +0000 (18:16 +0200)
src/callnotifier/CallMonitor/Detailler/CSV.php [new file with mode: 0644]
src/callnotifier/Logger/CallBase.php
src/callnotifier/Logger/CallEcho.php
src/callnotifier/Logger/CallFile.php

diff --git a/src/callnotifier/CallMonitor/Detailler/CSV.php b/src/callnotifier/CallMonitor/Detailler/CSV.php
new file mode 100644 (file)
index 0000000..2261169
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+namespace callnotifier;
+
+/**
+ * Fetch caller names from a CSV file.
+ *
+ * The entries need to be separated by semicolons ";".
+ * The first line is interpreted as table head with column names.
+ *
+ * By default, column names "number" and "name" are used.
+ *
+ * Sets "toName" or "fromName", depending on call type.
+ */
+class CallMonitor_Detailler_CSV implements CallMonitor_Detailler
+{
+    protected $data = array();
+
+    /**
+     * Create new csv name resolver
+     *
+     * @param string $file    Path to CSV file
+     * @param array  $columns Names of the CSV columns that contain "number"
+     *                        and "name", e.g.
+     *                        array('number' => 'telephone', 'name' => 'name')
+     */
+    public function __construct($file, $columns = null)
+    {
+        if ($columns === null) {
+            $columns = array('number' => 'number', 'name' => 'name');
+        }
+        $columns = array_merge(
+            array('number' => 'number', 'name' => 'name'),
+            $columns
+        );
+        $this->loadFile($file, $columns);
+    }
+
+    protected function loadFile($file, $columns)
+    {
+        if (!is_readable($file)) {
+            throw new \Exception('CSV file not readable: ' . $file);
+        }
+        $handle = fopen($file, 'r');
+        if ($handle === false) {
+            throw new \Exception('Error opening CSV file: ' . $file);
+        }
+
+        $colPos = array();
+        $head   = fgetcsv($handle, 1000, ';');
+        foreach ($columns as $key => $colName) {
+            $pos = array_search($colName, $head);
+            if ($pos === false) {
+                throw new \Exception(
+                    'CSV file does not have a colum with name: ' . $colName
+                );
+            }
+            $colPos[$key] = $pos;
+        }
+
+        while (($lineData = fgetcsv($handle, 1000, ';')) !== false) {
+            $this->data[$lineData[$colPos['number']]]
+                = $lineData[$colPos['name']];
+        }
+    }
+
+    public function loadCallDetails(CallMonitor_Call $call)
+    {
+        if ($call->type == CallMonitor_Call::INCOMING) {
+            $call->fromName = $this->loadName($call->from);
+        } else {
+            $call->toName = $this->loadName($call->to);
+        }
+    }
+
+    protected function loadName($number)
+    {
+        if (isset($this->data[$number])) {
+            return $this->data[$number];
+        }
+
+        return null;
+    }
+}
+
+?>
index 8bd143cf623debde66bda0c9b319b8920409d19b..96fe76f550ba446e8a586079ebb513cf0df4eee4 100644 (file)
@@ -14,6 +14,33 @@ abstract class Logger_CallBase implements Logger
             }
         }
     }
+
+
+    protected function getNumberString($call, $type)
+    {
+        $varNumber   = $type;
+        $varName     = $type . 'Name';
+        $varLocation = $type . 'Location';
+
+        if ($call->$varName !== null) {
+            return $call->$varName;
+        }
+
+        $str = $this->getNumber($call->$varNumber);
+        if ($call->$varLocation !== null) {
+            $str .= ' aus ' . $call->$varLocation;
+        }
+        return $str;
+    }
+
+    protected function getNumber($number)
+    {
+        if ($number == '') {
+            $number = '*anonym*';
+        }
+        return str_pad($number, 12, ' ', STR_PAD_RIGHT);
+    }
+
 }
 
 ?>
index 825bc2476a1cbf6465ad52010714a5553072a2b4..ad13406dc6eedcd6e3e8a872a2d366ab4f63d5c9 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 namespace callnotifier;
 
-class Logger_CallEcho implements Logger
+class Logger_CallEcho extends Logger_CallBase
 {
     public function log($type, $arData)
     {
@@ -18,16 +18,18 @@ class Logger_CallEcho implements Logger
 
     protected function displayStart(CallMonitor_Call $call)
     {
+        $this->addUnsetVars($call);
         echo 'Starting ' . $this->getTypeName($call)
-            . ' call from ' . $call->from
-            . ' to ' . $call->to . "\n";
+            . ' call from ' . trim($this->getNumberString($call, 'from'))
+            . ' to ' . trim($this->getNumberString($call, 'to')) . "\n";
     }
 
     protected function displayFinished(CallMonitor_Call $call)
     {
+        $this->addUnsetVars($call);
         echo 'Finished ' . $this->getTypeName($call)
-            . ' call from ' . $call->from
-            . ' to ' . $call->to
+            . ' call from ' . trim($this->getNumberString($call, 'from'))
+            . ' to ' . trim($this->getNumberString($call, 'to'))
             . ', length ' . date('H:i:s', $call->end - $call->start - 3600)
             . "\n";
     }
index ba45137d41b3ccd9f0c52f99a5957151f469f885..850e840f0c072c160a8dc3a64cd744d53fad50e0 100644 (file)
@@ -74,18 +74,10 @@ class Logger_CallFile extends Logger_CallBase
         $str = date('Y-m-d H:i:s', $call->start);
         if ($call->type == CallMonitor_Call::INCOMING) {
             $str .= ' ' . $call->to
-                . ' von  ' . $call->fromName;
-            if ($call->fromLocation) {
-                $str .= ' aus ' . $call->fromLocation;
-            }
-            $str .= ' ' . $this->getNumber($call->from);
+                . ' von  ' . $this->getNumberString($call, 'from');
         } else {
             $str .= ' ' . $call->from
-                . ' nach ' . $call->toName;
-            if ($call->toLocation) {
-                $str .= ' aus ' . $call->toLocation;
-            }
-            $str .= ' ' . $this->getNumber($call->to);
+                . ' nach ' . $this->getNumberString($call, 'to');
         }
 
         $str .= ', Dauer ' . date('H:i:s', $call->end - $call->start - 3600);
@@ -93,14 +85,6 @@ class Logger_CallFile extends Logger_CallBase
         return $str . "\n";
     }
 
-    protected function getNumber($number)
-    {
-        if ($number == '') {
-            $number = '*anonym*';
-        }
-        return str_pad($number, 12, ' ', STR_PAD_RIGHT);
-    }
-
 }
 
 ?>