Completed
Push — master ( f47147...0adc58 )
by
unknown
02:15
created

XoopsfaqCategoryHandler   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 9.57 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 115
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getObj() 0 18 5
A displayAdminListing() 0 4 1
B renderAdminListing() 0 39 4
A displayError() 11 11 2

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
 You may not change or alter any portion of this comment or credits of
4
 supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit
6
 authors.
7
8
 This program is distributed in the hope that it will be useful, but
9
 WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
/**
13
 * XOOPS FAQ Category & Category Handler Class Definitions
14
 *
15
 * @package   module\xoopsfaq\class
16
 * @author    John Neill
17
 * @author    XOOPS Module Development Team
18
 * @copyright Copyright (c) 2001-2017 {@link http://xoops.org XOOPS Project}
19
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU Public License
20
 * @since::   1.23
21
 *
22
 */
23
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
24
25
/**
26
 * XoopsfaqCategory
27
 *
28
 * Class used to handle all of the CRUD actions for FAQ categories
29
 *
30
 * @author::    John Neill
31
 * @copyright:: Copyright (c) 2009
32
 */
33
class XoopsfaqCategory extends XoopsObject
34
{
35
    /**
36
     * Constructor
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->initVar('category_id', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('category_title', XOBJ_DTYPE_TXTBOX, null, true, 255);
43
        $this->initVar('category_order', XOBJ_DTYPE_INT, 0, false);
44
    }
45
46
    /**
47
     * Display the category title
48
     *
49
     * @return string display the category title (filtered for display)
50
     */
51
    public function __toString()
52
    {
53
        return $this->getVar('category_title', 's');
54
    }
55
56
    /**
57
     * Display the category edit form
58
     *
59
     * @return void
60
     */
61
    public function displayForm()
62
    {
63
        echo $this->renderForm();
64
    }
65
66
    /**
67
     * Render the category edit form
68
     *
69
     * @return string HTML entities used to edit the catagory object
70
     */
71
    public function renderForm()
72
    {
73
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
74
        $permHelper = new Xmf\Module\Helper\Permission();
75
        xoops_load('constants', basename(dirname(__DIR__)));
76
77
        $caption = ($this->isNew()) ? _AM_XOOPSFAQ_CREATE_NEW : sprintf(_AM_XOOPSFAQ_MODIFY_ITEM, $this->getVar('category_title'));
78
79
        $form = new XoopsThemeForm($caption, 'content', xoops_getenv('PHP_SELF'), 'post', true);
80
        $form->addElement(new xoopsFormHidden('op', 'save'));
81
        $form->addElement(new xoopsFormHidden('category_id', $this->getVar('category_id')));
82
        // title
83
        $category_title = new XoopsFormText(_AM_XOOPSFAQ_E_CATEGORY_TITLE, 'category_title', 50, 150, $this->getVar('category_title', 'e'));
84
        $category_title->setDescription(_AM_XOOPSFAQ_E_CATEGORY_TITLE_DESC);
85
        $form->addElement($category_title, true);
86
        // order
87
        $category_order = new XoopsFormText(_AM_XOOPSFAQ_E_CATEGORY_ORDER, 'category_order', 5, 5, $this->getVar('category_order', 'e'));
88
        $category_order->setDescription(_AM_XOOPSFAQ_E_CATEGORY_ORDER_DESC);
89
        $form->addElement($category_order, false);
90
        $form->addElement($permHelper->getGroupSelectFormForItem('viewcat', $this->getVar('category_id'), _AM_XOOPSFAQ_CATEGORY_GROUP_PERMS, '', XoopsfaqConstants::INCLUDE_ANNON));
91
        $form->addElement(new XoopsFormButtonTray('category_form', _SUBMIT, 'submit'));
92
93
        return $form->render();
94
    }
95
}
96
97
/**
98
 * XoopsfaqCategoryHandler
99
 *
100
 * @package::   xoopsfaq
101
 * @author::    John Neill
102
 * @copyright:: Copyright (c) 2009
103
 */
104
class XoopsfaqCategoryHandler extends XoopsPersistableObjectHandler
105
{
106
    /**
107
     * Constructor
108
     *
109
     * @param mixed $db
110
     */
111
    public function __construct($db)
112
    {
113
        parent::__construct($db, 'xoopsfaq_categories', 'XoopsfaqCategory', 'category_id', 'category_title');
114
    }
115
116
    /**
117
     * XoopsfaqCategoryHandler::getObj()
118
     *
119
     * @param string $sort order ('id', order', or 'title') - default: id
120
     *
121
     * @return mixed XoopsfaqCategory | false on failure
122
     */
123
    public function getObj($sort = 'id')
124
    {
125
        $obj = false;
126
        if ((null !== $sort) && (!$sort instanceof CriteriaElement)) {
0 ignored issues
show
Bug introduced by
The class CriteriaElement does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
127
            $criteria = new CriteriaCompo();
128
            $obj['count'] = $this->getCount($criteria);
129
            $criteria->order = 'ASC';
130
            $sort = in_array(mb_strtolower($sort), array('id', 'order', 'title')) ? 'category_' . mb_strtolower($sort) : 'category_id';
131
            $criteria->setSort($sort);
132
            $criteria->setStart(0);
133
            $criteria->setLimit(0);
134
        } else {
135
            $criteria = $sort;
136
        }
137
        $obj['list'] = $this->getObjects($criteria, false);
138
        $obj['count'] = (false !== $obj['list']) ? count($obj['list']) : 0;
139
        return $obj;
140
    }
141
142
    /**
143
     * XoopsfaqCategoryHandler::displayAdminListing()
144
     *
145
     * @param string $sort
146
     * @return void
147
     */
148
    public function displayAdminListing($sort = 'id')
149
    {
150
        echo $this->renderAdminListing($sort);
151
    }
152
153
    /**
154
     * Display a Category listing for administrators
155
     *
156
     * @param string $sort listing order
157
     *
158
     * @return string HTML listing for Admin
159
     */
160
    public function renderAdminListing($sort = 'id')
161
    {
162
        if (!class_exists('XoopsfaqUtility')) {
163
            xoops_load('utility', basename(dirname(__DIR__)));
164
        }
165
166
        $objects = $this->getObj($sort);
167
168
        $buttons = array('edit', 'delete');
169
170
        $ret = '<table class="outer width100 bnone pad3 marg5">'
171
             . '  <thead>'
172
             . '  <tr class="xoopsCenter">'
173
             . '    <th class="width5">' . _AM_XOOPSFAQ_CATEGORY_ORDER . '</th>'
174
             . '    <th class="width5">' . _AM_XOOPSFAQ_CATEGORY_ID . '</th>'
175
             . '    <th class="txtleft">' . _AM_XOOPSFAQ_CATEGORY_TITLE . '</th>'
176
             . '    <th class="width20">' . _AM_XOOPSFAQ_ACTIONS . '</th>'
177
             . '  </tr>'
178
             . '  </thead>'
179
             . '  <tbody>';
180
        if ($objects['count'] > 0) {
181
            /** @var XoopsObject $object */
182
            foreach ($objects['list'] as $object) {
183
                $ret .= '  <tr class="xoopsCenter">'
184
                      . '    <td class="even txtcenter">' . $object->getVar('category_order') . '</td>'
185
                      . '    <td class="even txtcenter">' . $object->getVar('category_id') . '</td>'
186
                      . '    <td class="even txtleft">' . $object->getVar('category_title') . '</td>'
187
                      . '    <td class="even txtcenter">';
188
                $ret .= XoopsfaqUtility::renderIconLinks($buttons, 'category_id', $object->getVar('category_id'));
189
                $ret .= '    </td>'
190
                      . '  </tr>';
191
            }
192
        } else {
193
            $ret .= '  <tr class="txtcenter"><td colspan="4" class="even">' . _AM_XOOPSFAQ_NOLISTING . '</td></tr>';
194
        }
195
        $ret .= '  </tbody>'
196
              . '</table>';
197
        return $ret;
198
    }
199
200
    /**
201
     * Display the class error(s) encountered
202
     *
203
     * @param array|string $errors the error(s) to be displayed
204
     *
205
     * @return void
206
     */
207 View Code Duplication
    public function displayError($errors = '')
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...
208
    {
209
        if ('' !== $errors) {
210
            xoops_cp_header();
211
            $moduleAdmin = Xmf\Module\Admin::getInstance();
212
            $moduleAdmin->displayNavigation('index.php');
213
            xoops_error($errors, _AM_XOOPSFAQ_ERROR_SUB);
214
            xoops_cp_footer();
215
        }
216
        return;
217
    }
218
}
219