aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/CallDb.php
blob: b45577430a39e49425adb874ac87fdb36348bb54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace callnotifier;

/**
 * Logs finished calls into a SQL database.
 *
 * To use this, setup the database table using the script
 * in docs/create-call-log.sql
 */
class Logger_CallDb extends Logger_CallBase
{
    /**
     * 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(
            $dsn, $username, $password,
            array(
                \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
            )
        );
        $this->stmt = $this->db->prepare(
            'INSERT INTO finished ('
            . '  call_start'
            . ', call_end'
            . ', call_type'
            . ', call_from'
            . ', call_from_name'
            . ', call_from_location'
            . ', call_to'
            . ', call_to_name'
            . ', call_to_location'
            . ', call_length'
            . ') VALUES ('
            . '  :call_start'
            . ', :call_end'
            . ', :call_type'
            . ', :call_from'
            . ', :call_from_name'
            . ', :call_from_location'
            . ', :call_to'
            . ', :call_to_name'
            . ', :call_to_location'
            . ', :call_length'
            . ')'
        );
    }

    public function log($type, $arData)
    {
        if ($type != 'finishedCall') {
            return;
        }

        $call = $arData['call'];
        $this->addUnsetVars($call);

        $ret = $this->stmt->execute(
            array(
                'call_start'         => date('Y-m-d H:i:s', $call->start),
                'call_end'           => date('Y-m-d H:i:s', $call->end),
                'call_type'          => $call->type,
                'call_from'          => $call->from,
                'call_from_name'     => $call->fromName,
                'call_from_location' => $call->fromLocation,
                'call_to'            => $call->to,
                'call_to_name'       => $call->toName,
                'call_to_location'   => $call->toLocation,
                'call_length'        => $call->end - $call->start
            )
        );
        if ($ret === false) {
            throw new \Exception(
                'Error logging call to database: '
                . implode(' / ', $this->stmt->errorInfo())
            );
        }
    }

}

?>