File size: 3,937 Bytes
9046370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Goals;

use Piwik\Common;
use Piwik\Db;

/**
 * @phpstan-type GoalStoredRecord array{
 *     idgoal: int|string,
 *     idsite: int|string,
 *     name: string,
 *     description: string,
 *     match_attribute: string,
 *     pattern: string,
 *     pattern_type: string,
 *     case_sensitive: int|string,
 *     allow_multiple: int|string,
 *     revenue: float|int|string,
 *     event_value_as_revenue: int|string,
 *     deleted?: int|string
 * }
 */
class Model
{
    private static $rawPrefix = 'goal';
    private $table;

    public function __construct()
    {
        $this->table = Common::prefixTable(self::$rawPrefix);
    }

    private function getNextIdGoal($idSite)
    {
        $db     = $this->getDb();
        $idGoal = $db->fetchOne("SELECT max(idgoal) + 1 FROM `" . $this->table . "`
                                 WHERE idsite = ?", $idSite);

        if (empty($idGoal)) {
            $idGoal = 1;
        }

        return $idGoal;
    }

    public function createGoalForSite($idSite, $goal)
    {
        $db     = $this->getDb();
        $goalId = $this->getNextIdGoal($idSite);

        $goal['idgoal'] = $goalId;
        $goal['idsite'] = $idSite;

        $db->insert($this->table, $goal);

        return $goalId;
    }

    public function updateGoal($idSite, $idGoal, $goal)
    {
        $idSite = (int) $idSite;
        $idGoal = (int) $idGoal;

        $db = $this->getDb();
        $db->update($this->table, $goal, "idsite = '$idSite' AND idgoal = '$idGoal'");
    }

    // actually this should be in a log_conversion model
    public function deleteGoalConversions($idSite, $idGoal)
    {
        $table = Common::prefixTable("log_conversion");

        Db::deleteAllRows($table, "WHERE idgoal = ? AND idsite = ?", "idvisit", 100000, array($idGoal, $idSite));
    }

    /**
     * @param string|int $idSite
     * @param string|int $idGoal
     * @phpstan-return GoalStoredRecord|array{}
     */
    public function getActiveGoal($idSite, $idGoal)
    {
        $idSite = (int) $idSite;
        $idGoal = (int) $idGoal;
        $goals = Db::fetchRow("SELECT * FROM `" . $this->table . "`
                                WHERE idsite = $idSite AND idgoal = $idGoal
                                      AND deleted = 0 LIMIT 1");

        return $goals;
    }

    /**
     * @param int|string|array<int|string> $idSite
     * @phpstan-return list<GoalStoredRecord>
     */
    public function getActiveGoals($idSite)
    {
        $idSite = array_map('intval', $idSite);
        $goals = Db::fetchAll("SELECT * FROM `" . $this->table . "`
                                WHERE idsite IN (" . implode(", ", $idSite) . ")
                                      AND deleted = 0");

        return $goals;
    }

    public function deleteGoalsForSite($idSite): void
    {
        Db::query("DELETE FROM `" . $this->table . "` WHERE idsite = ? ", [$idSite]);
    }

    public function deleteGoal($idSite, $idGoal)
    {
        $query = "UPDATE " . $this->table . " SET deleted = 1
                  WHERE idsite = ? AND idgoal = ?";
        $bind  = array($idSite, $idGoal);

        Db::query($query, $bind);
    }

    public function getActiveGoalCount(): int
    {
        return (int)Db::fetchOne("SELECT count(*) FROM `" . $this->table . "`
                                WHERE deleted = 0");
    }

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

    /**
     * Checks if an active idgoal exists for the site
     */
    public function doesGoalExist(int $idGoal, int $idSite): bool
    {
        return (bool)Db::fetchOne("SELECT COUNT(*) FROM `" . $this->table . "`
                                WHERE deleted = 0 AND idsite = ? AND idgoal = ?", [$idSite, $idGoal]);
    }
}