Completed
Push — master ( 01b1a5...81f493 )
by Michael
04:03
created

SmartobjectCustomtag::generateTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 69
Code Lines 59

Duplication

Lines 69
Ratio 100 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 1
eloc 59
c 5
b 1
f 0
nc 1
nop 0
dl 69
loc 69
rs 9.2083

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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
30
// 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...
31
32
include_once XOOPS_ROOT_PATH . '/modules/smartobject/class/smartobject.php';
33
34
/**
35
 * Class SmartobjectCustomtag
36
 */
37
class SmartobjectCustomtag extends SmartObject
38
{
39
    public $content = false;
40
41
    /**
42
     * SmartobjectCustomtag constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->quickInitVar('customtagid', XOBJ_DTYPE_INT, true);
47
        $this->quickInitVar('name', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_CUSTOMTAG_NAME, _CO_SOBJECT_CUSTOMTAG_NAME_DSC);
48
        $this->quickInitVar('description', XOBJ_DTYPE_TXTAREA, false, _CO_SOBJECT_CUSTOMTAG_DESCRIPTION, _CO_SOBJECT_CUSTOMTAG_DESCRIPTION_DSC);
49
        $this->quickInitVar('content', XOBJ_DTYPE_TXTAREA, true, _CO_SOBJECT_CUSTOMTAG_CONTENT, _CO_SOBJECT_CUSTOMTAG_CONTENT_DSC);
50
        $this->quickInitVar('language', XOBJ_DTYPE_TXTBOX, true, _CO_SOBJECT_CUSTOMTAG_LANGUAGE, _CO_SOBJECT_CUSTOMTAG_LANGUAGE_DSC);
51
52
        $this->initNonPersistableVar('dohtml', XOBJ_DTYPE_INT, 'class', 'dohtml', '', true);
53
        $this->initNonPersistableVar('doimage', XOBJ_DTYPE_INT, 'class', 'doimage', '', true);
54
        $this->initNonPersistableVar('doxcode', XOBJ_DTYPE_INT, 'class', 'doxcode', '', true);
55
        $this->initNonPersistableVar('dosmiley', XOBJ_DTYPE_INT, 'class', 'dosmiley', '', true);
56
57
        $this->setControl('content', array(
58
            'name'        => 'textarea',
59
            'form_editor' => 'textarea',
60
            'form_rows'   => 25
61
        ));
62
        $this->setControl('language', array(
63
            'name' => 'language',
64
            'all'  => true
65
        ));
66
    }
67
68
    /**
69
     * @param  string $key
70
     * @param  string $format
71
     * @return mixed
72
     */
73
    public function getVar($key, $format = 's')
74
    {
75
        if ($format === 's' && in_array($key, array())) {
76
            //            return call_user_func(array($this, $key));
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...
77
            return $this->{$key}();
78
        }
79
80
        return parent::getVar($key, $format);
81
    }
82
83
    /**
84
     * @return bool|mixed
85
     */
86
    public function render()
87
    {
88
        if (!$this->content) {
89
            $ret           = $this->getVar('content');
90
            $this->content = $ret;
91
        }
92
93
        return $this->content;
94
    }
95
96
    /**
97
     * @return bool|mixed|string
98
     */
99
    public function renderWithPhp()
100
    {
101
        if (!$this->content) {
102
            $ret           = $this->getVar('content');
103
            $this->content = $ret;
104
        } else {
105
            $ret = $this->content;
106
        }
107
108
        // check for PHP if we are not on admin side
109
        if (!defined('XOOPS_CPFUNC_LOADED') && !(strpos($ret, '[php]') === false)) {
110
            $ret = str_replace('[php]', '', $ret);
111
            // we have PHP code, let's evaluate
112
            eval($ret);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
113
114
            return '';
115
        }
116
117
        return $this->content;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public function getXoopsCode()
124
    {
125
        $ret = '[customtag]' . $this->getVar('tag', 'n') . '[/customtag]';
126
127
        return $ret;
128
    }
129
130
    /**
131
     * @return string
132
     */
133
    public function getCloneLink()
134
    {
135
        $ret = '<a href="' .
136
               SMARTOBJECT_URL .
137
               'admin/customtag.php?op=clone&customtagid=' .
138
               $this->id() .
139
               '"><img src="' .
140
               SMARTOBJECT_IMAGES_ACTIONS_URL .
141
               'editcopy.png" style="vertical-align: middle;" alt="' .
142
               _CO_SOBJECT_CUSTOMTAG_CLONE .
143
               '" title="' .
144
               _CO_SOBJECT_CUSTOMTAG_CLONE .
145
               '" /></a>';
146
147
        return $ret;
148
    }
149
150
    /**
151
     * @param $var
152
     * @return bool
153
     */
154
    public function emptyString($var)
155
    {
156
        return (strlen($var) > 0);
157
    }
158
159
    /**
160
     * @return mixed|string
161
     */
162 View Code Duplication
    public function generateTag()
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...
163
    {
164
        $title = rawurlencode(strtolower($this->getVar('description', 'e')));
165
        $title = xoops_substr($title, 0, 10, '');
166
        // Transformation des ponctuations
167
        $pattern = array(
168
            '/%09/', // Tab
169
            '/%20/', // Space
170
            '/%21/', // !
171
            '/%22/', // "
172
            '/%23/', // #
173
            '/%25/', // %
174
            '/%26/', // &
175
            '/%27/', // '
176
            '/%28/', // (
177
            '/%29/', // )
178
            '/%2C/', // ,
179
            '/%2F/', // /
180
            '/%3A/', // :
181
            '/%3B/', // ;
182
            '/%3C/', // <
183
            '/%3D/', // =
184
            '/%3E/', // >
185
            '/%3F/', // ?
186
            '/%40/', // @
187
            '/%5B/', // [
188
            '/%5C/', // \
189
            '/%5D/', // ]
190
            '/%5E/', // ^
191
            '/%7B/', // {
192
            '/%7C/', // |
193
            '/%7D/', // }
194
            '/%7E/', // ~
195
            "/\./" // .
196
        );
197
        $rep_pat = array('-', '-', '-', '-', '-', '-100', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-at-', '-', '-', '-', '-', '-', '-', '-', '-', '-');
198
        $title   = preg_replace($pattern, $rep_pat, $title);
199
200
        // Transformation des caract�res accentu�s
201
        $pattern = array(
202
            '/%B0/', // °
203
            '/%E8/', // è
204
            '/%E9/', // é
205
            '/%EA/', // ê
206
            '/%EB/', // ë
207
            '/%E7/', // ç
208
            '/%E0/', // à
209
            '/%E2/', // â
210
            '/%E4/', // ä
211
            '/%EE/', // î
212
            '/%EF/', // ï
213
            '/%F9/', // ù
214
            '/%FC/', // ü
215
            '/%FB/', // û
216
            '/%F4/', // ô
217
            '/%F6/', // ö
218
        );
219
        $rep_pat = array('-', 'e', 'e', 'e', 'e', 'c', 'a', 'a', 'a', 'i', 'i', 'u', 'u', 'u', 'o', 'o');
220
        $title   = preg_replace($pattern, $rep_pat, $title);
221
222
        $tableau = explode('-', $title); // Transforme la chaine de caract�res en tableau
223
        $tableau = array_filter($tableau, array($this, 'emptyString')); // Supprime les chaines vides du tableau
224
        $title   = implode('-', $tableau); // Transforme un tableau en chaine de caract�res s�par� par un tiret
225
226
        $title .= time();
227
        $title = md5($title);
228
229
        return $title;
230
    }
231
232
    /**
233
     * @return mixed
234
     */
235
    public function getCustomtagName()
236
    {
237
        $ret = $this->getVar('name');
238
239
        return $ret;
240
    }
241
}
242
243
/**
244
 * Class SmartobjectCustomtagHandler
245
 */
246
class SmartobjectCustomtagHandler extends SmartPersistableObjectHandler
247
{
248
    public $objects = false;
249
250
    /**
251
     * SmartobjectCustomtagHandler constructor.
252
     * @param XoopsDatabase $db
253
     */
254
    public function __construct(XoopsDatabase $db)
255
    {
256
        parent::__construct($db, 'customtag', 'customtagid', 'name', 'description', 'smartobject');
257
        $this->addPermission('view', _CO_SOBJECT_CUSTOMTAG_PERMISSION_VIEW, _CO_SOBJECT_CUSTOMTAG_PERMISSION_VIEW_DSC);
258
    }
259
260
    /**
261
     * @return array|bool
262
     */
263
    public function getCustomtagsByName()
264
    {
265
        if (!$this->objects) {
266
            global $xoopsConfig;
267
268
            $ret = array();
269
270
            $criteria = new CriteriaCompo();
271
272
            $criteria_language = new CriteriaCompo();
273
            $criteria_language->add(new Criteria('language', $xoopsConfig['language']));
274
            $criteria_language->add(new Criteria('language', 'all'), 'OR');
275
            $criteria->add($criteria_language);
276
277
            $smartobjectPermissionsHandler = new SmartObjectPermissionHandler($this);
278
            $granted_ids                   = $smartobjectPermissionsHandler->getGrantedItems('view');
279
280
            if ($granted_ids && count($granted_ids) > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $granted_ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
281
                $criteria->add(new Criteria('customtagid', '(' . implode(', ', $granted_ids) . ')', 'IN'));
282
                $customtagsObj = $this->getObjects($criteria, true);
283
                foreach ($customtagsObj as $customtagObj) {
284
                    $ret[$customtagObj->getVar('name')] = $customtagObj;
285
                }
286
            }
287
            $this->objects = $ret;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ret of type array is incompatible with the declared type boolean of property $objects.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
288
        }
289
290
        return $this->objects;
291
    }
292
}
293