b_mymodule2_articles_show()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 63
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 48
nc 12
nop 1
dl 0
loc 63
rs 7.8901
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_articles_show($options)
30
{
31
    include_once XOOPS_ROOT_PATH.'/modules/mymodule2/class/articles.php';
32
    $myts = MyTextSanitizer::getInstance();
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
    $articlesHandler = $helper->getHandler('articles');
40
    $criteria = new \CriteriaCompo();
41
    array_shift($options);
42
    array_shift($options);
43
    array_shift($options);
44
	switch($typeBlock)
45
	{
46
		// For the block: articles last
47
		case 'last':
48
			//$criteria->add(new \Criteria('art_display', 1));
49
			$criteria->setSort('art_created');
50
			$criteria->setOrder('DESC');
51
		break;
52
		// For the block: articles new
53
		case 'new':
54
			//$criteria->add(new \Criteria('art_display', 1));
55
			$criteria->add(new \Criteria('art_created', strtotime(date(_SHORTDATESTRING)), '>='));
56
			$criteria->add(new \Criteria('art_created', strtotime(date(_SHORTDATESTRING))+86400, '<='));
57
			$criteria->setSort('art_created');
58
			$criteria->setOrder('ASC');
59
		break;
60
		// For the block: articles hits
61
		case 'hits':
62
            $criteria->setSort('art_hits');
63
            $criteria->setOrder('DESC');
64
        break;
65
		// For the block: articles top
66
		case 'top':
67
            $criteria->setSort('art_top');
68
            $criteria->setOrder('ASC');
69
        break;
70
		// For the block: articles random
71
		case 'random':
72
			//$criteria->add(new \Criteria('art_display', 1));
73
			$criteria->setSort('RAND()');
74
		break;
75
	}
76
    $criteria->setLimit($limit);
77
    $articlesAll = $articlesHandler->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
    $articlesAll = $articlesHandler->getAll($criteria);
Loading history...
78
	unset($criteria);
79
	if (count($articlesAll) > 0) {
80
        foreach(array_keys($articlesAll) as $i)
81
        {
82
            $block[$i]['cat'] = $articlesAll[$i]->getVar('art_cat');
83
            $block[$i]['title'] = $myts->htmlSpecialChars($articlesAll[$i]->getVar('art_title'));
84
		    $block[$i]['descr'] = strip_tags($articlesAll[$i]->getVar('art_descr'));
85
            $block[$i]['img'] = $articlesAll[$i]->getVar('art_img');
86
            $block[$i]['file'] = $articlesAll[$i]->getVar('art_file');
87
            $block[$i]['created'] = formatTimeStamp($articlesAll[$i]->getVar('art_created'));
88
		    $block[$i]['submitter'] = \XoopsUser::getUnameFromId($articlesAll[$i]->getVar('art_submitter'));
89
        }
90
    }
91
    return $block;
92
}
93
94
// Function edit block
95
function b_mymodule2_articles_edit($options)
96
{
97
    include_once XOOPS_ROOT_PATH.'/modules/mymodule2/class/articles.php';
98
    $helper = Helper::getInstance();
99
    $articlesHandler = $helper->getHandler('articles');
100
    $GLOBALS['xoopsTpl']->assign('mymodule2_upload_url', MYMODULE2_UPLOAD_URL);
101
    $form  = _MB_MYMODULE2_DISPLAY;
102
    $form .= "<input type='hidden' name='options[0]' value='".$options[0]."' />";
103
    $form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='" . $options[1] . "' />&nbsp;<br>";
104
    $form .= _MB_MYMODULE2_TITLE_LENGTH." : <input type='text' name='options[2]' size='5' maxlength='255' value='" . $options[2] . "' /><br><br>";
105
    array_shift($options);
106
    array_shift($options);
107
    array_shift($options);
108
    $criteria = new \CriteriaCompo();
109
    $criteria->add(new \Criteria('art_id', 0, '!='));
110
    $criteria->setSort('art_id');
111
    $criteria->setOrder('ASC');
112
    $articlesAll = $articlesHandler->getAll($criteria);
113
    unset($criteria);
114
    $form .= _MB_MYMODULE2_ARTICLES_TO_DISPLAY."<br><select name='options[]' multiple='multiple' size='5'>";
115
    $form .= "<option value='0' " . (in_array(0, $options) === false ? '' : "selected='selected'") . '>' . _MB_MYMODULE2_ALL_ARTICLES . '</option>';
116
    foreach (array_keys($articlesAll) as $i) {
117
        $art_id = $articlesAll[$i]->getVar('art_id');
118
        $form .= "<option value='" . $art_id . "' " . (in_array($art_id, $options) === false ? '' : "selected='selected'") . '>' . $articlesAll[$i]->getVar('art_title') . '</option>';
119
    }
120
    $form .= '</select>';
121
    return $form;
122
}