/** * synchronizes the connections and the associated contacts for one or more lines * @param array $lines */ public function syncConnections($lines) { if (is_array($lines) && count($lines) > 0) { foreach ($lines as $line) { $ids[] = $line['id']; } } $results = array(); $paging = new Tinebase_Model_Pagination(); $filter = new Sipgate_Model_LineFilter(array('AND', array())); $filter->addFilter(new Tinebase_Model_Filter_Id('id', 'in', $ids)); $lines = Sipgate_Controller_Line::getInstance()->search($filter); $totalcount = 0; if ($lines->count()) { $scc = Sipgate_Controller_Connection::getInstance(); $scc->syncContacts($lines); foreach ($lines as $line) { $count = $scc->syncLine($line); $totalcount += $count; $results[$line->getId()] = $count; } } return array('success' => true, 'results' => $results, 'totalcount' => $totalcount); }
/** * assign contacts to calls without assignment, reassign if contact has changed */ public function sync_contacts() { $connections = Sipgate_Controller_Connection::getInstance()->syncContacts(); }
/** * inspects delete action * * @param array $_ids * @return array of ids to actually delete */ protected function _inspectDelete(array $_ids) { if (!is_array($_ids)) { $_ids = array($_ids); } $filter = new Sipgate_Model_ConnectionFilter(array(), 'OR'); foreach ($_ids as $id) { $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'line_id', 'operator' => 'equals', 'value' => $id))); } Sipgate_Controller_Connection::getInstance()->deleteByFilter($filter); return $_ids; }
/** * sync line * @param Sipgate_Model_Line $_line * @param Tinebase_DateTime $_from * @param Tinebase_DateTime $_to * @param boolean $verbose */ public function syncLine(Sipgate_Model_Line $_line, Tinebase_DateTime $_from = NULL, Tinebase_DateTime $_to = NULL, $verbose = false) { $app = Tinebase_Application::getInstance()->getApplicationByName('Sipgate'); if (!Tinebase_Core::getUser()->hasRight($app->getId(), Sipgate_Acl_Rights::SYNC_LINES)) { throw new Tinebase_Exception_AccessDenied('You are not allowed to sync lines!'); } if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->debug('Synchronizing Line ' . $_line->sip_uri . '...'); } $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb()); $count = 0; if ($_from == null) { if ($_line->last_sync == null) { $_from = new Tinebase_DateTime(); $_from->subMonth(2); } else { $_from = $_line->last_sync; } } $_to = $_to ? $_to : new Tinebase_DateTime(); // timezone corrections $_from->setTimezone(Tinebase_Core::getUserTimezone()); $_to->setTimezone(Tinebase_Core::getUserTimezone()); if ($verbose) { echo 'Syncing line ' . $_line->sip_uri . ' from ' . $_from->getIso() . ' to ' . $_to->getIso() . PHP_EOL; } try { if (!$this->_apiBackend) { $this->_apiBackend = Sipgate_Backend_Api::getInstance()->connect($_line->account_id, $sipgate_user, $sipgate_pwd); } $response = $this->_apiBackend->getCallHistory($_line->__get('sip_uri'), $_from, $_to, 0, 1000); if (is_array($response['history']) && $response['totalcount'] > 0) { $paging = new Tinebase_Model_Pagination(); // find already synced entries foreach ($response['history'] as $call) { $entryIds[] = $call['EntryID']; } $filter = new Sipgate_Model_ConnectionFilter(array(), 'AND'); $filter->addFilter(new Tinebase_Model_Filter_Id('entry_id', 'in', $entryIds)); $result = Sipgate_Controller_Connection::getInstance()->search($filter, $paging, false, false); $oldEntries = array(); foreach ($result->getIterator() as $connection) { $oldEntries[] = $connection->entry_id; } unset($result, $connection, $filter); $count = 0; foreach ($response['history'] as $call) { // skip sync if already in db if (in_array($call['EntryID'], $oldEntries)) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug('Skipping call Id' . $call['EntryID'] . '.'); } continue; } $localNumber = $this->_getLocalNumber($call['LocalUri']); $remoteNumber = $this->_getRemoteNumber($call['RemoteUri'], $localNumber, true); $filter = new Addressbook_Model_ContactFilter(array(array("field" => "telephone", "operator" => "contains", "value" => $remoteNumber))); $s = Addressbook_Controller_Contact::getInstance()->search($filter, $paging); $now = new Tinebase_DateTime(); $connection = new Sipgate_Model_Connection(array('tos' => $call['TOS'], 'entry_id' => $call['EntryID'], 'local_uri' => $call['LocalUri'], 'remote_uri' => $call['RemoteUri'], 'status' => $call['Status'], 'local_number' => '+' . $localNumber, 'remote_number' => $remoteNumber == 'anonymous' ? 'anonymous' : '+' . $remoteNumber, 'line_id' => $_line->getId(), 'timestamp' => strtotime($call['Timestamp']), 'creation_time' => $now, 'contact_id' => $s->getFirstRecord() ? $s->getFirstRecord()->getId() : null, 'contact_name' => $s->getFirstRecord() ? $s->getFirstRecord()->n_fn : '')); $count++; $this->create($connection); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug('Creating call Id' . $call['EntryID'] . '.'); } } } } catch (Exception $e) { Tinebase_TransactionManager::getInstance()->rollBack(); throw $e; } $_to->setTimezone('UTC'); $_line->last_sync = $_to; Sipgate_Controller_Line::getInstance()->update($_line); Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId); if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->debug('Synchronizing Line ' . $_line->sip_uri . ' completed. Got ' . $count . ' new connections.'); } return $count; }