diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-08-10 18:16:02 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-08-10 18:16:02 +0200 |
| commit | e839909cbd2ac51620fa1607d92b8b6d5a54f837 (patch) | |
| tree | 5c15f80199f7501b3cfd66469a2e53bb41076222 /src | |
| parent | 4bc203883b0b35be4892768679340effd1348a2f (diff) | |
| download | auerswald-callnotifier-e839909cbd2ac51620fa1607d92b8b6d5a54f837.tar.gz auerswald-callnotifier-e839909cbd2ac51620fa1607d92b8b6d5a54f837.zip | |
show names in CallEcho logger
Diffstat (limited to 'src')
| -rw-r--r-- | src/callnotifier/CallMonitor/Detailler/CSV.php | 85 | ||||
| -rw-r--r-- | src/callnotifier/Logger/CallBase.php | 27 | ||||
| -rw-r--r-- | src/callnotifier/Logger/CallEcho.php | 12 | ||||
| -rw-r--r-- | src/callnotifier/Logger/CallFile.php | 20 |
4 files changed, 121 insertions, 23 deletions
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 @@ +<?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; + } +} + +?> 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 @@ <?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"; } 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); - } - } ?> |
