PersistentCartHandler::deleteProductForAllCarts()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
use XoopsModules\Oledrion;
24
25
/**
26
 * Panier persistant
27
 *
28
 * Lorque l'option correspondante dans le module est activée, tout produit rajouté dans le panier est
29
 * enregistré en base de données (à condition que l'utilisateur soit connecté).
30
 * Si l'utilisateur quitte le site et revient plus tard, cela permet de recharger son panier.
31
 */
32
33
/**
34
 * Class PersistentCartHandler
35
 */
36
class PersistentCartHandler extends OledrionPersistableObjectHandler
37
{
38
    /**
39
     * PersistentCartHandler constructor.
40
     * @param \XoopsDatabase|null $db
41
     */
42
    public function __construct(\XoopsDatabase $db = null)
43
    {
44
        //                          Table                     Classe                        Id
45
        parent::__construct($db, 'oledrion_persistent_cart', PersistentCart::class, 'persistent_id');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Oledrion\Ol...tHandler::__construct() does only seem to accept XoopsDatabase, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'oledrion_persistent_cart', PersistentCart::class, 'persistent_id');
Loading history...
46
    }
47
48
    /**
49
     * Supprime un produit des paniers enregistrés
50
     *
51
     * @param  mixed $persistent_product_id L'ID du produit à supprimer ou un tableau d'identifiants à supprimer
52
     * @return bool
53
     */
54
    public function deleteProductForAllCarts($persistent_product_id)
55
    {
56
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
57
            return true;
58
        }
59
        if (is_array($persistent_product_id)) {
60
            $criteria = new \Criteria('persistent_product_id', '(' . implode(',', $persistent_product_id) . ')', 'IN');
61
        } else {
62
            $criteria = new \Criteria('persistent_product_id', $persistent_product_id, '=');
63
        }
64
65
        return $this->deleteAll($criteria);
66
    }
67
68
    /**
69
     * Purge des produits d'un utilisateur
70
     *
71
     * @param int $persistent_uid L'identifiant de l'utilisateur
72
     * @return bool Le résultat de la suppression
73
     */
74
    public function deleteAllUserProducts($persistent_uid = 0)
75
    {
76
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
77
            return true;
78
        }
79
        $persistent_uid = 0 == $persistent_uid ? Oledrion\Utility::getCurrentUserID() : $persistent_uid;
80
81
        $criteria = new \Criteria('persistent_uid', $persistent_uid, '=');
82
83
        return $this->deleteAll($criteria);
84
    }
85
86
    /**
87
     * Supprime UN produit d'un utilisateur
88
     *
89
     * @param int $persistent_product_id L'identifiant du produit
90
     * @param int $persistent_uid        L'identifiant de l'utilisateur
91
     * @return bool Le résultat de la suppression
92
     */
93
    public function deleteUserProduct($persistent_product_id, $persistent_uid = 0)
94
    {
95
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
96
            return true;
97
        }
98
        $persistent_uid = 0 == $persistent_uid ? Oledrion\Utility::getCurrentUserID() : $persistent_uid;
99
        $criteria       = new \CriteriaCompo();
100
        $criteria->add(new \Criteria('persistent_uid', $persistent_uid, '='));
101
        $criteria->add(new \Criteria('persistent_product_id', $persistent_product_id, '='));
102
103
        return $this->deleteAll($criteria);
104
    }
105
106
    /**
107
     * Ajoute un produit au panier d'un utilisateur
108
     *
109
     * @param int $persistent_product_id L'ID du produit
110
     * @param int $persistent_qty        La quantité de produits
111
     * @param int $persistent_uid        L'ID de l'utilisateur
112
     * @return bool Le résultat de l'ajout du produit
113
     */
114
    public function addUserProduct($persistent_product_id, $persistent_qty, $persistent_uid = 0)
115
    {
116
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
117
            return true;
118
        }
119
        $persistent_uid  = 0 == $persistent_uid ? Oledrion\Utility::getCurrentUserID() : $persistent_uid;
120
        $persistent_cart = $this->create(true);
121
        $persistent_cart->setVar('persistent_product_id', $persistent_product_id);
122
        $persistent_cart->setVar('persistent_uid', $persistent_uid);
123
        $persistent_cart->setVar('persistent_date', time());
124
        $persistent_cart->setVar('persistent_qty', $persistent_qty);
125
126
        return $this->insert($persistent_cart, true);
127
    }
128
129
    /**
130
     * Mise à jour de la quantité de produit d'un utilisateur
131
     *
132
     * @param int $persistent_product_id L'identifiant du produit
133
     * @param int $persistent_qty        La quantité de produit
134
     * @param int $persistent_uid        L'ID de l'utilisateur
135
     * @return bool Le résultat de la mise à jour
136
     */
137
    public function updateUserProductQuantity($persistent_product_id, $persistent_qty, $persistent_uid = 0)
138
    {
139
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
140
            return true;
141
        }
142
        $persistent_uid = 0 == $persistent_uid ? Oledrion\Utility::getCurrentUserID() : $persistent_uid;
143
        $criteria       = new \CriteriaCompo();
144
        $criteria->add(new \Criteria('persistent_uid', $persistent_uid, '='));
145
        $criteria->add(new \Criteria('persistent_product_id', $persistent_product_id, '='));
146
147
        return $this->updateAll('persistent_qty', $persistent_qty, $criteria, true);
148
    }
149
150
    /**
151
     * Indique s'il existe un panier pour un utilisateur
152
     *
153
     * @param int $persistent_uid L'id de l'utilisateur
154
     * @return bool
155
     */
156
    public function isCartExists($persistent_uid = 0)
157
    {
158
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
159
            return false;
160
        }
161
        $persistent_uid = 0 == $persistent_uid ? Oledrion\Utility::getCurrentUserID() : $persistent_uid;
162
        $criteria       = new \Criteria('persistent_uid', $persistent_uid, '=');
163
164
        return (bool)$this->getCount($criteria);
165
    }
166
167
    /**
168
     * Retourne les produits d'un utilisateur
169
     *
170
     * @param int $persistent_uid L'ID de l'utilisateur
171
     * @return array|bool   Tableaux d'objets de type PersistentCart
172
     */
173
    public function getUserProducts($persistent_uid = 0)
174
    {
175
        if (0 == Oledrion\Utility::getModuleOption('persistent_cart')) {
176
            return false;
177
        }
178
        $persistent_uid = 0 == $persistent_uid ? Oledrion\Utility::getCurrentUserID() : $persistent_uid;
179
        $criteria       = new \Criteria('persistent_uid', $persistent_uid, '=');
180
181
        return $this->getObjects($criteria);
182
    }
183
}
184