Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Model.php « SegmentEditor « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e73b6a1ab7c1b0b1d9bd02669a41075b190aaf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\SegmentEditor;

use Piwik\Common;
use Piwik\Db;
use Piwik\DbHelper;

/**
 * The SegmentEditor Model lets you persist and read custom Segments from the backend without handling any logic.
 */
class Model
{
    private static $rawPrefix = 'segment';

    protected function getTable()
    {
        return Common::prefixTable(self::$rawPrefix);
    }

    /**
     * Returns all stored segments that haven't been deleted. Ignores the site the segments are enabled
     * for and whether to auto archive or not.
     *
     * @return array
     */
    public function getAllSegmentsAndIgnoreVisibility()
    {
        $sql = "SELECT * FROM " . $this->getTable() . " WHERE deleted = 0";

        $segments = $this->getDb()->fetchAll($sql);

        return $segments;
    }

    /**
     * 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 = $this->getDb()->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 = $this->getDb()->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 = $this->getDb()->fetchAll($sql, $bind);

        return $segments;
    }

    /**
     * This should be used _only_ by Super Users
     * @param $idSite
     * @return array
     */
    public function getAllSegmentsForAllUsers($idSite = false)
    {
        $bind = array();
        $sqlWhereCondition = '';

        if(!empty($idSite)) {
            $bind = array($idSite);
            $sqlWhereCondition = '(enable_only_idsite = ? OR enable_only_idsite = 0) AND';
        }

        $sqlWhereCondition  = $this->buildQuerySortedByName($sqlWhereCondition . ' deleted = 0');
        $segments = $this->getDb()->fetchAll($sqlWhereCondition, $bind);

        return $segments;
    }

    public function deleteSegment($idSegment)
    {
        $db = $this->getDb();
        $db->delete($this->getTable(), 'idsegment = ' . (int) $idSegment);
    }

    public function updateSegment($idSegment, $segment)
    {
        $idSegment = (int) $idSegment;

        $db = $this->getDb();
        $db->update($this->getTable(), $segment, "idsegment = $idSegment");

        return true;
    }

    public function createSegment($segment)
    {
        $db = $this->getDb();
        $db->insert($this->getTable(), $segment);
        $id = $db->lastInsertId();

        return $id;
    }

    public function getSegment($idSegment)
    {
        $db = $this->getDb();
        $segment = $db->fetchRow("SELECT * FROM " . $this->getTable() . " WHERE idsegment = ?", $idSegment);

        return $segment;
    }

    private function getDb()
    {
        return Db::get();
    }

    private function buildQuerySortedByName($where)
    {
        return "SELECT * FROM " . $this->getTable() . " WHERE $where ORDER BY name ASC";
    }

    public static function install()
    {
        $segmentTable = "`idsegment` INT(11) NOT NULL AUTO_INCREMENT,
					     `name` VARCHAR(255) NOT NULL,
					     `definition` TEXT NOT NULL,
					     `login` VARCHAR(100) NOT NULL,
					     `enable_all_users` tinyint(4) NOT NULL default 0,
					     `enable_only_idsite` INTEGER(11) NULL,
					     `auto_archive` tinyint(4) NOT NULL default 0,
					     `ts_created` TIMESTAMP NULL,
					     `ts_last_edit` TIMESTAMP NULL,
					     `deleted` tinyint(4) NOT NULL default 0,
					     PRIMARY KEY (`idsegment`)";

        DbHelper::createTable(self::$rawPrefix, $segmentTable);
    }

}