File::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.8666
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
\defined('XOOPS_ROOT_PATH') || die('Restricted access');
30
31
/**
32
 * Class Object File
33
 */
34
class File extends \XoopsObject
35
{
36
    /**
37
     * @var int
38
     */
39
    public $start = 0;
40
41
    /**
42
     * @var int
43
     */
44
    public $limit = 0;
45
46
    /**
47
     * Constructor
48
     *
49
     */
50
    public function __construct()
51
    {
52
        $this->initVar('id', \XOBJ_DTYPE_INT);
53
        $this->initVar('directory_id', \XOBJ_DTYPE_INT);
54
        $this->initVar('name', \XOBJ_DTYPE_TXTBOX);
55
        $this->initVar('description', \XOBJ_DTYPE_OTHER);
56
        $this->initVar('mimetype', \XOBJ_DTYPE_TXTBOX);
57
        $this->initVar('size', \XOBJ_DTYPE_INT);
58
        $this->initVar('ctime', \XOBJ_DTYPE_INT);
59
        $this->initVar('mtime', \XOBJ_DTYPE_INT);
60
        $this->initVar('ip', \XOBJ_DTYPE_TXTBOX);
61
        $this->initVar('status', \XOBJ_DTYPE_INT);
62
        $this->initVar('date_created', \XOBJ_DTYPE_INT);
63
        $this->initVar('submitter', \XOBJ_DTYPE_INT);
64
    }
65
66
    /**
67
     * @static function &getInstance
68
     *
69
     */
70
    public static function getInstance()
71
    {
72
        static $instance = false;
73
        if (!$instance) {
74
            $instance = new self();
75
        }
76
    }
77
78
    /**
79
     * The new inserted $Id
80
     * @return integer
81
     */
82
    public function getNewInsertedId()
83
    {
84
        return $GLOBALS['xoopsDB']->getInsertId();
85
    }
86
87
    /**
88
     * @public function getForm
89
     * @param bool $action
90
     * @return \XoopsThemeForm
91
     */
92
    public function getForm($action = false)
93
    {
94
        $helper             = \XoopsModules\Wgfilemanager\Helper::getInstance();
95
        $fileHandler        = $helper->getHandler('File');
96
        $permissionsHandler = $helper->getHandler('Permissions');
97
        if (!$action) {
98
            $action = $_SERVER['REQUEST_URI'];
99
        }
100
        $isAdmin = \is_object($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser']->isAdmin($GLOBALS['xoopsModule']->mid());
101
        // Title
102
        $title = $this->isNew() ? \_MA_WGFILEMANAGER_FILE_ADD : \_MA_WGFILEMANAGER_FILE_EDIT;
103
        // Get Theme Form
104
        \xoops_load('XoopsFormLoader');
105
        $form = new \XoopsThemeForm($title, 'form', $action, 'post', true);
106
        $form->setExtra('enctype="multipart/form-data"');
107
        //$form->addElement(new \XoopsFormHidden('formtype', $formType));
108
        // Form Table directory
109
        $directoryId = (int)$this->getVar('directory_id');
110
        $directoryHandler = $helper->getHandler('Directory');
111
        $fileDirectory_idSelect = new \XoopsFormSelect(\_MA_WGFILEMANAGER_FILE_DIRECTORY_ID, 'directory_id', $directoryId);
112
        $dirListSelect = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($directoryHandler->getDirListFormSelect(0)));
113
        foreach ($dirListSelect as $key => $value) {
114
            $fileDirectory_idSelect->addOption($key, $value);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type true; however, parameter $value of XoopsFormSelect::addOption() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

114
            $fileDirectory_idSelect->addOption(/** @scrutinizer ignore-type */ $key, $value);
Loading history...
115
        }
116
        $form->addElement($fileDirectory_idSelect, true);
117
        $form->addElement(new \XoopsFormHidden('directory_id_old', $directoryId));
118
        // Form File: Upload fileName
119
        $fileName = $this->isNew() ? '' : $this->getVar('name');
120
        if ($this->isNew()) {
121
            if ($permissionsHandler->getPermUploadFileToDir($directoryId)) {
122
                $fileUploadTray = new \XoopsFormElementTray(\_MA_WGFILEMANAGER_FILE_NAME, '<br>');
123
                //$fileDirectory = '/uploads/wgfilemanager/files/file';
124
                //$fileUploadTray->addElement(new \XoopsFormLabel(\sprintf(\_MA_WGFILEMANAGER_FILE_NAME_UPLOADS, ".$fileDirectory/"), $fileName));
125
                $maxsize = $helper->getConfig('maxsize_file');
126
                $fileUploadTray->addElement(new \XoopsFormFile('', 'fil_name', $maxsize));
127
                $fileUploadTray->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FORM_UPLOAD_SIZE, ($maxsize / 1048576) . ' ' . \_MA_WGFILEMANAGER_FORM_UPLOAD_SIZE_MB));
128
                $form->addElement($fileUploadTray, true);
129
            } else {
130
                $form->addElement(new \XoopsFormHidden('fil_name', $fileName));
0 ignored issues
show
Bug introduced by
It seems like $fileName can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

130
                $form->addElement(new \XoopsFormHidden('fil_name', /** @scrutinizer ignore-type */ $fileName));
Loading history...
131
            }
132
        } else {
133
            $form->addElement(new \XoopsFormText(\_MA_WGFILEMANAGER_FILE_NAME, 'fil_name', 100, 150, $fileName));
0 ignored issues
show
Bug introduced by
It seems like $fileName can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

133
            $form->addElement(new \XoopsFormText(\_MA_WGFILEMANAGER_FILE_NAME, 'fil_name', 100, 150, /** @scrutinizer ignore-type */ $fileName));
Loading history...
134
            $form->addElement(new \XoopsFormHidden('fil_name_old', $fileName));
135
        }
136
        // Form Editor DhtmlTextArea fileDescription
137
        $editorConfigs = [];
138
        if ($isAdmin) {
139
            $editor = $helper->getConfig('editor_admin');
140
        } else {
141
            $editor = $helper->getConfig('editor_user');
142
        }
143
        $editorConfigs['name'] = 'description';
144
        $editorConfigs['value'] = $this->getVar('description', 'e');
145
        $editorConfigs['rows'] = 5;
146
        $editorConfigs['cols'] = 40;
147
        $editorConfigs['width'] = '100%';
148
        $editorConfigs['height'] = '400px';
149
        $editorConfigs['editor'] = $editor;
150
        $form->addElement(new \XoopsFormEditor(\_MA_WGFILEMANAGER_FILE_DESCRIPTION, 'description', $editorConfigs));
151
        // Form Text fileType
152
        $fileType = $this->isNew() ? '' : $this->getVar('mimetype');
153
        if (!$this->isNew()) {
154
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_MIMETYPE, $fileType));
0 ignored issues
show
Bug introduced by
It seems like $fileType can also be of type array and array; however, parameter $value of XoopsFormLabel::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

154
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_MIMETYPE, /** @scrutinizer ignore-type */ $fileType));
Loading history...
155
        }
156
        //$form->addElement(new \XoopsFormHidden('mimetype', $fileType));
157
        // Form Text Date file modification date
158
        $fileSize = $this->isNew() ? \time() : $this->getVar('size');
159
        if (!$this->isNew()) {
160
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_SIZE, $fileHandler->FileSizeConvert($fileSize)));
0 ignored issues
show
Bug introduced by
It seems like $fileSize can also be of type array and array; however, parameter $bytes of XoopsModules\Wgfilemanag...dler::FileSizeConvert() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

160
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_SIZE, $fileHandler->FileSizeConvert(/** @scrutinizer ignore-type */ $fileSize)));
Loading history...
161
        }
162
        // Form Text Date file modification date
163
        $fileMtime = $this->isNew() ? \time() : $this->getVar('mtime');
164
        if (!$this->isNew()) {
165
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_MTIME, \formatTimestamp($fileMtime, 's')));
166
        }
167
        // Form Text Date file creation date
168
        $fileCtime = $this->isNew() ? \time() : $this->getVar('ctime');
169
        if (!$this->isNew()) {
170
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_CTIME, \formatTimestamp($fileCtime, 's')));
171
        }
172
        //$form->addElement(new \XoopsFormHidden('mtime', $fileMtime));
173
        //$form->addElement(new \XoopsFormTextDateSelect(\_MA_WGFILEMANAGER_FILE_MTIME, 'mtime', '', $fileMtime));
174
        // Form Text IP fileIp
175
        $fileIp = $_SERVER['REMOTE_ADDR'];
176
        if ($isAdmin) {
177
            $form->addElement(new \XoopsFormText(\_MA_WGFILEMANAGER_FILE_IP, 'ip', 20, 150, $fileIp));
178
        } else {
179
            $form->addElement(new \XoopsFormHidden('ip', $fileIp));
180
        }
181
        // Form Select Status fileStatus
182
        $fileStatus = $this->isNew() ? Constants::STATUS_SUBMITTED : $this->getVar('status');
183
        if ($isAdmin) {
184
            $fileStatusSelect = new \XoopsFormSelect(\_MA_WGFILEMANAGER_FILE_STATUS, 'status', $fileStatus);
185
            $fileStatusSelect->addOption(Constants::STATUS_NONE, \_AM_WGFILEMANAGER_STATUS_NONE);
186
            //$fileStatusSelect->addOption(Constants::STATUS_OFFLINE, \_AM_WGFILEMANAGER_STATUS_OFFLINE);
187
            $fileStatusSelect->addOption(Constants::STATUS_SUBMITTED, \_AM_WGFILEMANAGER_STATUS_SUBMITTED);
188
            //$fileStatusSelect->addOption(Constants::STATUS_APPROVED, \_AM_WGFILEMANAGER_STATUS_APPROVED);
189
            $fileStatusSelect->addOption(Constants::STATUS_BROKEN, \_AM_WGFILEMANAGER_STATUS_BROKEN);
190
            $form->addElement($fileStatusSelect);
191
        } else {
192
            $form->addElement(new \XoopsFormHidden('status', $fileStatus));
193
        }
194
195
        // Form Text Date Select fileDate_created
196
        $fileDate_created = $this->isNew() ? \time() : $this->getVar('date_created');
197
        if ($isAdmin) {
198
            $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGFILEMANAGER_FILE_DATE_CREATED, 'date_created', '', $fileDate_created));
0 ignored issues
show
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $size of XoopsFormTextDateSelect::__construct(). ( Ignorable by Annotation )

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

198
            $form->addElement(new \XoopsFormTextDateSelect(\_MA_WGFILEMANAGER_FILE_DATE_CREATED, 'date_created', /** @scrutinizer ignore-type */ '', $fileDate_created));
Loading history...
199
        } elseif (!$this->isNew()) {
200
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_DATE_CREATED, \formatTimestamp($fileDate_created, 's')));
201
        }
202
203
        // Form Select User fileSubmitter
204
        $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
205
        $fileSubmitter = $this->isNew() ? $uidCurrent : $this->getVar('submitter');
206
        if ($isAdmin) {
207
            $form->addElement(new \XoopsFormSelectUser(\_MA_WGFILEMANAGER_FILE_SUBMITTER, 'submitter', false, $fileSubmitter));
208
        } else {
209
            $form->addElement(new \XoopsFormLabel(\_MA_WGFILEMANAGER_FILE_SUBMITTER, \XoopsUser::getUnameFromId($fileSubmitter)));
210
            $form->addElement(new \XoopsFormHidden('submitter', $fileSubmitter));
211
        }
212
        // To Save
213
        $form->addElement(new \XoopsFormHidden('op', 'save'));
214
        $form->addElement(new \XoopsFormHidden('start', $this->start));
215
        $form->addElement(new \XoopsFormHidden('limit', $this->limit));
216
        $form->addElement(new \XoopsFormButtonTray('', \_SUBMIT, 'submit', '', false));
217
        return $form;
218
    }
219
220
    /**
221
     * Get Values
222
     * @param null $keys
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $keys is correct as it would always require null to be passed?
Loading history...
223
     * @param null $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
224
     * @param null $maxDepth
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $maxDepth is correct as it would always require null to be passed?
Loading history...
225
     * @return array
226
     */
227
    public function getValuesFile($keys = null, $format = null, $maxDepth = null)
228
    {
229
        $helper  = \XoopsModules\Wgfilemanager\Helper::getInstance();
230
        $utility = new \XoopsModules\Wgfilemanager\Utility();
231
232
        $directoryHandler = $helper->getHandler('Directory');
233
        $fileHandler      = $helper->getHandler('File');
234
        $editorMaxchar    = $helper->getConfig('editor_maxchar');
235
        $ret              = $this->getValues($keys, $format, $maxDepth);
236
        $fileName         = $this->getVar('name');
237
        $ret['dir_name']  = \_MA_WGFILEMANAGER_DIRECTORY_HOME;
238
        $ret['real_url']  = \WGFILEMANAGER_REPO_URL . '/' . $fileName;
239
        //$ret['real_path'] = \WGFILEMANAGER_REPO_PATH . '/' . $fileName;
240
        $directoryObj = $directoryHandler->get($this->getVar('directory_id'));
241
        if (\is_object($directoryObj) && '' !== $directoryObj->getVar('name')) {
242
            $ret['dir_name']     = $directoryObj->getVar('name');
243
            $ret['dir_fullpath'] = $directoryObj->getVar('fullpath');
244
            $ret['real_url']     = \WGFILEMANAGER_REPO_URL . $directoryObj->getVar('fullpath') . '/' . $fileName;
245
            $ret['real_path']    = \WGFILEMANAGER_REPO_PATH . $directoryObj->getVar('fullpath') . '/' . $fileName;
246
        }
247
        //get current user
248
        $userUid = 0;
249
        if (isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser'])) {
250
            $userUid = $GLOBALS['xoopsUser']->uid();
251
        }
252
        $ret['favorite_id'] = 0;
253
        if ($userUid > 0) {
254
            $favoriteHandler = $helper->getHandler('Favorite');
255
            $crFavorite = new \CriteriaCompo();
256
            $crFavorite->add(new \Criteria('file_id', $this->getVar('id')));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('id') can also be of type array and array; however, parameter $value of Criteria::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

256
            $crFavorite->add(new \Criteria('file_id', /** @scrutinizer ignore-type */ $this->getVar('id')));
Loading history...
257
            $crFavorite->add(new \Criteria('submitter', $userUid));
258
            if ($favoriteHandler->getCount($crFavorite)) {
259
                $favoriteObj = $favoriteHandler->getObjects($crFavorite);
260
                $ret['favorite_id'] = $favoriteObj[0]->getVar('id');
261
            }
262
            unset($favoriteObj);
263
            unset($crFavorite);
264
        }
265
        $ret['print_url']         = $ret['real_url'];
266
        $ret['description_text']  = $this->getVar('description', 'e');
267
        $ret['description_short'] = $utility::truncateHtml($ret['description'], $editorMaxchar);
268
        $status                   = $this->getVar('status');
269
        switch ($status) {
270
            case Constants::STATUS_NONE:
271
            default:
272
                $status_text = \_AM_WGFILEMANAGER_STATUS_NONE;
273
                break;
274
            /*case Constants::STATUS_OFFLINE:
275
                $status_text = \_AM_WGFILEMANAGER_STATUS_OFFLINE;
276
                break;
277
            case Constants::STATUS_APPROVED:
278
                $status_text = \_AM_WGFILEMANAGER_STATUS_APPROVED;
279
                break;*/
280
            case Constants::STATUS_SUBMITTED:
281
                $status_text = \_AM_WGFILEMANAGER_STATUS_SUBMITTED;
282
                break;
283
            case Constants::STATUS_BROKEN:
284
                $status_text = \_AM_WGFILEMANAGER_STATUS_BROKEN;
285
                break;
286
        }
287
        $ret['status_text']       = $status_text;
288
        $ret['mtime_text']        = \formatTimestamp($this->getVar('mtime'), 's');
289
        $ret['ctime_text']        = \formatTimestamp($this->getVar('ctime'), 's');
290
        $ret['size_text']         = $fileHandler->FileSizeConvert($this->getVar('size'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('size') can also be of type array and array; however, parameter $bytes of XoopsModules\Wgfilemanag...dler::FileSizeConvert() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

290
        $ret['size_text']         = $fileHandler->FileSizeConvert(/** @scrutinizer ignore-type */ $this->getVar('size'));
Loading history...
291
        $ret['date_created_text'] = \formatTimestamp($this->getVar('date_created'), 's');
292
        $ret['submitter_text']    = \XoopsUser::getUnameFromId($this->getVar('submitter'));
293
        return $ret;
294
    }
295
296
    /**
297
     * Returns an array representation of the object
298
     *
299
     * @return array
300
     */
301
    public function toArrayFile()
302
    {
303
        $ret = [];
304
        $vars = $this->getVars();
305
        foreach (\array_keys($vars) as $var) {
306
            $ret[$var] = $this->getVar($var);
307
        }
308
        return $ret;
309
    }
310
}
311