Lists::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
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
/**
24
 * Gestion des listes utilisateurs
25
 *
26
 * @since 2.3.2009.06.13
27
 */
28
29
use XoopsModules\Oledrion;
30
31
/**
32
 * Class Lists
33
 */
34
class Lists extends OledrionObject
35
{
36
    /**
37
     * constructor
38
     *
39
     * normally, this is called from child classes only
40
     */
41
    public function __construct()
42
    {
43
        $this->initVar('list_id', XOBJ_DTYPE_INT, null, false);
44
        $this->initVar('list_uid', XOBJ_DTYPE_INT, null, false);
45
        $this->initVar('list_title', XOBJ_DTYPE_TXTBOX, null, false);
46
        $this->initVar('list_date', XOBJ_DTYPE_INT, null, false);
47
        $this->initVar('list_productscount', XOBJ_DTYPE_INT, null, false);
48
        $this->initVar('list_views', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('list_password', XOBJ_DTYPE_TXTBOX, null, false);
50
        $this->initVar('list_type', XOBJ_DTYPE_INT, null, false);
51
        $this->initVar('list_description', XOBJ_DTYPE_OTHER, null, false);
52
    }
53
54
    /**
55
     * Indique si la liste courante est accessible de l'utilisateur courant
56
     *
57
     * @return bool
58
     */
59
    public function isSuitableForCurrentUser()
60
    {
61
        $uid = Oledrion\Utility::getCurrentUserID();
62
        if (Constants::OLEDRION_LISTS_PRIVATE == $this->getVar('list_type')) {
63
            if (0 == $uid || $uid != $this->getVar('list_uid')) {
64
                return false;
65
            }
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * Retourne un tableau associatif qui pour chaque type de liste indique son type sous forme de texte
73
     *
74
     * @return array
75
     */
76
    public static function getTypesArray()
77
    {
78
        return [
79
            Constants::OLEDRION_LISTS_PRIVATE   => _OLEDRION_LIST_PRIVATE,
80
            Constants::OLEDRION_LISTS_WISH      => _OLEDRION_LIST_PUBLIC_WISH_LIST,
81
            Constants::OLEDRION_LISTS_RECOMMEND => _OLEDRION_LIST_PUBLIC_RECOMMENDED_LIST,
82
        ];
83
    }
84
85
    /**
86
     * Retourne la description de la liste courante
87
     *
88
     * @return string
89
     */
90
    public function getListTypeDescription()
91
    {
92
        $description = static::getTypesArray();
93
94
        return $description[$this->list_type];
0 ignored issues
show
Bug Best Practice introduced by
The property list_type does not exist on XoopsModules\Oledrion\Lists. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
    }
96
97
    /**
98
     * Retourne l'url à utiliser pour accéder à la liste en tenant compte des préférences du module
99
     *
100
     * @return string L'url à utiliser
101
     */
102
    public function getLink()
103
    {
104
        $url = '';
105
        if (1 == Oledrion\Utility::getModuleOption('urlrewriting')) {
106
            // On utilise l'url rewriting
107
            $url = OLEDRION_URL . 'list-' . $this->getVar('list_id') . Oledrion\Utility::makeSeoUrl($this->getVar('list_title', 'n')) . '.html';
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('list_title', 'n') can also be of type array and array; however, parameter $content of XoopsModules\Oledrion\Utility::makeSeoUrl() does only seem to accept string, 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

107
            $url = OLEDRION_URL . 'list-' . $this->getVar('list_id') . Oledrion\Utility::makeSeoUrl(/** @scrutinizer ignore-type */ $this->getVar('list_title', 'n')) . '.html';
Loading history...
108
        } else {
109
            // Pas d'utilisation de l'url rewriting
110
            $url = OLEDRION_URL . 'list.php?list_id=' . $this->getVar('list_id');
111
        }
112
113
        return $url;
114
    }
115
116
    /**
117
     * Retourne la date de création de la liste formatée
118
     *
119
     * @param  string $format
120
     * @return string
121
     */
122
    public function getFormatedDate($format = 's')
123
    {
124
        return formatTimestamp($this->list_date, $format);
0 ignored issues
show
Bug Best Practice introduced by
The property list_date does not exist on XoopsModules\Oledrion\Lists. Since you implemented __get, consider adding a @property annotation.
Loading history...
125
    }
126
127
    /**
128
     * Rentourne la chaine à utiliser dans une balise <a> pour l'attribut href
129
     *
130
     * @return string
131
     */
132
    public function getHrefTitle()
133
    {
134
        return Oledrion\Utility::makeHrefTitle($this->getVar('list_title'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('list_title') can also be of type array and array; however, parameter $title of XoopsModules\Oledrion\Utility::makeHrefTitle() does only seem to accept string, 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

134
        return Oledrion\Utility::makeHrefTitle(/** @scrutinizer ignore-type */ $this->getVar('list_title'));
Loading history...
135
    }
136
137
    /**
138
     * Retourne le nom de l'auteur de la liste courante
139
     *
140
     * @return string
141
     */
142
    public function getListAuthorName()
143
    {
144
        return \XoopsUser::getUnameFromId($this->getVar('list_uid', true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type null|string expected by parameter $format of XoopsObject::getVar(). ( Ignorable by Annotation )

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

144
        return \XoopsUser::getUnameFromId($this->getVar('list_uid', /** @scrutinizer ignore-type */ true));
Loading history...
145
    }
146
147
    /**
148
     * Retourne les éléments formatés pour affichage (en général)
149
     *
150
     * @param  string $format
151
     * @return array
152
     */
153
    public function toArray($format = 's')
154
    {
155
        $ret                          = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
156
        $ret                          = parent::toArray($format);
157
        $ret['list_type_description'] = $this->getListTypeDescription();
158
        $ret['list_href_title']       = $this->getHrefTitle();
159
        $ret['list_url_rewrited']     = $this->getLink();
160
        $ret['list_formated_date']    = $this->getFormatedDate();
161
        $ret['list_username']         = $this->getListAuthorName();
162
        $ret['list_formated_count']   = sprintf(_OLEDRION_PRODUCTS_COUNT, $this->getVar('list_productscount'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('list_productscount') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, 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

162
        $ret['list_formated_count']   = sprintf(_OLEDRION_PRODUCTS_COUNT, /** @scrutinizer ignore-type */ $this->getVar('list_productscount'));
Loading history...
163
164
        return $ret;
165
    }
166
}
167