ListsHandler   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 322
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 34
eloc 131
dl 0
loc 322
rs 9.68
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A incrementListProductsCount() 0 8 1
A isThisMyList() 0 15 4
A listsFromCurrentCategory() 0 33 3
A getRecentLists() 0 39 4
A getUsersFromLists() 0 11 3
A decrementListProductsCount() 0 9 1
A incrementListViews() 0 10 2
A getRecentListsCount() 0 26 4
A __construct() 0 7 2
A isProductInUserList() 0 28 5
A getListProducts() 0 19 4
A deleteList() 0 6 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
/**
24
 * Gestion des listes utilisateurs
25
 *
26
 * @since 2.3.2009.06.13
27
 */
28
29
use XoopsModules\Oledrion;
30
31
/**
32
 * Class ListsHandler
33
 */
34
class ListsHandler extends OledrionPersistableObjectHandler
35
{
36
    /**
37
     * ListsHandler constructor.
38
     * @param \XoopsDatabase|null $db
39
     */
40
    public function __construct(\XoopsDatabase $db = null)
41
    {
42
        if (null === $db) {
43
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
44
        }
45
        //                            Table               Classe           Id       Identifiant
46
        parent::__construct($db, 'oledrion_lists', Lists::class, 'list_id', 'list_title');
47
    }
48
49
    /**
50
     * Incrémente le compteur de vues d'une liste
51
     *
52
     * @param  Lists $list
53
     * @return bool
54
     */
55
    public function incrementListViews(Lists $list)
56
    {
57
        $res = true;
58
        if (Oledrion\Utility::getCurrentUserID() != $list->getVar('list_uid')) {
59
            $sql = 'UPDATE ' . $this->table . ' SET list_views = list_views + 1 WHERE list_id = ' . $list->getVar('list_id');
60
            $res = $this->db->queryF($sql);
61
            $this->forceCacheClean();
62
        }
63
64
        return $res;
65
    }
66
67
    /**
68
     * Incrémente le nombre de produits dans une liste
69
     *
70
     * @param  Lists $list
71
     * @return bool
72
     */
73
    public function incrementListProductsCount(Lists $list)
74
    {
75
        $res = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
76
        $sql = 'UPDATE ' . $this->table . ' SET list_productscount = list_productscount + 1 WHERE list_id = ' . $list->getVar('list_id');
77
        $res = $this->db->queryF($sql);
78
        $this->forceCacheClean();
79
80
        return $res;
81
    }
82
83
    /**
84
     * Décrémente le nombre de produits dans une liste
85
     *
86
     * @param  Lists $list
87
     * @param  int   $value
88
     * @return bool
89
     */
90
    public function decrementListProductsCount(Lists $list, $value = 1)
91
    {
92
        $value = (int)$value;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
93
        $res   = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $res is dead and can be removed.
Loading history...
94
        $sql   = 'UPDATE ' . $this->table . ' SET list_productscount = list_productscount - $value WHERE list_id = ' . $list->getVar('list_id');
95
        $res   = $this->db->queryF($sql);
96
        $this->forceCacheClean();
97
98
        return $res;
99
    }
100
101
    /**
102
     * Retourne la liste des listes récentes
103
     *
104
     * @param  Parameters $parameters
105
     * @return array               Tableau d'objets de type Lists [clé] = id liste
106
     * @internal param int $start
107
     * @internal param int $limit
108
     * @internal param string $sort
109
     * @internal param string $order
110
     * @internal param bool $idAsKey
111
     * @internal param int $listType
112
     * @internal param int $list_uid
113
     */
114
    public function getRecentLists(Parameters $parameters)
115
    {
116
        $parameters = $parameters->extend(new Oledrion\Parameters([
117
                                                                      'start'    => 0,
118
                                                                      'limit'    => 0,
119
                                                                      'sort'     => 'list_date',
120
                                                                      'order'    => 'DESC',
121
                                                                      'idAsKey'  => true,
122
                                                                      'listType' => Constants::OLEDRION_LISTS_ALL,
123
                                                                      'list_uid' => 0,
124
                                                                  ]));
125
        $criteria   = new \CriteriaCompo();
126
        switch ($parameters['listType']) {
127
            case Constants::OLEDRION_LISTS_ALL:
128
129
                $criteria->add(new \Criteria('list_id', 0, '<>'));
130
131
                break;
132
            case Constants::OLEDRION_LISTS_ALL_PUBLIC:
133
134
                $criteria->add(new \Criteria('list_type', Constants::OLEDRION_LISTS_WISH, '='));
135
                $criteria->add(new \Criteria('list_type', Constants::OLEDRION_LISTS_RECOMMEND, '='), 'OR');
136
137
                break;
138
            default:
139
140
                $criteria->add(new \Criteria('list_type', $parameters['listType'], '='));
141
142
                break;
143
        }
144
        if ($parameters['list_uid'] > 0) {
145
            $criteria->add(new \Criteria('list_uid', $parameters['list_uid'], '='));
146
        }
147
        $criteria->setSort($parameters['sort']);
148
        $criteria->setOrder($parameters['order']);
149
        $criteria->setStart($parameters['start']);
150
        $criteria->setLimit($parameters['limit']);
151
152
        return $this->getObjects($criteria, $parameters['idAsKey']);
153
    }
154
155
    /**
156
     * Retourne le nombre de listes d'un certain type
157
     *
158
     * @param int $listType
159
     * @param int $list_uid
160
     * @return int
161
     */
162
    public function getRecentListsCount($listType = Constants::OLEDRION_LISTS_ALL, $list_uid = 0)
163
    {
164
        $criteria = new \CriteriaCompo();
165
        switch ($listType) {
166
            case Constants::OLEDRION_LISTS_ALL:
167
168
                $criteria->add(new \Criteria('list_id', 0, '<>'));
169
170
                break;
171
            case Constants::OLEDRION_LISTS_ALL_PUBLIC:
172
173
                $criteria->add(new \Criteria('list_type', Constants::OLEDRION_LISTS_WISH, '='));
174
                $criteria->add(new \Criteria('list_type', Constants::OLEDRION_LISTS_RECOMMEND, '='), 'OR');
175
176
                break;
177
            default:
178
179
                $criteria->add(new \Criteria('list_type', $listType, '='));
180
181
                break;
182
        }
183
        if ($list_uid > 0) {
184
            $criteria->add(new \Criteria('list_uid', $list_uid, '='));
185
        }
186
187
        return $this->getCount($criteria);
188
    }
189
190
    /**
191
     * Retourne une liste d'utilisateurs Xoops en fonction d'une liste de listes
192
     *
193
     * @param  array $oledrion_lists
194
     * @return array [clé] = id utilisateur
195
     */
196
    public function getUsersFromLists($oledrion_lists)
197
    {
198
        $usersList = [];
199
        foreach ($oledrion_lists as $list) {
200
            $usersList[] = $list->list_uid;
201
        }
202
        if (count($usersList) > 0) {
203
            return Oledrion\Utility::getUsersFromIds($usersList);
204
        }
205
206
        return [];
207
    }
208
209
    /**
210
     * Suppression d'une liste (et des produits qui lui sont rattachés)
211
     *
212
     * @param  Lists $list
213
     * @return bool
214
     */
215
    public function deleteList(Lists $list)
216
    {
217
        //        $handlers = HandlerManager::getInstance();
218
        $productsListHandler->deleteListProducts($list);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $productsListHandler seems to be never defined.
Loading history...
219
220
        return $this->delete($list, true);
221
    }
222
223
    /**
224
     * Retourne les produits d'une liste
225
     *
226
     * @param  Lists $list
227
     * @return array          Objets de type Products
228
     */
229
    public function getListProducts(Lists $list)
230
    {
231
        $db                  = \XoopsDatabaseFactory::getDatabaseConnection();
232
        $productsListHandler = new Oledrion\ProductsListHandler($db);
233
        $productsHandler     = new Oledrion\ProductsHandler($db);
234
        $productsInList      = $ret = $productsIds = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $productsInList is dead and can be removed.
Loading history...
235
        //        $handlers       = HandlerManager::getInstance();
236
        $productsInList = $productsListHandler->getProductsFromList($list);
237
        if (0 === count($productsInList)) {
238
            return $ret;
239
        }
240
        foreach ($productsInList as $product) {
241
            $productsIds[] = $product->getVar('productlist_product_id');
242
        }
243
        if (count($productsIds) > 0) {
244
            $ret = $productsHandler->getProductsFromIDs($productsIds);
245
        }
246
247
        return $ret;
248
    }
249
250
    /**
251
     * Indique si une liste appartient bien à un utilisateur
252
     *
253
     * @param int $list_id
254
     * @param int $list_uid
255
     * @return bool
256
     */
257
    public function isThisMyList($list_id, $list_uid = 0)
258
    {
259
        if (0 == $list_uid) {
260
            $list_uid = Oledrion\Utility::getCurrentUserID();
261
        }
262
        $list = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $list is dead and can be removed.
Loading history...
263
        $list = $this->get((int)$list_id);
264
        if (!is_object($list)) {
265
            return false;
266
        }
267
        if ($list->getVar('list_uid') == $list_uid) {
268
            return true;
269
        }
270
271
        return false;
272
    }
273
274
    /**
275
     * Indique si un produit est dans une liste d'un utilisateur
276
     *
277
     * @param int $productlist_product_id
278
     * @param int $list_uid
279
     * @return bool
280
     */
281
    public function isProductInUserList($productlist_product_id, $list_uid = 0)
282
    {
283
        //require_once __DIR__ . '/lite.php';
284
        if (0 == $list_uid) {
285
            $list_uid = Oledrion\Utility::getCurrentUserID();
286
        }
287
        if (0 == $list_uid) {
288
            return true;
289
        }
290
        $ret                    = false;
291
        $start                  = $limit = 0;
292
        $list_uid               = (int)$list_uid;
293
        $productlist_product_id = (int)$productlist_product_id;
294
        $sql                    = 'SELECT COUNT(*) FROM ' . $this->table . ' l, ' . $this->db->prefix('oledrion_products_list') . ' p WHERE (p.productlist_list_id = l.list_id) AND (l.list_uid = ' . $list_uid . ') AND (p.productlist_product_id =' . $productlist_product_id . ')';
295
        //$CacheLite = new Oledrion_CacheLite($this->cacheOptions);
296
        $id = $this->_getIdForCache($sql, $start, $limit);
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
297
        //$cacheData = $CacheLite->get($id);
298
        //if ($cacheData === false) {
299
        $result = $this->db->query($sql, $limit, $start);
300
        if ($result) {
301
            list($count) = $this->db->fetchRow($result);
302
            if ($count > 0) {
303
                $ret = true;
304
            }
305
        }
306
307
        //$CacheLite->save($ret);
308
        return $ret;
309
        //} else {
310
        //return $cacheData;
311
        //}
312
    }
313
314
    /**
315
     * Retourne les x dernières listes qui contiennent des produits dans une certaine catégorie
316
     *
317
     * @param          $categoryId
318
     * @param int      $list_type Le type de liste
319
     * @param int      $limit     Le nombre maximum de listes à retourner
320
     * @return array   Objets de type Lists, [clé] = id liste
321
     * @internal param int $cateGoryId L'identifiant de la catégorie
322
     */
323
    public function listsFromCurrentCategory($categoryId, $list_type, $limit)
324
    {
325
        //require_once __DIR__ . '/lite.php';
326
        $ret        = [];
327
        $start      = 0;
328
        $categoryId = (int)$categoryId;
329
        $list_type  = (int)$list_type;
330
        $limit      = (int)$limit;
331
        $sql        = 'SELECT DISTINCT(z.productlist_list_id) FROM '
332
                      . $this->db->prefix('oledrion_products_list')
333
                      . ' z, '
334
                      . $this->db->prefix('oledrion_products')
335
                      . ' p, '
336
                      . $this->db->prefix('oledrion_lists')
337
                      . ' l WHERE (l.list_type = '
338
                      . $list_type
339
                      . ') AND (p.product_cid = '
340
                      . $categoryId
341
                      . ') AND (l.list_id = z.productlist_list_id) AND (z.productlist_product_id = p.product_id) AND (p.product_online = 1) ORDER BY l.list_date DESC';
342
        //$CacheLite = new Oledrion_CacheLite($this->cacheOptions);
343
        $id = $this->_getIdForCache($sql, $start, $limit);
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
344
        //$cacheData = $CacheLite->get($id);
345
        //if ($cacheData === false) {
346
        $result = $this->db->query($sql, $limit, $start);
347
        if ($result) {
348
            while (false !== ($row = $this->db->fetchArray($result))) {
349
                $ret[] = $row['productlist_list_id'];
350
            }
351
            $ret = $this->getItemsFromIds($ret);
352
        }
353
354
        //$CacheLite->save($ret);
355
        return $ret;
356
        //} else {
357
        //  return $cacheData;
358
        //}
359
    }
360
}
361