Passed
Pull Request — master (#88)
by Michael
02:56
created

Field::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 19
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tdmdownloads;
4
5
/**
6
 * TDMDownload
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
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 *
15
 * @copyright   Gregory Mage (Aka Mage)
16
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @author      Gregory Mage (Aka Mage)
18
 */
19
20
/**
21
 * Class Field
22
 * @package XoopsModules\Tdmdownloads
23
 */
24
class Field extends \XoopsObject
25
{
26
    // constructor
27
28
    public function __construct()
29
    {
30
        $this->initVar('fid', \XOBJ_DTYPE_INT, null, false, 11);
31
32
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false);
33
34
        $this->initVar('img', \XOBJ_DTYPE_TXTBOX, null, false);
35
36
        $this->initVar('weight', \XOBJ_DTYPE_INT, null, false, 11);
37
38
        $this->initVar('status', \XOBJ_DTYPE_INT, null, false, 5);
39
40
        $this->initVar('search', \XOBJ_DTYPE_INT, null, false, 5);
41
42
        $this->initVar('status_def', \XOBJ_DTYPE_INT, null, false, 5);
43
44
        //pour les jointures
45
46
        $this->initVar('data', \XOBJ_DTYPE_TXTAREA, null, false);
47
    }
48
49
    /**
50
     * @param null $db
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $db is correct as it would always require null to be passed?
Loading history...
51
     * @return int
52
     */
53
    public function getNewEnreg($db = null)
54
    {
55
        $newEnreg = 0;
56
57
        /** @var \XoopsMySQLDatabase $db */
58
59
        if (null !== $db) {
60
            $newEnreg = $db->getInsertId();
61
        }
62
63
        return $newEnreg;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $newEnreg also could return the type string which is incompatible with the documented return type integer.
Loading history...
64
    }
65
66
    /**
67
     * @param bool $action
68
     *
69
     * @return \XoopsThemeForm
70
     */
71
    public function getForm($action = false)
72
    {
73
        /** @var \XoopsModules\Tdmdownloads\Helper $helper */
74
75
        $helper = \XoopsModules\Tdmdownloads\Helper::getInstance();
76
77
        $moduleDirName = \basename(\dirname(__DIR__));
78
79
        if (!$action) {
80
            $action = $_SERVER['REQUEST_URI'];
81
        }
82
83
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
84
85
        //nom du formulaire selon l'action (editer ou ajouter):
86
87
        $title = $this->isNew() ? \sprintf(_AM_TDMDOWNLOADS_FORMADD) : \sprintf(_AM_TDMDOWNLOADS_FORMEDIT);
88
89
        //création du formulaire
90
91
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
92
93
        $form->setExtra('enctype="multipart/form-data"');
94
95
        //titre
96
97
        if (1 == $this->getVar('status_def')) {
98
            $form->addElement(new \XoopsFormLabel(_AM_TDMDOWNLOADS_FORMTITLE, $this->getVar('title')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('title') can also be of type array and array; however, parameter $value of XoopsFormLabel::__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

98
            $form->addElement(new \XoopsFormLabel(_AM_TDMDOWNLOADS_FORMTITLE, /** @scrutinizer ignore-type */ $this->getVar('title')));
Loading history...
99
100
            $form->addElement(new \XoopsFormHidden('title', $this->getVar('title')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('title') can also be of type array and array; however, parameter $value of XoopsFormHidden::__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

100
            $form->addElement(new \XoopsFormHidden('title', /** @scrutinizer ignore-type */ $this->getVar('title')));
Loading history...
101
        } else {
102
            $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'title', 50, 255, $this->getVar('title')), true);
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('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

102
            $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'title', 50, 255, /** @scrutinizer ignore-type */ $this->getVar('title')), true);
Loading history...
103
        }
104
105
        //image
106
107
        $downloadsfield_img = $this->getVar('img') ?: 'blank.gif';
108
109
        $uploadirectory = '/uploads/' . $moduleDirName . '/images/field';
110
111
        $imgtray = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMAGE, '<br>');
112
113
        $imgpath = \sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory);
114
115
        $imageselect = new \XoopsFormSelect($imgpath, 'downloadsfield_img', $downloadsfield_img);
116
117
        $topics_array = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadirectory);
118
119
        foreach ($topics_array as $image) {
120
            $imageselect->addOption($image, $image);
121
        }
122
123
        $imageselect->setExtra("onchange='showImgSelected(\"image3\", \"downloadsfield_img\", \"" . $uploadirectory . '", "", "' . XOOPS_URL . "\")'");
124
125
        $imgtray->addElement($imageselect, false);
126
127
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadirectory . '/' . $downloadsfield_img . "' name='image3' id='image3' alt=''><br>"));
128
129
        $fileseltray = new \XoopsFormElementTray('', '<br>');
130
131
        $fileseltray->addElement(new \XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD, 'attachedfile', $helper->getConfig('maxuploadsize')), false);
132
133
        $fileseltray->addElement(new \XoopsFormLabel(''), false);
134
135
        $imgtray->addElement($fileseltray);
136
137
        $form->addElement($imgtray);
138
139
        //poids du champ
140
141
        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMWEIGHT, 'weight', 5, 5, $this->getVar('weight', 'e')), false);
142
143
        // affiché?
144
145
        $status = $this->getVar('status') ?: 0;
146
147
        $form->addElement(new \XoopsFormRadioYN(_AM_TDMDOWNLOADS_FORMAFFICHE, 'status', $status));
148
149
        // affiché dans le champ de recherche?
150
151
        $search = $this->getVar('search') ?: 0;
152
153
        $form->addElement(new \XoopsFormRadioYN(_AM_TDMDOWNLOADS_FORMAFFICHESEARCH, 'search', $search));
154
155
        // pour passer "fid" si on modifie le champ
156
157
        if (!$this->isNew()) {
158
            $form->addElement(new \XoopsFormHidden('fid', $this->getVar('fid')));
159
160
            $form->addElement(new \XoopsFormHidden('status_def', $this->getVar('status_def')));
161
        } else {
162
            $form->addElement(new \XoopsFormHidden('status_def', 0));
163
        }
164
165
        //pour enregistrer le formulaire
166
167
        $form->addElement(new \XoopsFormHidden('op', 'save_field'));
168
169
        //boutton d'envoi du formulaire
170
171
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', 'submit', false));
172
173
        return $form;
174
    }
175
}
176