Passed
Push — master ( 1e16e5...7bdd06 )
by Goffy
03:14
created

Textblock::getForm()   F

Complexity

Conditions 11
Paths 768

Size

Total Lines 62
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 40
nc 768
nop 1
dl 0
loc 62
rs 3.4722
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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 declare(strict_types=1);
2
3
namespace XoopsModules\Wgevents;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wgEvents module for xoops
17
 *
18
 * @copyright    2021 XOOPS Project (https://xoops.org)
19
 * @license      GPL 2.0 or later
20
 * @package      wgevents
21
 * @since        1.0.0
22
 * @min_xoops    2.5.11 Beta1
23
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
24
 */
25
26
use XoopsModules\Wgevents;
27
use XoopsModules\Wgevents\Helper;
28
29
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
30
31
/**
32
 * Class Object Textblock
33
 */
34
class Textblock extends \XoopsObject
35
{
36
    /**
37
     * @var int
38
     */
39
    public $start = 0;
40
41
    /**
42
     * @var int
43
     */
44
    public $limit = 0;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param null
50
     */
51
    public function __construct()
52
    {
53
        $this->initVar('id', \XOBJ_DTYPE_INT);
54
        $this->initVar('catid', \XOBJ_DTYPE_INT);
55
        $this->initVar('name', \XOBJ_DTYPE_TXTBOX);
56
        $this->initVar('text', \XOBJ_DTYPE_OTHER);
57
        $this->initVar('class', \XOBJ_DTYPE_INT);
58
        $this->initVar('weight', \XOBJ_DTYPE_INT);
59
        $this->initVar('datecreated', \XOBJ_DTYPE_INT);
60
        $this->initVar('submitter', \XOBJ_DTYPE_INT);
61
    }
62
63
    /**
64
     * @static function &getInstance
65
     *
66
     * @param null
67
     */
68
    public static function getInstance()
69
    {
70
        static $instance = false;
71
        if (!$instance) {
72
            $instance = new self();
73
        }
74
    }
75
76
    /**
77
     * The new inserted $Id
78
     * @return inserted id
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wgevents\inserted was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
     */
80
    public function getNewInsertedId()
81
    {
82
        return $GLOBALS['xoopsDB']->getInsertId();
83
    }
84
85
    /**
86
     * @public function getForm
87
     * @param bool $action
88
     * @return \XoopsThemeForm
89
     */
90
    public function getForm($action = false)
91
    {
92
        $helper = Helper::getInstance();
93
        //$categoryHandler = $helper->getHandler('Category');
94
        $textblockHandler = $helper->getHandler('Textblock');
95
96
        if (!$action) {
97
            $action = $_SERVER['REQUEST_URI'];
98
        }
99
        $isAdmin = (\is_object($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsModule'])) && $GLOBALS['xoopsUser']->isAdmin($GLOBALS['xoopsModule']->mid());
100
        // Title
101
        $title = $this->isNew() ? \_MA_WGEVENTS_TEXTBLOCK_ADD : \_MA_WGEVENTS_TEXTBLOCK_EDIT;
102
        // Get Theme Form
103
        \xoops_load('XoopsFormLoader');
104
        $form = new \XoopsThemeForm($title, 'formTextblock', $action, 'post', true);
105
        $form->setExtra('enctype="multipart/form-data"');
106
        // Form Table categories
107
        /*  for the moment textblocks are valid for all categories
108
        $evCatidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_EVENT_CATID, 'catid', $this->getVar('catid'));
109
        $evCatidSelect->addOptionArray($categoryHandler->getList());
110
        $form->addElement($evCatidSelect);
111
        */
112
        // Form Text tbName
113
        $form->addElement(new \XoopsFormText(\_MA_WGEVENTS_TEXTBLOCK_NAME, 'name', 50, 255, $this->getVar('name')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('name') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        $form->addElement(new \XoopsFormText(\_MA_WGEVENTS_TEXTBLOCK_NAME, 'name', 50, 255, /** @scrutinizer ignore-type */ $this->getVar('name')));
Loading history...
114
        // Form Editor DhtmlTextArea tbText
115
        $editorConfigs = [];
116
        if ($isAdmin) {
117
            $editor = $helper->getConfig('editor_admin');
118
        } else {
119
            $editor = $helper->getConfig('editor_user');
120
        }
121
        $editorConfigs['name'] = 'text';
122
        $editorConfigs['value'] = $this->getVar('text', 'e');
123
        $editorConfigs['rows'] = 5;
124
        $editorConfigs['cols'] = 40;
125
        $editorConfigs['width'] = '100%';
126
        $editorConfigs['height'] = '400px';
127
        $editorConfigs['editor'] = $editor;
128
        $form->addElement(new \XoopsFormEditor(\_MA_WGEVENTS_TEXTBLOCK_TEXT, 'text', $editorConfigs));
129
        // Form select tbClass
130
        $tbClass = $this->isNew() ? Constants::TEXTBLOCK_CLASS_PRIVATE : $this->getVar('class');
131
        $tbclassSelect = new \XoopsFormSelect(\_MA_WGEVENTS_TEXTBLOCK_CLASS, 'class', $tbClass);
132
        $tbclassSelect->addOption(Constants::TEXTBLOCK_CLASS_PRIVATE, \_MA_WGEVENTS_TEXTBLOCK_CLASS_PRIVATE);
133
        $tbclassSelect->addOption(Constants::TEXTBLOCK_CLASS_PUBLIC, \_MA_WGEVENTS_TEXTBLOCK_CLASS_PUBLIC);
134
        $form->addElement($tbclassSelect);
135
        // Form Text tbWeight
136
        $tbWeight = $this->isNew() ? $textblockHandler->getNextWeight() : $this->getVar('weight');
137
        $form->addElement(new \XoopsFormText(\_MA_WGEVENTS_WEIGHT, 'weight', 50, 255, $tbWeight));
138
        // Form Text Date Select tbDatecreated
139
        $tbDatecreated = $this->isNew() ? \time() : $this->getVar('datecreated');
140
        $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGEVENTS_DATECREATED, 'datecreated', '', $tbDatecreated));
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $size of XoopsFormTextDateSelect::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
        $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGEVENTS_DATECREATED, 'datecreated', /** @scrutinizer ignore-type */ '', $tbDatecreated));
Loading history...
141
        // Form Select User tbSubmitter
142
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
143
        $tbSubmitter = $this->isNew() ? $uidCurrent : $this->getVar('submitter');
144
        $form->addElement(new \XoopsFormSelectUser(\_MA_WGEVENTS_SUBMITTER, 'submitter', false, $tbSubmitter));
145
146
        // To Save
147
        $form->addElement(new \XoopsFormHidden('op', 'save'));
148
        $form->addElement(new \XoopsFormHidden('start', $this->start));
149
        $form->addElement(new \XoopsFormHidden('limit', $this->limit));
150
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false));
151
        return $form;
152
    }
153
154
    /**
155
     * Get Values
156
     * @param null $keys
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $keys is correct as it would always require null to be passed?
Loading history...
157
     * @param null $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
158
     * @param null $maxDepth
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $maxDepth is correct as it would always require null to be passed?
Loading history...
159
     * @return array
160
     */
161
    public function getValuesTextblocks($keys = null, $format = null, $maxDepth = null)
162
    {
163
        $helper  = \XoopsModules\Wgevents\Helper::getInstance();
164
        $utility = new \XoopsModules\Wgevents\Utility();
165
        $ret = $this->getValues($keys, $format, $maxDepth);
166
        $editorMaxchar = $helper->getConfig('admin_maxchar');
167
        $categoryHandler = $helper->getHandler('Category');
168
        $categoryObj = $categoryHandler->get($this->getVar('catid'));
169
        $catName = '';
170
        if (\is_object($categoryObj)) {
171
            $catName = $categoryObj->getVar('name');
172
        }
173
        $ret['catname']          = $catName;
174
        $ret['text_text']        = $this->getVar('text', 'e');
175
        $ret['text_short']       = $utility::truncateHtml($ret['text'], $editorMaxchar);
176
        $ret['cat_text']         = $this->getVar('catid');
177
        $ret['class_text']       = Constants::TEXTBLOCK_CLASS_PUBLIC == $this->getVar('class') ? \_MA_WGEVENTS_TEXTBLOCK_CLASS_PUBLIC : \_MA_WGEVENTS_TEXTBLOCK_CLASS_PRIVATE;
178
        $ret['datecreated_text'] = \formatTimestamp($this->getVar('datecreated'), 's');
179
        $ret['submitter_text']   = \XoopsUser::getUnameFromId($this->getVar('submitter'));
180
        return $ret;
181
    }
182
183
    /**
184
     * Returns an array representation of the object
185
     *
186
     * @return array
187
     */
188
    /*
189
    public function toArray()
190
    {
191
        $ret = [];
192
        $vars = $this->getVars();
193
        foreach (\array_keys($vars) as $var) {
194
            $ret[$var] = $this->getVar($var);
195
        }
196
        return $ret;
197
    }
198
    */
199
}
200