load call details on finished calls, too
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor.php
index be155e12387bb81fe0eeea0f3083e1319ed24013..4923967d793f767d4eac129a5b1e7b36e765e4d2 100644 (file)
@@ -6,19 +6,31 @@ namespace callnotifier;
  * and notifies loggers of incoming and finished calls.
  *
  * Notifications:
- * - incomingCall
+ * - startingCall
  * - finishedCall
  */
 class CallMonitor
 {
     protected $currentCalls = array();
 
+    /**
+     * Array of objects that are able to load details for a call
+     *
+     * @var array
+     */
+    protected $detaillers = array();
+
     public function __construct($config, $log)
     {
         $this->config = $config;
         $this->log = $log;
     }
 
+    public function addDetailler(CallMonitor_Detailler $detailler)
+    {
+        $this->detaillers[] = $detailler;
+    }
+
     public function handle(EDSS1_Message $msg)
     {
         $callId = $msg->callRef;
@@ -49,7 +61,7 @@ class CallMonitor
             $call->type = CallMonitor_Call::OUTGOING;
         }
 
-        $this->handleParams($call, $msg);
+        $this->handleParams($msg, $call, $callId);
     }
 
 
@@ -59,7 +71,7 @@ class CallMonitor
 
         switch ($msg->type) {
         case EDSS1_Message::INFORMATION:
-            $this->handleParams($call, $msg);
+            $this->handleParams($msg, $call, $callId);
             break;
         case EDSS1_Message::ALERTING:
             if ($call->type == CallMonitor_Call::OUTGOING) {
@@ -82,19 +94,23 @@ class CallMonitor
                     unset($this->currentCalls[$otherCallId]);
                 }
             }
+            $this->loadCallDetails($call);
             $this->log->log('startingCall', array('call' => $call));
             break;
 
         case EDSS1_Message::RELEASE:
         case EDSS1_Message::RELEASE_COMPLETE:
             $call->end = time();
+            //we need to load details here because they might not have been
+            //loaded yet, e.g. for calls to MSNs that have no phones.
+            $this->loadCallDetails($call);
             $this->log->log('finishedCall', array('call' => $call));
             unset($this->currentCalls[$callId]);
             break;
         }
     }
 
-    protected function handleParams($call, $msg)
+    protected function handleParams($msg, $call, $callId)
     {
         foreach ($msg->parameters as $param) {
             switch ($param->type) {
@@ -107,7 +123,18 @@ class CallMonitor
                 $call->to = $this->getFullNumber(
                     $param->number, $param->numberType
                 );
+                if ($call->type == CallMonitor_Call::INCOMING
+                    && $param->numberType != EDSS1_Parameter_Names::NUMBER_SUBSCRIBER
+                ) {
+                    //only keep incoming calls that arrive at the switchboard,
+                    // not the ones from the switchboard to the telephones
+                    unset($this->currentCalls[$callId]);
+                }
                 break;
+            case EDSS1_Parameter::KEYPAD:
+                if ($call->to === null) {
+                    $call->to = $param->data;
+                }
             }
         }
     }
@@ -117,9 +144,24 @@ class CallMonitor
     {
         if ($type == EDSS1_Parameter_Names::NUMBER_NATIONAL) {
             return '0' . $number;
+        } else if ($type == EDSS1_Parameter_Names::NUMBER_INTERNATIONAL) {
+            return '+' . $number;
         }
         return $number;
     }
+
+    /**
+     * Load details for a call, e.g. the name of the calling person
+     * or the area
+     *
+     * @return void
+     */
+    protected function loadCallDetails($call)
+    {
+        foreach ($this->detaillers as $detailler) {
+            $detailler->loadCallDetails($call);
+        }
+    }
 }
 
 ?>