Passed
Push — master ( 0c230a...202965 )
by Michael
02:53
created

TDMCreateTables::getFormTables()   F

Complexity

Conditions 12
Paths 512

Size

Total Lines 104
Code Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 12
eloc 79
nc 512
nop 1
dl 0
loc 104
rs 3.2404
c 4
b 0
f 1

How to fix   Long Method    Complexity   

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
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * tdmcreate module.
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
18
 *
19
 * @since           2.5.7
20
 *
21
 * @author          Txmod Xoops <[email protected]> - <http://www.txmodxoops.org/>
22
 *
23
 * @version         $Id: 1.91 tables.php 11297 2013-03-24 10:58:10Z timgno $
24
 */
25
include __DIR__ . '/autoload.php';
26
27
/**
28
 * Class TDMCreateTables.
29
 */
30
class TDMCreateTables extends XoopsObject
31
{
32
    /**
33
     * Options.
34
     */
35
    public $options = [
36
        'install',
37
        'index',
38
        'blocks',
39
        'admin',
40
        'user',
41
        'submenu',
42
        'submit',
43
        'tag',
44
        'broken',
45
        'search',
46
        'comments',
47
        'notifications',
48
        'permissions',
49
        'rate',
50
        'print',
51
        'pdf',
52
        'rss',
53
        'single',
54
        'visit',
55
    ];
56
57
    /**
58
     *  @public function constructor class
59
     *  @param null
60
     */
61
    public function __construct()
62
    {
63
        $this->initVar('table_id', XOBJ_DTYPE_INT);
64
        $this->initVar('table_mid', XOBJ_DTYPE_INT);
65
        $this->initVar('table_category', XOBJ_DTYPE_INT);
66
        $this->initVar('table_name', XOBJ_DTYPE_TXTBOX);
67
        $this->initVar('table_solename', XOBJ_DTYPE_TXTBOX);
68
        $this->initVar('table_fieldname', XOBJ_DTYPE_TXTBOX);
69
        $this->initVar('table_nbfields', XOBJ_DTYPE_INT);
70
        $this->initVar('table_order', XOBJ_DTYPE_INT);
71
        $this->initVar('table_image', XOBJ_DTYPE_TXTBOX);
72
        $this->initVar('table_autoincrement', XOBJ_DTYPE_INT);
73
        $this->initVar('table_install', XOBJ_DTYPE_INT);
74
        $this->initVar('table_index', XOBJ_DTYPE_INT);
75
        $this->initVar('table_blocks', XOBJ_DTYPE_INT);
76
        $this->initVar('table_admin', XOBJ_DTYPE_INT);
77
        $this->initVar('table_user', XOBJ_DTYPE_INT);
78
        $this->initVar('table_submenu', XOBJ_DTYPE_INT);
79
        $this->initVar('table_submit', XOBJ_DTYPE_INT);
80
        $this->initVar('table_tag', XOBJ_DTYPE_INT);
81
        $this->initVar('table_broken', XOBJ_DTYPE_INT);
82
        $this->initVar('table_search', XOBJ_DTYPE_INT);
83
        $this->initVar('table_comments', XOBJ_DTYPE_INT);
84
        $this->initVar('table_notifications', XOBJ_DTYPE_INT);
85
        $this->initVar('table_permissions', XOBJ_DTYPE_INT);
86
        $this->initVar('table_rate', XOBJ_DTYPE_INT);
87
        $this->initVar('table_print', XOBJ_DTYPE_INT);
88
        $this->initVar('table_pdf', XOBJ_DTYPE_INT);
89
        $this->initVar('table_rss', XOBJ_DTYPE_INT);
90
        $this->initVar('table_single', XOBJ_DTYPE_INT);
91
        $this->initVar('table_visit', XOBJ_DTYPE_INT);
92
    }
93
94
    /**
95
     * @param string $method
96
     * @param array  $args
97
     *
98
     * @return mixed
99
     */
100
    public function __call($method, $args)
101
    {
102
        $arg = isset($args[0]) ? $args[0] : null;
103
104
        return $this->getVar($method, $arg);
105
    }
106
107
    /**
108
     *  @static function getInstance
109
     *  @param null
110
     * @return TDMCreateTables
111
     */
112
    public static function getInstance()
113
    {
114
        static $instance = false;
115
        if (!$instance) {
116
            $instance = new self();
117
        }
118
119
        return $instance;
120
    }
121
122
    /**
123
     *  @static function getFormTables
124
     *  @param mixed $action
125
     *
126
     * @return XoopsThemeForm
127
     */
128
    public function getFormTables($action = false)
129
    {
130
        if (false === $action) {
131
            $action = $_SERVER['REQUEST_URI'];
132
        }
133
        $tdmcreate = TDMCreateHelper::getInstance();
134
        $isNew = $this->isNew();
135
        $tableName = $this->getVar('table_name');
136
        $tableMid = $this->getVar('table_mid');
137
        $title = $isNew ? sprintf(_AM_TDMCREATE_TABLE_NEW) : sprintf(_AM_TDMCREATE_TABLE_EDIT);
138
139
        xoops_load('XoopsFormLoader');
140
        $form = new XoopsThemeForm($title, 'tableform', $action, 'post', true);
141
        $form->setExtra('enctype="multipart/form-data"');
142
143
        $modules = $tdmcreate->getHandler('modules')->getObjects(null);
0 ignored issues
show
Bug introduced by
The method getObjects cannot be called on $tdmcreate->getHandler('modules') (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
144
        $modulesSelect = new XoopsFormSelect(_AM_TDMCREATE_TABLE_MODULES, 'table_mid', $tableMid);
145
        $modulesSelect->addOption('', _AM_TDMCREATE_TABLE_MODSELOPT);
146
        foreach ($modules as $mod) {
147
            $modulesSelect->addOption($mod->getVar('mod_id'), $mod->getVar('mod_name'));
148
        }
149
        $form->addElement($modulesSelect, true);
150
151
        $tableNameText = new XoopsFormText(_AM_TDMCREATE_TABLE_NAME, 'table_name', 40, 150, $tableName);
152
        $tableNameText->setDescription(_AM_TDMCREATE_TABLE_NAME_DESC);
153
        $form->addElement($tableNameText, true);
154
155
        $tableSoleNameText = new XoopsFormText(_AM_TDMCREATE_TABLE_SOLENAME, 'table_solename', 40, 150, $this->getVar('table_solename'));
156
        $tableSoleNameText->setDescription(_AM_TDMCREATE_TABLE_SOLENAME_DESC);
157
        $form->addElement($tableSoleNameText, true);
158
159
        $radioCategory = $isNew ? 0 : $this->getVar('table_category');
160
        $category = new XoopsFormRadioYN(_AM_TDMCREATE_TABLE_CATEGORY, 'table_category', $radioCategory);
161
        $category->setDescription(_AM_TDMCREATE_TABLE_CATEGORY_DESC);
162
        $form->addElement($category);
163
164
        $tableFieldname = new XoopsFormText(_AM_TDMCREATE_TABLE_FIELDNAME, 'table_fieldname', 30, 50, $this->getVar('table_fieldname'));
165
        $tableFieldname->setDescription(_AM_TDMCREATE_TABLE_FIELDNAME_DESC);
166
        $form->addElement($tableFieldname);
167
168
        $tableNumbFileds = new XoopsFormText(_AM_TDMCREATE_TABLE_NBFIELDS, 'table_nbfields', 10, 25, $this->getVar('table_nbfields'));
169
        $tableNumbFileds->setDescription(_AM_TDMCREATE_TABLE_NBFIELDS_DESC);
170
        $form->addElement($tableNumbFileds, true);
171
172
        if (!$isNew) {
173
            $tableOrder = new XoopsFormText(_AM_TDMCREATE_TABLE_ORDER, 'table_order', 5, 10, $this->getVar('table_order'));
174
            $tableOrder->setDescription(_AM_TDMCREATE_TABLE_ORDER_DESC);
175
            $form->addElement($tableOrder, true);
176
        }
177
178
        $getTableImage = $this->getVar('table_image');
179
        $tableImage = $getTableImage ?: 'blank.gif';
180
        $icons32Directory = '/Frameworks/moduleclasses/icons/32';
181
        $uploadsDirectory = '/uploads/tdmcreate/images/tables';
182
        $iconsDirectory = is_dir(XOOPS_ROOT_PATH . $icons32Directory) ? $icons32Directory : $uploadsDirectory;
183
184
        $imgtray1 = new XoopsFormElementTray(_AM_TDMCREATE_TABLE_IMAGE, '<br>');
185
        $imgpath1 = sprintf(_AM_TDMCREATE_FORMIMAGE_PATH, ".{$iconsDirectory}/");
186
        $imageSelect1 = new XoopsFormSelect($imgpath1, 'table_image', $tableImage, 10);
187
        $imageArray1 = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $iconsDirectory);
188
        foreach ($imageArray1 as $image1) {
189
            $imageSelect1->addOption("{$image1}", $image1);
190
        }
191
        $imageSelect1->setExtra("onchange='showImgSelected(\"image1\", \"table_image\", \"" . $iconsDirectory . '", "", "' . XOOPS_URL . "\")'");
192
        $imgtray1->addElement($imageSelect1, false);
193
        $imgtray1->addElement(new XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $iconsDirectory . '/' . $tableImage . "' name='image1' id='image1' alt='' />"));
194
        $fileseltray1 = new XoopsFormElementTray('', '<br>');
195
        $fileseltray1->addElement(new XoopsFormFile(_AM_TDMCREATE_FORMUPLOAD, 'attachedfile', $tdmcreate->getConfig('maxsize')));
196
        $fileseltray1->addElement(new XoopsFormLabel(''));
197
        $imgtray1->addElement($fileseltray1);
198
        $imgtray1->setDescription(_AM_TDMCREATE_TABLE_IMAGE_DESC);
199
        $form->addElement($imgtray1);
200
201
        $tableAutoincrement = $this->isNew() ? 1 : $this->getVar('table_autoincrement');
202
        $checkTableAutoincrement = new XoopsFormRadioYN(_AM_TDMCREATE_TABLE_AUTO_INCREMENT, 'table_autoincrement', $tableAutoincrement);
203
        $checkTableAutoincrement->setDescription(_AM_TDMCREATE_TABLE_AUTO_INCREMENT_DESC);
204
        $form->addElement($checkTableAutoincrement);
205
206
        $optionsTray = new XoopsFormElementTray(_OPTIONS, '<br>');
207
208
        $tableCheckAll = new XoopsFormCheckBox('', 'tablebox', 1);
209
        $tableCheckAll->addOption('allbox', _AM_TDMCREATE_TABLE_ALL);
210
        $tableCheckAll->setExtra(' onclick="xoopsCheckAll(\'tableform\', \'tablebox\');" ');
211
        $tableCheckAll->setClass('xo-checkall');
212
        $optionsTray->addElement($tableCheckAll);
213
        // Options
214
        $checkbox = new XoopsFormCheckbox(' ', 'table_option', $this->getOptionsTables(), '<br>');
215
        $checkbox->setDescription(_AM_TDMCREATE_OPTIONS_DESC);
216
        foreach ($this->options as $option) {
217
            $checkbox->addOption($option, self::getDefinedLanguage('_AM_TDMCREATE_TABLE_' . mb_strtoupper($option)));
218
        }
219
        $optionsTray->addElement($checkbox);
220
221
        $optionsTray->setDescription(_AM_TDMCREATE_TABLE_OPTIONS_CHECKS_DESC);
222
223
        $form->addElement($optionsTray);
224
225
        $buttonTray = new XoopsFormElementTray(_REQUIRED . ' <sup class="red bold">*</sup>', '');
226
        $buttonTray->addElement(new XoopsFormHidden('op', 'save'));
227
        $buttonTray->addElement(new XoopsFormHidden('table_id', ($isNew ? 0 : $this->getVar('table_id'))));
228
        $buttonTray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
229
        $form->addElement($buttonTray);
230
231
        return $form;
232
    }
233
234
    /**
235
     * Get Values.
236
     *
237
     * @param null $keys
238
     * @param null $format
239
     * @param null $maxDepth
240
     *
241
     * @return array
242
     */
243
    public function getValuesTables($keys = null, $format = null, $maxDepth = null)
244
    {
245
        $ret = $this->getValues($keys, $format, $maxDepth);
246
        // Values
247
        $ret['id'] = $this->getVar('table_id');
248
        $ret['mid'] = $this->getVar('table_mid');
249
        $ret['name'] = ucfirst($this->getVar('table_name'));
250
        $ret['image'] = $this->getVar('table_image');
251
        $ret['nbfields'] = $this->getVar('table_nbfields');
252
        $ret['order'] = $this->getVar('table_order');
253
        $ret['blocks'] = $this->getVar('table_blocks');
254
        $ret['admin'] = $this->getVar('table_admin');
255
        $ret['user'] = $this->getVar('table_user');
256
        $ret['submenu'] = $this->getVar('table_submenu');
257
        $ret['search'] = $this->getVar('table_search');
258
        $ret['comments'] = $this->getVar('table_comments');
259
        $ret['notifications'] = $this->getVar('table_notifications');
260
        $ret['permissions'] = $this->getVar('table_permissions');
261
262
        return $ret;
263
    }
264
265
    /**
266
     * Get Options.
267
     *
268
     * @return array
269
     */
270
    public function getOptionsTables()
271
    {
272
        $retTable = [];
273
        foreach ($this->options as $option) {
274
            if (1 == $this->getVar('table_' . $option)) {
275
                $retTable[] = $option;
276
            }
277
        }
278
279
        return $retTable;
280
    }
281
282
    /**
283
     * Get Defined Language.
284
     *
285
     * @param $lang
286
     *
287
     * @return string
288
     */
289
    private static function getDefinedLanguage($lang)
290
    {
291
        if (defined($lang)) {
292
            return constant($lang);
293
        }
294
295
        return $lang;
296
    }
297
}
298
299
/**
300
 *  @Class TDMCreateTablesHandler
301
 *  @extends XoopsPersistableObjectHandler
302
 */
303
class TDMCreateTablesHandler extends XoopsPersistableObjectHandler
304
{
305
    /**
306
     *  @public function constructor class
307
     *
308
     * @param null|XoopsDatabase $db
309
     */
310
    public function __construct(XoopsDatabase $db)
311
    {
312
        parent::__construct($db, 'tdmcreate_tables', 'TDMCreateTables', 'table_id', 'table_name');
313
    }
314
315
    /**
316
     * @param bool $isNew
317
     *
318
     * @return object
319
     */
320
    public function create($isNew = true)
321
    {
322
        return parent::create($isNew);
323
    }
324
325
    /**
326
     * retrieve a field.
327
     *
328
     * @param int  $i      field id
329
     * @param null $fields
330
     *
331
     * @return mixed reference to the <a href='psi_element://TDMCreateFields'>TDMCreateFields</a> object
332
     *               object
333
     */
334
    public function get($i = null, $fields = null)
335
    {
336
        return parent::get($i, $fields);
337
    }
338
339
    /**
340
     * get inserted id.
341
     *
342
     * @param null
343
     *
344
     * @return int reference to the {@link TDMCreateTables} object
345
     */
346
    public function getInsertId()
347
    {
348
        return $this->db->getInsertId();
349
    }
350
351
    /**
352
     * Get Count Modules.
353
     *
354
     * @param int    $start
355
     * @param int    $limit
356
     * @param string $sort
357
     * @param string $order
358
     *
359
     * @return int
360
     */
361
    public function getCountTables($start = 0, $limit = 0, $sort = 'table_id ASC, table_name', $order = 'ASC')
362
    {
363
        $crCountTables = new CriteriaCompo();
364
        $crCountTables = $this->getTablesCriteria($crCountTables, $start, $limit, $sort, $order);
365
366
        return $this->getCount($crCountTables);
367
    }
368
369
    /**
370
     * Get All Modules.
371
     *
372
     * @param int    $start
373
     * @param int    $limit
374
     * @param string $sort
375
     * @param string $order
376
     *
377
     * @return array
378
     */
379
    public function getAllTables($start = 0, $limit = 0, $sort = 'table_id ASC, table_name', $order = 'ASC')
380
    {
381
        $crAllTables = new CriteriaCompo();
382
        $crAllTables = $this->getTablesCriteria($crAllTables, $start, $limit, $sort, $order);
383
384
        return $this->getAll($crAllTables);
385
    }
386
387
    /**
388
     * Get All Tables By Module Id.
389
     *
390
     * @param        $modId
391
     * @param int    $start
392
     * @param int    $limit
393
     * @param string $sort
394
     * @param string $order
395
     *
396
     * @return array
397
     */
398
    public function getAllTablesByModuleId($modId, $start = 0, $limit = 0, $sort = 'table_order ASC, table_id, table_name', $order = 'ASC')
399
    {
400
        $crAllTablesByModuleId = new CriteriaCompo();
401
        $crAllTablesByModuleId->add(new Criteria('table_mid', $modId));
402
        $crAllTablesByModuleId = $this->getTablesCriteria($crAllTablesByModuleId, $start, $limit, $sort, $order);
403
404
        return $this->getAll($crAllTablesByModuleId);
405
    }
406
407
    /**
408
     * Get Tables Criteria.
409
     *
410
     * @param $crTables
411
     * @param $start
412
     * @param $limit
413
     * @param $sort
414
     * @param $order
415
     *
416
     * @return mixed
417
     */
418
    private function getTablesCriteria($crTables, $start, $limit, $sort, $order)
419
    {
420
        $crTables->setStart($start);
421
        $crTables->setLimit($limit);
422
        $crTables->setSort($sort);
423
        $crTables->setOrder($order);
424
425
        return $crTables;
426
    }
427
}
428