From 1238ea35c8329c90180431f6f387f77e04d003f1 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Sun, 16 Feb 2014 23:15:45 +0100
Subject: [PATCH] refs #4679 splitted this method into 3 methods and extracted
 them into a model so we do not have to check for permission when archiving

---
 plugins/SegmentEditor/API.php                 | 35 ++------
 plugins/SegmentEditor/Model.php               | 89 +++++++++++++++++++
 plugins/SegmentEditor/SegmentEditor.php       |  8 +-
 .../Integration/Plugins/SegmentEditorTest.php |  8 +-
 4 files changed, 107 insertions(+), 33 deletions(-)
 create mode 100644 plugins/SegmentEditor/Model.php

diff --git a/plugins/SegmentEditor/API.php b/plugins/SegmentEditor/API.php
index c1d4ee72f1..37e2d880a5 100644
--- a/plugins/SegmentEditor/API.php
+++ b/plugins/SegmentEditor/API.php
@@ -261,11 +261,10 @@ class API extends \Piwik\Plugin\API
     /**
      * Returns all stored segments.
      *
-     * @param bool $idSite Whether to return stored segments that are only auto-archived for a specific idSite, or all of them. If supplied, must be a valid site ID.
-     * @param bool $returnOnlyAutoArchived Whether to only return stored segments that are auto-archived or not.
+     * @param bool|int $idSite Whether to return stored segments for a specific idSite, or all of them. If supplied, must be a valid site ID.
      * @return array
      */
-    public function getAll($idSite = false, $returnOnlyAutoArchived = false)
+    public function getAll($idSite = false)
     {
         if (!empty($idSite)) {
             Piwik::checkUserHasViewAccess($idSite);
@@ -273,35 +272,15 @@ class API extends \Piwik\Plugin\API
             Piwik::checkUserHasSomeViewAccess();
         }
 
-        if ($returnOnlyAutoArchived) {
-            Piwik::checkUserHasSuperUserAccess();
-        }
-
-        $bind = array();
-
-        // Build basic segment filtering
-        $whereIdSite = '';
-        if (!empty($idSite)) {
-            $whereIdSite = 'enable_only_idsite = ? OR ';
-            $bind[] = $idSite;
-        }
+        $userLogin = Piwik::getCurrentUserLogin();
 
-        if ($returnOnlyAutoArchived) {
-            $extraWhere = ' AND auto_archive = 1';
+        $model = new Model();
+        if (empty($idSite)) {
+            $segments = $model->getAllSegments($userLogin);
         } else {
-            $extraWhere = ' AND (enable_all_users = 1 OR login = ?)';
-            $bind[] = Piwik::getCurrentUserLogin();
+            $segments = $model->getAllSegmentsForSite($idSite, $userLogin);
         }
 
-        // Query
-        $sql = "SELECT * " .
-            " FROM " . Common::prefixTable("segment") .
-            " WHERE ($whereIdSite enable_only_idsite = 0)
-                        AND deleted = 0
-                        $extraWhere
-                      ORDER BY name ASC";
-        $segments = Db::get()->fetchAll($sql, $bind);
-
         return $segments;
     }
 
diff --git a/plugins/SegmentEditor/Model.php b/plugins/SegmentEditor/Model.php
new file mode 100644
index 0000000000..a58f25385c
--- /dev/null
+++ b/plugins/SegmentEditor/Model.php
@@ -0,0 +1,89 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+namespace Piwik\Plugins\SegmentEditor;
+
+use Exception;
+use Piwik\Common;
+use Piwik\Date;
+use Piwik\Db;
+use Piwik\Piwik;
+use Piwik\Segment;
+
+/**
+ * The SegmentEditor Model lets you persist and read custom Segments from the backend without handling any logic.
+ */
+class Model
+{
+    /**
+     * Returns all stored segments.
+     *
+     * @param bool|int $idSite Whether to return stored segments for a specific idSite, or segments that are available
+     *                         for all sites. If supplied, must be a valid site ID.
+     * @return array
+     */
+    public function getSegmentsToAutoArchive($idSite = false)
+    {
+        $bind = array();
+
+        $whereIdSite = '';
+        if (!empty($idSite)) {
+            $whereIdSite = 'enable_only_idsite = ? OR ';
+            $bind[] = $idSite;
+        }
+
+        $sql = $this->buildQuerySortedByName("($whereIdSite enable_only_idsite = 0)
+                                              AND deleted = 0 AND auto_archive = 1");
+
+        $segments = Db::get()->fetchAll($sql, $bind);
+
+        return $segments;
+    }
+
+    /**
+     * Returns all stored segments that are available to the given login.
+     *
+     * @param  string $userLogin
+     * @return array
+     */
+    public function getAllSegments($userLogin)
+    {
+        $bind = array($userLogin);
+        $sql  = $this->buildQuerySortedByName('deleted = 0 AND (enable_all_users = 1 OR login = ?)');
+
+        $segments = Db::get()->fetchAll($sql, $bind);
+
+        return $segments;
+    }
+
+    /**
+     * Returns all stored segments that are available for the given site and login.
+     *
+     * @param  string $userLogin
+     * @param  int    $idSite Whether to return stored segments for a specific idSite, or all of them. If supplied, must be a valid site ID.
+     * @return array
+     */
+    public function getAllSegmentsForSite($idSite, $userLogin)
+    {
+        $bind = array($idSite, $userLogin);
+        $sql  = $this->buildQuerySortedByName('(enable_only_idsite = ? OR enable_only_idsite = 0)
+                                               AND deleted = 0
+                                               AND (enable_all_users = 1 OR login = ?)');
+        $segments = Db::get()->fetchAll($sql, $bind);
+
+        return $segments;
+    }
+
+    private function buildQuerySortedByName($where)
+    {
+        $sql = "SELECT * FROM " . Common::prefixTable("segment") .
+               " WHERE $where ORDER BY name ASC";
+
+        return $sql;
+    }
+}
diff --git a/plugins/SegmentEditor/SegmentEditor.php b/plugins/SegmentEditor/SegmentEditor.php
index 3fa2c46464..89c8ddc6de 100644
--- a/plugins/SegmentEditor/SegmentEditor.php
+++ b/plugins/SegmentEditor/SegmentEditor.php
@@ -54,7 +54,9 @@ class SegmentEditor extends \Piwik\Plugin
 
     public function getKnownSegmentsToArchiveAllSites(&$segments)
     {
-        $segmentsToAutoArchive = API::getInstance()->getAll($idSite = false, $returnOnlyAutoArchived = true);
+        $model = new Model();
+        $segmentsToAutoArchive = $model->getSegmentsToAutoArchive($idSite = false);
+
         foreach ($segmentsToAutoArchive as $segment) {
             $segments[] = $segment['definition'];
         }
@@ -62,11 +64,13 @@ class SegmentEditor extends \Piwik\Plugin
 
     public function getKnownSegmentsToArchiveForSite(&$segments, $idSite)
     {
-        $segmentToAutoArchive = API::getInstance()->getAll($idSite, $returnOnlyAutoArchived = true);
+        $model = new Model();
+        $segmentToAutoArchive = $model->getSegmentsToAutoArchive($idSite);
 
         foreach ($segmentToAutoArchive as $segmentInfo) {
             $segments[] = $segmentInfo['definition'];
         }
+
         $segments = array_unique($segments);
     }
 
diff --git a/tests/PHPUnit/Integration/Plugins/SegmentEditorTest.php b/tests/PHPUnit/Integration/Plugins/SegmentEditorTest.php
index 19fe8fa0a5..51d081de66 100644
--- a/tests/PHPUnit/Integration/Plugins/SegmentEditorTest.php
+++ b/tests/PHPUnit/Integration/Plugins/SegmentEditorTest.php
@@ -10,6 +10,7 @@ use Piwik\Date;
 use Piwik\Piwik;
 use Piwik\Plugins\SegmentEditor\API;
 use Piwik\Plugins\SitesManager\API as APISitesManager;
+use Piwik\Plugins\SegmentEditor\Model;
 
 /**
  * Class Plugins_SegmentEditorTest
@@ -108,20 +109,21 @@ class Plugins_SegmentEditorTest extends DatabaseTestCase
         $this->assertEquals($segment, $expected);
 
         // There is a segment to process for this particular site
-        $segments = API::getInstance()->getAll($idSite, $autoArchived = true);
+        $model = new Model();
+        $segments = $model->getSegmentsToAutoArchive($idSite);
         unset($segments[0]['ts_created']);
         $this->assertEquals($segments, array($expected));
 
         // There is no segment to process for a non existing site
         try {
-            $segments = API::getInstance()->getAll(33, $autoArchived = true);
+            $segments = $model->getSegmentsToAutoArchive(33);
             $this->fail();
         } catch(Exception $e) {
             // expected
         }
 
         // There is no segment to process across all sites
-        $segments = API::getInstance()->getAll($idSite = false, $autoArchived = true);
+        $segments = $model->getSegmentsToAutoArchive($idSite = false);
         $this->assertEquals($segments, array());
     }
 
-- 
GitLab