Field   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 93
rs 10
c 1
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewEnreg() 0 8 2
A __construct() 0 11 1
B getForm() 0 58 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Tdmdownloads;
6
7
/**
8
 * TDMDownload
9
 *
10
 * You may not change or alter any portion of this comment or credits
11
 * of supporting developers from this source code or any supporting source code
12
 * which is considered copyrighted (c) material of the original comment or credit authors.
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * @copyright   Gregory Mage (Aka Mage)
18
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
19
 * @author      Gregory Mage (Aka Mage)
20
 */
21
22
/**
23
 * Class Field
24
 * @package XoopsModules\Tdmdownloads
25
 */
26
class Field extends \XoopsObject
27
{
28
    // constructor
29
    public function __construct()
30
    {
31
        $this->initVar('fid', \XOBJ_DTYPE_INT, null, false, 11);
32
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false);
33
        $this->initVar('img', \XOBJ_DTYPE_TXTBOX, null, false);
34
        $this->initVar('weight', \XOBJ_DTYPE_INT, null, false, 11);
35
        $this->initVar('status', \XOBJ_DTYPE_INT, null, false, 5);
36
        $this->initVar('search', \XOBJ_DTYPE_INT, null, false, 5);
37
        $this->initVar('status_def', \XOBJ_DTYPE_INT, null, false, 5);
38
        //pour les jointures
39
        $this->initVar('data', \XOBJ_DTYPE_TXTAREA, null, false);
40
    }
41
42
    /**
43
     * @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...
44
     * @return int|string
45
     */
46
    public function getNewEnreg($db = null)
47
    {
48
        $newEnreg = 0;
49
        /** @var \XoopsMySQLDatabase $db */
50
        if (null !== $db) {
51
            $newEnreg = $db->getInsertId();
52
        }
53
        return $newEnreg;
54
    }
55
56
    /**
57
     * @param bool $action
58
     *
59
     * @return \XoopsThemeForm
60
     */
61
    public function getForm($action = false)
62
    {
63
        $helper        = Helper::getInstance();
64
        $moduleDirName = \basename(\dirname(__DIR__));
65
        if (!$action) {
66
            $action = $_SERVER['REQUEST_URI'];
67
        }
68
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
69
        //nom du formulaire selon l'action (editer ou ajouter):
70
        $title = $this->isNew() ? \sprintf(_AM_TDMDOWNLOADS_FORMADD) : \sprintf(_AM_TDMDOWNLOADS_FORMEDIT);
71
        //création du formulaire
72
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
73
        $form->setExtra('enctype="multipart/form-data"');
74
        //titre
75
        if (1 == $this->getVar('status_def')) {
76
            $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

76
            $form->addElement(new \XoopsFormLabel(_AM_TDMDOWNLOADS_FORMTITLE, /** @scrutinizer ignore-type */ $this->getVar('title')));
Loading history...
77
            $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

77
            $form->addElement(new \XoopsFormHidden('title', /** @scrutinizer ignore-type */ $this->getVar('title')));
Loading history...
78
        } else {
79
            $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

79
            $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'title', 50, 255, /** @scrutinizer ignore-type */ $this->getVar('title')), true);
Loading history...
80
        }
81
        //image
82
        $downloadsfield_img = $this->getVar('img') ?: 'blank.gif';
83
        $uploadirectory     = '/uploads/' . $moduleDirName . '/images/field';
84
        $imgtray            = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMAGE, '<br>');
85
        $imgpath            = \sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory);
86
        $imageselect        = new \XoopsFormSelect($imgpath, 'downloadsfield_img', $downloadsfield_img);
87
        $topics_array       = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadirectory);
88
        foreach ($topics_array as $image) {
89
            $imageselect->addOption($image, $image);
90
        }
91
        $imageselect->setExtra("onchange='showImgSelected(\"image3\", \"downloadsfield_img\", \"" . $uploadirectory . '", "", "' . XOOPS_URL . "\")'");
92
        $imgtray->addElement($imageselect, false);
93
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadirectory . '/' . $downloadsfield_img . "' name='image3' id='image3' alt=''><br>"));
94
        $fileseltray = new \XoopsFormElementTray('', '<br>');
95
        $fileseltray->addElement(new \XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD, 'attachedfile', $helper->getConfig('maxuploadsize')), false);
96
        $fileseltray->addElement(new \XoopsFormLabel(''), false);
97
        $imgtray->addElement($fileseltray);
98
        $form->addElement($imgtray);
99
        //poids du champ
100
        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMWEIGHT, 'weight', 5, 5, $this->getVar('weight', 'e')), false);
101
        // affiché?
102
        $status = $this->getVar('status') ?: 0;
103
        $form->addElement(new \XoopsFormRadioYN(_AM_TDMDOWNLOADS_FORMAFFICHE, 'status', $status));
104
        // affiché dans le champ de recherche?
105
        $search = $this->getVar('search') ?: 0;
106
        $form->addElement(new \XoopsFormRadioYN(_AM_TDMDOWNLOADS_FORMAFFICHESEARCH, 'search', $search));
107
        // pour passer "fid" si on modifie le champ
108
        if (!$this->isNew()) {
109
            $form->addElement(new \XoopsFormHidden('fid', $this->getVar('fid')));
110
            $form->addElement(new \XoopsFormHidden('status_def', $this->getVar('status_def')));
111
        } else {
112
            $form->addElement(new \XoopsFormHidden('status_def', 0));
113
        }
114
        //pour enregistrer le formulaire
115
        $form->addElement(new \XoopsFormHidden('op', 'save_field'));
116
        //boutton d'envoi du formulaire
117
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', 'submit', false));
118
        return $form;
119
    }
120
}
121