Response::getTicket()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
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
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Eric Juden <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
if (!\defined('XHELP_CLASS_PATH')) {
23
    exit();
24
}
25
26
// require_once XHELP_CLASS_PATH . '/BaseObjectHandler.php';
27
28
/**
29
 * Response class
30
 *
31
 * @author  Eric Juden <[email protected]>
32
 */
33
class Response extends \XoopsObject
34
{
35
    private $helper;
36
37
    /**
38
     * Response constructor.
39
     * @param int|array|null $id
40
     */
41
    public function __construct($id = null)
42
    {
43
        $this->initVar('id', \XOBJ_DTYPE_INT, null, false);
44
        $this->initVar('uid', \XOBJ_DTYPE_INT, null, false);
45
        $this->initVar('ticketid', \XOBJ_DTYPE_INT, null, false);
46
        $this->initVar('message', \XOBJ_DTYPE_TXTAREA, null, false, 1000000);
47
        $this->initVar('timeSpent', \XOBJ_DTYPE_INT, null, false);
48
        $this->initVar('updateTime', \XOBJ_DTYPE_INT, null, true);
49
        $this->initVar('userIP', \XOBJ_DTYPE_TXTBOX, null, true, 35);
50
        $this->initVar('private', \XOBJ_DTYPE_INT, null, false);
51
52
        $this->helper = Helper::getInstance();
53
        if (null !== $id) {
54
            if (\is_array($id)) {
55
                $this->assignVars($id);
56
            }
57
        } else {
58
            $this->setNew();
59
        }
60
    }
61
62
    /**
63
     * Formats the posted date as the XOOPS date formate
64
     *
65
     * @param string $format
66
     * @return string Formatted posted date
67
     */
68
    public function posted(string $format = 'l'): string
69
    {
70
        return \formatTimestamp($this->getVar('updateTime'), $format);
71
    }
72
73
    /**
74
     * @param string $post_field
75
     * @param null   $response
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $response is correct as it would always require null to be passed?
Loading history...
76
     * @param null   $allowed_mimetypes
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $allowed_mimetypes is correct as it would always require null to be passed?
Loading history...
77
     * @return array|false|object|string|void
78
     */
79
    public function storeUpload(string $post_field, $response = null, $allowed_mimetypes = null)
80
    {
81
        //global $xoopsModuleConfig, $xoopsUser, $xoopsDB, $xoopsModule;
82
        // require_once XHELP_CLASS_PATH . '/uploader.php';
83
        $config = Utility::getModuleConfig();
84
85
        $ticketid = $this->getVar('id');
86
87
        if (null === $allowed_mimetypes) {
0 ignored issues
show
introduced by
The condition null === $allowed_mimetypes is always true.
Loading history...
88
            /** @var \XoopsModules\Xhelp\MimetypeHandler $mimetypeHandler */
89
            $mimetypeHandler   = $this->helper->getHandler('Mimetype');
90
            $allowed_mimetypes = $mimetypeHandler->checkMimeTypes($post_field);
91
            if (!$allowed_mimetypes) {
0 ignored issues
show
introduced by
The condition $allowed_mimetypes is always false.
Loading history...
92
                return false;
93
            }
94
        }
95
96
        $maxfilesize   = $config['xhelp_uploadSize'];
97
        $maxfilewidth  = $config['xhelp_uploadWidth'];
98
        $maxfileheight = $config['xhelp_uploadHeight'];
99
        if (!\is_dir(XHELP_UPLOAD_PATH)) {
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xhelp\XHELP_UPLOAD_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
100
            if (!\mkdir($concurrentDirectory = XHELP_UPLOAD_PATH, 0757) && !\is_dir($concurrentDirectory)) {
101
                throw new \RuntimeException(\sprintf('Directory "%s" was not created', $concurrentDirectory));
102
            }
103
        }
104
105
        $uploader = new MediaUploader(XHELP_UPLOAD_PATH . '/', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
106
        if ($uploader->fetchMedia($post_field)) {
107
            if (null === $response) {
108
                $uploader->setTargetFileName($ticketid . '_' . $uploader->getMediaName());
109
            } else {
110
                $uploader->setTargetFileName($ticketid . '_' . $response . '_' . $uploader->getMediaName());
111
            }
112
            if ($uploader->upload()) {
113
                $fileHandler = $this->helper->getHandler('File');
114
                $file        = $fileHandler->create();
115
                $file->setVar('filename', $uploader->getSavedFileName());
116
                $file->setVar('ticketid', $ticketid);
117
                $file->setVar('mimetype', $allowed_mimetypes);
118
                $file->setVar('responseid', (null !== $response ? (int)$response : 0));
119
120
                if ($fileHandler->insert($file)) {
121
                    return $file;
122
                }
123
124
                return $uploader->getErrors();
125
            }
126
127
            return $uploader->getErrors();
128
        }
129
    }
130
131
    /**
132
     * @param string $post_field
133
     * @param array  $allowed_mimetypes
134
     * @param array  $errors
135
     * @return bool
136
     */
137
    public function checkUpload(string $post_field, array &$allowed_mimetypes, array &$errors): bool
138
    {
139
        //global $xoopsModuleConfig;
140
        // require_once XHELP_CLASS_PATH . '/uploader.php';
141
        $config        = Utility::getModuleConfig();
142
        $maxfilesize   = $config['xhelp_uploadSize'];
143
        $maxfilewidth  = $config['xhelp_uploadWidth'];
144
        $maxfileheight = $config['xhelp_uploadHeight'];
145
        $errors        = [];
146
147
        if (null === $allowed_mimetypes) {
0 ignored issues
show
introduced by
The condition null === $allowed_mimetypes is always false.
Loading history...
148
            $mimetypeHandler   = $this->helper->getHandler('Mimetype');
149
            $allowed_mimetypes = $mimetypeHandler->checkMimeTypes($post_field);
150
            if (!$allowed_mimetypes) {
151
                $errors[] = \_XHELP_MESSAGE_WRONG_MIMETYPE;
152
153
                return false;
154
            }
155
        }
156
        $uploader = new MediaUploader(XHELP_UPLOAD_PATH . '/', $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xhelp\XHELP_UPLOAD_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
157
158
        if ($uploader->fetchMedia($post_field)) {
159
            return true;
160
        }
161
162
        $errors = \array_merge($errors, $uploader->getErrors(false));
163
164
        return false;
165
    }
166
167
    /**
168
     * Get the ticket to which the response is attached
169
     * @return \XoopsObject|null The ticket ID
170
     */
171
    public function getTicket(): ?\XoopsObject
172
    {
173
        $ticketHandler = $this->helper->getHandler('Ticket');
174
175
        return $ticketHandler->get($this->getVar('ticketid'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ticketHandler->g...is->getVar('ticketid')) could return the type false which is incompatible with the type-hinted return XoopsObject|null. Consider adding an additional type-check to rule them out.
Loading history...
176
    }
177
}
178