chg phone<->addressbook relation to contact_id db field
authorPaul Mehrer <p.mehrer@metaways.de>
Wed, 14 Sep 2016 10:27:41 +0000 (12:27 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 26 Oct 2016 12:12:38 +0000 (14:12 +0200)
see 0011934: show contacts in phone call grid

Change-Id: I60361f95c4376f6e52b4a8411e725ac35cba6c19
Reviewed-on: http://gerrit.tine20.com/customers/3569
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Phone/Call/ControllerTest.php
tests/tine20/Phone/JsonTest.php
tine20/Phone/Controller.php
tine20/Phone/Controller/Call.php
tine20/Phone/Model/Call.php
tine20/Phone/Setup/Update/Release9.php
tine20/Phone/Setup/setup.xml
tine20/Phone/js/Phone.js
tine20/Tinebase/Relation/Backend/Sql.php

index 4d661bf..c4f3298 100644 (file)
@@ -16,7 +16,7 @@ class Phone_Call_ControllerTest extends TestCase
     /**
      * @see 0011934: show contacts in phone call grid
      */
-    public function testContactRelation()
+    public function testContactId()
     {
         $phoneNumber = '0406437435';
         $myContact = Addressbook_Controller_Contact::getInstance()->getContactByUserId(Tinebase_Core::getUser()->getId());
@@ -32,7 +32,7 @@ class Phone_Call_ControllerTest extends TestCase
         ));
         $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);
+        $this->assertTrue(isset($call->contact_id));
+        $this->assertEquals($myContact->getId(), $call->contact_id);
     }
 }
index 8d1277e..5e8c581 100644 (file)
@@ -490,7 +490,7 @@ class Phone_JsonTest extends TestCase
     /**
      * @see 0011934: show contacts in phone call grid
      */
-    public function testContactRelations()
+    public function testContactId()
     {
         // search phone 2 calls (on should be linked to sclever)
         $result = $this->_json->searchCalls($this->_objects['filter3'], $this->_objects['paging']);
@@ -503,8 +503,7 @@ class Phone_JsonTest extends TestCase
         }
 
         $this->assertTrue($scleverCall !== null);
-        $this->assertTrue(isset($scleverCall['relations']));
-        $this->assertEquals(1, count($scleverCall['relations']), print_r($scleverCall, true));
-        $this->assertEquals('Susan', $scleverCall['relations'][0]['related_record']['n_given'], print_r($scleverCall, true));
+        $this->assertTrue(isset($scleverCall['contact_id']));
+        $this->assertEquals($this->_personas['sclever']->getId(), $scleverCall['contact_id']['account_id'], print_r($scleverCall['contact_id'], true));
     }
 }
index 1f138ba..5c308d2 100644 (file)
@@ -181,11 +181,9 @@ class Phone_Controller extends Tinebase_Controller_Abstract
             $_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));
+        $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),
@@ -193,30 +191,18 @@ class Phone_Controller extends Tinebase_Controller_Abstract
 
             $controller = Addressbook_Controller_Contact::getInstance();
             $oldAclChecks = $controller->doContainerACLChecks();
+
             $controller->doContainerACLChecks(false);
             $contacts = $controller->search($filter);
+            $controller->doContainerACLChecks($oldAclChecks);
 
-            $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',
-                    ))
-                );
+            if ($contacts->count() > 0) {
+                $_call->contact_id = $contacts->getFirstRecord()->getId();
             }
-
-            $controller->doContainerACLChecks($oldAclChecks);
         }
-        
+
+        $call = $backend->create($_call);
+
         return $call;
     }
     
index 8405158..2a80592 100644 (file)
@@ -72,17 +72,9 @@ class Phone_Controller_Call extends Tinebase_Controller_Record_Abstract
             $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',
-                );
+            if ($contacts->count() > 0) {
+                $_record->contact_id = $contacts->getFirstRecord()->getId();
             }
-            $_record->relations = $relations;
         }
     }
 
index 8e943a5..29d241b 100644 (file)
@@ -121,16 +121,14 @@ class Phone_Model_Call extends Tinebase_Record_Abstract
                 'validators' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
                 'queryFilter' => TRUE,
             ),
-            'contact' => array(
-                'type' => 'virtual',
+            'contact_id'              => array(
+                'label'      => 'Contact',
+                'validators' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
+                'type'       => 'record',
                 'config' => array(
-                    'type' => 'relation',
-                    'label' => 'Contact',    // _('Contact')
-                    'config' => array(
-                        'appName'   => 'Addressbook',
-                        'modelName' => 'Contact',
-                        'type' => 'CALLER'
-                    )
+                    'appName'     => 'Addressbook',
+                    'modelName'   => 'Contact',
+                    'idProperty'  => 'id',
                 )
             ),
         )
index e240503..cbec446 100644 (file)
@@ -17,6 +17,9 @@ class Phone_Setup_Update_Release9 extends Setup_Update_Abstract
      */
     public function update_0()
     {
+        /**
+         * this is obsolete due to update_1, in case it was not yet applied, we skip it!
+
         // we need at least addressbook version 9,7
         if (version_compare($this->getApplicationVersion('Addressbook'), '9.8') < 0 ) {
             return;
@@ -69,6 +72,65 @@ class Phone_Setup_Update_Release9 extends Setup_Update_Abstract
             }
         }
 
+        // we dont even want to move to that app version, as update_1 will check for it!
         $this->setApplicationVersion('Phone', '9.1');
+         * */
+    }
+
+    /**
+     * update to 9.2
+     *
+     * @return void
+     */
+    public function update_1()
+    {
+        // we need at least addressbook version 9,7
+        if (version_compare($this->getApplicationVersion('Addressbook'), '9.8') < 0) {
+            return;
+        }
+
+        // undo update_0
+        if (version_compare($this->getApplicationVersion('Phone'), '9.1') == 0) {
+            $relationBackend = new Tinebase_Relation_Backend_Sql();
+            $relationBackend->purgeRelationsByType('CALLER');
+        }
+
+        $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)) // <= 3 month back
+            ), '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);
+
+                if ($contacts->count() > 0) {
+                    $_record->contact_id = $contacts->getFirstRecord()->getId();
+                    $phoneController->update($_record);
+                }
+            }
+        }
+
+        $this->setApplicationVersion('Phone', '9.2');
     }
 }
index 1650343..063af08 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <application>
     <name>Phone</name>
-    <version>9.1</version>
+    <version>9.2</version>
     <order>11</order>
     <depends>
         <application>Admin</application>
                     <length>64</length>
                     <notnull>true</notnull>
                 </field>
+                <field>
+                    <name>contact_id</name>
+                    <type>text</type>
+                    <length>40</length>
+                </field>
                 <index>
                     <name>id</name>
                     <primary>true</primary>
index a81172e..865bee8 100644 (file)
@@ -119,6 +119,10 @@ Tine.widgets.grid.RendererManager.register('Phone', 'Call', 'line_id', function(
     return (v && v.hasOwnProperty('linenumber')) ? v.linenumber : '';
 });
 
+Tine.widgets.grid.RendererManager.register('Phone', 'Call', 'contact_id', function(v) {
+    return (v && v.hasOwnProperty('n_fn')) ? v.n_fn : '';
+});
+
 /***************************** utils ****************************************/
 
 Ext.namespace('Tine.Phone.utils');
index adb8e80..dcb6381 100644 (file)
@@ -509,4 +509,30 @@ class Tinebase_Relation_Backend_Sql extends Tinebase_Backend_Sql_Abstract
             }
         } while(true);
     }
+
+    /**
+     * remove all relations of a specific type
+     *
+     * @param string $_type
+     *
+     * @return void
+     */
+    public function purgeRelationsByType($_type)
+    {
+        $tableName = SQL_TABLE_PREFIX . 'relations';
+
+        $select = $this->_db->select()->from($tableName, array('rel_id'))
+            ->where($this->_db->quoteIdentifier('type') . ' = ?', $_type)
+            ->limit(10000);
+
+        do {
+            $relation_ids = $this->_db->fetchCol($select);
+
+            if (is_array($relation_ids) && count($relation_ids) > 0) {
+                $this->_db->delete($tableName, $this->_db->quoteInto($this->_db->quoteIdentifier('rel_id') . ' IN (?)', $relation_ids));
+            } else {
+                break;
+            }
+        } while(true);
+    }
 }