Category::weight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Smartfaq;
4
5
/**
6
 * Module: SmartFAQ
7
 * Author: The SmartFactory <www.smartfactory.ca>
8
 * Licence: GNU
9
 */
10
11
use MyTextSanitizer;
12
use XoopsDatabaseFactory;
13
use XoopsModules\Smartfaq;
14
use XoopsObject;
15
16
/**
17
 * Class Category
18
 */
19
class Category extends XoopsObject
20
{
21
    public $db;
22
    /**
23
     * @var array|null
24
     */
25
    private $groups_read = [];
26
    /**
27
     * @var array|null
28
     */
29
    private $groups_admin = [];
0 ignored issues
show
introduced by
The private property $groups_admin is not used, and could be removed.
Loading history...
30
31
    /**
32
     * constructor
33
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
34
     */
35
    public function __construct($id = null)
36
    {
37
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
38
        $this->initVar('categoryid', \XOBJ_DTYPE_INT, null, false);
39
        $this->initVar('parentid', \XOBJ_DTYPE_INT, null, false);
40
        $this->initVar('name', \XOBJ_DTYPE_TXTBOX, null, true, 100);
41
        $this->initVar('description', \XOBJ_DTYPE_TXTAREA, null, false, 255);
42
        $this->initVar('total', \XOBJ_DTYPE_INT, 1, false);
43
        $this->initVar('weight', \XOBJ_DTYPE_INT, 1, false);
44
        $this->initVar('created', \XOBJ_DTYPE_INT, null, false);
45
        $this->initVar('last_faq', \XOBJ_DTYPE_INT);
46
47
        //not persistent values
48
        $this->initVar('faqcount', \XOBJ_DTYPE_INT, 0, false);
49
        $this->initVar('last_faqid', \XOBJ_DTYPE_INT);
50
        $this->initVar('last_question_link', \XOBJ_DTYPE_TXTBOX);
51
52
        if (null !== $id) {
0 ignored issues
show
introduced by
The condition null !== $id is always false.
Loading history...
53
            if (\is_array($id)) {
54
                $this->assignVars($id);
55
            } else {
56
                /** @var Smartfaq\CategoryHandler $categoryHandler */
57
                $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
58
                $category        = $categoryHandler->get($id);
59
                foreach ($category->vars as $k => $v) {
60
                    $this->assignVar($k, $v['value']);
61
                }
62
                $this->assignOtherProperties();
63
            }
64
        }
65
    }
66
67
    /**
68
     * @return bool
69
     */
70
    public function notLoaded()
71
    {
72
        return (-1 == $this->getVar('categoryid'));
73
    }
74
75
    public function assignOtherProperties(): void
76
    {
77
        global $xoopsUser;
78
        $smartModule = Smartfaq\Utility::getModuleInfo();
79
        $module_id   = $smartModule->getVar('mid');
80
        /** @var \XoopsGroupPermHandler $grouppermHandler */
81
        $grouppermHandler = \xoops_getHandler('groupperm');
82
83
        $this->groups_read = $grouppermHandler->getGroupIds('category_read', $this->categoryid(), $module_id);
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    public function checkPermission()
90
    {
91
        //        require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
92
93
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
94
        if ($userIsAdmin) {
95
            return true;
96
        }
97
98
        /** @var Smartfaq\PermissionHandler $smartPermHandler */
99
        $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission');
100
101
        $categoriesGranted = $smartPermHandler->getPermissions('category');
102
        if (\in_array($this->categoryid(), $categoriesGranted, true)) {
103
            $ret = true;
104
        }
105
106
        return $ret;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret does not seem to be defined for all execution paths leading up to this point.
Loading history...
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function categoryid()
113
    {
114
        return $this->getVar('categoryid');
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function parentid()
121
    {
122
        return $this->getVar('parentid');
123
    }
124
125
    /**
126
     * @param  string $format
127
     * @return mixed
128
     */
129
    public function name($format = 'S')
130
    {
131
        $ret = $this->getVar('name', $format);
132
        if (('s' === $format) || ('S' === $format) || ('show' === $format)) {
133
            $myts = MyTextSanitizer::getInstance();
134
            $ret  = $myts->displayTarea($ret);
0 ignored issues
show
Bug introduced by
It seems like $ret can also be of type array and array; however, parameter $text of MyTextSanitizer::displayTarea() 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
            $ret  = $myts->displayTarea(/** @scrutinizer ignore-type */ $ret);
Loading history...
135
        }
136
137
        return $ret;
138
    }
139
140
    /**
141
     * @param  string $format
142
     * @return mixed
143
     */
144
    public function description($format = 'S')
145
    {
146
        return $this->getVar('description', $format);
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152
    public function weight()
153
    {
154
        return $this->getVar('weight');
155
    }
156
157
    /**
158
     * @param  bool $withAllLink
159
     * @param  bool $open
160
     * @return mixed|string
161
     */
162
    public function getCategoryPath($withAllLink = false, $open = false)
163
    {
164
        $filename = 'category.php';
165
        if ($open) {
166
            $filename = 'open_category.php';
167
        }
168
        if ($withAllLink) {
169
            $ret = "<a href='" . XOOPS_URL . '/modules/smartfaq/' . $filename . '?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
170
        } else {
171
            $ret = $this->name();
172
        }
173
        $parentid        = $this->parentid();
174
        /** @var Smartfaq\CategoryHandler $categoryHandler */
175
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
176
        if (0 != $parentid) {
177
            $parentObj = $categoryHandler->get($parentid);
178
            if ($parentObj->notLoaded()) {
179
                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...
180
            }
181
            $parentid = $parentObj->parentid();
0 ignored issues
show
Unused Code introduced by
The assignment to $parentid is dead and can be removed.
Loading history...
182
            $ret      = $parentObj->getCategoryPath(true, $open) . ' > ' . $ret;
183
        }
184
185
        return $ret;
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public function getGroups_read()
192
    {
193
        //        if(count($this->groups_read) < 1) {
194
        if (!\is_array($this->groups_read)) {
195
            $this->assignOtherProperties();
196
        }
197
198
        return $this->groups_read;
199
    }
200
201
    /**
202
     * @param array $groups_read
203
     */
204
    public function setGroups_read($groups_read = ['0']): void
205
    {
206
        $this->groups_read = $groups_read;
207
    }
208
209
    /**
210
     * @param  bool $sendNotifications
211
     * @param  bool $force
212
     * @return bool
213
     */
214
    public function store($sendNotifications = true, $force = true)
215
    {
216
        //        $categoryHandler = new sfCategoryHandler($this->db);
217
        /** @var Smartfaq\CategoryHandler $categoryHandler */
218
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
219
220
        $ret = $categoryHandler->insert($this, $force);
221
        if ($sendNotifications && $ret && $this->isNew()) {
222
            $this->sendNotifications();
223
        }
224
        $this->unsetNew();
225
226
        return $ret;
227
    }
228
229
    public function sendNotifications(): void
230
    {
231
        $smartModule = Smartfaq\Utility::getModuleInfo();
232
233
        $myts                = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
234
        /** @var \XoopsNotificationHandler $notificationHandler */
235
        $notificationHandler = \xoops_getHandler('notification');
0 ignored issues
show
Unused Code introduced by
The assignment to $notificationHandler is dead and can be removed.
Loading history...
236
237
        $tags                  = [];
238
        $tags['MODULE_NAME']   = \htmlspecialchars($smartModule->getVar('name'), ENT_QUOTES | ENT_HTML5);
239
        $tags['CATEGORY_NAME'] = $this->name();
240
        $tags['CATEGORY_URL']  = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/category.php?categoryid=' . $this->categoryid();
241
242
        $notificationHandler = \xoops_getHandler('notification');
243
        $notificationHandler->triggerEvent('global_faq', 0, 'category_created', $tags);
0 ignored issues
show
Bug introduced by
The method triggerEvent() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsNotificationHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

243
        $notificationHandler->/** @scrutinizer ignore-call */ 
244
                              triggerEvent('global_faq', 0, 'category_created', $tags);
Loading history...
244
    }
245
246
    /**
247
     * @param  array $category
248
     * @param  bool  $open
249
     * @return array
250
     */
251
    public function toArray($category = [], $open = false)
252
    {
253
        $category['categoryid'] = $this->categoryid();
254
        $category['name']       = $this->name();
255
        if ($open) {
256
            $category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/open_category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
257
        } else {
258
            $category['categorylink'] = "<a href='" . XOOPS_URL . '/modules/smartfaq/category.php?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
259
        }
260
        $category['total']       = $this->getVar('faqcount');
261
        $category['description'] = $this->description();
262
263
        if ($this->getVar('last_faqid') > 0) {
264
            $category['last_faqid']         = $this->getVar('last_faqid', 'n');
265
            $category['last_question_link'] = $this->getVar('last_question_link', 'n');
266
        }
267
268
        return $category;
269
    }
270
}
271