FileHandler::getInsertId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
use XoopsModules\Wgfilemanager\Common\SysUtility;
29
30
31
/**
32
 * Class Object Handler File
33
 */
34
class FileHandler extends \XoopsPersistableObjectHandler
35
{
36
    /**
37
     * Constructor
38
     *
39
     * @param \XoopsDatabase $db
40
     */
41
    public function __construct(\XoopsDatabase $db)
42
    {
43
        parent::__construct($db, 'wgfilemanager_file', File::class, 'id', 'directory_id');
44
    }
45
46
    /**
47
     * @param bool $isNew
48
     *
49
     * @return object
50
     */
51
    public function create($isNew = true)
52
    {
53
        return parent::create($isNew);
54
    }
55
56
    /**
57
     * retrieve a field
58
     *
59
     * @param int $id field id
60
     * @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...
61
     * @return \XoopsObject|null reference to the {@link Get} object
62
     */
63
    public function get($id = null, $fields = null)
64
    {
65
        return parent::get($id, $fields);
66
    }
67
68
    /**
69
     * get inserted id
70
     *
71
     * @return int reference to the {@link Get} object
72
     */
73
    public function getInsertId()
74
    {
75
        return $this->db->getInsertId();
76
    }
77
78
    /**
79
     * Get Count File in the database
80
     * @param int    $start
81
     * @param int    $limit
82
     * @param string $sort
83
     * @param string $order
84
     * @return int
85
     */
86
    public function getCountFile($start = 0, $limit = 0, $sort = 'id', $order = 'DESC')
87
    {
88
        $crCountFile = new \CriteriaCompo();
89
        $crCountFile = $this->getFileCriteria($crCountFile, $start, $limit, $sort, $order);
90
        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

90
        return $this->getCount(/** @scrutinizer ignore-type */ $crCountFile);
Loading history...
91
    }
92
93
    /**
94
     * Get All File in the database
95
     * @param int    $start
96
     * @param int    $limit
97
     * @param string $sort
98
     * @param string $order
99
     * @return array
100
     */
101
    public function getAllFile($start = 0, $limit = 0, $sort = 'id', $order = 'DESC')
102
    {
103
        $crAllFile = new \CriteriaCompo();
104
        $crAllFile = $this->getFileCriteria($crAllFile, $start, $limit, $sort, $order);
105
        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

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