SmartpartnerCategory::categoryid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 36 and the first side effect is on line 30.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// 
3
// ------------------------------------------------------------------------ //
4
//               XOOPS - PHP Content Management System                      //
5
//                   Copyright (c) 2000-2016 XOOPS.org                           //
6
//                      <http://xoops.org/>                             //
7
// ------------------------------------------------------------------------ //
8
// This program is free software; you can redistribute it and/or modify     //
9
// it under the terms of the GNU General Public License as published by     //
10
// the Free Software Foundation; either version 2 of the License, or        //
11
// (at your option) any later version.                                      //
12
13
// You may not change or alter any portion of this comment or credits       //
14
// of supporting developers from this source code or any supporting         //
15
// source code which is considered copyrighted (c) material of the          //
16
// original comment or credit authors.                                      //
17
// This program is distributed in the hope that it will be useful,          //
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
20
// GNU General Public License for more details.                             //
21
22
// You should have received a copy of the GNU General Public License        //
23
// along with this program; if not, write to the Free Software              //
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
25
// ------------------------------------------------------------------------ //
26
// URL: http://xoops.org/                                               //
27
// Project: XOOPS Project                                               //
28
// -------------------------------------------------------------------------//
29
if (!class_exists('smartpartner_PersistableObjectHandler')) {
30
    include_once XOOPS_ROOT_PATH . '/modules/smartpartner/class/object.php';
31
}
32
33
/**
34
 * Class SmartpartnerCategory
35
 */
36
class SmartpartnerCategory extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
37
{
38
    /**
39
     * SmartpartnerCategory constructor.
40
     */
41
    public function __construct()
42
    {
43
        $this->initVar('categoryid', XOBJ_DTYPE_INT, null, false);
44
        $this->initVar('parentid', XOBJ_DTYPE_INT, null, false);
45
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
46
        $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false, 255);
47
        $this->initVar('image', XOBJ_DTYPE_TXTBOX, null, false, 255);
48
        $this->initVar('total', XOBJ_DTYPE_INT, 1, false);
49
        $this->initVar('weight', XOBJ_DTYPE_INT, 1, false);
50
        $this->initVar('created', XOBJ_DTYPE_INT, null, false);
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56
    public function categoryid()
57
    {
58
        return $this->getVar('categoryid');
59
    }
60
61
    /**
62
     * @return mixed
63
     */
64
    public function parentid()
65
    {
66
        return $this->getVar('parentid');
67
    }
68
69
    /**
70
     * @param  string $format
71
     * @return mixed
72
     */
73 View Code Duplication
    public function name($format = 'S')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        $ret = $this->getVar('name', $format);
76
        if (($format === 's') || ($format === 'S') || ($format === 'show')) {
77
            $myts = MyTextSanitizer::getInstance();
78
            $ret  = $myts->displayTarea($ret);
79
        }
80
81
        return $ret;
82
    }
83
84
    /**
85
     * @param  string $format
86
     * @return mixed
87
     */
88
    public function description($format = 'S')
89
    {
90
        return $this->getVar('description', $format);
91
    }
92
93
    /**
94
     * @param  string $format
95
     * @return mixed|string
96
     */
97
    public function image($format = 'S')
98
    {
99
        if ($this->getVar('image') != '') {
100
            return $this->getVar('image', $format);
101
        } else {
102
            return 'blank.png';
103
        }
104
    }
105
106
    /**
107
     * @param  bool $falseIfNoImage
108
     * @return bool|mixed|string
109
     */
110
    public function getImageUrl($falseIfNoImage = false)
111
    {
112 View Code Duplication
        if (($this->getVar('image') !== '') && ($this->getVar('image') !== 'blank.png') && ($this->getVar('image') !== '-1')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
            return smartpartner_getImageDir('category', false) . $this->image();
114
        } elseif ($falseIfNoImage) {
115
            return false;
116
        } elseif (!$this->getVar('image_url')) {
117
            return smartpartner_getImageDir('category', false) . 'blank.png';
118
        } else {
119
            return $this->getVar('image_url');
120
        }
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function weight()
127
    {
128
        return $this->getVar('weight');
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function notLoaded()
135
    {
136
        return ($this->getVar('categoryid') == -1);
137
    }
138
139
    /**
140
     * @param  bool $withAllLink
141
     * @return mixed|string
142
     */
143
    public function getCategoryPath($withAllLink = true)
144
    {
145
        $filename = 'category.php';
0 ignored issues
show
Unused Code introduced by
$filename is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
        if ($withAllLink) {
147
            $ret = $this->getCategoryLink();
148
        } else {
149
            $ret = $this->name();
150
        }
151
        $parentid = $this->parentid();
152
        global $smartPartnerCategoryHandler;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
153
        if ($parentid != 0) {
154
            $parentObj = $smartPartnerCategoryHandler->get($parentid);
155
            if ($parentObj->notLoaded()) {
156
                exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method getCategoryPath() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
157
            }
158
            $parentid = $parentObj->parentid();
0 ignored issues
show
Unused Code introduced by
$parentid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
            $ret      = $parentObj->getCategoryPath($withAllLink) . ' > ' . $ret;
160
        }
161
162
        return $ret;
163
    }
164
165
    /**
166
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
167
     */
168
    public function getCategoryUrl()
169
    {
170
        return smartpartner_seo_genUrl('category', $this->categoryid(), $this->name());
171
    }
172
173
    /**
174
     * @param  bool $class
175
     * @return string
176
     */
177
    public function getCategoryLink($class = false)
178
    {
179
        if ($class) {
180
            return "<a class='$class' href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>';
181
        } else {
182
            return "<a href='" . $this->getCategoryUrl() . "'>" . $this->name() . '</a>';
183
        }
184
    }
185
186
    /**
187
     * @param  bool $sendNotifications
188
     * @param  bool $force
189
     * @return mixed
190
     */
191
    public function store($sendNotifications = true, $force = true)
192
    {
193
        global $smartPartnerCategoryHandler;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
194
        $ret = $smartPartnerCategoryHandler->insert($this, $force);
195
        if ($sendNotifications && $ret && $this->isNew()) {
196
            $this->sendNotifications();
197
        }
198
        $this->unsetNew();
199
200
        return $ret;
201
    }
202
203
    public function sendNotifications()
204
    {
205
        $hModule     = xoops_getHandler('module');
206
        $smartModule =& $hModule->getByDirname('smartpartner');
207
        $module_id   = $smartModule->getVar('mid');
0 ignored issues
show
Unused Code introduced by
$module_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
209
        $myts                = MyTextSanitizer::getInstance();
210
        $notificationHandler = xoops_getHandler('notification');
0 ignored issues
show
Unused Code introduced by
$notificationHandler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
211
212
        $tags                  = array();
213
        $tags['MODULE_NAME']   = $myts->displayTarea($smartModule->getVar('name'));
214
        $tags['CATEGORY_NAME'] = $this->name();
215
        $tags['CATEGORY_URL']  = $this->getCategoryUrl();
216
217
        $notificationHandler = xoops_getHandler('notification');
218
        $notificationHandler->triggerEvent('global_item', 0, 'category_created', $tags);
219
    }
220
221
    /**
222
     * @param  array $category
223
     * @return array
224
     */
225
    public function toArray($category = array())
226
    {
227
        $category['categoryid']   = $this->categoryid();
228
        $category['name']         = $this->name();
229
        $category['categorylink'] = $this->getCategoryLink();
230
        $category['total']        = $this->getVar('itemcount');
231
        $category['description']  = $this->description();
232
233
        if ($this->image() !== 'blank.png') {
234
            $category['image_path'] = smartpartner_getImageDir('category', false) . $this->image();
235
        } else {
236
            $category['image_path'] = '';
237
        }
238
239
        return $category;
240
    }
241
}
242
243
/**
244
 * Class SmartpartnerCategoryHandler
245
 */
246
class SmartpartnerCategoryHandler extends smartpartner_PersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
247
{
248
    /**
249
     * SmartpartnerCategoryHandler constructor.
250
     * @param object|XoopsDatabase $db
251
     */
252
    public function __construct(XoopsDatabase $db)
253
    {
254
        parent::__construct($db, 'smartpartner_categories', 'SmartpartnerCategory', 'categoryid', 'name');
255
    }
256
257
    /**
258
     * @param  XoopsObject $category
259
     * @param  bool        $force
260
     * @return bool
261
     */
262
    public function delete(XoopsObject $category, $force = false)
263
    {
264
        /*if (parent::delete($object, $force)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
265
            global $xoopsModule;
266
267
            // TODO: Delete partners in this category
268
            return true;
269
        }
270
271
        return false;*/
272
273
        if (strtolower(get_class($category)) !== 'smartpartnercategory') {
274
            return false;
275
        }
276
277
        // Deleting the partners
278
        global $smartPartnerPartnerHandler;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
279
        if (!isset($smartPartnerPartnerHandler)) {
280
            $smartPartnerPartnerHandler = smartpartner_gethandler('partner');
281
        }
282
        $criteria = new Criteria('category', $category->categoryid());
283
        $partners = $smartPartnerPartnerHandler->getObjects($criteria);
284
        if ($partners) {
285
            foreach ($partners as $partner) {
286
                $smartPartnerPartnerHandler->delete($partner);
287
            }
288
        }
289
290
        // Deleteing the sub categories
291
        $subcats = $this->getCategories(0, 0, $category->categoryid());
292
        foreach ($subcats as $subcat) {
293
            $this->delete($subcat);
294
        }
295
296
        $sql = sprintf('DELETE FROM %s WHERE categoryid = %u ', $this->db->prefix('smartpartner_categories'), $category->getVar('categoryid'));
297
298 View Code Duplication
        if (false != $force) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
            $result = $this->db->queryF($sql);
300
        } else {
301
            $result = $this->db->query($sql);
302
        }
303
304
        if (!$result) {
305
            return false;
306
        }
307
308
        return true;
309
    }
310
311
    /**
312
     * @param  int    $limit
313
     * @param  int    $start
314
     * @param  int    $parentid
315
     * @param  string $sort
316
     * @param  string $order
317
     * @param  bool   $id_as_key
318
     * @return array
319
     */
320
    public function getCategories($limit = 0, $start = 0, $parentid = 0, $sort = 'weight', $order = 'ASC', $id_as_key = true)
321
    {
322
        $criteria = new CriteriaCompo();
323
324
        $criteria->setSort($sort);
325
        $criteria->setOrder($order);
326
327
        if ($parentid != -1) {
328
            $criteria->add(new Criteria('parentid', $parentid));
329
        }
330
331
        $criteria->setStart($start);
332
        $criteria->setLimit($limit);
333
        $ret = $this->getObjects($criteria, $id_as_key);
334
335
        return $ret;
336
    }
337
338
    /**
339
     * @param  int $parentid
340
     * @return int
341
     */
342
    public function getCategoriesCount($parentid = 0)
343
    {
344
        if ($parentid == -1) {
345
            return $this->getCount();
346
        }
347
        $criteria = new CriteriaCompo();
348
        if (isset($parentid) && ($parentid != -1)) {
349
            $criteria->add(new criteria('parentid', $parentid));
350
        }
351
352
        return $this->getCount($criteria);
353
    }
354
}
355