/**
  * 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');
 }
 /**
  * inspect creation of one record (before create)
  *
  * @param   Tinebase_Record_Interface $_record
  * @return  void
  */
 protected function _inspectBeforeCreate(Tinebase_Record_Interface $_record)
 {
     // resolve telephone number to contacts if possible
     $telNumber = Addressbook_Model_Contact::normalizeTelephoneNoCountry($this->resolveInternalNumber($_record->destination));
     if (null !== $telNumber) {
         $filter = new Addressbook_Model_ContactFilter(array(array('field' => 'telephone_normalized', 'operator' => 'equals', 'value' => $telNumber)));
         $controller = Addressbook_Controller_Contact::getInstance();
         $contacts = $controller->search($filter);
         $relations = $_record->relations;
         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');
         }
         $_record->relations = $relations;
     }
 }
 /**
  * update to 9.8
  *
  * @see 0011934: show contacts in phone call grid
  *
  * @return void
  */
 public function update_7()
 {
     if ($this->getTableVersion('addressbook') < 19) {
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_assistent_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_car_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_cell_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_cell_private_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_fax_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_fax_home_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_home_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_other_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_pager_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_prefer_normalized</name>
                 <type>text</type>
                 <length>32</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $declaration = new Setup_Backend_Schema_Field_Xml('<field>
                 <name>tel_work_normalized</name>
                 <type>text</type>
                 <length>40</length>
                 <notnull>false</notnull>
             </field>');
         $this->_backend->addCol('addressbook', $declaration);
         $this->setTableVersion('addressbook', 19);
         // fill normalized columns with data
         $db = Tinebase_Core::getDb();
         $select = $db->select();
         $columns = array('id', 'tel_assistent', 'tel_car', 'tel_cell', 'tel_cell_private', 'tel_fax', 'tel_fax_home', 'tel_home', 'tel_pager', 'tel_work', 'tel_other', 'tel_prefer');
         // get all telephone columns
         $select->from(SQL_TABLE_PREFIX . 'addressbook', $columns);
         $result = $db->query($select);
         $data = array();
         array_shift($columns);
         $results = $result->fetchAll(Zend_Db::FETCH_ASSOC);
         foreach ($results as $row) {
             foreach ($columns as $col) {
                 if (!empty($row[$col])) {
                     $data[$col . '_normalized'] = Addressbook_Model_Contact::normalizeTelephoneNoCountry((string) $row[$col]);
                 }
             }
             if (count($data) > 0) {
                 $db->update(SQL_TABLE_PREFIX . 'addressbook', $data, $db->quoteInto('id = ?', $row['id']));
                 $data = array();
             }
         }
     }
     $this->setApplicationVersion('Addressbook', '9.8');
 }
 /**
  * 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;
 }