wgfilemanager_search()   F
last analyzed

Complexity

Conditions 23
Paths 9216

Size

Total Lines 122
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 23
eloc 89
nc 9216
nop 5
dl 0
loc 122
rs 0
c 2
b 0
f 0

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
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
22
23
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
24
 */
25
26
use XoopsModules\Wgfilemanager;
27
28
29
/**
30
 * search callback functions
31
 *
32
 * @param $queryarray
33
 * @param $andor
34
 * @param $limit
35
 * @param $offset
36
 * @param $userid
37
 * @return array $itemIds
38
 */
39
function wgfilemanager_search($queryarray, $andor, $limit, $offset, $userid)
40
{
41
    $ret = [];
42
    $helper = \XoopsModules\Wgfilemanager\Helper::getInstance();
43
44
    // search in table file
45
    // search keywords
46
    $elementCount = 0;
47
    $fileHandler = $helper->getHandler('File');
48
    if (\is_array($queryarray)) {
49
        $elementCount = \count($queryarray);
50
    }
51
    if ($elementCount > 0) {
52
        $crKeywords = new \CriteriaCompo();
53
        for ($i = 0; $i  <  $elementCount; $i++) {
54
            $crKeyword = new \CriteriaCompo();
55
            if ('exact' === $andor) {
56
                $crKeyword->add(new \Criteria('name', $queryarray[$i]));
57
                $crKeyword->add(new \Criteria('name', $queryarray[$i]));
58
            } else {
59
                $crKeyword->add(new \Criteria('name', '%' . $queryarray[$i] . '%', 'LIKE'));
60
                $crKeyword->add(new \Criteria('description', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR');
61
            }
62
            $crKeywords->add($crKeyword, $andor);
63
            unset($crKeyword);
64
        }
65
    }
66
67
    // search user(s)
68
    if ($userid && \is_array($userid)) {
69
        $userid = array_map('\intval', $userid);
70
        $crUser = new \CriteriaCompo();
71
        $crUser->add(new \Criteria('submitter', '(' . \implode(',', $userid) . ')', 'IN'), 'OR');
72
    } elseif (is_numeric($userid) && $userid > 0) {
73
        $crUser = new \CriteriaCompo();
74
        $crUser->add(new \Criteria('submitter', $userid), 'OR');
75
    }
76
77
    $crSearch = new \CriteriaCompo();
78
    if (isset($crKeywords)) {
79
        $crSearch->add($crKeywords);
80
    }
81
    if (isset($crUser)) {
82
        $crSearch->add($crUser);
83
    }
84
    $crSearch->setStart($offset);
85
    $crSearch->setLimit($limit);
86
    $crSearch->setSort('date_created');
87
    $crSearch->setOrder('DESC');
88
    $fileAll = $fileHandler->getAll($crSearch);
89
    foreach (\array_keys($fileAll) as $i) {
90
        $ret[] = [
91
            'image'  => 'assets/icons/16/file.png',
92
            'link'   => 'file.php?op=show&amp;file_id=' . $fileAll[$i]->getVar('id') . '&amp;dir_id=' . $fileAll[$i]->getVar('directory_id'),
93
            'title'  => $fileAll[$i]->getVar('name'),
94
            'time'   => $fileAll[$i]->getVar('date_created')
95
        ];
96
    }
97
    unset($crKeywords);
98
    unset($crKeyword);
99
    unset($crUser);
100
    unset($crSearch);
101
102
    // search in table directory
103
    // search keywords
104
    $elementCount = 0;
105
    $directoryHandler = $helper->getHandler('Directory');
106
    if (\is_array($queryarray)) {
107
        $elementCount = \count($queryarray);
108
    }
109
    if ($elementCount > 0) {
110
        $crKeywords = new \CriteriaCompo();
111
        for ($i = 0; $i  <  $elementCount; $i++) {
112
            $crKeyword = new \CriteriaCompo();
113
            if ('exact' === $andor) {
114
                $crKeyword->add(new \Criteria('name', $queryarray[$i]));
115
                $crKeyword->add(new \Criteria('name', $queryarray[$i]));
116
            } else {
117
                $crKeyword->add(new \Criteria('name', '%' . $queryarray[$i] . '%', 'LIKE'));
118
                $crKeyword->add(new \Criteria('description', '%' . $queryarray[$i] . '%', 'LIKE'), 'OR');
119
            }
120
            $crKeywords->add($crKeyword, $andor);
121
            unset($crKeyword);
122
        }
123
    }
124
125
    // search user(s)
126
    if ($userid && \is_array($userid)) {
127
        $userid = array_map('\intval', $userid);
128
        $crUser = new \CriteriaCompo();
129
        $crUser->add(new \Criteria('submitter', '(' . \implode(',', $userid) . ')', 'IN'), 'OR');
130
    } elseif (is_numeric($userid) && $userid > 0) {
131
        $crUser = new \CriteriaCompo();
132
        $crUser->add(new \Criteria('submitter', $userid), 'OR');
133
    }
134
135
    $crSearch = new \CriteriaCompo();
136
    if (isset($crKeywords)) {
137
        $crSearch->add($crKeywords);
138
    }
139
    if (isset($crUser)) {
140
        $crSearch->add($crUser);
141
    }
142
    $crSearch->setStart($offset);
143
    $crSearch->setLimit($limit);
144
    $crSearch->setSort('date_created');
145
    $crSearch->setOrder('DESC');
146
    $directoryAll = $directoryHandler->getAll($crSearch);
147
    foreach (\array_keys($directoryAll) as $i) {
148
        $ret[] = [
149
            'image'  => 'assets/icons/16/folder.png',
150
            'link'   => 'directory.php?op=list&amp;dir_id=' . $directoryAll[$i]->getVar('id'),
151
            'title'  => $directoryAll[$i]->getVar('name'),
152
            'time'   => $directoryAll[$i]->getVar('date_created')
153
        ];
154
    }
155
    unset($crKeywords);
156
    unset($crKeyword);
157
    unset($crUser);
158
    unset($crSearch);
159
160
    return $ret;
161
162
}
163