/**
  * Search for calls matching given arguments
  *
  * @param  array $filter
  * @param  array $paging
  * @return array
  */
 public function searchCalls($filter, $paging)
 {
     $result = $this->_search($filter, $paging, Phone_Controller_Call::getInstance(), 'Phone_Model_CallFilter');
     $countResult = Phone_Controller_Call::getInstance()->search($this->_decodeFilter($filter, 'Phone_Model_CallFilter'), NULL, FALSE, TRUE);
     $result['totalcount'] = count($countResult);
     return $result;
 }
 /**
  * update to 9.1
  *
  * @return void
  */
 public function update_0()
 {
     // we need at least addressbook version 9,7
     if (version_compare($this->getApplicationVersion('Addressbook'), '9.8') < 0) {
         return;
     }
     $setupUser = $this->_getSetupFromConfigOrCreateOnTheFly();
     if ($setupUser) {
         Tinebase_Core::set(Tinebase_Core::USER, $setupUser);
         $filter = new Phone_Model_CallFilter(array(array('field' => 'start', 'operator' => 'after', 'value' => date('Y-m-d H:i:s', time() - 3600 * 24 * 30 * 3))), 'AND', array('ignoreAcl' => true));
         $addressbookController = Addressbook_Controller_Contact::getInstance();
         $phoneController = Phone_Controller_Call::getInstance();
         $calls = $phoneController->search($filter);
         foreach ($calls as $_record) {
             // resolve telephone number to contacts if possible
             $telNumber = Addressbook_Model_Contact::normalizeTelephoneNoCountry($phoneController->resolveInternalNumber($_record->destination));
             if (null === $telNumber) {
                 continue;
             }
             $filter = new Addressbook_Model_ContactFilter(array(array('field' => 'telephone_normalized', 'operator' => 'equals', 'value' => $telNumber)));
             $contacts = $addressbookController->search($filter);
             $relations = array();
             foreach ($contacts as $contact) {
                 $relations[] = array('related_model' => 'Addressbook_Model_Contact', 'related_id' => $contact->getId(), 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_backend' => Tinebase_Model_Relation::DEFAULT_RECORD_BACKEND, 'type' => 'CALLER');
             }
             if (count($relations) > 0) {
                 $_record->relations = $relations;
                 $phoneController->update($_record);
             }
         }
     }
     $this->setApplicationVersion('Phone', '9.1');
 }
 /**
  * the singleton pattern
  *
  * @return Phone_Controller_Call
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * @see 0011934: show contacts in phone call grid
  */
 public function testContactRelation()
 {
     $phoneNumber = '0406437435';
     $myContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
     $myContact->tel_work = $phoneNumber;
     Addressbook_Controller_Contact::getInstance()->update($myContact);
     $call = new Phone_Model_Call(array('line_id' => 'phpunitlineid', 'phone_id' => 'phpunitphoneid', 'direction' => Phone_Model_Call::TYPE_INCOMING, 'source' => '26', 'destination' => $phoneNumber));
     $call = Phone_Controller_Call::getInstance()->create($call);
     $this->assertEquals(1, count($call->relations), 'my contact should be added as relation to the call' . print_r($call->toArray(), true));
     $this->assertEquals($myContact->getId(), $call->relations->getFirstRecord()->related_id);
 }
 /**
  * Sets up the fixture.
  * This method is called before a test is executed.
  *
  * @access protected
  */
 protected function setUp()
 {
     parent::setUp();
     $this->_json = new Phone_Frontend_Json();
     $this->_adminUser = Tinebase_Core::getUser();
     $this->_objects['location'] = new Voipmanager_Model_Snom_Location(array('id' => Tinebase_Record_Abstract::generateUID(), 'name' => 'phpunit test location', 'registrar' => 'registrar'));
     $this->_objects['software'] = new Voipmanager_Model_Snom_Software(array('id' => Tinebase_Record_Abstract::generateUID()));
     $this->_objects['setting'] = new Voipmanager_Model_Snom_Setting(array('id' => Tinebase_Record_Abstract::generateUID(), 'name' => 'test setting', 'description' => 'test setting'));
     $this->_objects['template'] = new Voipmanager_Model_Snom_Template(array('id' => Tinebase_Record_Abstract::generateUID(), 'name' => 'phpunit test location', 'model' => 'snom320', 'software_id' => $this->_objects['software']->getId(), 'setting_id' => $this->_objects['setting']->getId()));
     $this->_objects['phone'] = new Voipmanager_Model_Snom_Phone(array('id' => Tinebase_Record_Abstract::generateUID(), 'macaddress' => "1234567890cd", 'description' => 'user phone', 'location_id' => $this->_objects['location']->getId(), 'template_id' => $this->_objects['template']->getId(), 'current_model' => 'snom320', 'redirect_event' => 'none'));
     $this->_objects['phone1'] = new Voipmanager_Model_Snom_Phone(array('id' => Tinebase_Record_Abstract::generateUID(), 'macaddress' => "a234567890cd", 'description' => 'second user phone', 'location_id' => $this->_objects['location']->getId(), 'template_id' => $this->_objects['template']->getId(), 'current_model' => 'snom320', 'redirect_event' => 'none'));
     $this->_objects['phone2'] = new Voipmanager_Model_Snom_Phone(array('id' => Tinebase_Record_Abstract::generateUID(), 'macaddress' => "1a34567890cd", 'description' => 'jsmith phone', 'location_id' => $this->_objects['location']->getId(), 'template_id' => $this->_objects['template']->getId(), 'current_model' => 'snom320', 'redirect_event' => 'none'));
     $this->_objects['phonesetting'] = new Voipmanager_Model_Snom_PhoneSettings(array('phone_id' => $this->_objects['phone']->getId(), 'web_language' => 'English'));
     $this->_objects['phonesetting1'] = new Voipmanager_Model_Snom_PhoneSettings(array('phone_id' => $this->_objects['phone1']->getId(), 'web_language' => 'English'));
     $this->_objects['phonesetting2'] = new Voipmanager_Model_Snom_PhoneSettings(array('phone_id' => $this->_objects['phone2']->getId(), 'web_language' => 'English'));
     $this->_objects['phoneOwner'] = array('account_id' => $this->_adminUser->getId(), 'account_type' => 'user');
     $this->_objects['phoneOwner2'] = array('account_id' => $this->_personas['jsmith']->getId(), 'account_type' => 'user');
     $rights = new Tinebase_Record_RecordSet('Voipmanager_Model_Snom_PhoneRight', array($this->_objects['phoneOwner']));
     $rights1 = new Tinebase_Record_RecordSet('Voipmanager_Model_Snom_PhoneRight', array($this->_objects['phoneOwner']));
     $rights2 = new Tinebase_Record_RecordSet('Voipmanager_Model_Snom_PhoneRight', array($this->_objects['phoneOwner2']));
     $this->_objects['phone']->rights = $rights;
     $this->_objects['phone1']->rights = $rights1;
     $this->_objects['phone2']->rights = $rights2;
     $this->_objects['context'] = new Voipmanager_Model_Asterisk_Context(array('id' => Tinebase_Record_Abstract::generateUID(), 'name' => 'test context', 'description' => 'test context'));
     $this->_objects['sippeer'] = new Voipmanager_Model_Asterisk_SipPeer(array('id' => Tinebase_Record_Abstract::generateUID(), 'context_id' => $this->_objects['context']->getId()));
     $this->_objects['sippeer1'] = new Voipmanager_Model_Asterisk_SipPeer(array('id' => Tinebase_Record_Abstract::generateUID(), 'context_id' => $this->_objects['context']->getId()));
     $this->_objects['sippeer2'] = new Voipmanager_Model_Asterisk_SipPeer(array('id' => Tinebase_Record_Abstract::generateUID(), 'context_id' => $this->_objects['context']->getId()));
     $this->_objects['line'] = new Voipmanager_Model_Snom_Line(array('id' => Tinebase_Record_Abstract::generateUID(), 'snomphone_id' => $this->_objects['phone']->getId(), 'asteriskline_id' => $this->_objects['sippeer']->getId(), 'linenumber' => 1, 'lineactive' => 1));
     $this->_objects['line1'] = new Voipmanager_Model_Snom_Line(array('id' => Tinebase_Record_Abstract::generateUID(), 'snomphone_id' => $this->_objects['phone1']->getId(), 'asteriskline_id' => $this->_objects['sippeer1']->getId(), 'linenumber' => 1, 'lineactive' => 1));
     $this->_objects['line2'] = new Voipmanager_Model_Snom_Line(array('id' => Tinebase_Record_Abstract::generateUID(), 'snomphone_id' => $this->_objects['phone2']->getId(), 'asteriskline_id' => $this->_objects['sippeer2']->getId(), 'linenumber' => 1, 'lineactive' => 1));
     // create phone, location, template, rights
     $phoneBackend = new Voipmanager_Backend_Snom_Phone(array('modelName' => 'Phone_Model_MyPhone'));
     $snomLocationBackend = new Voipmanager_Backend_Snom_Location();
     $snomSettingBackend = new Voipmanager_Backend_Snom_Setting();
     $snomPhoneSettingBackend = new Voipmanager_Backend_Snom_PhoneSettings();
     $snomTemplateBackend = new Voipmanager_Backend_Snom_Template();
     $snomSoftwareBackend = new Voipmanager_Backend_Snom_Software();
     $snomLineBackend = new Voipmanager_Backend_Snom_Line();
     $asteriskSipPeerBackend = new Voipmanager_Backend_Asterisk_SipPeer();
     $asteriskContextBackend = new Voipmanager_Backend_Asterisk_Context();
     $snomSoftwareBackend->create($this->_objects['software']);
     $snomLocationBackend->create($this->_objects['location']);
     $snomSettingBackend->create($this->_objects['setting']);
     $snomTemplateBackend->create($this->_objects['template']);
     $phoneBackend->create($this->_objects['phone']);
     $phoneBackend->create($this->_objects['phone1']);
     $phoneBackend->create($this->_objects['phone2']);
     $phoneBackend->setPhoneRights($this->_objects['phone']);
     $phoneBackend->setPhoneRights($this->_objects['phone1']);
     $phoneBackend->setPhoneRights($this->_objects['phone2']);
     $asteriskContextBackend->create($this->_objects['context']);
     $asteriskSipPeerBackend->create($this->_objects['sippeer']);
     $asteriskSipPeerBackend->create($this->_objects['sippeer1']);
     $asteriskSipPeerBackend->create($this->_objects['sippeer2']);
     $snomLineBackend->create($this->_objects['line']);
     $snomLineBackend->create($this->_objects['line1']);
     $snomLineBackend->create($this->_objects['line2']);
     $snomPhoneSettingBackend->create($this->_objects['phonesetting']);
     $snomPhoneSettingBackend->create($this->_objects['phonesetting1']);
     $snomPhoneSettingBackend->create($this->_objects['phonesetting2']);
     /******************** call history *************************/
     $phoneController = Phone_Controller::getInstance();
     $this->_objects['call1'] = new Phone_Model_Call(array('id' => 'phpunitcallhistoryid1', 'line_id' => $this->_objects['line']->getId(), 'phone_id' => $this->_objects['phone']->getId(), 'direction' => Phone_Model_Call::TYPE_INCOMING, 'source' => '26', 'destination' => '0406437435'));
     $this->_objects['call2'] = new Phone_Model_Call(array('id' => 'phpunitcallhistoryid2', 'line_id' => $this->_objects['line']->getId(), 'phone_id' => $this->_objects['phone']->getId(), 'direction' => Phone_Model_Call::TYPE_INCOMING, 'source' => '26', 'destination' => '050364354'));
     $this->_objects['call2a'] = new Phone_Model_Call(array('id' => 'phpunitcallhistoryid2a', 'line_id' => $this->_objects['line1']->getId(), 'phone_id' => $this->_objects['phone1']->getId(), 'direction' => Phone_Model_Call::TYPE_INCOMING, 'source' => '27', 'destination' => '050364354'));
     $this->_objects['call3'] = new Phone_Model_Call(array('id' => 'phpunitcallhistoryid3', 'line_id' => $this->_objects['line2']->getId(), 'phone_id' => $this->_objects['phone2']->getId(), 'direction' => Phone_Model_Call::TYPE_INCOMING, 'source' => '78', 'destination' => '050998877'));
     $scleverContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId($this->_personas['sclever']->getId());
     $this->_objects['call4'] = new Phone_Model_Call(array('id' => 'phpunitcallhistoryid4', 'line_id' => $this->_objects['line2']->getId(), 'phone_id' => $this->_objects['phone']->getId(), 'direction' => Phone_Model_Call::TYPE_INCOMING, 'source' => '78', 'destination' => $scleverContact->tel_work));
     $this->_objects['paging'] = array('start' => 0, 'limit' => 50, 'sort' => 'start', 'dir' => 'ASC');
     $this->_objects['filter1'] = array(array('field' => 'query', 'operator' => 'contains', 'value' => ''));
     $this->_objects['filter2'] = array(array('field' => 'query', 'operator' => 'contains', 'value' => '05036'), array('field' => 'phone_id', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'equals', 'value' => $this->_objects['phone1']->getId()))));
     $this->_objects['filter2a'] = array(array('field' => 'query', 'operator' => 'contains', 'value' => '05036'), array('field' => 'phone_id', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'equals', 'value' => $this->_objects['phone1']->getId()))));
     $this->_objects['filter2b'] = array(array('field' => 'destination', 'operator' => 'contains', 'value' => '05036'));
     $this->_objects['filter3'] = array(array('field' => 'phone_id', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'equals', 'value' => $this->_objects['phone']->getId()))));
     $this->_objects['filter4'] = array(array('field' => 'phone_id', 'operator' => 'AND', 'value' => array(array('field' => ':id', 'operator' => 'equals', 'value' => $this->_objects['phone2']->getId()))));
     $this->_objects['filter5'] = array(array('field' => 'query', 'operator' => 'contains', 'value' => '998877'));
     // create calls
     $phoneController->callStarted($this->_objects['call1']);
     $phoneController->callStarted($this->_objects['call2']);
     $phoneController->callStarted($this->_objects['call2a']);
     $phoneController->callStarted($this->_objects['call3']);
     // create with normal controller to make sure we get contact relation
     Phone_Controller_Call::getInstance()->create($this->_objects['call4']);
 }
 protected function _createCalls()
 {
     $lineId = $this->_phone->lines[0]['id'];
     $phoneId = $this->_phone->getId();
     $d1 = Tinebase_DateTime::now()->subDay()->setTime(12, 00, 00);
     $d2 = clone $d1;
     $d2->addSecond(10);
     $d3 = clone $d2;
     $d3->addMinute(20);
     $i = 1;
     while ($i < 20) {
         $call = array('line_id' => $lineId, 'phone_id' => $phoneId, 'callerid' => $i * 100, 'start' => $d1, 'connected' => $d2, 'disconnected' => $d3, 'duration' => $d3->getTimestamp() - $d2->getTimestamp(), 'ringing' => $i, 'direction' => $i % 2 ? 'in' : 'out', 'source' => '+49123456789', 'destination' => '+44' . $i . $i . $i . $i);
         Phone_Controller_Call::getInstance()->create(new Phone_Model_Call($call));
         $d1->addMinute(20)->subSecond($i);
         $d2->addMinute(20)->addSecond($i);
         $d3->addMinute(20)->addSecond($i * 1);
         $i++;
     }
 }
 /**
  * start phone call and save in history
  *
  * @param Phone_Model_Call $_call
  * @return Phone_Model_Call
  */
 public function callStarted(Phone_Model_Call $_call)
 {
     $backend = Phone_Backend_Factory::factory(Phone_Backend_Factory::CALLHISTORY);
     $_call->start = Tinebase_DateTime::now();
     $filter = new Voipmanager_Model_Asterisk_SipPeerFilter(array(array('field' => 'name', 'operator' => 'equals', 'value' => $_call->line_id)));
     $asteriskSipPeers = Voipmanager_Controller_Asterisk_SipPeer::getInstance()->search($filter);
     if (count($asteriskSipPeers) > 0) {
         $_call->callerid = $asteriskSipPeers[0]->callerid;
     } else {
         $_call->callerid = $_call->line_id;
     }
     $call = $backend->create($_call);
     // resolve telephone number to contacts if possible
     $phoneController = Phone_Controller_Call::getInstance();
     $telNumber = Addressbook_Model_Contact::normalizeTelephoneNoCountry($phoneController->resolveInternalNumber($call->destination));
     if (null !== $telNumber) {
         $filter = new Addressbook_Model_ContactFilter(array(array('field' => 'telephone_normalized', 'operator' => 'equals', 'value' => $telNumber)));
         $controller = Addressbook_Controller_Contact::getInstance();
         $oldAclChecks = $controller->doContainerACLChecks();
         $controller->doContainerACLChecks(false);
         $contacts = $controller->search($filter);
         $relationBackend = new Tinebase_Relation_Backend_Sql();
         foreach ($contacts as $contact) {
             // we dont add the relations to the call record as this is called by the phone server, so no need to return the relations
             $relationBackend->addRelation(new Tinebase_Model_Relation(array('own_model' => 'Phone_Model_Call', 'own_id' => $call->getId(), 'own_backend' => Tinebase_Model_Relation::DEFAULT_RECORD_BACKEND, 'related_model' => 'Addressbook_Model_Contact', 'related_id' => $contact->getId(), 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_backend' => Tinebase_Model_Relation::DEFAULT_RECORD_BACKEND, 'type' => 'CALLER')));
         }
         $controller->doContainerACLChecks($oldAclChecks);
     }
     return $call;
 }