Completed
Push — master ( 15a86c...ff0242 )
by Michael
03:23
created

Bcategory   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 13
loc 13
rs 10
wmc 1
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
//
3
// ------------------------------------------------------------------------ //
4
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
// URL: http://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
/**
28
 * @package   module::article
29
 * @copyright copyright &copy; 2005 XoopsForge.com
30
 */
31
32
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
33
include_once dirname(__DIR__) . '/include/vars.php';
34
mod_loadFunctions('', $GLOBALS['moddirname']);
35
36
/**
37
 * Xcategory
38
 *
39
 * @author    D.J. (phppp)
40
 * @copyright copyright &copy; 2005 XoopsForge.com
41
 * @package   module::article
42
 *
43
 * {@link XoopsObject}
44
 **/
45 View Code Duplication
if (!class_exists('Bcategory')):
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...
46
47
    /**
48
     * Class Bcategory
49
     */
50
    class Bcategory extends XoopsObject
51
    {
52
        /**
53
         * Constructor
54
         */
55
        public function __construct() {
56
            //            $this->ArtObject();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
57
            $this->table = planet_DB_prefix('category');
58
            $this->initVar('cat_id', XOBJ_DTYPE_INT, null, false);
59
            $this->initVar('cat_title', XOBJ_DTYPE_TXTBOX, '', true);
60
            $this->initVar('cat_order', XOBJ_DTYPE_INT, 1, false);
61
        }
62
    }
63
64
endif;
65
/**
66
 * Category object handler class.
67
 * @package   module::article
68
 *
69
 * @author    D.J. (phppp)
70
 * @copyright copyright &copy; 2005 XOOPS Project
71
 *
72
 * {@link XoopsPersistableObjectHandler}
73
 *
74
 * @param CLASS_PREFIX variable prefix for the class name
75
 */
76
77
planet_parse_class('
78
class [CLASS_PREFIX]CategoryHandler extends XoopsPersistableObjectHandler
79
{
80
    /**
81
     * Constructor
82
     *
83
     * @param object $db reference to the {@link XoopsDatabase} object
84
     **/
85
    public function __construct(XoopsDatabase $db) {
86
        parent::__construct($db, planet_DB_prefix("category", true), "Bcategory", "cat_id", "cat_title");
87
    }
88
89
    public function delete(XoopsObject $category, $force = false)
90
    {
91
        xoops_notification_deletebyitem($GLOBALS["xoopsModule"]->getVar("mid"), "category", $category->getVar("cat_id"));
92
93
        /* remove category-blog links */
94
        $sql = "DELETE FROM ".planet_DB_prefix("blogcat")." WHERE cat_id = ".$category->getVar("cat_id");
95
        if (!$result = $this->db->queryF($sql)) {
96
        }
97
98
        parent::delete($category, true);
99
    }
100
101
    /**
102
     * get a list of categories including a blog
103
     *
104
     * @param  object $criteria {@link CriteriaElement} to match
105
     * @param  bool   $asObject flag indicating as object, otherwise as array
106
     * @return array  of categories {@link Bcategory}
107
     */
108
       public function &getByBlog($criteria = null, $asObject = false)
109
    {
110
        $sql = "SELECT bc.cat_id".
111
                " FROM " . planet_DB_prefix("blogcat")." AS bc";
112
        $limit = null;
113
        $start = null;
114
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
115
            $sql .= " ".$criteria->renderWhere();
116
            if ($criteria->getSort() != "") {
117
                $sql .= " ORDER BY ".$criteria->getSort()." ".$criteria->getOrder();
118
                $orderSet = true;
119
            }
120
            $limit = $criteria->getLimit();
121
            $start = $criteria->getStart();
122
        }
123
        if(empty($orderSet)) $sql .= " ORDER BY cat_id DESC";
124
        $result = $this->db->query($sql, $limit, $start);
125
        $ret = array();
126
        while ($myrow = $this->db->fetchArray($result)) {
127
            $ret[$myrow["cat_id"]] = 1;
128
        }
129
        if (!empty($asObject)) {
130
            $crit = new Criteria("cat_id", "(".implode(",",array_keys($ret)).")", "IN");
131
            $ret = $this->getObjects($crit);
132
        }
133
134
        return $ret;
135
    }
136
137
    /**
138
     * get a list of blogs to a category
139
     *
140
     * @param  int   $category category ID
141
     * @param  array $blogs    array of blog IDs
142
     * @return bool
143
     */
144
       public function addBlogs($category, $blogs)
145
    {
146
        $_values = array();
147
        foreach ($blogs as $blog) {
148
            $sql = "SELECT COUNT(*)".
149
                " FROM ".planet_DB_prefix("blogcat").
150
                " WHERE cat_id=".(int)($category)." AND blog_id=".(int)($blog);
151
            if (!$result = $this->db->query($sql)) {
152
                continue;
153
            }
154
            list($count) = $this->db->fetchRow($result);
155
            if($count>0) continue;
156
            $_values[] = "(".(int)($blog).", ".(int)($category).")";
157
        }
158
        $values = implode(",",$_values);
159
        $sql = "INSERT INTO ".planet_DB_prefix("blogcat")." (blog_id, cat_id) VALUES ". $values;
160
        if (!$result = $this->db->queryF($sql)) {
161
            planet_message("Insert blog-cat error:" . $sql);
162
163
            return false;
164
        }
165
166
        return count($_values);
167
    }
168
    /**
169
     * remove a list of blogs from a category
170
     *
171
     * @param  int   $category category ID
172
     * @param  array $blogs    array of blog IDs
173
     * @return bool
174
     */
175
       public function removeBlogs($category, $blogs)
176
    {
177
        if (count($blogs)>0) {
178
            $sql = "DELETE FROM ".planet_DB_prefix("blogcat")." WHERE cat_id=".(int)($category)." AND blog_id IN (".implode(",", $blogs).")";
179
            if (!$result = $this->db->queryF($sql)) {
180
                planet_message("remove blog-cat error:" . $sql);
181
            }
182
          }
183
184
        return count($blogs);
185
    }
186
}
187
');
188