Passed
Branch master (31be8b)
by Gino
03:04
created

TDMCreateTables::getFormTables()   F

Complexity

Conditions 12
Paths 1024

Size

Total Lines 114
Code Lines 85

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 114
rs 2
cc 12
eloc 85
nc 1024
nop 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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 25.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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       The XOOPS Project http://sourceforge.net/projects/xoops/
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
*  @Class TDMCreateTables
28
*  @extends XoopsObject
29
*/
30
31
/**
32
 * Class TDMCreateTables.
33
 */
34
class TDMCreateTables extends XoopsObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
35
{
36
    /**
37
     * Instance of TDMCreate class.
38
     *
39
     * @var mixed
40
     */
41
    private $tdmcreate = null;
42
43
    /**
44
     * Options.
45
     */
46
    public $options = array(
47
        'install',
48
        'index',
49
        'blocks',
50
        'admin',
51
        'user',
52
        'submenu',
53
        'submit',
54
        'tag',
55
        'broken',
56
        'search',
57
        'comments',
58
        'notifications',
59
        'permissions',
60
        'rate',
61
        'print',
62
        'pdf',
63
        'rss',
64
        'single',
65
        'visit',
66
    );
67
68
    /*
69
    *  @public function constructor class
70
    *  @param null
71
    */
72
    /**
73
     *
74
     */
75
    public function __construct()
76
    {
77
        $this->tdmcreate = TDMCreateHelper::getInstance();
78
        $this->initVar('table_id', XOBJ_DTYPE_INT);
79
        $this->initVar('table_mid', XOBJ_DTYPE_INT);
80
        $this->initVar('table_category', XOBJ_DTYPE_INT);
81
        $this->initVar('table_name', XOBJ_DTYPE_TXTBOX);
82
        $this->initVar('table_solename', XOBJ_DTYPE_TXTBOX);
83
        $this->initVar('table_fieldname', XOBJ_DTYPE_TXTBOX);
84
        $this->initVar('table_nbfields', XOBJ_DTYPE_INT);
85
        $this->initVar('table_order', XOBJ_DTYPE_INT);
86
        $this->initVar('table_image', XOBJ_DTYPE_TXTBOX);
87
        $this->initVar('table_autoincrement', XOBJ_DTYPE_INT);
88
        $this->initVar('table_install', XOBJ_DTYPE_INT);
89
        $this->initVar('table_index', XOBJ_DTYPE_INT);
90
        $this->initVar('table_blocks', XOBJ_DTYPE_INT);
91
        $this->initVar('table_admin', XOBJ_DTYPE_INT);
92
        $this->initVar('table_user', XOBJ_DTYPE_INT);
93
        $this->initVar('table_submenu', XOBJ_DTYPE_INT);
94
        $this->initVar('table_submit', XOBJ_DTYPE_INT);
95
        $this->initVar('table_tag', XOBJ_DTYPE_INT);
96
        $this->initVar('table_broken', XOBJ_DTYPE_INT);
97
        $this->initVar('table_search', XOBJ_DTYPE_INT);
98
        $this->initVar('table_comments', XOBJ_DTYPE_INT);
99
        $this->initVar('table_notifications', XOBJ_DTYPE_INT);
100
        $this->initVar('table_permissions', XOBJ_DTYPE_INT);
101
        $this->initVar('table_rate', XOBJ_DTYPE_INT);
102
        $this->initVar('table_print', XOBJ_DTYPE_INT);
103
        $this->initVar('table_pdf', XOBJ_DTYPE_INT);
104
        $this->initVar('table_rss', XOBJ_DTYPE_INT);
105
        $this->initVar('table_single', XOBJ_DTYPE_INT);
106
        $this->initVar('table_visit', XOBJ_DTYPE_INT);
107
    }
108
109
    /**
110
     * @param string $method
111
     * @param array  $args
112
     *
113
     * @return mixed
114
     */
115
    public function __call($method, $args)
116
    {
117
        $arg = isset($args[0]) ? $args[0] : null;
118
119
        return $this->getVar($method, $arg);
120
    }
121
122
    /*
123
    *  @static function &getInstance
124
    *  @param null
125
    */
126
    /**
127
     * @return TDMCreateTables
128
     */
129
    public static function &getInstance()
130
    {
131
        static $instance = false;
132
        if (!$instance) {
133
            $instance = new self();
134
        }
135
136
        return $instance;
137
    }
138
139
    /*
140
    *  @static function getFormTables
141
    *  @param mixed $action
142
    */
143
    /**
144
     * @param bool $action
145
     *
146
     * @return XoopsThemeForm
147
     */
148
    public function getFormTables($action = false)
149
    {
150
        if ($action === false) {
151
            $action = $_SERVER['REQUEST_URI'];
152
        }
153
154
        $isNew = $this->isNew();
155
        $tableName = $this->getVar('table_name');
156
        $tableMid = $this->getVar('table_mid');
157
        $title = $isNew ? sprintf(_AM_TDMCREATE_TABLE_NEW) : sprintf(_AM_TDMCREATE_TABLE_EDIT);
158
159
        xoops_load('XoopsFormLoader');
160
        $form = new XoopsThemeForm($title, 'tableform', $action, 'post', true);
161
        $form->setExtra('enctype="multipart/form-data"');
162
        //
163
        $modules = $this->tdmcreate->getHandler('modules')->getObjects(null);
164
        $modulesSelect = new XoopsFormSelect(_AM_TDMCREATE_TABLE_MODULES, 'table_mid', $tableMid);
165
        $modulesSelect->addOption('', _AM_TDMCREATE_TABLE_MODSELOPT);
166
        foreach ($modules as $mod) {
167
            //$modulesSelect->addOptionArray();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
            $modulesSelect->addOption($mod->getVar('mod_id'), $mod->getVar('mod_name'));
169
        }
170
        $form->addElement($modulesSelect, true);
171
        //
172
        $tableNameText = new XoopsFormText(_AM_TDMCREATE_TABLE_NAME, 'table_name', 40, 150, $tableName);
173
        $tableNameText->setDescription(_AM_TDMCREATE_TABLE_NAME_DESC);
174
        $form->addElement($tableNameText, true);
175
        //
176
        $tableSoleNameText = new XoopsFormText(_AM_TDMCREATE_TABLE_SOLENAME, 'table_solename', 40, 150, $this->getVar('table_solename'));
177
        $tableSoleNameText->setDescription(_AM_TDMCREATE_TABLE_SOLENAME_DESC);
178
        $form->addElement($tableSoleNameText, true);
179
        //
180
        $tablesHandler = &$this->tdmcreate->getHandler('tables');
181
        $criteria = new CriteriaCompo(new Criteria('table_category', 0), 'AND');
182
        $criteria->add(new Criteria('table_mid', $tableMid), 'AND');
183
        $criteria->add(new Criteria('table_name', $tableName));
184
        $tableCategory = $tablesHandler->getCount($criteria);
0 ignored issues
show
Unused Code introduced by
$tableCategory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
185
        unset($criteria);
186
        //if ( ($tableCategory == 0) || $isNew ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
187
        $radioCategory = $isNew ? 0 : $this->getVar('table_category');
188
        $category = new XoopsFormRadioYN(_AM_TDMCREATE_TABLE_CATEGORY, 'table_category', $radioCategory);
189
        $category->setDescription(_AM_TDMCREATE_TABLE_CATEGORY_DESC);
190
        $form->addElement($category);
191
        //}
192
        //
193
        $tableFieldname = new XoopsFormText(_AM_TDMCREATE_TABLE_FIELDNAME, 'table_fieldname', 30, 50, $this->getVar('table_fieldname'));
194
        $tableFieldname->setDescription(_AM_TDMCREATE_TABLE_FIELDNAME_DESC);
195
        $form->addElement($tableFieldname);
196
        //
197
        $tableNumbFileds = new XoopsFormText(_AM_TDMCREATE_TABLE_NBFIELDS, 'table_nbfields', 10, 25, $this->getVar('table_nbfields'));
198
        $tableNumbFileds->setDescription(_AM_TDMCREATE_TABLE_NBFIELDS_DESC);
199
        $form->addElement($tableNumbFileds, true);
200
        //
201
        if (!$isNew) {
202
            $tableOrder = new XoopsFormText(_AM_TDMCREATE_TABLE_ORDER, 'table_order', 5, 10, $this->getVar('table_order'));
203
            $tableOrder->setDescription(_AM_TDMCREATE_TABLE_ORDER_DESC);
204
            $form->addElement($tableOrder, true);
205
        }
206
        //
207
        $getTableImage = $this->getVar('table_image');
208
        $tableImage = $getTableImage ?: 'blank.gif';
209
        $icons32Directory = '/Frameworks/moduleclasses/icons/32';
210
        $uploadsDirectory = '/uploads/tdmcreate/images/tables';
211
        $iconsDirectory = is_dir(XOOPS_ROOT_PATH.$icons32Directory) ? $icons32Directory : $uploadsDirectory;
212
        //
213
        $imgtray1 = new XoopsFormElementTray(_AM_TDMCREATE_TABLE_IMAGE, '<br />');
214
        $imgpath1 = sprintf(_AM_TDMCREATE_FORMIMAGE_PATH, ".{$iconsDirectory}/");
215
        $imageSelect1 = new XoopsFormSelect($imgpath1, 'table_image', $tableImage, 10);
216
        $imageArray1 = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH.$iconsDirectory);
217
        foreach ($imageArray1 as $image1) {
218
            $imageSelect1->addOption("{$image1}", $image1);
219
        }
220
        $imageSelect1->setExtra("onchange='showImgSelected(\"image1\", \"table_image\", \"".$iconsDirectory.'", "", "'.XOOPS_URL."\")'");
221
        $imgtray1->addElement($imageSelect1, false);
222
        $imgtray1->addElement(new XoopsFormLabel('', "<br /><img src='".XOOPS_URL.'/'.$iconsDirectory.'/'.$tableImage."' name='image1' id='image1' alt='' />"));
223
        $fileseltray1 = new XoopsFormElementTray('', '<br />');
224
        $fileseltray1->addElement(new XoopsFormFile(_AM_TDMCREATE_FORMUPLOAD, 'attachedfile', $this->tdmcreate->getConfig('maxsize')));
225
        $fileseltray1->addElement(new XoopsFormLabel(''));
226
        $imgtray1->addElement($fileseltray1);
227
        $imgtray1->setDescription(_AM_TDMCREATE_TABLE_IMAGE_DESC);
228
        $form->addElement($imgtray1);
229
        //
230
        $tableAutoincrement = $this->isNew() ? 1 : $this->getVar('table_autoincrement');
231
        $checkTableAutoincrement = new XoopsFormRadioYN(_AM_TDMCREATE_TABLE_AUTO_INCREMENT, 'table_autoincrement', $tableAutoincrement);
232
        $checkTableAutoincrement->setDescription(_AM_TDMCREATE_TABLE_AUTO_INCREMENT_DESC);
233
        $form->addElement($checkTableAutoincrement);
234
        //
235
        $optionsTray = new XoopsFormElementTray(_OPTIONS, '<br />');
236
        //
237
        $tableCheckAll = new XoopsFormCheckBox('', 'tablebox', 1);
238
        $tableCheckAll->addOption('allbox', _AM_TDMCREATE_TABLE_ALL);
239
        $tableCheckAll->setExtra(' onclick="xoopsCheckAll(\'tableform\', \'tablebox\');" ');
240
        $tableCheckAll->setClass('xo-checkall');
241
        $optionsTray->addElement($tableCheckAll);
242
        // Options
243
        $checkbox = new XoopsFormCheckbox(' ', 'table_option', $this->getOptionsTables(), '<br />');
244
        $checkbox->setDescription(_AM_TDMCREATE_OPTIONS_DESC);
245
        foreach ($this->options as $option) {
246
            $checkbox->addOption($option, self::getDefinedLanguage('_AM_TDMCREATE_TABLE_'.strtoupper($option)));
247
        }
248
        $optionsTray->addElement($checkbox);
249
        //
250
        $optionsTray->setDescription(_AM_TDMCREATE_TABLE_OPTIONS_CHECKS_DESC);
251
        //
252
        $form->addElement($optionsTray);
253
        //
254
        $buttonTray = new XoopsFormElementTray(_REQUIRED.' <sup class="red bold">*</sup>', '');
255
        $buttonTray->addElement(new XoopsFormHidden('op', 'save'));
256
        $buttonTray->addElement(new XoopsFormHidden('table_id', ($isNew ? 0 : $this->getVar('table_id'))));
257
        $buttonTray->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
258
        $form->addElement($buttonTray);
259
260
        return $form;
261
    }
262
263
    /**
264
     * Get Values.
265
     */
266
    public function getValuesTables($keys = null, $format = null, $maxDepth = null)
267
    {
268
        $ret = parent::getValues($keys, $format, $maxDepth);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getValues() instead of getValuesTables()). Are you sure this is correct? If so, you might want to change this to $this->getValues().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
269
        // Values
270
        $ret['id'] = $this->getVar('table_id');
271
        $ret['mid'] = $this->getVar('table_mid');
272
        $ret['name'] = ucfirst($this->getVar('table_name'));
273
        $ret['image'] = $this->getVar('table_image');
274
        $ret['nbfields'] = $this->getVar('table_nbfields');
275
        $ret['order'] = $this->getVar('table_order');
276
        $ret['blocks'] = $this->getVar('table_blocks');
277
        $ret['admin'] = $this->getVar('table_admin');
278
        $ret['user'] = $this->getVar('table_user');
279
        $ret['submenu'] = $this->getVar('table_submenu');
280
        $ret['search'] = $this->getVar('table_search');
281
        $ret['comments'] = $this->getVar('table_comments');
282
        $ret['notifications'] = $this->getVar('table_notifications');
283
        $ret['permissions'] = $this->getVar('table_permissions');
284
285
        return $ret;
286
    }
287
288
    /**
289
     * Get Options.
290
     */
291
    /**
292
     * @param $key
293
     *
294
     * @return string
295
     */
296
    public function getOptionsTables()
297
    {
298
        $retTable = array();
299
        foreach ($this->options as $option) {
300
            if ($this->getVar('table_'.$option) == 1) {
301
                array_push($retTable, $option);
302
            }
303
        }
304
305
        return $retTable;
306
    }
307
308
    /**
309
     * Get Defined Language.
310
     */
311
    /**
312
     * @param $lang
313
     *
314
     * @return string
315
     */
316
    private static function getDefinedLanguage($lang)
317
    {
318
        if (defined($lang)) {
319
            return constant($lang);
320
        }
321
322
        return $lang;
323
    }
324
}
325
326
/*
327
*  @Class TDMCreateTablesHandler
328
*  @extends XoopsPersistableObjectHandler
329
*/
330
331
/**
332
 * Class TDMCreateTablesHandler.
333
 */
334
class TDMCreateTablesHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
335
{
336
    /*
337
    *  @public function constructor class
338
    *  @param mixed $db
339
    */
340
    /**
341
     * @param null|object $db
342
     */
343
    public function __construct(&$db)
344
    {
345
        parent::__construct($db, 'tdmcreate_tables', 'tdmcreatetables', 'table_id', 'table_name');
346
    }
347
348
    /**
349
     * @param bool $isNew
350
     *
351
     * @return object
352
     */
353
    public function &create($isNew = true)
354
    {
355
        return parent::create($isNew);
356
    }
357
358
    /**
359
     * retrieve a field.
360
     *
361
     * @param int  $i      field id
362
     * @param null $fields
363
     *
364
     * @return mixed reference to the <a href='psi_element://TDMCreateFields'>TDMCreateFields</a> object
365
     *               object
366
     */
367
    public function &get($i = null, $fields = null)
368
    {
369
        return parent::get($i, $fields);
370
    }
371
372
    /**
373
     * get inserted id.
374
     *
375
     * @param null
376
     *
377
     * @return int reference to the {@link TDMCreateTables} object
378
     */
379
    public function &getInsertId()
380
    {
381
        return $this->db->getInsertId();
382
    }
383
384
    /**
385
     * insert a new field in the database.
386
     *
387
     * @param object $field reference to the {@link TDMCreateFields} object
388
     * @param bool   $force
389
     *
390
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
391
     */
392
    public function &insert(&$field, $force = false)
393
    {
394
        if (!parent::insert($field, $force)) {
395
            return false;
396
        }
397
398
        return true;
399
    }
400
401
    /**
402
     * Get Count Modules.
403
     */
404
    public function getCountTables($start = 0, $limit = 0, $sort = 'table_id ASC, table_name', $order = 'ASC')
405
    {
406
        $criteriaCountTables = new CriteriaCompo();
407
        $criteriaCountTables = $this->getTablesCriteria($criteriaCountTables, $start, $limit, $sort, $order);
408
409
        return $this->getCount($criteriaCountTables);
410
    }
411
412
    /**
413
     * Get All Modules.
414
     */
415
    public function getAllTables($start = 0, $limit = 0, $sort = 'table_id ASC, table_name', $order = 'ASC')
416
    {
417
        $criteriaAllTables = new CriteriaCompo();
418
        $criteriaAllTables = $this->getTablesCriteria($criteriaAllTables, $start, $limit, $sort, $order);
419
420
        return $this->getAll($criteriaAllTables);
421
    }
422
423
    /**
424
     * Get All Tables By Module Id.
425
     */
426
    public function getAllTablesByModuleId($modId, $start = 0, $limit = 0, $sort = 'table_order ASC, table_id, table_name', $order = 'ASC')
427
    {
428
        $criteriaAllTablesByModuleId = new CriteriaCompo();
429
        $criteriaAllTablesByModuleId->add(new Criteria('table_mid', $modId));
430
        $criteriaAllTablesByModuleId = $this->getTablesCriteria($criteriaAllTablesByModuleId, $start, $limit, $sort, $order);
431
432
        return $this->getAll($criteriaAllTablesByModuleId);
433
    }
434
435
    /**
436
     * Get Tables Criteria.
437
     */
438
    private function getTablesCriteria($criteriaTables, $start, $limit, $sort, $order)
439
    {
440
        $criteriaTables->setStart($start);
441
        $criteriaTables->setLimit($limit);
442
        $criteriaTables->setSort($sort);
443
        $criteriaTables->setOrder($order);
444
445
        return $criteriaTables;
446
    }
447
}
448