rename database call log script
[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($dsn, $username, $password)
21     {
22         $this->db = new \PDO(
23             $dsn, $username, $password,
24             array(
25                 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
26                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
27             )
28         );
29         $this->stmt = $this->db->prepare(
30             'INSERT INTO finished ('
31             . '  call_start'
32             . ', call_end'
33             . ', call_type'
34             . ', call_from'
35             . ', call_from_name'
36             . ', call_from_location'
37             . ', call_to'
38             . ', call_to_name'
39             . ', call_to_location'
40             . ', call_length'
41             . ') VALUES ('
42             . '  :call_start'
43             . ', :call_end'
44             . ', :call_type'
45             . ', :call_from'
46             . ', :call_from_name'
47             . ', :call_from_location'
48             . ', :call_to'
49             . ', :call_to_name'
50             . ', :call_to_location'
51             . ', :call_length'
52             . ')'
53         );
54     }
55
56     public function log($type, $arData)
57     {
58         if ($type != 'finishedCall') {
59             return;
60         }
61
62         $call = $arData['call'];
63         $this->addUnsetVars($call);
64
65         $ret = $this->stmt->execute(
66             array(
67                 'call_start'         => date('Y-m-d H:i:s', $call->start),
68                 'call_end'           => date('Y-m-d H:i:s', $call->end),
69                 'call_type'          => $call->type,
70                 'call_from'          => $call->from,
71                 'call_from_name'     => $call->fromName,
72                 'call_from_location' => $call->fromLocation,
73                 'call_to'            => $call->to,
74                 'call_to_name'       => $call->toName,
75                 'call_to_location'   => $call->toLocation,
76                 'call_length'        => $call->end - $call->start
77             )
78         );
79         if ($ret === false) {
80             throw new \Exception(
81                 'Error logging call to database: '
82                 . implode(' / ', $this->stmt->errorInfo())
83             );
84         }
85     }
86
87 }
88
89 ?>