aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/CallMonitor
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-08-10 18:16:02 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-08-10 18:16:02 +0200
commite839909cbd2ac51620fa1607d92b8b6d5a54f837 (patch)
tree5c15f80199f7501b3cfd66469a2e53bb41076222 /src/callnotifier/CallMonitor
parent4bc203883b0b35be4892768679340effd1348a2f (diff)
downloadauerswald-callnotifier-e839909cbd2ac51620fa1607d92b8b6d5a54f837.tar.gz
auerswald-callnotifier-e839909cbd2ac51620fa1607d92b8b6d5a54f837.zip
show names in CallEcho logger
Diffstat (limited to 'src/callnotifier/CallMonitor')
-rw-r--r--src/callnotifier/CallMonitor/Detailler/CSV.php85
1 files changed, 85 insertions, 0 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;
+ }
+}
+
+?>