From e839909cbd2ac51620fa1607d92b8b6d5a54f837 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 10 Aug 2012 18:16:02 +0200 Subject: [PATCH] show names in CallEcho logger --- .../CallMonitor/Detailler/CSV.php | 85 +++++++++++++++++++ src/callnotifier/Logger/CallBase.php | 27 ++++++ src/callnotifier/Logger/CallEcho.php | 12 +-- src/callnotifier/Logger/CallFile.php | 20 +---- 4 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 src/callnotifier/CallMonitor/Detailler/CSV.php diff --git a/src/callnotifier/CallMonitor/Detailler/CSV.php b/src/callnotifier/CallMonitor/Detailler/CSV.php new file mode 100644 index 0000000..2261169 --- /dev/null +++ b/src/callnotifier/CallMonitor/Detailler/CSV.php @@ -0,0 +1,85 @@ + '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; + } +} + +?> diff --git a/src/callnotifier/Logger/CallBase.php b/src/callnotifier/Logger/CallBase.php index 8bd143c..96fe76f 100644 --- a/src/callnotifier/Logger/CallBase.php +++ b/src/callnotifier/Logger/CallBase.php @@ -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); + } + } ?> diff --git a/src/callnotifier/Logger/CallEcho.php b/src/callnotifier/Logger/CallEcho.php index 825bc24..ad13406 100644 --- a/src/callnotifier/Logger/CallEcho.php +++ b/src/callnotifier/Logger/CallEcho.php @@ -1,7 +1,7 @@ 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"; } diff --git a/src/callnotifier/Logger/CallFile.php b/src/callnotifier/Logger/CallFile.php index ba45137..850e840 100644 --- a/src/callnotifier/Logger/CallFile.php +++ b/src/callnotifier/Logger/CallFile.php @@ -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); - } - } ?> -- 2.30.2