Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

Category::parentid()   A

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
2
3
namespace XoopsModules\Smartfaq;
4
5
/**
6
 * Module: SmartFAQ
7
 * Author: The SmartFactory <www.smartfactory.ca>
8
 * Licence: GNU
9
 */
10
11
use XoopsModules\Smartfaq;
12
13
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
14
15
/**
16
 * Class Category
17
 * @package XoopsModules\Smartfaq
18
 */
19
class Category extends \XoopsObject
20
{
21
    public $db;
22
23
    /**
24
     * @var array
25
     * @access private
26
     */
27
    private $groups_read = null;
28
29
    /**
30
     * @var array
31
     * @access private
32
     */
33
    private $groups_admin = null;
0 ignored issues
show
introduced by
The private property $groups_admin is not used, and could be removed.
Loading history...
34
35
    /**
36
     * constructor
37
     * @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...
38
     */
39
    public function __construct($id = null)
40
    {
41
        $this->db = \XoopsDatabaseFactory::getDatabaseConnection();
42
        $this->initVar('categoryid', XOBJ_DTYPE_INT, null, false);
43
        $this->initVar('parentid', XOBJ_DTYPE_INT, null, false);
44
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
45
        $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255);
46
        $this->initVar('total', XOBJ_DTYPE_INT, 1, false);
47
        $this->initVar('weight', XOBJ_DTYPE_INT, 1, false);
48
        $this->initVar('created', XOBJ_DTYPE_INT, null, false);
49
        $this->initVar('last_faq', XOBJ_DTYPE_INT);
50
51
        //not persistent values
52
        $this->initVar('faqcount', XOBJ_DTYPE_INT, 0, false);
53
        $this->initVar('last_faqid', XOBJ_DTYPE_INT);
54
        $this->initVar('last_question_link', XOBJ_DTYPE_TXTBOX);
55
56
        if (null !== $id) {
0 ignored issues
show
introduced by
The condition null !== $id is always false.
Loading history...
57
            if (is_array($id)) {
58
                $this->assignVars($id);
59
            } else {
60
                /** @var Smartfaq\CategoryHandler $categoryHandler */
61
                $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
62
                $category        = $categoryHandler->get($id);
63
                foreach ($category->vars as $k => $v) {
64
                    $this->assignVar($k, $v['value']);
65
                }
66
                $this->assignOtherProperties();
67
            }
68
        }
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    public function notLoaded()
75
    {
76
        return (-1 == $this->getVar('categoryid'));
77
    }
78
79
    public function assignOtherProperties()
80
    {
81
        global $xoopsUser;
82
        $smartModule = Smartfaq\Utility::getModuleInfo();
83
        $module_id   = $smartModule->getVar('mid');
84
85
        $grouppermHandler = xoops_getHandler('groupperm');
86
87
        $this->groups_read = $grouppermHandler->getGroupIds('category_read', $this->categoryid(), $module_id);
0 ignored issues
show
Bug introduced by
The method getGroupIds() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler 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

87
        /** @scrutinizer ignore-call */ 
88
        $this->groups_read = $grouppermHandler->getGroupIds('category_read', $this->categoryid(), $module_id);
Loading history...
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function checkPermission()
94
    {
95
        //        require_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.php';
96
97
        $userIsAdmin = Smartfaq\Utility::userIsAdmin();
98
        if ($userIsAdmin) {
99
            return true;
100
        }
101
102
        /** @var Smartfaq\PermissionHandler $smartPermHandler */
103
        $smartPermHandler = Smartfaq\Helper::getInstance()->getHandler('Permission');
104
105
        $categoriesGranted = $smartPermHandler->getPermissions('category');
106
        if (in_array($this->categoryid(), $categoriesGranted)) {
107
            $ret = true;
108
        }
109
110
        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...
111
    }
112
113
    /**
114
     * @return mixed
115
     */
116
    public function categoryid()
117
    {
118
        return $this->getVar('categoryid');
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function parentid()
125
    {
126
        return $this->getVar('parentid');
127
    }
128
129
    /**
130
     * @param  string $format
131
     * @return mixed
132
     */
133
    public function name($format = 'S')
134
    {
135
        $ret = $this->getVar('name', $format);
136
        if (('s' === $format) || ('S' === $format) || ('show' === $format)) {
137
            $myts = \MyTextSanitizer::getInstance();
138
            $ret  = $myts->displayTarea($ret);
139
        }
140
141
        return $ret;
142
    }
143
144
    /**
145
     * @param  string $format
146
     * @return mixed
147
     */
148
    public function description($format = 'S')
149
    {
150
        return $this->getVar('description', $format);
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function weight()
157
    {
158
        return $this->getVar('weight');
159
    }
160
161
    /**
162
     * @param  bool $withAllLink
163
     * @param  bool $open
164
     * @return mixed|string
165
     */
166
    public function getCategoryPath($withAllLink = false, $open = false)
167
    {
168
        $filename = 'category.php';
169
        if (false !== $open) {
170
            $filename = 'open_category.php';
171
        }
172
        if ($withAllLink) {
173
            $ret = "<a href='" . XOOPS_URL . '/modules/smartfaq/' . $filename . '?categoryid=' . $this->categoryid() . "'>" . $this->name() . '</a>';
174
        } else {
175
            $ret = $this->name();
176
        }
177
        $parentid = $this->parentid();
178
        /** @var Smartfaq\CategoryHandler $categoryHandler */
179
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
180
        if (0 != $parentid) {
181
            $parentObj = $categoryHandler->get($parentid);
182
            if ($parentObj->notLoaded()) {
183
                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...
184
            }
185
            $parentid = $parentObj->parentid();
0 ignored issues
show
Unused Code introduced by
The assignment to $parentid is dead and can be removed.
Loading history...
186
            $ret      = $parentObj->getCategoryPath(true, $open) . ' > ' . $ret;
187
        }
188
189
        return $ret;
190
    }
191
192
    /**
193
     * @return array
194
     */
195
    public function getGroups_read()
196
    {
197
        //        if(count($this->groups_read) < 1) {
198
        if (!is_array($this->groups_read)) {
0 ignored issues
show
introduced by
The condition is_array($this->groups_read) is always true.
Loading history...
199
            $this->assignOtherProperties();
200
        }
201
202
        return $this->groups_read;
203
    }
204
205
    /**
206
     * @param array $groups_read
207
     */
208
    public function setGroups_read($groups_read = ['0'])
209
    {
210
        $this->groups_read = $groups_read;
211
    }
212
213
    /**
214
     * @param  bool $sendNotifications
215
     * @param  bool $force
216
     * @return bool
217
     */
218
    public function store($sendNotifications = true, $force = true)
219
    {
220
        //        $categoryHandler = new sfCategoryHandler($this->db);
221
        /** @var Smartfaq\CategoryHandler $categoryHandler */
222
        $categoryHandler = Smartfaq\Helper::getInstance()->getHandler('Category');
223
224
        $ret = $categoryHandler->insert($this, $force);
225
        if ($sendNotifications && $ret && $this->isNew()) {
226
            $this->sendNotifications();
227
        }
228
        $this->unsetNew();
229
230
        return $ret;
231
    }
232
233
    public function sendNotifications()
234
    {
235
        $smartModule = Smartfaq\Utility::getModuleInfo();
236
237
        $myts = \MyTextSanitizer::getInstance();
238
        /** @var \XoopsNotificationHandler $notificationHandler */
239
        $notificationHandler = xoops_getHandler('notification');
0 ignored issues
show
Unused Code introduced by
The assignment to $notificationHandler is dead and can be removed.
Loading history...
240
241
        $tags                  = [];
242
        $tags['MODULE_NAME']   = $myts->htmlSpecialChars($smartModule->getVar('name'));
243
        $tags['CATEGORY_NAME'] = $this->name();
244
        $tags['CATEGORY_URL']  = XOOPS_URL . '/modules/' . $smartModule->getVar('dirname') . '/category.php?categoryid=' . $this->categoryid();
245
246
        $notificationHandler = xoops_getHandler('notification');
247
        $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

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