begin work on call detaillers (load name from ldap, load location from opengeodb)
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor / Detailler / OpenGeoDb.php
1 <?php
2 namespace callnotifier;
3
4 class CallMonitor_Detailler_OpenGeoDb implements CallMonitor_Detailler
5 {
6     protected $db;
7
8     public function __construct()
9     {
10         $this->db = new \PDO(
11             'mysql:host=dojo;dbname=opengeodb',
12             'opengeodb-read',
13             'opengeodb',
14             array(
15                 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
16                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
17             )
18         );
19     }
20
21     public function loadCallDetails(CallMonitor_Call $call)
22     {
23         if ($call->type == CallMonitor_Call::INCOMING) {
24             $call->fromLocation = $this->loadLocation($call->from);
25         } else {
26             $call->toLocation = $this->loadLocation($call->to);
27         }
28     }
29
30     protected function loadLocation($number)
31     {
32         //area codes in germany can be 3 to 6 numbers
33         //FIXME: what about international numbers?
34         for ($n = 3; $n <= 6; $n++) {
35             $areacode = substr($number, 0, $n);
36             $name = $this->getNameForAreaCode($areacode);
37             if ($name !== null) {
38                 return $name;
39             }
40         }
41
42         return null;
43     }
44
45     protected function getNameForAreaCode($areacode)
46     {
47         $stm = $this->db->query(
48             'SELECT loc_id FROM geodb_textdata'
49             . ' WHERE text_type = "500400000"'//area code
50             . ' AND text_val = ' . $this->db->quote($areacode)
51         );
52         $res = $stm->fetch();
53         if ($res === false) {
54             //area code does not exist
55             return null;
56         }
57
58         $locId = $res['loc_id'];
59         $stm = $this->db->query(
60             'SELECT text_val FROM geodb_textdata'
61             . ' WHERE text_type = "500100000"'//name
62             . ' AND loc_id = ' . $this->db->quote($locId)
63         );
64         $res = $stm->fetch();
65         if ($res === false) {
66             return null;
67         }
68
69         return $res['text_val'];
70     }
71
72 }
73
74 ?>