b_mymodule2_testfields_show()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 56
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 41
nc 12
nop 1
dl 0
loc 56
rs 8.0195
c 1
b 0
f 1

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * My Module 2 module for xoops
14
 *
15
 * @copyright     2020 XOOPS Project (https://xooops.org)
16
 * @license        GPL 2.0 or later
17
 * @package        mymodule2
18
 * @since          1.0
19
 * @min_xoops      2.5.9
20
 * @author         TDM XOOPS - Email:<[email protected]> - Website:<http://xoops.org>
21
 */
22
23
use XoopsModules\Mymodule2;
24
use XoopsModules\Mymodule2\Helper;
25
use XoopsModules\Mymodule2\Constants;
26
27
include_once XOOPS_ROOT_PATH.'/modules/mymodule2/include/common.php';
28
// Function show block
29
function b_mymodule2_testfields_show($options)
30
{
31
    include_once XOOPS_ROOT_PATH.'/modules/mymodule2/class/testfields.php';
32
    $myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
33
    $GLOBALS['xoopsTpl']->assign('mymodule2_upload_url', MYMODULE2_UPLOAD_URL);
34
    $block       = array();
35
    $typeBlock   = $options[0];
36
    $limit       = $options[1];
37
    $lenghtTitle = $options[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $lenghtTitle is dead and can be removed.
Loading history...
38
    $helper = Helper::getInstance();
39
    $testfieldsHandler = $helper->getHandler('testfields');
40
    $criteria = new \CriteriaCompo();
41
    array_shift($options);
42
    array_shift($options);
43
    array_shift($options);
44
	switch($typeBlock)
45
	{
46
		// For the block: testfields last
47
		case 'last':
48
			//$criteria->add(new \Criteria('tf_display', 1));
49
			$criteria->setSort('tf_created');
50
			$criteria->setOrder('DESC');
51
		break;
52
		// For the block: testfields new
53
		case 'new':
54
			//$criteria->add(new \Criteria('tf_display', 1));
55
			$criteria->add(new \Criteria('tf_created', strtotime(date(_SHORTDATESTRING)), '>='));
56
			$criteria->add(new \Criteria('tf_created', strtotime(date(_SHORTDATESTRING))+86400, '<='));
57
			$criteria->setSort('tf_created');
58
			$criteria->setOrder('ASC');
59
		break;
60
		// For the block: testfields hits
61
		case 'hits':
62
            $criteria->setSort('tf_hits');
63
            $criteria->setOrder('DESC');
64
        break;
65
		// For the block: testfields top
66
		case 'top':
67
            $criteria->setSort('tf_top');
68
            $criteria->setOrder('ASC');
69
        break;
70
		// For the block: testfields random
71
		case 'random':
72
			//$criteria->add(new \Criteria('tf_display', 1));
73
			$criteria->setSort('RAND()');
74
		break;
75
	}
76
    $criteria->setLimit($limit);
77
    $testfieldsAll = $testfieldsHandler->getAll($criteria);
0 ignored issues
show
Bug introduced by
The method getAll() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoUserHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

77
    /** @scrutinizer ignore-call */ 
78
    $testfieldsAll = $testfieldsHandler->getAll($criteria);
Loading history...
78
	unset($criteria);
79
	if (count($testfieldsAll) > 0) {
80
        foreach(array_keys($testfieldsAll) as $i)
81
        {
82
        }
83
    }
84
    return $block;
85
}
86
87
// Function edit block
88
function b_mymodule2_testfields_edit($options)
89
{
90
    include_once XOOPS_ROOT_PATH.'/modules/mymodule2/class/testfields.php';
91
    $helper = Helper::getInstance();
92
    $testfieldsHandler = $helper->getHandler('testfields');
93
    $GLOBALS['xoopsTpl']->assign('mymodule2_upload_url', MYMODULE2_UPLOAD_URL);
94
    $form  = _MB_MYMODULE2_DISPLAY;
95
    $form .= "<input type='hidden' name='options[0]' value='".$options[0]."' />";
96
    $form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='" . $options[1] . "' />&nbsp;<br>";
97
    $form .= _MB_MYMODULE2_TITLE_LENGTH." : <input type='text' name='options[2]' size='5' maxlength='255' value='" . $options[2] . "' /><br><br>";
98
    array_shift($options);
99
    array_shift($options);
100
    array_shift($options);
101
    $criteria = new \CriteriaCompo();
102
    $criteria->add(new \Criteria('tf_id', 0, '!='));
103
    $criteria->setSort('tf_id');
104
    $criteria->setOrder('ASC');
105
    $testfieldsAll = $testfieldsHandler->getAll($criteria);
106
    unset($criteria);
107
    $form .= _MB_MYMODULE2_TESTFIELDS_TO_DISPLAY."<br><select name='options[]' multiple='multiple' size='5'>";
108
    $form .= "<option value='0' " . (in_array(0, $options) === false ? '' : "selected='selected'") . '>' . _MB_MYMODULE2_ALL_TESTFIELDS . '</option>';
109
    foreach (array_keys($testfieldsAll) as $i) {
110
        $tf_id = $testfieldsAll[$i]->getVar('tf_id');
111
        $form .= "<option value='" . $tf_id . "' " . (in_array($tf_id, $options) === false ? '' : "selected='selected'") . '>' . $testfieldsAll[$i]->getVar('tf_text') . '</option>';
112
    }
113
    $form .= '</select>';
114
    return $form;
115
}