Passed
Pull Request — master (#81)
by Michael
02:58
created

ImagesForm::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 93
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 52
c 2
b 1
f 0
dl 0
loc 93
rs 8.7361
cc 5
nc 8
nop 1

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
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt\Form;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         yogurt
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use Xmf\Module\Helper\Permission;
26
use XoopsFormButton;
27
use XoopsFormHidden;
28
use XoopsFormLabel;
29
use XoopsFormSelectUser;
30
use XoopsFormText;
31
use XoopsFormTextArea;
32
use XoopsFormTextDateSelect;
33
use XoopsModules\Yogurt;
34
use XoopsThemeForm;
35
36
require_once \dirname(__DIR__, 2) . '/include/common.php';
37
38
$moduleDirName = \basename(\dirname(__DIR__, 2));
39
//$helper = Yogurt\Helper::getInstance();
40
$permHelper = new Permission();
41
42
\xoops_load('XoopsFormLoader');
43
44
/**
45
 * Class ImagesForm
46
 */
47
class ImagesForm extends XoopsThemeForm
48
{
49
    public $targetObject;
50
51
    public $helper;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param $target
57
     */
58
    public function __construct($target)
59
    {
60
        $this->helper       = $target->helper;
61
        $this->targetObject = $target;
62
63
        $title = $this->targetObject->isNew() ? \sprintf(\AM_YOGURT_IMAGES_ADD) : \sprintf(\AM_YOGURT_IMAGES_EDIT);
64
        parent::__construct($title, 'form', \xoops_getenv('SCRIPT_NAME'), 'post', true);
65
        $this->setExtra('enctype="multipart/form-data"');
66
67
        //include ID field, it's needed so the module knows if it is a new form or an edited form
68
69
        $hidden = new XoopsFormHidden(
70
            'cod_img', $this->targetObject->getVar(
71
            'cod_img'
72
        )
73
        );
74
        $this->addElement($hidden);
75
        unset($hidden);
76
77
        // Cod_img
78
        $this->addElement(
79
            new XoopsFormLabel(\AM_YOGURT_IMAGES_COD_IMG, $this->targetObject->getVar('cod_img'), 'cod_img')
80
        );
81
        // Title
82
        $this->addElement(
83
            new XoopsFormText(\AM_YOGURT_IMAGES_TITLE, 'title', 50, 255, $this->targetObject->getVar('title')),
84
            false
85
        );
86
87
        // Caption
88
        $this->addElement(
89
            new XoopsFormText(\AM_YOGURT_IMAGES_CAPTION, 'caption', 50, 255, $this->targetObject->getVar('caption')),
90
            false
91
        );
92
93
        // Data_creation
94
        $this->addElement(
95
            new XoopsFormTextDateSelect(
96
                \AM_YOGURT_IMAGES_DATE_CREATED, 'date_created', 0, \formatTimestamp($this->targetObject->getVar('date_created'), 's')
0 ignored issues
show
Bug introduced by
formatTimestamp($this->t...r('date_created'), 's') of type string is incompatible with the type integer expected by parameter $value 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

96
                \AM_YOGURT_IMAGES_DATE_CREATED, 'date_created', 0, /** @scrutinizer ignore-type */ \formatTimestamp($this->targetObject->getVar('date_created'), 's')
Loading history...
97
            )
98
        );
99
        // Data_update
100
        $this->addElement(
101
            new XoopsFormTextDateSelect(
102
                \AM_YOGURT_IMAGES_DATE_UPDATED, 'date_updated', 0, \formatTimestamp($this->targetObject->getVar('date_updated'), 's')
103
            )
104
        );
105
        // Uid_owner
106
        $this->addElement(
107
            new XoopsFormSelectUser(
108
                \AM_YOGURT_IMAGES_UID_OWNER, 'uid_owner', false, $this->targetObject->getVar(
109
                'uid_owner'
110
            ), 1, false
111
            ),
112
            false
113
        );
114
        // Url
115
        //        $this->addElement(
116
        //            new XoopsFormTextArea(AM_YOGURT_IMAGES_URL, 'filename', $this->targetObject->getVar('filename'), 4, 47),
117
        //            false
118
        //        );
119
120
        $filename = $this->targetObject->getVar('filename') ?: 'blank.png';
121
122
        $uploadDir   = '/uploads/yogurt/images/';
123
        $imgtray     = new \XoopsFormElementTray(\AM_YOGURT_IMAGES_URL, '<br>');
124
        $imgpath     = \sprintf(\AM_YOGURT_FORMIMAGE_PATH, $uploadDir);
125
        $imageselect = new \XoopsFormSelect($imgpath, 'filename', $filename);
126
        $imageArray  = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir);
127
        foreach ($imageArray as $image) {
128
            $imageselect->addOption($image, $image);
129
        }
130
        $imageselect->setExtra("onchange='showImgSelected(\"image_url\", \"filename\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'");
131
        $imgtray->addElement($imageselect);
132
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $url . "' name='image_url' id='image_url' alt='' style='max-width:300px'>"));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
133
        $fileseltray = new \XoopsFormElementTray('', '<br>');
134
        $fileseltray->addElement(new \XoopsFormFile(\AM_YOGURT_FORMUPLOAD, 'filename', $this->helper->getConfig('maxsize')));
135
        $fileseltray->addElement(new \XoopsFormLabel(''));
136
        $imgtray->addElement($fileseltray);
137
        $this->addElement($imgtray);
138
        // Private
139
        //        $this->addElement(
140
        //            new XoopsFormText(AM_YOGURT_IMAGES_PRIVATE, 'private', 50, 255, $this->targetObject->getVar('private')),
141
        //            false
142
        //        );
143
144
        $private       = $this->targetObject->isNew() ? 0 : $this->targetObject->getVar('private');
145
        $check_private = new \XoopsFormCheckBox(\AM_YOGURT_IMAGES_PRIVATE, 'private', $private);
146
        $check_private->addOption(1, ' ');
147
        $this->addElement($check_private);
148
149
        $this->addElement(new XoopsFormHidden('op', 'save'));
150
        $this->addElement(new XoopsFormButton('', 'submit', \_SUBMIT, 'submit'));
151
    }
152
}
153