From cbe88c1ab471c717fbee06eebe556cc97c5da6e5 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Mon, 3 Mar 2014 21:24:46 +0100
Subject: [PATCH] refs #57 destroy all tables but the insights tables

---
 plugins/Insights/API.php | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/plugins/Insights/API.php b/plugins/Insights/API.php
index 4234bdd4dc..f26bec619c 100644
--- a/plugins/Insights/API.php
+++ b/plugins/Insights/API.php
@@ -43,14 +43,25 @@ class API extends \Piwik\Plugin\API
             'Referrers_getCampaigns',
             'Referrers_getAll'
         );
-
         // post event to add other reports?
 
+        $reportTableIds   = array();
+        $dataTableManager = DataTable\Manager::getInstance();
+
         $tables = array();
         foreach ($reports as $report) {
-            $tableId  = DataTable\Manager::getInstance()->getMostRecentTableId();
-            $tables[] = $this->getInsightOverview($idSite, $period, $date, $report);
-            DataTable\Manager::getInstance()->deleteAll($tableId);
+            $firstTableId     = $dataTableManager->getMostRecentTableId();
+            $table            = $this->getInsightOverview($idSite, $period, $date, $report);
+            $reportTableIds[] = $table->getId();
+            $lastTableId      = $dataTableManager->getMostRecentTableId();
+
+            for ($index = $firstTableId; $index < $lastTableId; $index++) {
+                if (!in_array($index, $reportTableIds)) {
+                    DataTable\Manager::getInstance()->deleteTable($index);
+                }
+            }
+
+            $tables[] = $table;
         }
 
         $map = new DataTable\Map();
-- 
GitLab