Completed
Push — master ( 658b35...b781ca )
by Richard
28s queued 23s
created

Category   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 109
dl 0
loc 253
rs 9.36
c 0
b 0
f 0
wmc 38

17 Methods

Rating   Name   Duplication   Size   Complexity  
A createMetaTags() 0 4 1
A store() 0 9 4
A template() 0 3 1
A checkPermission() 0 11 4
A toArray() 0 24 4
A sendNotifications() 0 10 2
A getGroups_submit() 0 3 1
A getCategoryUrl() 0 3 1
A toArrayTable() 0 18 4
A getCategoryPathForMetaTitle() 0 14 3
A getCategoryPath() 0 20 5
A getGroups_moderation() 0 3 1
A __construct() 0 22 1
A notLoaded() 0 3 1
A getCategoryLink() 0 7 2
A getGroups_read() 0 3 1
A image() 0 7 2
1
<?php
2
3
namespace XoopsModules\Publisher;
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
use Notifications;
16
use Xoops;
17
use Xoops\Core\Kernel\XoopsObject;
18
use XoopsModules\Publisher;
19
20
/**
21
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
22
 * @license         GNU GPL V2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @package         Publisher
24
 * @since           1.0
25
 * @author          trabis <[email protected]>
26
 * @author          The SmartFactory <www.smartfactory.ca>
27
 * @version         $Id$
28
 */
29
require_once \dirname(__DIR__) . '/include/common.php';
30
31
/**
32
 * Class Category
33
 * @package XoopsModules\Publisher
34
 */
35
class Category extends XoopsObject
36
{
37
    /**
38
     * @var Helper
39
     * @access public
40
     */
41
    public $helper = null;
42
43
    /**
44
     * @var array
45
     */
46
    public $_categoryPath = false;
47
48
    /**
49
     * constructor
50
     */
51
    public function __construct()
52
    {
53
        $this->helper = Helper::getInstance();
54
        $this->initVar('categoryid', \XOBJ_DTYPE_INT, null, false);
55
        $this->initVar('parentid', \XOBJ_DTYPE_INT, null, false);
56
        $this->initVar('name', \XOBJ_DTYPE_TXTBOX, null, true, 100);
57
        $this->initVar('description', \XOBJ_DTYPE_TXTAREA, null, false, 255);
58
        $this->initVar('image', \XOBJ_DTYPE_TXTBOX, null, false, 255);
59
        $this->initVar('total', \XOBJ_DTYPE_INT, 1, false);
60
        $this->initVar('weight', \XOBJ_DTYPE_INT, 1, false);
61
        $this->initVar('created', \XOBJ_DTYPE_INT, null, false);
62
        $this->initVar('template', \XOBJ_DTYPE_TXTBOX, null, false, 255);
63
        $this->initVar('header', \XOBJ_DTYPE_TXTAREA, null, false);
64
        $this->initVar('meta_keywords', \XOBJ_DTYPE_TXTAREA, null, false);
65
        $this->initVar('meta_description', \XOBJ_DTYPE_TXTAREA, null, false);
66
        $this->initVar('short_url', \XOBJ_DTYPE_TXTBOX, null, false, 255);
67
        $this->initVar('moderator', \XOBJ_DTYPE_INT, null, false, 0);
68
        //not persistent values
69
        $this->initVar('itemcount', \XOBJ_DTYPE_INT, 0, false);
70
        $this->initVar('last_itemid', \XOBJ_DTYPE_INT);
71
        $this->initVar('last_title_link', \XOBJ_DTYPE_TXTBOX);
72
        $this->initVar('dohtml', \XOBJ_DTYPE_INT, 1, false);
73
    }
74
75
    public function notLoaded(): bool
76
    {
77
        return (-1 == $this->getVar('categoryid'));
78
    }
79
80
    public function checkPermission(): bool
81
    {
82
        $xoops = Xoops::getInstance();
83
        if ($this->helper->isUserAdmin()) {
84
            return true;
85
        }
86
        if ($xoops->isUser() && $xoops->user->getVar('uid') == $this->getVar('moderator')) {
87
            return true;
88
        }
89
90
        return $this->helper->getPermissionHandler()->isGranted('category_read', $this->getVar('categoryid'));
91
    }
92
93
    /**
94
     * @param string $format
95
     *
96
     * @return mixed|string
97
     */
98
    public function image($format = 's')
99
    {
100
        if ('' != $this->getVar('image')) {
101
            return $this->getVar('image', $format);
102
        }
103
104
        return 'blank.png';
105
    }
106
107
    /**
108
     * @param string $format
109
     *
110
     * @return mixed
111
     */
112
    public function template($format = 'n')
113
    {
114
        return $this->getVar('template', $format);
115
    }
116
117
    /**
118
     * @param bool $withAllLink
119
     *
120
     * @return array|bool|string
121
     */
122
    public function getCategoryPath($withAllLink = true)
123
    {
124
        if (!$this->_categoryPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_categoryPath of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
125
            if ($withAllLink) {
126
                $ret = $this->getCategoryLink();
127
            } else {
128
                $ret = $this->getVar('name');
129
            }
130
            $parentid = $this->getVar('parentid');
131
            if (0 != $parentid) {
132
                $parentObj = $this->helper->getCategoryHandler()->get($parentid);
133
                if ($parentObj->notLoaded()) {
134
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
135
                }
136
                $ret = $parentObj->getCategoryPath($withAllLink) . ' > ' . $ret;
0 ignored issues
show
Bug introduced by
Are you sure $parentObj->getCategoryPath($withAllLink) of type array can be used in concatenation? ( Ignorable by Annotation )

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

136
                $ret = /** @scrutinizer ignore-type */ $parentObj->getCategoryPath($withAllLink) . ' > ' . $ret;
Loading history...
137
            }
138
            $this->_categoryPath = $ret;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ret can also be of type string. However, the property $_categoryPath is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
139
        }
140
141
        return $this->_categoryPath;
142
    }
143
144
    /**
145
     * @return mixed|string
146
     */
147
    public function getCategoryPathForMetaTitle()
148
    {
149
        $ret = '';
150
        $parentid = $this->getVar('parentid');
151
        if (0 != $parentid) {
152
            $parentObj = $this->helper->getCategoryHandler()->get($parentid);
153
            if ($parentObj->notLoaded()) {
154
                exit('NOT LOADED');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
155
            }
156
            $ret = $parentObj->getCategoryPath(false);
157
            $ret = \str_replace(' >', ' -', $ret);
158
        }
159
160
        return $ret;
161
    }
162
163
    public function getGroups_read(): ?array
164
    {
165
        return $this->helper->getPermissionHandler()->getGrantedGroupsById('category_read', $this->getVar('categoryid'));
166
    }
167
168
    public function getGroups_submit(): ?array
169
    {
170
        return $this->helper->getPermissionHandler()->getGrantedGroupsById('item_submit', $this->getVar('categoryid'));
171
    }
172
173
    public function getGroups_moderation(): ?array
174
    {
175
        return $this->helper->getPermissionHandler()->getGrantedGroupsById('category_moderation', $this->getVar('categoryid'));
176
    }
177
178
    public function getCategoryUrl(): string
179
    {
180
        return Publisher\Utils::seoGenUrl('category', $this->getVar('categoryid'), $this->getVar('short_url'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return XoopsModules\Publ...s->getVar('short_url')) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
181
    }
182
183
    /**
184
     * @param bool $class
185
     *
186
     * @return string
187
     */
188
    public function getCategoryLink($class = false): ?string
189
    {
190
        if ($class) {
191
            return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->getVar('name') . '</a>';
192
        }
193
194
        return "<a href='" . $this->getCategoryUrl() . "'>" . $this->getVar('name') . '</a>';
195
    }
196
197
    /**
198
     * @param bool $sendNotifications
199
     * @param bool $force
200
     *
201
     * @return mixed
202
     */
203
    public function store($sendNotifications = true, $force = true)
204
    {
205
        $ret = $this->helper->getCategoryHandler()->insert($this, $force);
206
        if ($sendNotifications && $ret && $this->isNew()) {
207
            $this->sendNotifications();
208
        }
209
        $this->unsetNew();
210
211
        return $ret;
212
    }
213
214
    /**
215
     * Send notifications
216
     */
217
    public function sendNotifications(): void
218
    {
219
        $xoops = Xoops::getInstance();
220
        if ($xoops->isActiveModule('notifications')) {
221
            $tags = [];
222
            $tags['MODULE_NAME'] = $this->helper->getModule()->getVar('name');
223
            $tags['CATEGORY_NAME'] = $this->getVar('name');
224
            $tags['CATEGORY_URL'] = $this->getCategoryUrl();
225
            $notificationHandler = Notifications::getInstance()->getHandlerNotification();
226
            $notificationHandler->triggerEvent('global', 0, 'category_created', $tags);
227
        }
228
    }
229
230
    /**
231
     * @param array $category
232
     *
233
     * @return array
234
     */
235
    public function toArray($category = [])
236
    {
237
        $category['categoryid'] = $this->getVar('categoryid');
238
        $category['name'] = $this->getVar('name');
239
        $category['categorylink'] = $this->getCategoryLink();
240
        $category['categoryurl'] = $this->getCategoryUrl();
241
        $category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : '';
242
        $category['description'] = $this->getVar('description');
243
        $category['header'] = $this->getVar('header');
244
        $category['meta_keywords'] = $this->getVar('meta_keywords');
245
        $category['meta_description'] = $this->getVar('meta_description');
246
        $category['short_url'] = $this->getVar('short_url');
247
        if ($this->getVar('last_itemid') > 0) {
248
            $category['last_itemid'] = $this->getVar('last_itemid', 'n');
249
            $category['last_title_link'] = $this->getVar('last_title_link', 'n');
250
        }
251
        if ('blank.png' !== $this->image()) {
252
            $category['image_path'] = Publisher\Utils::getImageDir('category', false) . $this->image();
253
        } else {
254
            $category['image_path'] = '';
255
        }
256
        $category['lang_subcategories'] = \sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->getVar('name'));
257
258
        return $category;
259
    }
260
261
    /**
262
     * @param array $category
263
     */
264
    public function toArrayTable($category = []): array
265
    {
266
        $category['categoryid'] = $this->getVar('categoryid');
267
        $category['categorylink'] = $this->getCategoryLink();
268
        $category['total'] = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : '';
269
        $category['description'] = $this->getVar('description');
270
        if ($this->getVar('last_itemid') > 0) {
271
            $category['last_itemid'] = $this->getVar('last_itemid', 'n');
272
            $category['last_title_link'] = $this->getVar('last_title_link', 'n');
273
        }
274
        if ('blank.png' !== $this->image()) {
275
            $category['image_path'] = Publisher\Utils::getImageDir('category', false) . $this->image();
276
        } else {
277
            $category['image_path'] = '';
278
        }
279
        $category['lang_subcategories'] = \sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->getVar('name'));
280
281
        return $category;
282
    }
283
284
    public function createMetaTags(): void
285
    {
286
        $publisher_metagen = new Publisher\Metagen($this->getVar('name'), $this->getVar('meta_keywords'), $this->getVar('meta_description'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('meta_keywords') can also be of type string[]; however, parameter $keywords of XoopsModules\Publisher\Metagen::__construct() 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

286
        $publisher_metagen = new Publisher\Metagen($this->getVar('name'), /** @scrutinizer ignore-type */ $this->getVar('meta_keywords'), $this->getVar('meta_description'));
Loading history...
Bug introduced by
It seems like $this->getVar('name') can also be of type string[]; however, parameter $title of XoopsModules\Publisher\Metagen::__construct() 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

286
        $publisher_metagen = new Publisher\Metagen(/** @scrutinizer ignore-type */ $this->getVar('name'), $this->getVar('meta_keywords'), $this->getVar('meta_description'));
Loading history...
Bug introduced by
It seems like $this->getVar('meta_description') can also be of type string[]; however, parameter $description of XoopsModules\Publisher\Metagen::__construct() 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

286
        $publisher_metagen = new Publisher\Metagen($this->getVar('name'), $this->getVar('meta_keywords'), /** @scrutinizer ignore-type */ $this->getVar('meta_description'));
Loading history...
287
        $publisher_metagen->createMetaTags();
288
    }
289
}
290