e2cdc6fd37946daae4e2310217b572e99b5a47c0
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallDb.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Logs finished calls into a SQL database.
6  *
7  * To use this, setup the database table using the script
8  * in docs/create-call-log.sql
9  */
10 class Logger_CallDb extends Logger_CallBase
11 {
12     /**
13      * Create new detailler object
14      *
15      * @param string $dsn      PDO connection string, for example
16      *                         'mysql:host=dojo;dbname=opengeodb'
17      * @param string $username Database username
18      * @param string $password Database password
19      */
20     public function __construct(
21         $dsn, $username, $password,
22         $callTypes = 'i', $msns = array()
23     ) {
24         parent::__construct($callTypes, $msns);
25         $this->db = new \PDO(
26             $dsn, $username, $password,
27             array(
28                 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
29                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
30             )
31         );
32         $this->stmt = $this->db->prepare(
33             'INSERT INTO finished ('
34             . '  call_start'
35             . ', call_end'
36             . ', call_type'
37             . ', call_from'
38             . ', call_from_name'
39             . ', call_from_location'
40             . ', call_to'
41             . ', call_to_name'
42             . ', call_to_location'
43             . ', call_length'
44             . ') VALUES ('
45             . '  :call_start'
46             . ', :call_end'
47             . ', :call_type'
48             . ', :call_from'
49             . ', :call_from_name'
50             . ', :call_from_location'
51             . ', :call_to'
52             . ', :call_to_name'
53             . ', :call_to_location'
54             . ', :call_length'
55             . ')'
56         );
57     }
58
59     public function log($type, $arData)
60     {
61         if ($type != 'finishedCall') {
62             return;
63         }
64
65         $call = $arData['call'];
66         if (!$this->hasValidType($call)) {
67             return;
68         }
69         if (!$this->hasValidMsn($call)) {
70             return;
71         }
72
73         $this->addUnsetVars($call);
74
75         $ret = $this->stmt->execute(
76             array(
77                 'call_start'         => date('Y-m-d H:i:s', $call->start),
78                 'call_end'           => date('Y-m-d H:i:s', $call->end),
79                 'call_type'          => $call->type,
80                 'call_from'          => $call->from,
81                 'call_from_name'     => $call->fromName,
82                 'call_from_location' => $call->fromLocation,
83                 'call_to'            => $call->to,
84                 'call_to_name'       => $call->toName,
85                 'call_to_location'   => $call->toLocation,
86                 'call_length'        => $call->end - $call->start
87             )
88         );
89         if ($ret === false) {
90             throw new \Exception(
91                 'Error logging call to database: '
92                 . implode(' / ', $this->stmt->errorInfo())
93             );
94         }
95     }
96
97 }
98
99 ?>