Completed
Push — master ( 9d3fbd...af269e )
by Michael
09:48
created

class/oledrion_lists.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link https://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
/**
20
 * Gestion des listes utilisateurs
21
 *
22
 * @since 2.3.2009.06.13
23
 */
24
require_once __DIR__ . '/classheader.php';
25
26
/**
27
 * Définition des types de listes
28
 */
29
define('OLEDRION_LISTS_ALL_PUBLIC', -2); // Que les publiques
30
define('OLEDRION_LISTS_ALL', -1); // Toutes sans distinction
31
define('OLEDRION_LISTS_PRIVATE', 0);
32
define('OLEDRION_LISTS_WISH', 1);
33
define('OLEDRION_LISTS_RECOMMEND', 2);
34
35
/**
36
 * Class Oledrion_lists
37
 */
38
class Oledrion_lists extends Oledrion_Object
39
{
40
    /**
41
     * constructor
42
     *
43
     * normally, this is called from child classes only
44
     *
45
     * @access public
46
     */
47
    public function __construct()
48
    {
49
        $this->initVar('list_id', XOBJ_DTYPE_INT, null, false);
50
        $this->initVar('list_uid', XOBJ_DTYPE_INT, null, false);
51
        $this->initVar('list_title', XOBJ_DTYPE_TXTBOX, null, false);
52
        $this->initVar('list_date', XOBJ_DTYPE_INT, null, false);
53
        $this->initVar('list_productscount', XOBJ_DTYPE_INT, null, false);
54
        $this->initVar('list_views', XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('list_password', XOBJ_DTYPE_TXTBOX, null, false);
56
        $this->initVar('list_type', XOBJ_DTYPE_INT, null, false);
57
        $this->initVar('list_description', XOBJ_DTYPE_TXTAREA, null, false);
58
    }
59
60
    /**
61
     * Indique si la liste courante est accessible de l'utilisateur courant
62
     *
63
     * @return boolean
64
     */
65
    public function isSuitableForCurrentUser()
66
    {
67
        $uid = OledrionUtility::getCurrentUserID();
68
        if ($this->getVar('list_type') == OLEDRION_LISTS_PRIVATE) {
69
            if ($uid == 0 || $uid != $this->getVar('list_uid')) {
70
                return false;
71
            }
72
        }
73
74
        return true;
75
    }
76
77
    /**
78
     * Retourne un tableau associatif qui pour chaque type de liste indique son type sous forme de texte
79
     *
80
     * @return array
81
     */
82
    public static function getTypesArray()
83
    {
84
        return array(
85
            OLEDRION_LISTS_PRIVATE   => _OLEDRION_LIST_PRIVATE,
86
            OLEDRION_LISTS_WISH      => _OLEDRION_LIST_PUBLIC_WISH_LIST,
87
            OLEDRION_LISTS_RECOMMEND => _OLEDRION_LIST_PUBLIC_RECOMMENDED_LIST
88
        );
89
    }
90
91
    /**
92
     * Retourne la description de la liste courante
93
     *
94
     * @return string
95
     */
96
    public function getListTypeDescription()
97
    {
98
        $description = static::getTypesArray();
99
100
        return $description[$this->list_type];
101
    }
102
103
    /**
104
     * Retourne l'url à utiliser pour accéder à la liste en tenant compte des préférences du module
105
     *
106
     * @return string L'url à utiliser
107
     */
108 View Code Duplication
    public function getLink()
109
    {
110
        $url = '';
111
        if (OledrionUtility::getModuleOption('urlrewriting') == 1) { // On utilise l'url rewriting
112
            $url = OLEDRION_URL . 'list-' . $this->getVar('list_id') . OledrionUtility::makeSeoUrl($this->getVar('list_title', 'n')) . '.html';
113
        } else { // Pas d'utilisation de l'url rewriting
114
            $url = OLEDRION_URL . 'list.php?list_id=' . $this->getVar('list_id');
115
        }
116
117
        return $url;
118
    }
119
120
    /**
121
     * Retourne la date de création de la liste formatée
122
     *
123
     * @param  string $format
124
     * @return string
125
     */
126
    public function getFormatedDate($format = 's')
127
    {
128
        return formatTimestamp($this->list_date, $format);
129
    }
130
131
    /**
132
     * Rentourne la chaine à utiliser dans une balise <a> pour l'attribut href
133
     *
134
     * @return string
135
     */
136
    public function getHrefTitle()
137
    {
138
        return OledrionUtility::makeHrefTitle($this->getVar('list_title'));
139
    }
140
141
    /**
142
     * Retourne le nom de l'auteur de la liste courante
143
     *
144
     * @return string
145
     */
146
    public function getListAuthorName()
147
    {
148
        return XoopsUser::getUnameFromId($this->getVar('list_uid', true));
149
    }
150
151
    /**
152
     * Retourne les éléments formatés pour affichage (en général)
153
     *
154
     * @param  string $format
155
     * @return array
156
     */
157
    public function toArray($format = 's')
158
    {
159
        $ret                          = array();
160
        $ret                          = parent::toArray($format);
161
        $ret['list_type_description'] = $this->getListTypeDescription();
162
        $ret['list_href_title']       = $this->getHrefTitle();
163
        $ret['list_url_rewrited']     = $this->getLink();
164
        $ret['list_formated_date']    = $this->getFormatedDate();
165
        $ret['list_username']         = $this->getListAuthorName();
166
        $ret['list_formated_count']   = sprintf(_OLEDRION_PRODUCTS_COUNT, $this->getVar('list_productscount'));
167
168
        return $ret;
169
    }
170
}
171
172
/**
173
 * Class OledrionOledrion_listsHandler
174
 */
175
class OledrionOledrion_listsHandler extends Oledrion_XoopsPersistableObjectHandler
176
{
177
    /**
178
     * OledrionOledrion_listsHandler constructor.
179
     * @param XoopsDatabase|null $db
180
     */
181
    public function __construct(XoopsDatabase $db)
182
    { //                            Table               Classe           Id       Identifiant
183
        parent::__construct($db, 'oledrion_lists', 'oledrion_lists', 'list_id', 'list_title');
184
    }
185
186
    /**
187
     * Incrémente le compteur de vues d'une liste
188
     *
189
     * @param  oledrion_lists $list
190
     * @return boolean
191
     */
192 View Code Duplication
    public function incrementListViews(Oledrion_lists $list)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
    {
194
        $res = true;
195
        if (OledrionUtility::getCurrentUserID() != $list->getVar('list_uid')) {
196
            $sql = 'UPDATE ' . $this->table . ' SET list_views = list_views + 1 WHERE list_id = ' . $list->getVar('list_id');
197
            $res = $this->db->queryF($sql);
198
            $this->forceCacheClean();
199
        }
200
201
        return $res;
202
    }
203
204
    /**
205
     * Incrémente le nombre de produits dans une liste
206
     *
207
     * @param  oledrion_lists $list
208
     * @return boolean
209
     */
210
    public function incrementListProductsCount(Oledrion_lists $list)
211
    {
212
        $res = true;
213
        $sql = 'UPDATE ' . $this->table . ' SET list_productscount = list_productscount + 1 WHERE list_id = ' . $list->getVar('list_id');
214
        $res = $this->db->queryF($sql);
215
        $this->forceCacheClean();
216
217
        return $res;
218
    }
219
220
    /**
221
     * Décrémente le nombre de produits dans une liste
222
     *
223
     * @param  oledrion_lists $list
224
     * @param  int            $value
225
     * @return bool
226
     */
227 View Code Duplication
    public function decrementListProductsCount(Oledrion_lists $list, $value = 1)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
    {
229
        $value = (int)$value;
230
        $res   = true;
231
        $sql   = 'UPDATE ' . $this->table . ' SET list_productscount = list_productscount - $value WHERE list_id = ' . $list->getVar('list_id');
232
        $res   = $this->db->queryF($sql);
233
        $this->forceCacheClean();
234
235
        return $res;
236
    }
237
238
    /**
239
     * Retourne la liste des listes récentes
240
     *
241
     * @param  Oledrion_parameters $parameters
242
     * @return array               Tableau d'objets de type oledrion_lists [clé] = id liste
243
     * @internal param int $start
244
     * @internal param int $limit
245
     * @internal param string $sort
246
     * @internal param string $order
247
     * @internal param bool $idAsKey
248
     * @internal param int $listType
249
     * @internal param int $list_uid
250
     */
251
    public function getRecentLists(Oledrion_parameters $parameters)
252
    {
253
        $parameters = $parameters->extend(new Oledrion_parameters(array(
254
                                                                      'start'    => 0,
255
                                                                      'limit'    => 0,
256
                                                                      'sort'     => 'list_date',
257
                                                                      'order'    => 'DESC',
258
                                                                      'idAsKey'  => true,
259
                                                                      'listType' => OLEDRION_LISTS_ALL,
260
                                                                      'list_uid' => 0
261
                                                                  )));
262
        $criteria   = new CriteriaCompo();
263 View Code Duplication
        switch ($parameters['listType']) {
264
            case OLEDRION_LISTS_ALL:
265
                $criteria->add(new Criteria('list_id', 0, '<>'));
266
                break;
267
            case OLEDRION_LISTS_ALL_PUBLIC:
268
                $criteria->add(new Criteria('list_type', OLEDRION_LISTS_WISH, '='));
269
                $criteria->add(new Criteria('list_type', OLEDRION_LISTS_RECOMMEND, '='), 'OR');
270
                break;
271
            default:
272
                $criteria->add(new Criteria('list_type', $parameters['listType'], '='));
273
                break;
274
        }
275
        if ($parameters['list_uid'] > 0) {
276
            $criteria->add(new Criteria('list_uid', $parameters['list_uid'], '='));
277
        }
278
        $criteria->setSort($parameters['sort']);
279
        $criteria->setOrder($parameters['order']);
280
        $criteria->setStart($parameters['start']);
281
        $criteria->setLimit($parameters['limit']);
282
283
        return $this->getObjects($criteria, $parameters['idAsKey']);
284
    }
285
286
    /**
287
     * Retourne le nombre de listes d'un certain type
288
     *
289
     * @param  integer $listType
290
     * @param  integer $list_uid
291
     * @return integer
292
     */
293
    public function getRecentListsCount($listType = OLEDRION_LISTS_ALL, $list_uid = 0)
294
    {
295
        $criteria = new CriteriaCompo();
296 View Code Duplication
        switch ($listType) {
297
            case OLEDRION_LISTS_ALL:
298
                $criteria->add(new Criteria('list_id', 0, '<>'));
299
                break;
300
            case OLEDRION_LISTS_ALL_PUBLIC:
301
                $criteria->add(new Criteria('list_type', OLEDRION_LISTS_WISH, '='));
302
                $criteria->add(new Criteria('list_type', OLEDRION_LISTS_RECOMMEND, '='), 'OR');
303
                break;
304
            default:
305
                $criteria->add(new Criteria('list_type', $listType, '='));
306
                break;
307
        }
308
        if ($list_uid > 0) {
309
            $criteria->add(new Criteria('list_uid', $list_uid, '='));
310
        }
311
312
        return $this->getCount($criteria);
313
    }
314
315
    /**
316
     * Retourne une liste d'utilisateurs Xoops en fonction d'une liste de listes
317
     *
318
     * @param  array $oledrion_lists
319
     * @return array [clé] = id utilisateur
320
     */
321
    public function getUsersFromLists($oledrion_lists)
322
    {
323
        $usersList = array();
324
        foreach ($oledrion_lists as $list) {
325
            $usersList[] = $list->list_uid;
326
        }
327
        if (count($usersList) > 0) {
328
            return OledrionUtility::getUsersFromIds($usersList);
329
        } else {
330
            return array();
331
        }
332
    }
333
334
    /**
335
     * Suppression d'une liste (et des produits qui lui sont rattachés)
336
     *
337
     * @param  oledrion_lists $list
338
     * @return boolean
339
     */
340
    public function deleteList(Oledrion_lists $list)
341
    {
342
        $handlers = OledrionHandler::getInstance();
343
        $handlers->h_oledrion_products_list->deleteListProducts($list);
344
345
        return $this->delete($list, true);
346
    }
347
348
    /**
349
     * Retourne les produits d'une liste
350
     *
351
     * @param  oledrion_lists $list
352
     * @return array          Objets de type oledrion_products
353
     */
354
    public function getListProducts(Oledrion_lists $list)
355
    {
356
        $productsInList = $ret = $productsIds = array();
357
        $handlers       = OledrionHandler::getInstance();
358
        $productsInList = $handlers->h_oledrion_products_list->getProductsFromList($list);
359
        if (count($productsInList) == 0) {
360
            return $ret;
361
        }
362
        foreach ($productsInList as $product) {
363
            $productsIds[] = $product->getVar('productlist_product_id');
364
        }
365
        if (count($productsIds) > 0) {
366
            $ret = $handlers->h_oledrion_products->getProductsFromIDs($productsIds);
367
        }
368
369
        return $ret;
370
    }
371
372
    /**
373
     * Indique si une liste appartient bien à un utilisateur
374
     *
375
     * @param  integer $list_id
376
     * @param  integer $list_uid
377
     * @return boolean
378
     */
379
    public function isThisMyList($list_id, $list_uid = 0)
380
    {
381
        if ($list_uid == 0) {
382
            $list_uid = OledrionUtility::getCurrentUserID();
383
        }
384
        $list = null;
385
        $list = $this->get((int)$list_id);
386
        if (!is_object($list)) {
387
            return false;
388
        }
389
        if ($list->getVar('list_uid') == $list_uid) {
390
            return true;
391
        } else {
392
            return false;
393
        }
394
    }
395
396
    /**
397
     * Indique si un produit est dans une liste d'un utilisateur
398
     *
399
     * @param  integer $productlist_product_id
400
     * @param  integer $list_uid
401
     * @return boolean
402
     */
403
    public function isProductInUserList($productlist_product_id, $list_uid = 0)
404
    {
405
        //require_once __DIR__ . '/lite.php';
406
        if ($list_uid == 0) {
407
            $list_uid = OledrionUtility::getCurrentUserID();
408
        }
409
        if ($list_uid == 0) {
410
            return true;
411
        }
412
        $ret                    = false;
413
        $start                  = $limit = 0;
414
        $list_uid               = (int)$list_uid;
415
        $productlist_product_id = (int)$productlist_product_id;
416
        $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 . ')';
417
        //$Cache_Lite = new oledrion_Cache_Lite($this->cacheOptions);
418
        $id = $this->_getIdForCache($sql, $start, $limit);
0 ignored issues
show
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
419
        //$cacheData = $Cache_Lite->get($id);
420
        //if ($cacheData === false) {
421
        $result = $this->db->query($sql, $limit, $start);
422
        if ($result) {
423
            list($count) = $this->db->fetchRow($result);
424
            if ($count > 0) {
425
                $ret = true;
426
            }
427
        }
428
429
        //$Cache_Lite->save($ret);
430
        return $ret;
431
        //} else {
432
        //return $cacheData;
433
        //}
434
    }
435
436
    /**
437
     * Retourne les x dernières listes qui contiennent des produits dans une certaine catégorie
438
     *
439
     * @param          $categoryId
440
     * @param  integer $list_type Le type de liste
441
     * @param  integer $limit     Le nombre maximum de listes à retourner
442
     * @return array   Objets de type oledrion_lists, [clé] = id liste
443
     * @internal param int $cateGoryId L'identifiant de la catégorie
444
     */
445
    public function listsFromCurrentCategory($categoryId, $list_type, $limit)
446
    {
447
        //require_once __DIR__ . '/lite.php';
448
        $ret        = array();
449
        $start      = 0;
450
        $categoryId = (int)$categoryId;
451
        $list_type  = (int)$list_type;
452
        $limit      = (int)$limit;
453
        $sql        = 'SELECT DISTINCT(z.productlist_list_id) FROM '
454
                      . $this->db->prefix('oledrion_products_list')
455
                      . ' z, '
456
                      . $this->db->prefix('oledrion_products')
457
                      . ' p, '
458
                      . $this->db->prefix('oledrion_lists')
459
                      . ' l WHERE (l.list_type = '
460
                      . $list_type
461
                      . ') AND (p.product_cid = '
462
                      . $categoryId
463
                      . ') 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';
464
        //$Cache_Lite = new oledrion_Cache_Lite($this->cacheOptions);
465
        $id = $this->_getIdForCache($sql, $start, $limit);
466
        //$cacheData = $Cache_Lite->get($id);
467
        //if ($cacheData === false) {
468
        $result = $this->db->query($sql, $limit, $start);
469
        if ($result) {
470
            while ($row = $this->db->fetchArray($result)) {
471
                $ret[] = $row['productlist_list_id'];
472
            }
473
            $ret = $this->getItemsFromIds($ret);
474
        }
475
476
        //$Cache_Lite->save($ret);
477
        return $ret;
478
        //} else {
479
        //  return $cacheData;
480
        //}
481
    }
482
}
483