/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @access protected */ protected function setUp() { $this->object = new Tinebase_Relation_Backend_Sql(); foreach ($this->relationData as $num => $relation) { $this->relations[$num] = $this->object->addRelation(new Tinebase_Model_Relation($relation, true)); } }
/** * adds a new relation * * @param Tinebase_Model_Relation $_relation * @return Tinebase_Model_Relation|NULL the new relation * @throws Tinebase_Exception_Record_Validation */ protected function _addRelation(Tinebase_Model_Relation $_relation) { $_relation->created_by = Tinebase_Core::getUser()->getId(); $_relation->creation_time = Tinebase_DateTime::now(); if (!$_relation->isValid()) { throw new Tinebase_Exception_Record_Validation('Relation is not valid' . print_r($_relation->getValidationErrors(), true)); } try { $result = $this->_backend->addRelation($_relation); } catch (Zend_Db_Statement_Exception $zse) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not add relation: ' . $zse->getMessage()); $result = NULL; } return $result; }
/** * update to 0.10 * - migrate old links to relations * */ public function update_9() { /************* migrate old links to relations *************/ // get all links $linksTable = new Tinebase_Db_Table(array('name' => SQL_TABLE_PREFIX . 'links')); echo "fetching links ... <br/>\n"; $links = $linksTable->fetchAll(); // create relations from links $relationsByLeadId = array(); foreach ($links as $link) { if ($link->link_app1 === 'crm') { switch (strtolower($link->link_app2)) { case 'tasks': $relatedModel = 'Tasks_Model_Task'; $backend = Tasks_Backend_Factory::SQL; $degree = Tinebase_Model_Relation::DEGREE_SIBLING; $type = 'TASK'; break; case 'addressbook': $relatedModel = 'Addressbook_Model_Contact'; $backend = Addressbook_Backend_Factory::SQL; switch ($link->link_remark) { case 'account': $type = 'RESPONSIBLE'; $degree = Tinebase_Model_Relation::DEGREE_CHILD; break; case 'customer': $type = 'CUSTOMER'; $degree = Tinebase_Model_Relation::DEGREE_SIBLING; break; case 'partner': $type = 'PARTNER'; $degree = Tinebase_Model_Relation::DEGREE_SIBLING; break; } break; default: echo 'link type (' . $link->link_app2 . ") not supported<br/>\n"; } if (isset($relatedModel) && isset($backend)) { $relationsByLeadId[$link->link_id1][] = new Tinebase_Model_Relation(array('own_model' => 'Crm_Model_Lead', 'own_backend' => 'SQL', 'own_id' => $link->link_id1, 'related_degree' => $degree, 'related_model' => $relatedModel, 'related_backend' => $backend, 'related_id' => $link->link_id2, 'type' => $type, 'created_by' => $link->link_owner, 'creation_time' => Tinebase_DateTime::now())); } } } echo "creating relations ...<br/>\n"; $relationsBackend = new Tinebase_Relation_Backend_Sql(); foreach ($relationsByLeadId as $leadId => $relations) { foreach ($relations as $relation) { try { echo "set relation: " . $relation->type . " " . $relation->related_model . "<br/>\n"; $relationsBackend->addRelation($relation); } catch (Exception $e) { // cweiss 2008-08-25 this duplicates come from an earlier upgrading failure don't confuse user with verbosity ;-) // echo 'do not add duplicate relation ' . $relation->own_id . '-' . $relation->related_id . "...<br/>\n"; } } } echo "done<br/>\n"; $this->setApplicationVersion('Tinebase', '0.10'); }
/** * - use relations to save lead products * - remove old crm products tables * * @return void */ public function update_2() { if (Setup_Controller::getInstance()->isInstalled('Crm')) { // get linked products $select = $this->_db->select()->from(SQL_TABLE_PREFIX . 'metacrm_leads_products'); $stmt = $this->_db->query($select); $queryResult = $stmt->fetchAll(); //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . print_r($queryResult, TRUE)); // insert values into relations table $relationsBackend = new Tinebase_Relation_Backend_Sql(); foreach ($queryResult as $row) { $relation = new Tinebase_Model_Relation(array('own_model' => 'Crm_Model_Lead', 'own_backend' => 'Sql', 'own_id' => $row['lead_id'], 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'type' => 'PRODUCT', 'related_model' => 'Sales_Model_Product', 'related_backend' => 'Sql', 'related_id' => $row['product_id'], 'remark' => Zend_Json::encode(array('description' => $row['product_desc'], 'price' => $row['product_price'], 'quantity' => 1)))); try { $relationsBackend->addRelation($relation); } catch (Zend_Db_Statement_Exception $zdse) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Found duplicate, increasing quantity (' . $zdse->getMessage() . ')'); // increase quantity $updateRelation = $relationsBackend->search(new Tinebase_Model_RelationFilter(array(array('field' => 'own_id', 'operator' => 'equals', 'value' => $relation->own_id), array('field' => 'related_id', 'operator' => 'equals', 'value' => $relation->related_id), array('field' => 'related_model', 'operator' => 'equals', 'value' => 'Sales_Model_Product'))))->getFirstRecord(); $remark = $updateRelation->remark; $remark['quantity'] = $remark['quantity'] + 1; $updateRelation->remark = $remark; //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($updateRelation->toArray(), TRUE)); $relationsBackend->updateRelation($updateRelation); } } // drop table metacrm_leadsproducts and metacrm_products $this->dropTable('metacrm_leads_products'); $this->dropTable('metacrm_products'); } $this->setApplicationVersion('Sales', '2.3'); }
/** * 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; }