Passed
Branch master (294c87)
by Michael
02:12
created

Category::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
/**
16
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
17
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @package         Publisher
19
 * @since           1.0
20
 * @author          trabis <[email protected]>
21
 * @author          The SmartFactory <www.smartfactory.ca>
22
 */
23
24
use XoopsModules\Publisher;
25
26
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
27
28
require_once dirname(__DIR__) . '/include/common.php';
29
30
/**
31
 * Class Publisher\Category
32
 */
33
class Category extends \XoopsObject
34
{
35
    /**
36
     * @var Publisher\Helper
37
     */
38
    public $helper;
39
40
    /**
41
     * @var array
42
     */
43
    public $categoryPath = false;
44
    public $categoryid;
45
    public $parentid;
46
    public $name;
47
    public $description;
48
    public $image;
49
    public $total;
50
    public $weight;
51
    public $created;
52
    public $template;
53
    public $header;
54
    public $meta_keywords;
55
    public $meta_description;
56
    public $short_url;
57
    public $moderator;
58
59
    /**
60
     * constructor
61
     */
62
    public function __construct()
63
    {
64
        /** @var \XoopsModules\Publisher\Helper $this->helper */
65
        $this->helper = \XoopsModules\Publisher\Helper::getInstance();
66
        $this->initVar('categoryid', XOBJ_DTYPE_INT, null, false);
67
        $this->initVar('parentid', XOBJ_DTYPE_INT, null, false);
68
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
69
        $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255);
70
        $this->initVar('image', XOBJ_DTYPE_TXTBOX, null, false, 255);
71
        $this->initVar('total', XOBJ_DTYPE_INT, 1, false);
72
        $this->initVar('weight', XOBJ_DTYPE_INT, 1, false);
73
        $this->initVar('created', XOBJ_DTYPE_INT, null, false);
74
        $this->initVar('template', XOBJ_DTYPE_TXTBOX, null, false, 255);
75
        $this->initVar('header', XOBJ_DTYPE_TXTAREA, null, false);
76
        $this->initVar('meta_keywords', XOBJ_DTYPE_TXTAREA, null, false);
77
        $this->initVar('meta_description', XOBJ_DTYPE_TXTAREA, null, false);
78
        $this->initVar('short_url', XOBJ_DTYPE_TXTBOX, null, false, 255);
79
        $this->initVar('moderator', XOBJ_DTYPE_INT, null, false, 0);
80
        //not persistent values
81
        $this->initVar('itemcount', XOBJ_DTYPE_INT, 0, false);
82
        $this->initVar('last_itemid', XOBJ_DTYPE_INT);
83
        $this->initVar('last_title_link', XOBJ_DTYPE_TXTBOX);
84
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
85
    }
86
87
    /**
88
     * @param string $method
89
     * @param array  $args
90
     *
91
     * @return mixed
92
     */
93
    public function __call($method, $args)
94
    {
95
        $arg = isset($args[0]) ? $args[0] : null;
96
97
        return $this->getVar($method, $arg);
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function notLoaded()
104
    {
105
        return (-1 == $this->getVar('categoryid'));
106
    }
107
108
    /**
109
     * @return bool
110
     */
111
    public function checkPermission()
112
    {
113
        //        global $publisherIsAdmin;
114
        $ret = false;
115
        if ($GLOBALS['publisherIsAdmin']) {
116
            return true;
117
        }
118
        if (is_object($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser']->getVar('uid') == $this->moderator) {
119
            return true;
120
        }
121
        /** @var \XoopsModules\Publisher\PermissionHandler $permissionHandler */
122
        $permissionHandler = $this->helper->getHandler('Permission');
123
        $categoriesGranted = $permissionHandler->getGrantedItems('category_read');
124
        if (in_array($this->categoryid(), $categoriesGranted, true)) {
0 ignored issues
show
Bug introduced by
The method categoryid() does not exist on XoopsModules\Publisher\Category. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

124
        if (in_array($this->/** @scrutinizer ignore-call */ categoryid(), $categoriesGranted, true)) {
Loading history...
125
            $ret = true;
126
        }
127
128
        return $ret;
129
    }
130
131
    /**
132
     * @param string $format
133
     *
134
     * @return mixed|string
135
     */
136
    public function getImage($format = 's')
137
    {
138
        if ('' != $this->getVar('image')) {
139
            return $this->getVar('image', $format);
140
        }
141
142
        return 'blank.png';
143
    }
144
145
    /**
146
     * @param string $format
147
     *
148
     * @return mixed
149
     */
150
    public function getTemplate($format = 'n')
151
    {
152
        return $this->getVar('template', $format);
153
    }
154
155
    /**
156
     * @param bool $withAllLink
157
     *
158
     * @return array|bool|string
159
     */
160
    public function getCategoryPath($withAllLink = true)
161
    {
162
        if (empty($this->categoryPath)) {
163
            if ($withAllLink) {
164
                $ret = $this->getCategoryLink();
165
            } else {
166
                $ret = $this->name();
0 ignored issues
show
Bug introduced by
The method name() does not exist on XoopsModules\Publisher\Category. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

166
                /** @scrutinizer ignore-call */ 
167
                $ret = $this->name();
Loading history...
167
            }
168
            $parentid = $this->parentid();
0 ignored issues
show
Bug introduced by
The method parentid() does not exist on XoopsModules\Publisher\Category. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

168
            /** @scrutinizer ignore-call */ 
169
            $parentid = $this->parentid();
Loading history...
169
            if (0 != $parentid) {
170
                /** @var Publisher\CategoryHandler $categoryHandler */
171
                $categoryHandler = $this->helper->getHandler('Category');
172
                $parentObj       = $categoryHandler->get($parentid);
173
                //                if ($parentObj->notLoaded()) {
174
                //                    exit;
175
                //                }
176
177
                try {
178
                    if ($parentObj->notLoaded()) {
0 ignored issues
show
Bug introduced by
The method notLoaded() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Publisher\Item or XoopsModules\Publisher\File or XoopsModules\Publisher\Category. ( Ignorable by Annotation )

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

178
                    if ($parentObj->/** @scrutinizer ignore-call */ notLoaded()) {
Loading history...
179
                        throw new \RuntimeException(_NOPERM);
180
                    }
181
                }
182
                catch (\Exception $e) {
183
                    $this->helper->addLog($e);
184
                    //                    redirect_header('javascript:history.go(-1)', 1, _NOPERM);
185
                }
186
187
                $ret = $parentObj->getCategoryPath($withAllLink) . ' <li> ' . $ret . '</li>';
0 ignored issues
show
Bug introduced by
The method getCategoryPath() does not exist on XoopsObject. It seems like you code against a sub-type of XoopsObject such as XoopsModules\Publisher\Item or XoopsModules\Publisher\File or XoopsModules\Publisher\Category. ( Ignorable by Annotation )

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

187
                $ret = $parentObj->/** @scrutinizer ignore-call */ getCategoryPath($withAllLink) . ' <li> ' . $ret . '</li>';
Loading history...
188
            }
189
            $this->categoryPath = $ret;
190
        }
191
192
        return $this->categoryPath;
193
    }
194
195
    /**
196
     * @return mixed|string
197
     */
198
    public function getCategoryPathForMetaTitle()
199
    {
200
        $ret      = '';
201
        $parentid = $this->parentid();
202
        if (0 != $parentid) {
203
            /** @var Publisher\CategoryHandler $categoryHandler */
204
            $categoryHandler = $this->helper->getHandler('Category');
205
            $parentObj       = $categoryHandler->get($parentid);
206
            //            if ($parentObj->notLoaded()) {
207
            //                exit('NOT LOADED');
208
            //            }
209
210
            try {
211
                if ($parentObj->notLoaded()) {
212
                    throw new \RuntimeException('NOT LOADED');
213
                }
214
            }
215
            catch (\Exception $e) {
216
                $this->helper->addLog($e);
217
                //                    redirect_header('javascript:history.go(-1)', 1, _NOPERM);
218
            }
219
220
            $ret = $parentObj->getCategoryPath(false);
221
            $ret = str_replace(' >', ' -', $ret);
222
        }
223
224
        return $ret;
225
    }
226
227
    /**
228
     * @return array|null
229
     */
230
    public function getGroupsRead()
231
    {
232
        /** @var Publisher\PermissionHandler $permissionHandler */
233
        $permissionHandler = $this->helper->getHandler('Permission');
234
235
        return $permissionHandler->getGrantedGroupsById('category_read', $this->categoryid());
236
    }
237
238
    /**
239
     * @return array|null
240
     */
241
    public function getGroupsSubmit()
242
    {
243
        /** @var Publisher\PermissionHandler $permissionHandler */
244
        $permissionHandler = $this->helper->getHandler('Permission');
245
246
        return $permissionHandler->getGrantedGroupsById('item_submit', $this->categoryid());
247
    }
248
249
    /**
250
     * @return array|null
251
     */
252
    public function getGroupsModeration()
253
    {
254
        /** @var Publisher\PermissionHandler $permissionHandler */
255
        $permissionHandler = $this->helper->getHandler('Permission');
256
257
        return $permissionHandler->getGrantedGroupsById('category_moderation', $this->categoryid());
258
    }
259
260
    /**
261
     * @return string
262
     */
263
    public function getCategoryUrl()
264
    {
265
        return Publisher\Seo::generateUrl('category', $this->categoryid(), $this->short_url());
0 ignored issues
show
Bug introduced by
It seems like $this->short_url() can also be of type array and array; however, parameter $shortUrl of XoopsModules\Publisher\Seo::generateUrl() 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

265
        return Publisher\Seo::generateUrl('category', $this->categoryid(), /** @scrutinizer ignore-type */ $this->short_url());
Loading history...
Bug introduced by
The method short_url() does not exist on XoopsModules\Publisher\Category. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

265
        return Publisher\Seo::generateUrl('category', $this->categoryid(), $this->/** @scrutinizer ignore-call */ short_url());
Loading history...
266
    }
267
268
    /**
269
     * @param bool $class
270
     *
271
     * @return string
272
     */
273
    public function getCategoryLink($class = false)
274
    {
275
        if ($class) {
276
            return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>';
277
        }
278
279
        return "<a href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>';
280
    }
281
282
    /**
283
     * @param bool $sendNotifications
284
     * @param bool $force
285
     *
286
     * @return mixed
287
     */
288
    public function store($sendNotifications = true, $force = true)
289
    {
290
        $ret = $this->helper->getHandler('Category')->insert($this, $force);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObjectHandler::insert() has too many arguments starting with $force. ( Ignorable by Annotation )

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

290
        $ret = $this->helper->getHandler('Category')->/** @scrutinizer ignore-call */ insert($this, $force);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
291
        if ($sendNotifications && $ret && $this->isNew()) {
292
            $this->sendNotifications();
293
        }
294
        $this->unsetNew();
295
296
        return $ret;
297
    }
298
299
    /**
300
     * Send notifications
301
     */
302
    public function sendNotifications()
303
    {
304
        $tags                  = [];
305
        $tags['MODULE_NAME']   = $this->helper->getModule()->getVar('name');
306
        $tags['CATEGORY_NAME'] = $this->name();
307
        $tags['CATEGORY_URL']  = $this->getCategoryUrl();
308
        /* @var  \XoopsNotificationHandler $notificationHandler */
309
        $notificationHandler = xoops_getHandler('notification');
310
        $notificationHandler->triggerEvent('global_item', 0, 'category_created', $tags);
311
    }
312
313
    /**
314
     * @param array $category
315
     *
316
     * @return array
317
     */
318
    public function toArraySimple($category = [])
319
    {
320
        $category['categoryid']       = $this->categoryid();
321
        $category['name']             = $this->name();
322
        $category['categorylink']     = $this->getCategoryLink();
323
        $category['categoryurl']      = $this->getCategoryUrl();
324
        $category['total']            = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : '';
325
        $category['description']      = $this->description();
326
        $category['header']           = $this->header();
327
        $category['meta_keywords']    = $this->meta_keywords();
328
        $category['meta_description'] = $this->meta_description();
329
        $category['short_url']        = $this->short_url();
330
        if ($this->getVar('last_itemid') > 0) {
331
            $category['last_itemid']     = $this->getVar('last_itemid', 'n');
332
            $category['last_title_link'] = $this->getVar('last_title_link', 'n');
333
        }
334
        if ('blank.png' !== $this->getImage()) {
335
            $category['image_path'] = Publisher\Utility::getImageDir('category', false) . $this->getImage();
336
        } else {
337
            $category['image_path'] = '';
338
        }
339
        $category['lang_subcategories'] = sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->name());
340
341
        return $category;
342
    }
343
344
    /**
345
     * @param array $category
346
     *
347
     * @return array
348
     */
349
    public function toArrayTable($category = [])
350
    {
351
        $category['categoryid']   = $this->categoryid();
352
        $category['categorylink'] = $this->getCategoryLink();
353
        $category['total']        = ($this->getVar('itemcount') > 0) ? $this->getVar('itemcount') : '';
354
        $category['description']  = $this->description();
355
        if ($this->getVar('last_itemid') > 0) {
356
            $category['last_itemid']     = $this->getVar('last_itemid', 'n');
357
            $category['last_title_link'] = $this->getVar('last_title_link', 'n');
358
        }
359
        if ('blank.png' !== $this->getImage()) {
360
            $category['image_path'] = Publisher\Utility::getImageDir('category', false) . $this->getImage();
361
        } else {
362
            $category['image_path'] = '';
363
        }
364
        $category['lang_subcategories'] = sprintf(_CO_PUBLISHER_SUBCATEGORIES_INFO, $this->name());
365
366
        return $category;
367
    }
368
369
    public function createMetaTags()
370
    {
371
        $publisherMetagen = new Publisher\Metagen($this->name(), $this->meta_keywords(), $this->meta_description());
0 ignored issues
show
Bug introduced by
It seems like $this->meta_description() can also be of type array and array; 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

371
        $publisherMetagen = new Publisher\Metagen($this->name(), $this->meta_keywords(), /** @scrutinizer ignore-type */ $this->meta_description());
Loading history...
Bug introduced by
The method meta_keywords() does not exist on XoopsModules\Publisher\Category. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

371
        $publisherMetagen = new Publisher\Metagen($this->name(), $this->/** @scrutinizer ignore-call */ meta_keywords(), $this->meta_description());
Loading history...
Bug introduced by
The method meta_description() does not exist on XoopsModules\Publisher\Category. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

371
        $publisherMetagen = new Publisher\Metagen($this->name(), $this->meta_keywords(), $this->/** @scrutinizer ignore-call */ meta_description());
Loading history...
Bug introduced by
It seems like $this->name() can also be of type array and array; 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

371
        $publisherMetagen = new Publisher\Metagen(/** @scrutinizer ignore-type */ $this->name(), $this->meta_keywords(), $this->meta_description());
Loading history...
Bug introduced by
It seems like $this->meta_keywords() can also be of type array and array; 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

371
        $publisherMetagen = new Publisher\Metagen($this->name(), /** @scrutinizer ignore-type */ $this->meta_keywords(), $this->meta_description());
Loading history...
372
        $publisherMetagen->createMetaTags();
373
    }
374
375
    /**
376
     * @param int $subCatsCount
377
     *
378
     * @return \XoopsModules\Publisher\Form\CategoryForm
379
     */
380
    public function getForm($subCatsCount = 4)
381
    {
382
        //        require_once $GLOBALS['xoops']->path('modules/' . PUBLISHER_DIRNAME . '/class/form/category.php');
383
        $form = new Publisher\Form\CategoryForm($this, $subCatsCount);
384
385
        return $form;
386
    }
387
}
388