Passed
Push — master ( e279b4...6912d9 )
by Goffy
03:06
created

b_wgfilemanager_directory_show()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 30
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 52
rs 9.1288

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
declare(strict_types=1);
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wgFileManager module for xoops
17
 *
18
 * @copyright    2021 XOOPS Project (https://xoops.org)
19
 * @license      GPL 2.0 or later
20
 * @package      wgfilemanager
21
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
22
 */
23
24
use XoopsModules\Wgfilemanager;
25
use XoopsModules\Wgfilemanager\Helper;
26
use XoopsModules\Wgfilemanager\Constants;
27
28
require_once \XOOPS_ROOT_PATH . '/modules/wgfilemanager/include/common.php';
29
30
/**
31
 * Function show block
32
 * @param  $options
33
 * @return array
34
 */
35
function b_wgfilemanager_directory_show($options)
36
{
37
    $block       = [];
38
    $typeBlock   = $options[0];
39
    $limit       = $options[1];
40
    //$lenghtTitle = $options[2];
41
    $helper      = Helper::getInstance();
42
    $directoryHandler = $helper->getHandler('Directory');
43
    $crDirectory = new \CriteriaCompo();
44
    \array_shift($options);
45
    \array_shift($options);
46
    \array_shift($options);
47
48
    switch ($typeBlock) {
49
        case 'last':
50
        default:
51
            // For the block: directory last
52
            $crDirectory->setSort('date_created');
53
            $crDirectory->setOrder('DESC');
54
            break;
55
        case 'new':
56
            // For the block: directory new
57
            // new since last week: 7 * 24 * 60 * 60 = 604800
58
            $crDirectory->add(new \Criteria('date_created', \time() - 604800, '>='));
59
            $crDirectory->add(new \Criteria('date_created', \time(), '<='));
60
            $crDirectory->setSort('date_created');
61
            $crDirectory->setOrder('ASC');
62
            break;
63
    }
64
65
    $crDirectory->setLimit($limit);
66
    $directoryAll = $directoryHandler->getAll($crDirectory);
67
    unset($crDirectory);
68
    if (\count($directoryAll) > 0) {
69
        foreach (\array_keys($directoryAll) as $i) {
70
            /**
71
             * If you want to use the parameter for limits you have to adapt the line where it should be applied
72
             * e.g. change
73
             *     $block[$i]['title'] = $directoryAll[$i]->getVar('art_title');
74
             * into
75
             *     $myTitle = $directoryAll[$i]->getVar('art_title');
76
             *     if ($limit > 0) {
77
             *         $myTitle = \substr($myTitle, 0, (int)$limit);
78
             *     }
79
             *     $block[$i]['title'] =  $myTitle;
80
             */
81
            $block[$i]['id'] = $directoryAll[$i]->getVar('id');
82
            $block[$i]['name'] = \htmlspecialchars($directoryAll[$i]->getVar('name'), ENT_QUOTES | ENT_HTML5);
83
        }
84
    }
85
    $GLOBALS['xoopsTpl']->assign('wgfilemanager_url', \WGFILEMANAGER_URL);
86
    return $block;
87
88
}
89
90
/**
91
 * Function edit block
92
 * @param  $options
93
 * @return string
94
 */
95
function b_wgfilemanager_directory_edit($options)
96
{
97
    $GLOBALS['xoopsTpl']->assign('wgfilemanager_upload_url', \WGFILEMANAGER_UPLOAD_URL);
98
    $form = \_MB_WGFILEMANAGER_DISPLAY . ' : ';
99
    $form .= "<input type='hidden' name='options[0]' value='".$options[0]."' >";
100
    $form .= "<input type='text' name='options[1]' size='5' maxlength='255' value='" . $options[1] . "' >&nbsp;<br>";
101
    $form .= \_MB_WGFILEMANAGER_TITLE_LENGTH . " : <input type='text' name='options[2]' size='5' maxlength='255' value='" . $options[2] . "' ><br><br>";
102
    \array_shift($options);
103
    \array_shift($options);
104
    \array_shift($options);
105
106
    $crDirectory = new \CriteriaCompo();
107
    $crDirectory->add(new \Criteria('id', 0, '!='));
108
    $crDirectory->setSort('id');
109
    $crDirectory->setOrder('ASC');
110
111
    /**
112
     * If you want to filter your results by e.g. a category used in yourdirectory
113
     * then you can activate the following code, but you have to change it according your category
114
     */
115
    /*
116
    $helper = Helper::getInstance();
117
    $directoryHandler = $helper->getHandler('Directory');
118
    $directoryAll = $directoryHandler->getAll($crDirectory);
119
    unset($crDirectory);
120
    $form .= \_MB_WGFILEMANAGER_DIRECTORY_TO_DISPLAY . "<br><select name='options[]' multiple='multiple' size='5'>";
121
    $form .= "<option value='0' " . (!\in_array(0, $options) && !\in_array('0', $options) ? '' : "selected='selected'") . '>' . \_MB_WGFILEMANAGER_ALL_DIRECTORY . '</option>';
122
    foreach (\array_keys($directoryAll) as $i) {
123
        $dir_id = $directoryAll[$i]->getVar('id');
124
        $form .= "<option value='" . $dir_id . "' " . (!\in_array($dir_id, $options) ? '' : "selected='selected'") . '>' . $directoryAll[$i]->getVar('name') . '</option>';
125
    }
126
    $form .= '</select>';
127
128
    */
129
    return $form;
130
131
}
132