Passed
Push — master ( a42092...8afea9 )
by Goffy
03:23
created

FileHandler::getFavFileList()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
c 0
b 0
f 0
nc 6
nop 0
dl 0
loc 23
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace XoopsModules\Wgfilemanager;
7
8
/*
9
 You may not change or alter any portion of this comment or credits
10
 of supporting developers from this source code or any supporting source code
11
 which is considered copyrighted (c) material of the original comment or credit authors.
12
13
 This program is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
*/
17
18
/**
19
 * wgFileManager module for xoops
20
 *
21
 * @copyright    2021 XOOPS Project (https://xoops.org)
22
 * @license      GPL 2.0 or later
23
 * @package      wgfilemanager
24
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
25
 */
26
27
use XoopsModules\Wgfilemanager;
28
29
30
/**
31
 * Class Object Handler File
32
 */
33
class FileHandler extends \XoopsPersistableObjectHandler
34
{
35
    /**
36
     * Constructor
37
     *
38
     * @param \XoopsDatabase $db
39
     */
40
    public function __construct(\XoopsDatabase $db)
41
    {
42
        parent::__construct($db, 'wgfilemanager_file', File::class, 'id', 'directory_id');
43
    }
44
45
    /**
46
     * @param bool $isNew
47
     *
48
     * @return object
49
     */
50
    public function create($isNew = true)
51
    {
52
        return parent::create($isNew);
53
    }
54
55
    /**
56
     * retrieve a field
57
     *
58
     * @param int $id field id
59
     * @param null $fields fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
60
     * @return \XoopsObject|null reference to the {@link Get} object
61
     */
62
    public function get($id = null, $fields = null)
63
    {
64
        return parent::get($id, $fields);
65
    }
66
67
    /**
68
     * get inserted id
69
     *
70
     * @return int reference to the {@link Get} object
71
     */
72
    public function getInsertId()
73
    {
74
        return $this->db->getInsertId();
75
    }
76
77
    /**
78
     * Get Count File in the database
79
     * @param int    $start
80
     * @param int    $limit
81
     * @param string $sort
82
     * @param string $order
83
     * @return int
84
     */
85
    public function getCountFile($start = 0, $limit = 0, $sort = 'id ASC, directory_id', $order = 'ASC')
86
    {
87
        $crCountFile = new \CriteriaCompo();
88
        $crCountFile = $this->getFileCriteria($crCountFile, $start, $limit, $sort, $order);
89
        return $this->getCount($crCountFile);
0 ignored issues
show
Bug introduced by
$crCountFile of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getCount(). ( Ignorable by Annotation )

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

89
        return $this->getCount(/** @scrutinizer ignore-type */ $crCountFile);
Loading history...
90
    }
91
92
    /**
93
     * Get All File in the database
94
     * @param int    $start
95
     * @param int    $limit
96
     * @param string $sort
97
     * @param string $order
98
     * @return array
99
     */
100
    public function getAllFile($start = 0, $limit = 0, $sort = 'id ASC, directory_id', $order = 'ASC')
101
    {
102
        $crAllFile = new \CriteriaCompo();
103
        $crAllFile = $this->getFileCriteria($crAllFile, $start, $limit, $sort, $order);
104
        return $this->getAll($crAllFile);
0 ignored issues
show
Bug introduced by
$crAllFile of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getAll(). ( Ignorable by Annotation )

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

104
        return $this->getAll(/** @scrutinizer ignore-type */ $crAllFile);
Loading history...
105
    }
106
107
    /**
108
     * Get Criteria File
109
     * @param        $crFile
110
     * @param int    $start
111
     * @param int    $limit
112
     * @param string $sort
113
     * @param string $order
114
     * @return int
115
     */
116
    private function getFileCriteria($crFile, $start, $limit, $sort, $order)
117
    {
118
        $crFile->setStart($start);
119
        $crFile->setLimit($limit);
120
        $crFile->setSort($sort);
121
        $crFile->setOrder($order);
122
        return $crFile;
123
    }
124
125
    /**
126
     * Returns an array of files in directory
127
     *
128
     * @return array
129
     */
130
    public function getFileList($dirId, $start = 0, $limit = 0, $sort = 'name', $order = 'ASC') {
131
132
        $result = [];
133
        $crFile = new \CriteriaCompo();
134
        $crFile->add(new \Criteria('directory_id', $dirId));
135
        $crFile->setStart($start);
136
        $crFile->setLimit($limit);
137
        $crFile->setSort($sort);
138
        $crFile->setOrder($order);
139
        if ($this->getCount($crFile) > 0) {
140
            $fileAll = $this->getAll($crFile);
141
            foreach (\array_keys($fileAll) as $i) {
142
                $result[$i] = $fileAll[$i]->getValuesFile();
143
            }
144
        }
145
146
        return $result;
147
148
    }
149
150
151
    /**
152
     * Converts bytes into human readable file size.
153
     *
154
     * @param string $bytes
155
     * @return string human readable file size (2,87 Мб)
156
     */
157
    public function FileSizeConvert($bytes)
158
    {
159
        $bytes = (float)$bytes;
160
        $arBytes = [
161
            0 => [
162
                "UNIT" => "TB",
163
                "VALUE" => \pow(1024, 4)
164
            ],
165
            1 => [
166
                "UNIT" => "GB",
167
                "VALUE" => \pow(1024, 3)
168
            ],
169
            2 => [
170
                "UNIT" => "MB",
171
                "VALUE" => \pow(1024, 2)
172
            ],
173
            3 => [
174
                "UNIT" => "KB",
175
                "VALUE" => 1024
176
            ],
177
            4 => [
178
                "UNIT" => "B",
179
                "VALUE" => 1
180
            ],
181
        ];
182
183
        $result = '';
184
        foreach($arBytes as $arItem) {
185
            if($bytes >= $arItem["VALUE"]) {
186
                $result = $bytes / $arItem["VALUE"];
187
                $result = \str_replace(".", "," , (string)\round($result, 2))." ".$arItem["UNIT"];
188
                break;
189
            }
190
        }
191
        return $result;
192
    }
193
194
    /**
195
     * Returns an array of files in directory
196
     *
197
     * @return bool
198
     * @throws \Exception
199
     * @throws \Exception
200
     */
201
    public function renameFile($oldFilename, $newFilename) {
202
203
        $oldFilePath = \WGFILEMANAGER_REPO_PATH . $oldFilename;
204
        $newFilePath = \WGFILEMANAGER_REPO_PATH . $newFilename;
205
206
        if (file_exists($oldFilePath)) {
207
            if (!file_exists($newFilePath)) {
208
                return rename($oldFilePath, $newFilePath);
209
            } else {
210
                throw new \Exception('New filename already exists.');
211
            }
212
        } else {
213
            throw new \Exception('Old file does not exist.');
214
        }
215
216
    }
217
218
    /**
219
     * Returns an array of files icons
220
     *
221
     * @return array
222
     * @throws \Exception
223
     * @throws \Exception
224
     */
225
    public function getFileIconCollection($iconSet) {
226
227
        $fileIcons = [];
228
        
229
        //get all allowed mime types
230
        $helper  = \XoopsModules\Wgfilemanager\Helper::getInstance();
231
        $mimetypeHandler   = $helper->getHandler('Mimetype');
232
        $allowedExtensions = $mimetypeHandler->getExtensionArray();
233
234
        $folderPath   = '';
235
        $folderUrl    = '';
236
        $fileTrailing = '';
237
        $fileDefault  = '';
238
        $fileIcons['type'] = '';
239
240
        switch ($iconSet) {
241
            case 'classic':
242
                $folderPath   = \WGFILEMANAGER_ICONS_PATH . '/fileicons/file-icon-vectors-master/dist/icons/classic/';
243
                $folderUrl    = \WGFILEMANAGER_ICONS_URL . '/fileicons/file-icon-vectors-master/dist/icons/classic/';
244
                $fileTrailing = '.svg';
245
                $fileDefault  = 'default.svg';
246
                $fileIcons['type'] = 'svg';
247
                break;
248
            case 'high-contrast':
249
                $folderPath   = \WGFILEMANAGER_ICONS_PATH . '/fileicons/file-icon-vectors-master/dist/icons/high-contrast/';
250
                $folderUrl    = \WGFILEMANAGER_ICONS_URL . '/fileicons/file-icon-vectors-master/dist/icons/high-contrast/';
251
                $fileTrailing = '.svg';
252
                $fileDefault  = 'default.svg';
253
                $fileIcons['type'] = 'svg';
254
                break;
255
            case 'square-o':
256
                $folderPath   = \WGFILEMANAGER_ICONS_PATH . '/fileicons/file-icon-vectors-master/dist/icons/square-o/';
257
                $folderUrl    = \WGFILEMANAGER_ICONS_URL . '/fileicons/file-icon-vectors-master/dist/icons/square-o/';
258
                $fileTrailing = '.svg';
259
                $fileDefault  = 'default.svg';
260
                $fileIcons['type'] = 'svg';
261
                break;
262
            case 'vivid':
263
                $folderPath   = \WGFILEMANAGER_ICONS_PATH . '/fileicons/file-icon-vectors-master/dist/icons/vivid/';
264
                $folderUrl    = \WGFILEMANAGER_ICONS_URL . '/fileicons/file-icon-vectors-master/dist/icons/vivid/';
265
                $fileTrailing = '.svg';
266
                $fileDefault  = 'default.svg';
267
                $fileIcons['type'] = 'svg';
268
                break;
269
            case 'teambox':
270
                $folderPath   = \WGFILEMANAGER_ICONS_PATH . '/fileicons/free-file-icons-master/512px/';
271
                $folderUrl    = \WGFILEMANAGER_ICONS_URL . '/fileicons/free-file-icons-master/512px/';
272
                $fileTrailing = '.png';
273
                $fileDefault  = '_blank.png';
274
                $fileIcons['type'] = 'png';
275
                break;
276
            case 'eagerterrier':
277
                $folderPath   = \WGFILEMANAGER_ICONS_PATH . '/fileicons/mimetypes-link-icons-master/images/';
278
                $folderUrl    = \WGFILEMANAGER_ICONS_URL . '/fileicons/mimetypes-link-icons-master/images/';
279
                $fileTrailing = '-icon-128x128.png';
280
                $fileDefault  = 'default-128x128.png';
281
                $fileIcons['type'] = 'png';
282
                break;
283
            case 'none':
284
                break;
285
            case '':
286
            default:
287
                throw new \Exception('Invalid iconset!');
288
        }
289
290
        if ($iconSet == 'none') {
291
            foreach ($allowedExtensions as $allowedExt) {
292
                $fileIcons['files'][$allowedExt['extension']]['src'] = '';
293
                $fileIcons['files'][$allowedExt['extension']]['category'] = $allowedExt['category'];
294
            }
295
        } else {
296
            if (file_exists($folderPath)) {
297
                foreach ($allowedExtensions as $allowedExt) {
298
                    $filePath = $folderPath . $allowedExt['extension'] . $fileTrailing;
299
                    if (file_exists($filePath)) {
300
                        $fileIcons['files'][$allowedExt['extension']]['src'] = $folderUrl . $allowedExt['extension'] . $fileTrailing;
301
                        $fileIcons['files'][$allowedExt['extension']]['category'] = $allowedExt['category'];
302
                    } else {
303
                        $fileIcons['default'] = $fileDefault;
304
                    }
305
                }
306
            } else {
307
                throw new \Exception('Folder ' . $folderPath . ' does not exist!');
308
            }
309
        }
310
311
        return $fileIcons;
312
313
    }
314
315
    /**
316
     * Returns an array of favorite files
317
     *
318
     * @return array
319
     */
320
    public function getFavFileList() {
321
        $result = [];
322
        //get current user
323
        $userUid = 0;
324
        if (isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser'])) {
325
            $userUid = $GLOBALS['xoopsUser']->uid();
326
        }
327
        if ($userUid > 0) {
328
            $crFile = new \CriteriaCompo();
329
            $fileCount = $this->getCount($crFile);
330
            if ($fileCount > 0) {
331
                $crFile->setSort('name');
332
                $crFile->setOrder('asc');
333
                $fileAll = $this->getAll($crFile);
334
                foreach (\array_keys($fileAll) as $i) {
335
                    $fileValues = $fileAll[$i]->getValuesFile();
336
                    if ((int)$fileValues['favorite_id'] > 0) {
337
                        $result[] = $fileValues;
338
                    }
339
                }
340
            }
341
        }
342
        return $result;
343
    }
344
345
}
346