all loggers support call type and MSN filtering now
[auerswald-callnotifier.git] / src / callnotifier / Logger / CallBase.php
1 <?php
2 namespace callnotifier;
3
4 abstract class Logger_CallBase implements Logger
5 {
6     protected $callTypes;
7     protected $msns;
8
9
10     /**
11      * Create a new call logger.
12      *
13      * @param string $callTypes Which types of call to log:
14      *                          - "i" - incoming calls only
15      *                          - "o" - outgoing calls only
16      *                          - "io" - both incoming and outgoing calls
17      * @param array  $msns      Array of MSN (Multi Subscriber Number) that
18      *                          calls to shall get logged.
19      *                          If the array is empty, calls to all MSNs get
20      *                          logged.
21      */
22     public function __construct($callTypes = 'io', $msns = array())
23     {
24         $this->callTypes = $callTypes;
25         $this->msns      = (array)$msns;
26     }
27
28     /**
29      * Check if the call type (incoming or outgoing) shall be logged.
30      *
31      * @return boolean True if it should be logged, false if not
32      */
33     protected function hasValidType($call)
34     {
35         if ($call->type == CallMonitor_Call::INCOMING && $this->callTypes == 'o') {
36             return false;
37         }
38         if ($call->type == CallMonitor_Call::OUTGOING && $this->callTypes == 'i') {
39             return false;
40         }
41
42         return true;
43     }
44
45     /**
46      * Check if the MSN shall be logged
47      *
48      * @return boolean True if it should be logged, false if not
49      */
50     protected function hasValidMsn($call)
51     {
52         if ($call->type == CallMonitor_Call::INCOMING) {
53             $msn = $call->to;
54         } else {
55             $msn = $call->from;
56         }
57         if (count($this->msns) > 0 && !in_array($msn, $this->msns)) {
58             //msn shall not be logged
59             return false;
60         }
61
62         return true;
63     }
64
65     protected function addUnsetVars($call)
66     {
67         static $expectedVars = array(
68             'toName', 'fromName', 'toLocation', 'fromLocation'
69         );
70         foreach ($expectedVars as $varName) {
71             if (!isset($call->$varName)) {
72                 $call->$varName = null;
73             }
74         }
75     }
76
77
78     protected function getNumberString($call, $type)
79     {
80         $varNumber   = $type;
81         $varName     = $type . 'Name';
82         $varLocation = $type . 'Location';
83
84         if ($call->$varName !== null) {
85             return $call->$varName;
86         }
87
88         $str = $this->getNumber($call->$varNumber);
89         if ($call->$varLocation !== null) {
90             $str .= ' aus ' . $call->$varLocation;
91         }
92         return $str;
93     }
94
95     protected function getNumber($number)
96     {
97         if ($number == '') {
98             $number = '*anonym*';
99         }
100         return str_pad($number, 12, ' ', STR_PAD_RIGHT);
101     }
102
103 }
104
105 ?>