Passed
Push — master ( d41337...f89f00 )
by Goffy
50s queued 13s
created

Images::getFormImages()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 153
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 54
nc 2048
nop 1
dl 0
loc 153
rs 2.8
c 2
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\Tdmdownloads\Common;
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
 * @copyright      2020 XOOPS Project (https://xoops.org)
17
 * @license        GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @link           https://xoops.org
19
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
20
 */
21
22
use XoopsModules\Tdmdownloads\{
23
    Helper
24
};
25
26
/**
27
 * Class Object Images
28
 */
29
class Images extends \XoopsObject
30
{
31
    /**
32
     * Constructor
33
     *
34
     * @param null
35
     */
36
    public function __construct()
37
    {
38
        $this->initVar('img_id', \XOBJ_DTYPE_INT);
39
40
        $this->initVar('img_title', \XOBJ_DTYPE_TXTBOX);
41
42
        $this->initVar('img_desc', \XOBJ_DTYPE_TXTAREA);
43
44
        $this->initVar('img_name', \XOBJ_DTYPE_TXTBOX);
45
46
        $this->initVar('img_namelarge', \XOBJ_DTYPE_TXTBOX);
47
48
        $this->initVar('img_nameorig', \XOBJ_DTYPE_TXTBOX);
49
50
        $this->initVar('img_mimetype', \XOBJ_DTYPE_TXTBOX);
51
52
        $this->initVar('img_size', \XOBJ_DTYPE_INT);
53
54
        $this->initVar('img_resx', \XOBJ_DTYPE_INT);
55
56
        $this->initVar('img_resy', \XOBJ_DTYPE_INT);
57
58
        $this->initVar('img_downloads', \XOBJ_DTYPE_INT);
59
60
        $this->initVar('img_ratinglikes', \XOBJ_DTYPE_INT);
61
62
        $this->initVar('img_votes', \XOBJ_DTYPE_INT);
63
64
        $this->initVar('img_weight', \XOBJ_DTYPE_INT);
65
66
        $this->initVar('img_albid', \XOBJ_DTYPE_INT);
67
68
        $this->initVar('img_state', \XOBJ_DTYPE_INT);
69
70
        $this->initVar('img_date', \XOBJ_DTYPE_INT);
71
72
        $this->initVar('img_submitter', \XOBJ_DTYPE_INT);
73
74
        $this->initVar('img_ip', \XOBJ_DTYPE_TXTAREA);
75
76
        $this->initVar('dohtml', \XOBJ_DTYPE_INT, 1, false);
77
    }
78
79
    /**
80
     * @static function &getInstance
81
     *
82
     * @param null
83
     */
84
    public static function getInstance()
85
    {
86
        static $instance = false;
87
88
        if (!$instance) {
89
            $instance = new self();
90
        }
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getNewInsertedIdImages()
97
    {
98
        return $GLOBALS['xoopsDB']->getInsertId();
99
    }
100
101
    /**
102
     * @public function getForm
103
     * @param bool $action
104
     * @return \XoopsThemeForm
105
     */
106
    public function getFormImages($action = false)
107
    {
108
        $moduleDirName = \basename(dirname(__DIR__, 2));
109
110
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
111
112
        $helper = Helper::getInstance();
113
114
        if (!$action) {
115
            $action = $_SERVER['REQUEST_URI'];
116
        }
117
118
        // Title
119
120
        $title = $this->isNew() ? \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_ADD')) : \sprintf(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_EDIT'));
121
122
        // Get Theme Form
123
124
        \xoops_load('XoopsFormLoader');
125
126
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
127
128
        $form->setExtra('enctype="multipart/form-data"');
129
130
        // Form Text ImgTitle
131
132
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_TITLE'), 'img_title', 50, 255, $this->getVar('img_title')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('img_title') 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

132
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_TITLE'), 'img_title', 50, 255, /** @scrutinizer ignore-type */ $this->getVar('img_title')));
Loading history...
133
134
        // Form editor ImgDesc
135
136
        $editorConfigs = [];
137
138
        $editorConfigs['name'] = 'img_desc';
139
140
        $editorConfigs['value'] = $this->getVar('img_desc', 'e');
141
142
        $editorConfigs['rows'] = 5;
143
144
        $editorConfigs['cols'] = 40;
145
146
        $editorConfigs['width'] = '100%';
147
148
        $editorConfigs['height'] = '400px';
149
150
        $editorConfigs['editor'] = $helper->getConfig('editor');
151
152
        $form->addElement(new \XoopsFormEditor(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_DESC'), 'img_desc', $editorConfigs));
153
154
        // Form Text ImgName
155
156
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_NAME'), 'img_name', 50, 255, $this->getVar('img_name')), true);
157
158
        // Form Text ImgNameLarge
159
160
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_NAMELARGE'), 'img_namelarge', 50, 255, $this->getVar('img_namelarge')), true);
161
162
        // Form Text ImgOrigname
163
164
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_NAMEORIG'), 'img_nameorig', 50, 255, $this->getVar('img_nameorig')), true);
165
166
        // Form Text ImgMimetype
167
168
        $imgMimetype = $this->isNew() ? '0' : $this->getVar('img_mimetype');
169
170
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_MIMETYPE'), 'img_mimetype', 20, 150, $imgMimetype));
171
172
        // Form Text ImgSize
173
174
        $imgSize = $this->isNew() ? '0' : $this->getVar('img_size');
175
176
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_SIZE'), 'img_size', 20, 150, $imgSize));
177
178
        // Form Text ImgResx
179
180
        $imgResx = $this->isNew() ? '0' : $this->getVar('img_resx');
181
182
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_RESX'), 'img_resx', 20, 150, $imgResx));
183
184
        // Form Text ImgResy
185
186
        $imgResy = $this->isNew() ? '0' : $this->getVar('img_resy');
187
188
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_RESY'), 'img_resy', 20, 150, $imgResy));
189
190
        // Form Text ImgDownloads
191
192
        $imgDownloads = $this->isNew() ? '0' : $this->getVar('img_downloads');
193
194
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_DOWNLOADS'), 'img_downloads', 20, 150, $imgDownloads));
195
196
        // Form Text ImgRatinglikes
197
198
        $imgRatinglikes = $this->isNew() ? '0' : $this->getVar('img_ratinglikes');
199
200
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_RATINGLIKES'), 'img_ratinglikes', 20, 150, $imgRatinglikes));
201
202
        // Form Text ImgVotes
203
204
        $imgVotes = $this->isNew() ? '0' : $this->getVar('img_votes');
205
206
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_VOTES'), 'img_votes', 20, 150, $imgVotes));
207
208
        // Form Text ImgWeight
209
210
        $imgWeight = $this->isNew() ? '0' : $this->getVar('img_weight');
211
212
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_WEIGHT'), 'img_weight', 20, 150, $imgWeight));
213
214
        // Form Table albums
215
216
        $albumsHandler = $helper->getHandler('Albums');
217
218
        $imgAlbidSelect = new \XoopsFormSelect(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_ALBID'), 'img_albid', $this->getVar('img_albid'));
219
220
        $imgAlbidSelect->addOptionArray($albumsHandler->getList());
0 ignored issues
show
Bug introduced by
The method getList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsImageHandler or XoopsRankHandler or XoopsCommentHandler or XoopsTplsetHandler or XoopsAvatarHandler or XoopsBlockHandler or XoopsImageSetHandler or XoopsPersistableObjectHandler or XoopsImagecategoryHandler. ( Ignorable by Annotation )

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

220
        $imgAlbidSelect->addOptionArray($albumsHandler->/** @scrutinizer ignore-call */ getList());
Loading history...
221
222
        $form->addElement($imgAlbidSelect, true);
223
224
        // Images handler
225
226
        $imagesHandler = $helper->getHandler('Images');
227
228
        // Form Select Images
229
230
        $imgStateSelect = new \XoopsFormSelect(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_STATE'), 'img_state', $this->getVar('img_state'));
231
232
        $imgStateSelect->addOption('Empty');
233
234
        $imgStateSelect->addOptionArray($imagesHandler->getList());
235
236
        $form->addElement($imgStateSelect, true);
237
238
        // Form Text Date Select ImgDate
239
240
        $imgDate = $this->isNew() ? 0 : $this->getVar('img_date');
241
242
        $form->addElement(new \XoopsFormTextDateSelect(\constant('CO_' . $moduleDirNameUpper . '_' . 'DATE'), 'img_date', '', $imgDate));
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

242
        $form->addElement(new \XoopsFormTextDateSelect(\constant('CO_' . $moduleDirNameUpper . '_' . 'DATE'), 'img_date', /** @scrutinizer ignore-type */ '', $imgDate));
Loading history...
243
244
        // Form Select User ImgSubmitter
245
246
        $form->addElement(new \XoopsFormSelectUser(\constant('CO_' . $moduleDirNameUpper . '_' . 'SUBMITTER'), 'img_submitter', false, $this->getVar('img_submitter')));
247
248
        // Form Text ImgIp
249
250
        $form->addElement(new \XoopsFormText(\constant('CO_' . $moduleDirNameUpper . '_' . 'IMAGE_IP'), 'img_ip', 50, 255, $this->getVar('img_ip')));
251
252
        // To Save
253
254
        $form->addElement(new \XoopsFormHidden('op', 'save'));
255
256
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false));
257
258
        return $form;
259
    }
260
261
    /**
262
     * Get Values
263
     * @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...
264
     * @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...
265
     * @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...
266
     * @return array
267
     */
268
    public function getValuesImages($keys = null, $format = null, $maxDepth = null)
269
    {
270
        $moduleDirName = \basename(dirname(__DIR__, 2));
271
272
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
273
274
        $helper = Helper::getInstance();
275
276
        $ret = $this->getValues($keys, $format, $maxDepth);
277
278
        $ret['id'] = $this->getVar('img_id');
279
280
        $ret['title'] = $this->getVar('img_title');
281
282
        $ret['desc'] = $this->getVar('img_desc', 'n');
283
284
        $ret['name'] = $this->getVar('img_name');
285
286
        $ret['namelarge'] = $this->getVar('img_namelarge');
287
288
        $ret['nameorig'] = $this->getVar('img_nameorig');
289
290
        $ret['mimetype'] = $this->getVar('img_mimetype');
291
292
        $ret['size'] = $this->getVar('img_size');
293
294
        $ret['resx'] = $this->getVar('img_resx');
295
296
        $ret['resy'] = $this->getVar('img_resy');
297
298
        $ret['downloads'] = $this->getVar('img_downloads');
299
300
        $ret['ratinglikes'] = $this->getVar('img_ratinglikes');
301
302
        $ret['votes'] = $this->getVar('img_votes');
303
304
        $ret['weight'] = $this->getVar('img_weight');
305
306
        $ret['albid'] = $this->getVar('img_albid');
307
308
        //$albums             = $helper->getHandler('Albums');
309
310
        //$albumsObj          = $albums->get($this->getVar('img_albid'));
311
312
        //if (isset($albumsObj) && is_object($albumsObj)) {
313
314
        //$ret['alb_name'] = $albumsObj->getVar('alb_name');
315
316
        //}
317
318
        $ret['state'] = $this->getVar('img_state');
319
320
        $ret['state_text'] = $helper->getStateText($this->getVar('img_state'));
0 ignored issues
show
Bug introduced by
The method getStateText() does not exist on XoopsModules\Tdmdownloads\Helper. ( Ignorable by Annotation )

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

320
        /** @scrutinizer ignore-call */ 
321
        $ret['state_text'] = $helper->getStateText($this->getVar('img_state'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
321
322
        $ret['date'] = \formatTimestamp($this->getVar('img_date'), 's');
323
324
        $ret['submitter'] = \XoopsUser::getUnameFromId($this->getVar('img_submitter'));
325
326
        $ret['ip'] = $this->getVar('img_ip');
327
328
        $ret['large'] = \constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_URL') . '/large/' . $this->getVar('img_namelarge');
329
330
        $ret['medium'] = \constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_URL') . '/medium/' . $this->getVar('img_name');
331
332
        $ret['thumb'] = \constant($moduleDirNameUpper . '_' . 'UPLOAD_IMAGE_URL') . '/thumbs/' . $this->getVar('img_name');
333
334
        return $ret;
335
    }
336
337
    /**
338
     * Returns an array representation of the object
339
     *
340
     * @return array
341
     */
342
    public function toArrayImages()
343
    {
344
        $ret = [];
345
346
        $vars = $this->getVars();
347
348
        foreach (\array_keys($vars) as $var) {
349
            $ret[$var] = $this->getVar('"{$var}"');
350
        }
351
352
        return $ret;
353
    }
354
}
355