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