Completed
Pull Request — master (#144)
by Michael
04:31
created

Tables   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 159
dl 0
loc 266
rs 10
c 1
b 0
f 0
wmc 23

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefinedLanguage() 0 7 2
A __construct() 0 31 1
A getInstance() 0 8 2
A __call() 0 5 2
A getValuesTables() 0 20 1
F getFormTables() 0 104 12
A getOptionsTables() 0 10 3
1
<?php namespace XoopsModules\Tdmcreate;
2
3
use XoopsModules\Tdmcreate;
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
 * tdmcreate module.
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
20
 *
21
 * @since           2.5.7
22
 *
23
 * @author          Txmod Xoops <[email protected]> - <http://www.txmodxoops.org/>
24
 *
25
 * @version         $Id: 1.91 tables.php 11297 2013-03-24 10:58:10Z timgno $
26
 */
27
// include __DIR__ . '/autoload.php';
28
29
/**
30
 * Class Tables.
31
 */
32
class Tables extends \XoopsObject
33
{
34
    /**
35
     * Options.
36
     */
37
    public $options = [
38
        'install',
39
        'index',
40
        'blocks',
41
        'admin',
42
        'user',
43
        'submenu',
44
        'submit',
45
        'tag',
46
        'broken',
47
        'search',
48
        'comments',
49
        'notifications',
50
        'permissions',
51
        'rate',
52
        'print',
53
        'pdf',
54
        'rss',
55
        'single',
56
        'visit',
57
    ];
58
59
    /**
60
     *  @public function constructor class
61
     *  @param null
62
     */
63
    public function __construct()
64
    {
65
        $this->initVar('table_id', XOBJ_DTYPE_INT);
66
        $this->initVar('table_mid', XOBJ_DTYPE_INT);
67
        $this->initVar('table_category', XOBJ_DTYPE_INT);
68
        $this->initVar('table_name', XOBJ_DTYPE_TXTBOX);
69
        $this->initVar('table_solename', XOBJ_DTYPE_TXTBOX);
70
        $this->initVar('table_fieldname', XOBJ_DTYPE_TXTBOX);
71
        $this->initVar('table_nbfields', XOBJ_DTYPE_INT);
72
        $this->initVar('table_order', XOBJ_DTYPE_INT);
73
        $this->initVar('table_image', XOBJ_DTYPE_TXTBOX);
74
        $this->initVar('table_autoincrement', XOBJ_DTYPE_INT);
75
        $this->initVar('table_install', XOBJ_DTYPE_INT);
76
        $this->initVar('table_index', XOBJ_DTYPE_INT);
77
        $this->initVar('table_blocks', XOBJ_DTYPE_INT);
78
        $this->initVar('table_admin', XOBJ_DTYPE_INT);
79
        $this->initVar('table_user', XOBJ_DTYPE_INT);
80
        $this->initVar('table_submenu', XOBJ_DTYPE_INT);
81
        $this->initVar('table_submit', XOBJ_DTYPE_INT);
82
        $this->initVar('table_tag', XOBJ_DTYPE_INT);
83
        $this->initVar('table_broken', XOBJ_DTYPE_INT);
84
        $this->initVar('table_search', XOBJ_DTYPE_INT);
85
        $this->initVar('table_comments', XOBJ_DTYPE_INT);
86
        $this->initVar('table_notifications', XOBJ_DTYPE_INT);
87
        $this->initVar('table_permissions', XOBJ_DTYPE_INT);
88
        $this->initVar('table_rate', XOBJ_DTYPE_INT);
89
        $this->initVar('table_print', XOBJ_DTYPE_INT);
90
        $this->initVar('table_pdf', XOBJ_DTYPE_INT);
91
        $this->initVar('table_rss', XOBJ_DTYPE_INT);
92
        $this->initVar('table_single', XOBJ_DTYPE_INT);
93
        $this->initVar('table_visit', XOBJ_DTYPE_INT);
94
    }
95
96
    /**
97
     * @param string $method
98
     * @param array  $args
99
     *
100
     * @return mixed
101
     */
102
    public function __call($method, $args)
103
    {
104
        $arg = isset($args[0]) ? $args[0] : null;
105
106
        return $this->getVar($method, $arg);
107
    }
108
109
    /**
110
     *  @static function getInstance
111
     *  @param null
112
     * @return Tables
113
     */
114
    public static function getInstance()
115
    {
116
        static $instance = false;
117
        if (!$instance) {
118
            $instance = new self();
119
        }
120
121
        return $instance;
122
    }
123
124
    /**
125
     *  @static function getFormTables
126
     *  @param mixed $action
127
     *
128
     * @return \XoopsThemeForm
129
     */
130
    public function getFormTables($action = false)
131
    {
132
        if (false === $action) {
133
            $action = $_SERVER['REQUEST_URI'];
134
        }
135
        $helper = Tdmcreate\Helper::getInstance();
136
        $isNew = $this->isNew();
137
        $tableName = $this->getVar('table_name');
138
        $tableMid = $this->getVar('table_mid');
139
        $title = $isNew ? sprintf(_AM_TDMCREATE_TABLE_NEW) : sprintf(_AM_TDMCREATE_TABLE_EDIT);
140
141
        xoops_load('XoopsFormLoader');
142
        $form = new \XoopsThemeForm($title, 'tableform', $action, 'post', true);
143
        $form->setExtra('enctype="multipart/form-data"');
144
145
        $modules = $helper->getHandler('Modules')->getObjects(null);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

145
        $modules = $helper->getHandler('Modules')->/** @scrutinizer ignore-call */ getObjects(null);
Loading history...
146
        $modulesSelect = new \XoopsFormSelect(_AM_TDMCREATE_TABLE_MODULES, 'table_mid', $tableMid);
147
        $modulesSelect->addOption('', _AM_TDMCREATE_TABLE_MODSELOPT);
148
        foreach ($modules as $mod) {
149
            $modulesSelect->addOption($mod->getVar('mod_id'), $mod->getVar('mod_name'));
150
        }
151
        $form->addElement($modulesSelect, true);
152
153
        $tableNameText = new \XoopsFormText(_AM_TDMCREATE_TABLE_NAME, 'table_name', 40, 150, $tableName);
0 ignored issues
show
Bug introduced by
It seems like $tableName 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

153
        $tableNameText = new \XoopsFormText(_AM_TDMCREATE_TABLE_NAME, 'table_name', 40, 150, /** @scrutinizer ignore-type */ $tableName);
Loading history...
154
        $tableNameText->setDescription(_AM_TDMCREATE_TABLE_NAME_DESC);
155
        $form->addElement($tableNameText, true);
156
157
        $tableSoleNameText = new \XoopsFormText(_AM_TDMCREATE_TABLE_SOLENAME, 'table_solename', 40, 150, $this->getVar('table_solename'));
158
        $tableSoleNameText->setDescription(_AM_TDMCREATE_TABLE_SOLENAME_DESC);
159
        $form->addElement($tableSoleNameText, true);
160
161
        $radioCategory = $isNew ? 0 : $this->getVar('table_category');
162
        $category = new \XoopsFormRadioYN(_AM_TDMCREATE_TABLE_CATEGORY, 'table_category', $radioCategory);
0 ignored issues
show
Bug introduced by
It seems like $radioCategory can also be of type array and array; however, parameter $value of XoopsFormRadioYN::__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

162
        $category = new \XoopsFormRadioYN(_AM_TDMCREATE_TABLE_CATEGORY, 'table_category', /** @scrutinizer ignore-type */ $radioCategory);
Loading history...
163
        $category->setDescription(_AM_TDMCREATE_TABLE_CATEGORY_DESC);
164
        $form->addElement($category);
165
166
        $tableFieldname = new \XoopsFormText(_AM_TDMCREATE_TABLE_FIELDNAME, 'table_fieldname', 30, 50, $this->getVar('table_fieldname'));
167
        $tableFieldname->setDescription(_AM_TDMCREATE_TABLE_FIELDNAME_DESC);
168
        $form->addElement($tableFieldname);
169
170
        $tableNumbFileds = new \XoopsFormText(_AM_TDMCREATE_TABLE_NBFIELDS, 'table_nbfields', 10, 25, $this->getVar('table_nbfields'));
171
        $tableNumbFileds->setDescription(_AM_TDMCREATE_TABLE_NBFIELDS_DESC);
172
        $form->addElement($tableNumbFileds, true);
173
174
        if (!$isNew) {
175
            $tableOrder = new \XoopsFormText(_AM_TDMCREATE_TABLE_ORDER, 'table_order', 5, 10, $this->getVar('table_order'));
176
            $tableOrder->setDescription(_AM_TDMCREATE_TABLE_ORDER_DESC);
177
            $form->addElement($tableOrder, true);
178
        }
179
180
        $getTableImage = $this->getVar('table_image');
181
        $tableImage = $getTableImage ?: 'blank.gif';
182
        $icons32Directory = '/Frameworks/moduleclasses/icons/32';
183
        $uploadsDirectory = '/uploads/tdmcreate/images/tables';
184
        $iconsDirectory = is_dir(XOOPS_ROOT_PATH . $icons32Directory) ? $icons32Directory : $uploadsDirectory;
185
186
        $imgtray1 = new \XoopsFormElementTray(_AM_TDMCREATE_TABLE_IMAGE, '<br>');
187
        $imgpath1 = sprintf(_AM_TDMCREATE_FORMIMAGE_PATH, ".{$iconsDirectory}/");
188
        $imageSelect1 = new \XoopsFormSelect($imgpath1, 'table_image', $tableImage, 10);
189
        $imageArray1 = \XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $iconsDirectory);
190
        foreach ($imageArray1 as $image1) {
191
            $imageSelect1->addOption($image1, $image1);
192
        }
193
        $imageSelect1->setExtra("onchange='showImgSelected(\"image1\", \"table_image\", \"" . $iconsDirectory . '", "", "' . XOOPS_URL . "\")'");
194
        $imgtray1->addElement($imageSelect1, false);
195
        $imgtray1->addElement(new \XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $iconsDirectory . '/' . $tableImage . "' name='image1' id='image1' alt='' />"));
196
        $fileseltray1 = new \XoopsFormElementTray('', '<br>');
197
        $fileseltray1->addElement(new \XoopsFormFile(_AM_TDMCREATE_FORMUPLOAD, 'attachedfile', $helper->getConfig('maxsize')));
198
        $fileseltray1->addElement(new \XoopsFormLabel(''));
199
        $imgtray1->addElement($fileseltray1);
200
        $imgtray1->setDescription(_AM_TDMCREATE_TABLE_IMAGE_DESC);
201
        $form->addElement($imgtray1);
202
203
        $tableAutoincrement = $this->isNew() ? 1 : $this->getVar('table_autoincrement');
204
        $checkTableAutoincrement = new \XoopsFormRadioYN(_AM_TDMCREATE_TABLE_AUTO_INCREMENT, 'table_autoincrement', $tableAutoincrement);
205
        $checkTableAutoincrement->setDescription(_AM_TDMCREATE_TABLE_AUTO_INCREMENT_DESC);
206
        $form->addElement($checkTableAutoincrement);
207
208
        $optionsTray = new \XoopsFormElementTray(_OPTIONS, '<br>');
209
210
        $tableCheckAll = new \XoopsFormCheckBox('', 'tablebox', 1);
211
        $tableCheckAll->addOption('allbox', _AM_TDMCREATE_TABLE_ALL);
212
        $tableCheckAll->setExtra(' onclick="xoopsCheckAll(\'tableform\', \'tablebox\');" ');
213
        $tableCheckAll->setClass('xo-checkall');
214
        $optionsTray->addElement($tableCheckAll);
215
        // Options
216
        $checkbox = new \XoopsFormCheckbox(' ', 'table_option', $this->getOptionsTables(), '<br>');
217
        $checkbox->setDescription(_AM_TDMCREATE_OPTIONS_DESC);
218
        foreach ($this->options as $option) {
219
            $checkbox->addOption($option, self::getDefinedLanguage('_AM_TDMCREATE_TABLE_' . mb_strtoupper($option)));
220
        }
221
        $optionsTray->addElement($checkbox);
222
223
        $optionsTray->setDescription(_AM_TDMCREATE_TABLE_OPTIONS_CHECKS_DESC);
224
225
        $form->addElement($optionsTray);
226
227
        $buttonTray = new \XoopsFormElementTray(_REQUIRED . ' <sup class="red bold">*</sup>', '');
228
        $buttonTray->addElement(new \XoopsFormHidden('op', 'save'));
229
        $buttonTray->addElement(new \XoopsFormHidden('table_id', ($isNew ? 0 : $this->getVar('table_id'))));
0 ignored issues
show
Bug introduced by
It seems like $isNew ? 0 : $this->getVar('table_id') 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

229
        $buttonTray->addElement(new \XoopsFormHidden('table_id', /** @scrutinizer ignore-type */ ($isNew ? 0 : $this->getVar('table_id'))));
Loading history...
230
        $buttonTray->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
231
        $form->addElement($buttonTray);
232
233
        return $form;
234
    }
235
236
    /**
237
     * Get Values.
238
     *
239
     * @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...
240
     * @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...
241
     * @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...
242
     *
243
     * @return array
244
     */
245
    public function getValuesTables($keys = null, $format = null, $maxDepth = null)
246
    {
247
        $ret = $this->getValues($keys, $format, $maxDepth);
248
        // Values
249
        $ret['id'] = $this->getVar('table_id');
250
        $ret['mid'] = $this->getVar('table_mid');
251
        $ret['name'] = ucfirst($this->getVar('table_name'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('table_name') can also be of type array and array; however, parameter $str of ucfirst() 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

251
        $ret['name'] = ucfirst(/** @scrutinizer ignore-type */ $this->getVar('table_name'));
Loading history...
252
        $ret['image'] = $this->getVar('table_image');
253
        $ret['nbfields'] = $this->getVar('table_nbfields');
254
        $ret['order'] = $this->getVar('table_order');
255
        $ret['blocks'] = $this->getVar('table_blocks');
256
        $ret['admin'] = $this->getVar('table_admin');
257
        $ret['user'] = $this->getVar('table_user');
258
        $ret['submenu'] = $this->getVar('table_submenu');
259
        $ret['search'] = $this->getVar('table_search');
260
        $ret['comments'] = $this->getVar('table_comments');
261
        $ret['notifications'] = $this->getVar('table_notifications');
262
        $ret['permissions'] = $this->getVar('table_permissions');
263
264
        return $ret;
265
    }
266
267
    /**
268
     * Get Options.
269
     *
270
     * @return array
271
     */
272
    public function getOptionsTables()
273
    {
274
        $retTable = [];
275
        foreach ($this->options as $option) {
276
            if (1 == $this->getVar('table_' . $option)) {
277
                $retTable[] = $option;
278
            }
279
        }
280
281
        return $retTable;
282
    }
283
284
    /**
285
     * Get Defined Language.
286
     *
287
     * @param $lang
288
     *
289
     * @return string
290
     */
291
    private static function getDefinedLanguage($lang)
292
    {
293
        if (defined($lang)) {
294
            return constant($lang);
295
        }
296
297
        return $lang;
298
    }
299
}
300