0011558: use is_deleted when related data is removed
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 26 Jan 2016 13:07:53 +0000 (14:07 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 15 Feb 2016 10:01:17 +0000 (11:01 +0100)
Added modlog to Tinebase_Notes

notes should not be deleted instantly instead soft delete should apply

Change-Id: I786699f42366c728ba902ec0866fbd7d396b8463
Reviewed-on: http://gerrit.tine20.com/customers/2668
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Felamimail/Frontend/JsonTest.php
tine20/Tinebase/Backend/Sql/Abstract.php
tine20/Tinebase/Notes.php

index c824925..21c1d0f 100644 (file)
@@ -560,17 +560,17 @@ class Felamimail_Frontend_JsonTest extends PHPUnit_Framework_TestCase
         $emailNoteType = Tinebase_Notes::getInstance()->getNoteTypeByName('email');
         
         // check / delete notes
-        $emailNoteIds = array();
+        $emailNotes = new Tinebase_Record_RecordSet('Tinebase_Model_Note');
         foreach ($contact->notes as $note) {
             if ($note->note_type_id == $emailNoteType->getId()) {
                 $this->assertContains($subject, $note->note, 'did not find note subject');
                 $this->assertEquals(Tinebase_Core::getUser()->getId(), $note->created_by);
                 $this->assertContains('aaaaaä', $note->note);
-                $emailNoteIds[] = $note->getId();
+                $emailNotes->addRecord($note);
             }
         }
-        $this->assertGreaterThan(0, count($emailNoteIds), 'no email notes found');
-        Tinebase_Notes::getInstance()->deleteNotes($emailNoteIds);
+        $this->assertGreaterThan(0, $emailNotes->count(), 'no email notes found');
+        Tinebase_Notes::getInstance()->deleteNotes($emailNotes);
     }
 
     /**
index 418b746..92f8c8e 100644 (file)
@@ -1315,7 +1315,7 @@ abstract class Tinebase_Backend_Sql_Abstract extends Tinebase_Backend_Abstract i
       * @return void
       * @return int The number of affected rows.
       */
-    public function delete($_id) 
+    public function delete($_id)
     {
         if (empty($_id)) {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' No records deleted.');
index 9d359a8..350e8c7 100644 (file)
@@ -239,7 +239,8 @@ class Tinebase_Notes implements Tinebase_Backend_Sql_Interface
      */
     public function getNote($_noteId)
     {
-        $row = $this->_notesTable->fetchRow($this->_db->quoteInto($this->_db->quoteIdentifier('id') . ' = ?', (string) $_noteId));
+        $row = $this->_notesTable->fetchRow($this->_db->quoteInto($this->_db->quoteIdentifier('id') . ' = ? AND '
+            . $this->_db->quoteIdentifier('is_deleted') . ' = 0', (string) $_noteId));
         
         if (!$row) {
             throw new Tinebase_Exception_NotFound('Note not found.');
@@ -321,7 +322,7 @@ class Tinebase_Notes implements Tinebase_Backend_Sql_Interface
         
         //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_record->toArray(), TRUE));
         
-        $currentNotesIds = $this->getNotesOfRecord($model, $_record->getId(), $backend)->getArrayOfIds();
+        $currentNotes = $this->getNotesOfRecord($model, $_record->getId(), $backend);
         $notes = $_record->$_notesProperty;
         
         if ($notes instanceOf Tinebase_Record_RecordSet) {
@@ -360,10 +361,14 @@ class Tinebase_Notes implements Tinebase_Backend_Sql_Interface
         }
         
         //$toAttach = array_diff($notesToSet->getArrayOfIds(), $currentNotesIds);
-        $toDetach = array_diff($currentNotesIds, $notesToSet->getArrayOfIds());
+        $toDetach = array_diff($currentNotes->getArrayOfIds(), $notesToSet->getArrayOfIds());
+        $toDelete = new Tinebase_Record_RecordSet('Tinebase_Model_Note');
+        foreach($toDetach as $detachee) {
+            $toDelete->addRecord($currentNotes->getById($detachee));
+        }
 
         // delete detached/deleted notes
-        $this->deleteNotes($toDetach);
+        $this->deleteNotes($toDelete);
         
         // add new notes
         Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Adding ' . count($notesToSet) . ' note(s) to record.');
@@ -495,17 +500,24 @@ class Tinebase_Notes implements Tinebase_Backend_Sql_Interface
             $this->addSystemNote($recordId, $_userId, Tinebase_Model_Note::SYSTEM_NOTE_NAME_CHANGED, $modsOfRecord, 'Sql', $modelName);
         }
     }
-    
+
     /**
      * delete notes
      *
-     * @param array $_noteIds
+     * @param Tinebase_Record_RecordSet $notes
      */
-    public function deleteNotes(array $_noteIds)
+    public function deleteNotes(Tinebase_Record_RecordSet $notes)
     {
-        if (!empty($_noteIds)) {
-            $where = array($this->_db->quoteInto($this->_db->quoteIdentifier('id') . ' IN (?)', $_noteIds));
-            $this->_notesTable->delete($where);
+        $sqlBackend = new Tinebase_Backend_Sql(
+            array(
+                'tableName' => $this->getTableName(),
+                'modelName' => 'Tinebase_Model_Note'
+            ),
+            $this->getAdapter());
+
+        foreach($notes as $note) {
+            Tinebase_Timemachine_ModificationLog::setRecordMetaData($note, 'delete', $note);
+            $sqlBackend->update($note);
         }
     }
 
@@ -521,8 +533,8 @@ class Tinebase_Notes implements Tinebase_Backend_Sql_Interface
         $backend = ucfirst(strtolower($_backend));
         
         $notes = $this->getNotesOfRecord($_model, $_id, $backend);
-        
-        $this->deleteNotes($notes->getArrayOfIdsAsString());
+
+        $this->deleteNotes($notes);
     }
     
     /**