X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/blobdiff_plain/ebb891c12348e7f1e44161acb605b81b7e799595..1b28b0289cdcb6adcbd496be07fda3a5ec705bfd:/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php diff --git a/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php b/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php index a37ede0..46711b0 100644 --- a/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php +++ b/src/callnotifier/CallMonitor/Detailler/OpenGeoDb.php @@ -1,6 +1,17 @@ 'O2', ); - public function __construct() + /** + * Create new detailler object + * + * @param string $dsn PDO connection string, for example + * 'mysql:host=dojo;dbname=opengeodb' + * @param string $username Database username + * @param string $password Database password + */ + public function __construct($dsn, $username, $password) { $this->db = new \PDO( - 'mysql:host=dojo;dbname=opengeodb', - 'opengeodb-read', - 'opengeodb', + $dsn, $username, $password, array( \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC @@ -41,9 +58,13 @@ class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler public function loadCallDetails(CallMonitor_Call $call) { if ($call->type == CallMonitor_Call::INCOMING) { - $call->fromLocation = $this->loadLocation($call->from); + if (!isset($call->fromLocation) || $call->fromLocation === null) { + $call->fromLocation = $this->loadLocation($call->from); + } } else { - $call->toLocation = $this->loadLocation($call->to); + if (!isset($call->toLocation) || $call->toLocation === null) { + $call->toLocation = $this->loadLocation($call->to); + } } } @@ -57,44 +78,29 @@ class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler } return null; } - //area codes in germany can be 3 to 6 numbers - //FIXME: what about international numbers? - for ($n = 3; $n <= 6; $n++) { - $areacode = substr($number, 0, $n); - $name = $this->getNameForAreaCode($areacode); - if ($name !== null) { - return $name; - } - } - - return null; - } - protected function getNameForAreaCode($areacode) - { + //FIXME: what about international numbers? + //area codes in germany can be 3 to 6 numbers $stm = $this->db->query( - 'SELECT loc_id FROM geodb_textdata' - . ' WHERE text_type = "500400000"'//area code - . ' AND text_val = ' . $this->db->quote($areacode) + 'SELECT name FROM my_orte' + . ' WHERE vorwahl = ' . $this->db->quote(substr($number, 0, 3)) + . ' OR vorwahl = ' . $this->db->quote(substr($number, 0, 4)) + . ' OR vorwahl = ' . $this->db->quote(substr($number, 0, 5)) + . ' OR vorwahl = ' . $this->db->quote(substr($number, 0, 6)) + . ' ORDER BY einwohner DESC' ); - $res = $stm->fetch(); - if ($res === false) { - //area code does not exist - return null; + if ($stm === false) { + throw new \Exception( + implode(' - ', $this->db->errorInfo()) + ); } - $locId = $res['loc_id']; - $stm = $this->db->query( - 'SELECT text_val FROM geodb_textdata' - . ' WHERE text_type = "500100000"'//name - . ' AND loc_id = ' . $this->db->quote($locId) - ); $res = $stm->fetch(); if ($res === false) { return null; } - return $res['text_val']; + return $res['name']; } }