Passed
Pull Request — master (#5)
by Michael
02:50
created

Category::getForm()   B

Complexity

Conditions 8
Paths 64

Size

Total Lines 106
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 74
nc 64
nop 1
dl 0
loc 106
rs 7.3228
c 0
b 0
f 0

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
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 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
17
 * @author      Gregory Mage (Aka Mage)
18
 */
19
20
use XoopsModules\Tdmdownloads;
21
22
defined('XOOPS_ROOT_PATH') || die('Restricted access');
23
24
/**
25
 * Class Category
26
 * @package XoopsModules\Tdmdownloads
27
 */
28
class Category extends \XoopsObject
29
{
30
    // constructor
31
32
    /**
33
     * Category constructor.
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
        $this->initVar('cat_cid', XOBJ_DTYPE_INT, null, false, 5);
39
        $this->initVar('cat_pid', XOBJ_DTYPE_INT, null, false, 5);
40
        $this->initVar('cat_title', XOBJ_DTYPE_TXTBOX, null, false);
41
        $this->initVar('cat_imgurl', XOBJ_DTYPE_TXTBOX, null, false);
42
        $this->initVar('cat_description_main', XOBJ_DTYPE_TXTAREA, null, false);
43
        // Pour autoriser le html
44
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
45
        $this->initVar('cat_weight', XOBJ_DTYPE_INT, 0, false, 11);
46
    }
47
48
    /**
49
     * @param null|\XoopsDatabase $db
50
     * @return mixed
51
     */
52
    public function getNewEnreg($db = null)
53
    {
54
        $newEnreg = 0;
55
        /** @var \XoopsMySQLDatabase $db */
56
        if (null !== $db) {
57
            $newEnreg = $db->getInsertId();
58
        }
59
60
        return $newEnreg;
61
    }
62
63
    /**
64
     * @param bool $action
65
     *
66
     * @return \XoopsThemeForm
67
     */
68
    public function getForm($action = false)
69
    {
70
        /** @var \XoopsModules\Tdmdownloads\Helper $helper */
71
        $helper = \XoopsModules\Tdmdownloads\Helper::getInstance();
72
73
        if (false === $action) {
74
            $action = $_SERVER['REQUEST_URI'];
75
        }
76
        $moduleDirName = basename(dirname(__DIR__));
77
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
78
79
        //nom du formulaire selon l'action (editer ou ajouter):
80
        $title = $this->isNew() ? sprintf(_AM_TDMDOWNLOADS_FORMADD) : sprintf(_AM_TDMDOWNLOADS_FORMEDIT);
81
82
        //création du formulaire
83
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
84
        $form->setExtra('enctype="multipart/form-data"');
85
        //titre
86
        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'cat_title', 50, 255, $this->getVar('cat_title')), true);
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('cat_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

86
        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMTITLE, 'cat_title', 50, 255, /** @scrutinizer ignore-type */ $this->getVar('cat_title')), true);
Loading history...
87
        //editeur
88
        $editor_configs           = [];
89
        $editor_configs['name']   = 'cat_description_main';
90
        $editor_configs['value']  = $this->getVar('cat_description_main', 'e');
91
        $editor_configs['rows']   = 20;
92
        $editor_configs['cols']   = 160;
93
        $editor_configs['width']  = '100%';
94
        $editor_configs['height'] = '400px';
95
        $editor_configs['editor'] = $helper->getConfig('editor');
96
        $form->addElement(new \XoopsFormEditor(_AM_TDMDOWNLOADS_FORMTEXT, 'cat_description_main', $editor_configs), false);
97
        //image
98
        $downloadscat_img = $this->getVar('cat_imgurl') ?: 'blank.gif';
99
        $uploadirectory   = '/uploads/' . $moduleDirName . '/images/cats';
100
        $imgtray          = new \XoopsFormElementTray(_AM_TDMDOWNLOADS_FORMIMG, '<br>');
101
        $imgpath          = sprintf(_AM_TDMDOWNLOADS_FORMPATH, $uploadirectory);
102
        $imageselect      = new \XoopsFormSelect($imgpath, 'downloadscat_img', $downloadscat_img);
103
        $topics_array     = \XoopsLists:: getImgListAsArray(XOOPS_ROOT_PATH . $uploadirectory);
104
        foreach ($topics_array as $image) {
105
            $imageselect->addOption((string)$image, $image);
106
        }
107
        $imageselect->setExtra("onchange='showImgSelected(\"image3\", \"downloadscat_img\", \"" . $uploadirectory . '", "", "' . XOOPS_URL . "\")'");
108
        $imgtray->addElement($imageselect, false);
109
        $imgtray->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadirectory . '/' . $downloadscat_img . "' name='image3' id='image3' alt=''>"));
110
        $fileseltray = new \XoopsFormElementTray('', '<br>');
111
        $fileseltray->addElement(new \XoopsFormFile(_AM_TDMDOWNLOADS_FORMUPLOAD, 'attachedfile', $helper->getConfig('maxuploadsize')), false);
112
        $fileseltray->addElement(new \XoopsFormLabel(''), false);
113
        $imgtray->addElement($fileseltray);
114
        $form->addElement($imgtray);
115
        // Pour faire une sous-catégorie
116
117
        /** @var \XoopsModules\Tdmdownloads\CategoryHandler $categoryHandler */
118
        $categoryHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Category');
119
        $criteria        = new \CriteriaCompo();
120
        $criteria->setSort('cat_weight ASC, cat_title');
121
        $criteria->setOrder('ASC');
122
        $downloadscatArray = $categoryHandler->getAll($criteria);
123
        $mytree            = new \XoopsModules\Tdmdownloads\Tree($downloadscatArray, 'cat_cid', 'cat_pid');
124
        $form->addElement($mytree->makeSelectElement('cat_pid', 'cat_title', '--', $this->getVar('cat_pid'), true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), true);
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('cat_pid') can also be of type array and array; however, parameter $selected of XoopsObjectTree::makeSelectElement() 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

124
        $form->addElement($mytree->makeSelectElement('cat_pid', 'cat_title', '--', /** @scrutinizer ignore-type */ $this->getVar('cat_pid'), true, 0, '', _AM_TDMDOWNLOADS_FORMINCAT), true);
Loading history...
125
        //poids de la catégorie
126
        $form->addElement(new \XoopsFormText(_AM_TDMDOWNLOADS_FORMWEIGHT, 'cat_weight', 5, 5, $this->getVar('cat_weight', 'e')), false);
127
128
        //permissions
129
        /** @var \XoopsMemberHandler $memberHandler */
130
        $memberHandler = xoops_getHandler('member');
131
        $group_list    = $memberHandler->getGroupList();
132
        /** @var \XoopsGroupPermHandler $grouppermHandler */
133
        $grouppermHandler = xoops_getHandler('groupperm');
134
        $full_list        = array_keys($group_list);
135
        global $xoopsModule;
136
        if (!$this->isNew()) {
137
            $groups_ids_view                   = $grouppermHandler->getGroupIds('tdmdownloads_view', $this->getVar('cat_cid'), $xoopsModule->getVar('mid'));
138
            $groups_ids_submit                 = $grouppermHandler->getGroupIds('tdmdownloads_submit', $this->getVar('cat_cid'), $xoopsModule->getVar('mid'));
139
            $groups_ids_download               = $grouppermHandler->getGroupIds('tdmdownloads_download', $this->getVar('cat_cid'), $xoopsModule->getVar('mid'));
140
            $groups_ids_view                   = array_values($groups_ids_view);
141
            $groups_news_can_view_checkbox     = new \XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_VIEW_DSC, 'groups_view[]', $groups_ids_view);
142
            $groups_ids_submit                 = array_values($groups_ids_submit);
143
            $groups_news_can_submit_checkbox   = new \XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_SUBMIT_DSC, 'groups_submit[]', $groups_ids_submit);
144
            $groups_ids_download               = array_values($groups_ids_download);
145
            $groups_news_can_download_checkbox = new \XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_DOWNLOAD_DSC, 'groups_download[]', $groups_ids_download);
146
        } else {
147
            $groups_news_can_view_checkbox     = new \XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_VIEW_DSC, 'groups_view[]', $full_list);
148
            $groups_news_can_submit_checkbox   = new \XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_SUBMIT_DSC, 'groups_submit[]', $full_list);
149
            $groups_news_can_download_checkbox = new \XoopsFormCheckBox(_AM_TDMDOWNLOADS_PERM_DOWNLOAD_DSC, 'groups_download[]', $full_list);
150
        }
151
        // pour voir
152
        $groups_news_can_view_checkbox->addOptionArray($group_list);
153
        $form->addElement($groups_news_can_view_checkbox);
154
        // pour editer
155
        $groups_news_can_submit_checkbox->addOptionArray($group_list);
156
        $form->addElement($groups_news_can_submit_checkbox);
157
        // pour télécharger
158
        if (1 == $helper->getConfig('permission_download')) {
159
            $groups_news_can_download_checkbox->addOptionArray($group_list);
160
            $form->addElement($groups_news_can_download_checkbox);
161
        }
162
163
        // pour passer "cid" si on modifie la catégorie
164
        if (!$this->isNew()) {
165
            $form->addElement(new \XoopsFormHidden('cat_cid', $this->getVar('cat_cid')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('cat_cid') 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

165
            $form->addElement(new \XoopsFormHidden('cat_cid', /** @scrutinizer ignore-type */ $this->getVar('cat_cid')));
Loading history...
166
            $form->addElement(new \XoopsFormHidden('categorie_modified', true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $value of XoopsFormHidden::__construct(). ( Ignorable by Annotation )

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

166
            $form->addElement(new \XoopsFormHidden('categorie_modified', /** @scrutinizer ignore-type */ true));
Loading history...
167
        }
168
        //pour enregistrer le formulaire
169
        $form->addElement(new \XoopsFormHidden('op', 'save_cat'));
170
        //boutton d'envoi du formulaire
171
        $form->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
172
173
        return $form;
174
    }
175
}
176