Passed
Pull Request — master (#81)
by Michael
03:31
created

Audio::getAllyogurt_audios()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 35
rs 8.0555
cc 9
nc 12
nop 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A Audio::load() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         yogurt
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use Xmf\Module\Helper\Permission;
26
use XoopsDatabaseFactory;
27
use XoopsModules\Yogurt\Form\AudioForm;
28
use XoopsObject;
29
30
31
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
32
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
33
34
/**
35
 * Class Audio
36
 * $this class is responsible for providing data access mechanisms to the data source
37
 * of XOOPS user class objects.
38
 */
39
class Audio extends XoopsObject
40
{
41
    public $db;
42
43
    public $helper;
44
45
    public $permHelper;
46
47
    // constructor
48
49
    /**
50
     * Audio constructor.
51
     * @param null|int|array $id
52
     */
53
54
    public function __construct($id = null)
55
    {
56
        /** @var Helper $helper */
57
58
        $this->helper = Helper::getInstance();
59
60
        $this->permHelper = new Permission();
61
62
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
63
64
        $this->initVar('audio_id', \XOBJ_DTYPE_INT, null, false, 10);
65
66
        $this->initVar('uid_owner', \XOBJ_DTYPE_INT, null, false, 10);
67
68
        $this->initVar('author', \XOBJ_DTYPE_TXTBOX, null, false);
69
70
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false);
71
72
        $this->initVar('description', \XOBJ_DTYPE_OTHER, null, false);
73
74
        $this->initVar('filename', \XOBJ_DTYPE_TXTBOX, null, false);
75
76
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
77
78
        $this->initVar('date_updated', \XOBJ_DTYPE_INT, 0, false);
79
80
        if (null !== ($id)) {
81
            if (\is_array($id)) {
82
                $this->assignVars($id);
83
            } else {
84
                $this->load((int)$id);
85
            }
86
        } else {
87
            $this->setNew();
88
        }
89
    }
90
91
    /**
92
     * @param int $id
93
     */
94
95
    public function load($id)
96
    {
97
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_audios') . ' WHERE audio_id=' . $id;
98
99
        $myrow = $this->db->fetchArray($this->db->query($sql));
100
101
        $this->assignVars($myrow);
102
103
        if (!$myrow) {
104
            $this->setNew();
105
        }
106
    }
107
108
    /**
109
     * @param array  $criteria
110
     * @param bool   $asobject
111
     * @param string $sort
112
     * @param string $order
113
     * @param int    $limit
114
     * @param int    $start
115
     * @return array
116
     */
117
118
    public function getAllAudios(
119
        $criteria = [],
120
        $asobject = false,
121
        $sort = 'audio_id',
122
        $order = 'ASC',
123
        $limit = 0,
124
        $start = 0
125
    ) {
126
        $db = XoopsDatabaseFactory::getDatabaseConnection();
127
128
        $ret = [];
129
130
        $whereQuery = '';
131
132
        if (\is_array($criteria) && \count($criteria) > 0) {
133
            $whereQuery = ' WHERE';
134
135
            foreach ($criteria as $c) {
136
                $whereQuery .= " ${c} AND";
137
            }
138
139
            $whereQuery = mb_substr($whereQuery, 0, -4);
140
        } elseif (!\is_array($criteria) && $criteria) {
141
            $whereQuery = ' WHERE ' . $criteria;
142
        }
143
144
        if (!$asobject) {
145
            $sql = 'SELECT audio_id FROM ' . $db->prefix('yogurt_audios') . "${whereQuery} ORDER BY ${sort} ${order}";
146
147
            $result = $db->query($sql, $limit, $start);
148
149
            while (false !== ($myrow = $db->fetchArray($result))) {
150
                $ret[] = $myrow['yogurt_audio_id'];
151
            }
152
        } else {
153
            $sql = 'SELECT * FROM ' . $db->prefix('yogurt_audios') . "${whereQuery} ORDER BY ${sort} ${order}";
154
155
            $result = $db->query($sql, $limit, $start);
156
157
            while (false !== ($myrow = $db->fetchArray($result))) {
158
                $ret[] = new self($myrow);
159
            }
160
        }
161
162
        return $ret;
163
    }
164
165
    /**
166
     * Get form
167
     *
168
     * @return AudioForm
169
     */
170
171
    public function getForm()
172
    {
173
        return new Form\AudioForm($this);
174
    }
175
176
    /**
177
     * @return array|null
178
     */
179
180
    public function getGroupsRead()
181
    {
182
        //$permHelper = new \Xmf\Module\Helper\Permission();
183
184
        return $this->permHelper->getGroupsForItem(
185
            'sbcolumns_read',
186
            $this->getVar('audio_id')
187
        );
188
    }
189
190
    /**
191
     * @return array|null
192
     */
193
194
    public function getGroupsSubmit()
195
    {
196
        //$permHelper = new \Xmf\Module\Helper\Permission();
197
198
        return $this->permHelper->getGroupsForItem(
199
            'sbcolumns_submit',
200
            $this->getVar('audio_id')
201
        );
202
    }
203
204
    /**
205
     * @return array|null
206
     */
207
208
    public function getGroupsModeration()
209
    {
210
        //$permHelper = new \Xmf\Module\Helper\Permission();
211
212
        return $this->permHelper->getGroupsForItem(
213
            'sbcolumns_moderation',
214
            $this->getVar('audio_id')
215
        );
216
    }
217
}
218