| 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: https://xoops.org                         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | // Project: Article Project                                                 // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | // ------------------------------------------------------------------------ // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @package   module::article | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @copyright copyright © 2005 XoopsForge.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | // defined('XOOPS_ROOT_PATH') || die('Restricted access'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | require_once __DIR__ . '/../include/vars.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | //mod_loadFunctions('', $GLOBALS['moddirname']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * Xcategory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * @author    D.J. (phppp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * @copyright copyright © 2005 XoopsForge.com | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * @package   module::article | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  * {@link XoopsObject} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 45 |  | View Code Duplication | if (!class_exists('Bcategory')): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Class Bcategory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     class Bcategory extends XoopsObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |          * Constructor | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             //            $this->ArtObject(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $this->table = planet_DB_prefix('category'); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $this->initVar('cat_id', XOBJ_DTYPE_INT, null, false); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $this->initVar('cat_title', XOBJ_DTYPE_TXTBOX, '', true); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $this->initVar('cat_order', XOBJ_DTYPE_INT, 1, false); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | endif; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  * Category object handler class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  * @package   module::article | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  * @author    D.J. (phppp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  * @copyright copyright © 2005 XOOPS Project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  * {@link XoopsPersistableObjectHandler} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  * @param CLASS_PREFIX variable prefix for the class name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | PlanetUtility::planetParseClass(' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | class [CLASS_PREFIX]CategoryHandler extends XoopsPersistableObjectHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param object $db reference to the {@link XoopsDatabase} object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function __construct(\XoopsDatabase $db) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         parent::__construct($db, planet_DB_prefix("category", true), "Bcategory", "cat_id", "cat_title"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function delete(\XoopsObject $category, $force = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         xoops_notification_deletebyitem($GLOBALS["xoopsModule"]->getVar("mid"), "category", $category->getVar("cat_id")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         /* remove category-blog links */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $sql = "DELETE FROM ".planet_DB_prefix("blogcat")." WHERE cat_id = ".$category->getVar("cat_id"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (!$result = $this->db->queryF($sql)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         parent::delete($category, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * get a list of categories including a blog | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param  object $criteria {@link CriteriaElement} to match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param  bool   $asObject flag indicating as object, otherwise as array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return array  of categories {@link Bcategory} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |        public function &getByBlog($criteria = null, $asObject = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $sql = "SELECT bc.cat_id". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 " FROM " . planet_DB_prefix("blogcat")." AS bc"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $limit = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $start = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $sql .= " ".$criteria->renderWhere(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             if ($criteria->getSort() != "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $sql .= " ORDER BY ".$criteria->getSort()." ".$criteria->getOrder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $orderSet = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $limit = $criteria->getLimit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $start = $criteria->getStart(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if(empty($orderSet)) $sql .= " ORDER BY cat_id DESC"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $result = $this->db->query($sql, $limit, $start); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $ret = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |        while (false !== ($myrow = $this->db->fetchArray($result))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $ret[$myrow["cat_id"]] = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         if (!empty($asObject)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $crit = new \Criteria("cat_id", "(".implode(",",array_keys($ret)).")", "IN"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             $ret =& $this->getObjects($crit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * get a list of blogs to a category | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param  int   $category category ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param  array $blogs    array of blog IDs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |        public function addBlogs($category, $blogs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $_values = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         foreach ($blogs as $blog) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $sql = "SELECT COUNT(*)". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 " FROM ".planet_DB_prefix("blogcat"). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 " WHERE cat_id=".(int)($category)." AND blog_id=".(int)($blog); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             if (!$result = $this->db->query($sql)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             list($count) = $this->db->fetchRow($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             if($count>0) continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $_values[] = "(".(int)($blog).", ".(int)($category).")"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $values = implode(",",$_values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $sql = "INSERT INTO ".planet_DB_prefix("blogcat")." (blog_id, cat_id) VALUES ". $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if (!$result = $this->db->queryF($sql)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             PlanetUtility::planetDisplayMessage("Insert blog-cat error:" . $sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         return count($_values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * remove a list of blogs from a category | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @param  int   $category category ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @param  array $blogs    array of blog IDs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |        public function removeBlogs($category, $blogs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         if (count($blogs)>0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             $sql = "DELETE FROM ".planet_DB_prefix("blogcat")." WHERE cat_id=".(int)($category)." AND blog_id IN (".implode(",", $blogs).")"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             if (!$result = $this->db->queryF($sql)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |                 PlanetUtility::planetDisplayMessage("remove blog-cat error:" . $sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |           } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         return count($blogs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  | '); | 
            
                                                        
            
                                    
            
            
                | 189 |  |  |  | 
            
                        
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.