b_mymodule3_testfields_show()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 62
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 8
eloc 49
nc 12
nop 1
dl 0
loc 62
rs 7.8682
c 1
b 1
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 3 module for xoops
14
 *
15
 * @copyright     2020 XOOPS Project (https://xooops.org)
16
 * @license        GPL 2.0 or later
17
 * @package        mymodule3
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\Mymodule3;
24
use XoopsModules\Mymodule3\Helper;
25
use XoopsModules\Mymodule3\Constants;
26
27
include_once XOOPS_ROOT_PATH . '/modules/mymodule3/include/common.php';
28
29
/**
30
 * Function show block
31
 * @param  $options 
32
 * @return array
33
 */
34
function b_mymodule3_testfields_show($options)
35
{
36
	include_once XOOPS_ROOT_PATH . '/modules/mymodule3/class/testfields.php';
37
$myts = MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
38
	$GLOBALS['xoopsTpl']->assign('mymodule3_upload_url', MYMODULE3_UPLOAD_URL);
39
	$block       = [];
40
	$typeBlock   = $options[0];
41
	$limit       = $options[1];
42
	$lenghtTitle = $options[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $lenghtTitle is dead and can be removed.
Loading history...
43
	$helper      = Helper::getInstance();
44
	$testfieldsHandler = $helper->getHandler('testfields');
45
	$crTestfields = new \CriteriaCompo();
46
	array_shift($options);
47
	array_shift($options);
48
	array_shift($options);
49
50
	switch($typeBlock) {
51
		case 'last':
52
		default:
53
			// For the block: testfields last
54
			$crTestfields->add( new \Criteria( 'tf_status', Constants::PERM_GLOBAL_VIEW ) );
55
			$crTestfields->setSort( 'tf_date' );
56
			$crTestfields->setOrder( 'DESC' );
57
		break;
58
		case 'new':
59
			// For the block: testfields new
60
			$crTestfields->add( new \Criteria( 'tf_status', Constants::PERM_GLOBAL_VIEW ) );
61
			$crTestfields->add( new \Criteria( 'tf_date', strtotime(date(_SHORTDATESTRING)), '>=' ) );
62
			$crTestfields->add( new \Criteria( 'tf_date', strtotime(date(_SHORTDATESTRING))+86400, '<=' ) );
63
			$crTestfields->setSort( 'tf_date' );
64
			$crTestfields->setOrder( 'ASC' );
65
		break;
66
		case 'hits':
67
			// For the block: testfields hits
68
			$crTestfields->add( new \Criteria( 'tf_status', Constants::PERM_GLOBAL_VIEW ) );
69
			$crTestfields->setSort( 'tf_hits' );
70
			$crTestfields->setOrder( 'DESC' );
71
		break;
72
		case 'top':
73
			// For the block: testfields top
74
			$crTestfields->add( new \Criteria( 'tf_status', Constants::PERM_GLOBAL_VIEW ) );
75
			$crTestfields->add( new \Criteria( 'tf_date', strtotime(date(_SHORTDATESTRING))+86400, '<=' ) );
76
			$crTestfields->setSort( 'tf_top' );
77
			$crTestfields->setOrder( 'ASC' );
78
		break;
79
		case 'random':
80
			// For the block: testfields random
81
			$crTestfields->add( new \Criteria( 'tf_status', Constants::PERM_GLOBAL_VIEW ) );
82
			$crTestfields->add( new \Criteria( 'tf_date', strtotime(date(_SHORTDATESTRING))+86400, '<=' ) );
83
			$crTestfields->setSort( 'RAND()' );
84
		break;
85
	}
86
87
	$crTestfields->setLimit( $limit );
88
	$testfieldsAll = $testfieldsHandler->getAll($crTestfields);
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

88
	/** @scrutinizer ignore-call */ 
89
 $testfieldsAll = $testfieldsHandler->getAll($crTestfields);
Loading history...
89
	unset($crTestfields);
90
	if (count($testfieldsAll) > 0) {
91
		foreach(array_keys($testfieldsAll) as $i) {
92
		}
93
	}
94
95
	return $block;
96
97
}
98
99
/**
100
 * Function edit block
101
 * @param  $options 
102
 * @return string
103
 */
104
function b_mymodule3_testfields_edit($options)
105
{
106
	include_once XOOPS_ROOT_PATH . '/modules/mymodule3/class/testfields.php';
107
	$helper = Helper::getInstance();
108
	$testfieldsHandler = $helper->getHandler('testfields');
109
	$GLOBALS['xoopsTpl']->assign('mymodule3_upload_url', MYMODULE3_UPLOAD_URL);
110
	$form = _MB_MYMODULE3_DISPLAY;
111
	$form .= "<input type='hidden' name='options[0]' value='".$options[0]."' />";
112
	$form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='" . $options[1] . "' />&nbsp;<br>";
113
	$form .= _MB_MYMODULE3_TITLE_LENGTH . " : <input type='text' name='options[2]' size='5' maxlength='255' value='" . $options[2] . "' /><br><br>";
114
	array_shift($options);
115
	array_shift($options);
116
	array_shift($options);
117
118
	$crTestfields = new \CriteriaCompo();
119
	$crTestfields->add( new \Criteria( 'tf_id', 0, '!=' ) );
120
	$crTestfields->setSort( 'tf_id' );
121
	$crTestfields->setOrder( 'ASC' );
122
	$testfieldsAll = $testfieldsHandler->getAll($crTestfields);
123
	unset($crTestfields);
124
	$form .= _MB_MYMODULE3_TESTFIELDS_TO_DISPLAY . "<br><select name='options[]' multiple='multiple' size='5'>";
125
	$form .= "<option value='0' " . (in_array(0, $options) == false ? '' : "selected='selected'") . '>' . _MB_MYMODULE3_ALL_TESTFIELDS . '</option>';
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
126
	foreach(array_keys($testfieldsAll) as $i) {
127
		$tf_id = $testfieldsAll[$i]->getVar('tf_id');
128
		$form .= "<option value='" . $tf_id . "' " . (in_array($tf_id, $options) == false ? '' : "selected='selected'") . '>' . $testfieldsAll[$i]->getVar('tf_text') . '</option>';
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
129
	}
130
	$form .= '</select>';
131
132
	return $form;
133
134
}
135