2 namespace callnotifier;
5 * Logs finished calls into a SQL database.
7 * To use this, setup the database table using the script
8 * in docs/create-call-log.sql
10 class Logger_CallDb extends Logger_CallBase
18 * Create new detailler object
20 * @param string $dsn PDO connection string, for example
21 * 'mysql:host=dojo;dbname=opengeodb'
22 * @param string $username Database username
23 * @param string $password Database password
25 public function __construct(
26 $dsn, $username, $password, $callTypes = 'io', $msns = array()
28 parent::__construct($callTypes, $msns);
31 $this->username = $username;
32 $this->password = $password;
33 //check if the credentials are correct
38 * Connect to the SQL server.
39 * SQL servers close the connection automatically after some hours,
40 * and since calls often don't come in every minute, we will have
41 * disconnects in between.
42 * Thus, we will reconnect on every location load.
46 protected function connect()
49 $this->dsn, $this->username, $this->password,
51 \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
52 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
53 \PDO::ATTR_PERSISTENT => true
58 public function log($type, $arData)
60 if ($type != 'finishedCall') {
64 $call = $arData['call'];
65 if (!$this->hasValidType($call)) {
68 if (!$this->hasValidMsn($call)) {
72 $this->addUnsetVars($call);
75 $stmt = $this->prepareDbStatement();
76 $ret = $stmt->execute(
78 'call_start' => date('Y-m-d H:i:s', $call->start),
79 'call_end' => date('Y-m-d H:i:s', $call->end),
80 'call_type' => $call->type,
81 'call_from' => $call->from,
82 'call_from_name' => $call->fromName,
83 'call_from_location' => $call->fromLocation,
84 'call_to' => $call->to,
85 'call_to_name' => $call->toName,
86 'call_to_location' => $call->toLocation,
87 'call_length' => $call->end - $call->start
92 'Error logging call to database: '
93 . implode(' / ', $stmt->errorInfo())
98 protected function prepareDbStatement()
100 return $this->db->prepare(
101 'INSERT INTO finished ('
107 . ', call_from_location'
110 . ', call_to_location'
117 . ', :call_from_name'
118 . ', :call_from_location'
121 . ', :call_to_location'