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

Audio::getAllAudios()   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
<?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
 * Module: Yogurt
19
 *
20
 * @category        Module
21
 * @package         yogurt
22
 * @author          XOOPS Development Team <https://xoops.org>
23
 * @copyright       {@link https://xoops.org/ XOOPS Project}
24
 * @license         GPL 2.0 or later
25
 * @link            https://xoops.org/
26
 * @since           1.0.0
27
 */
28
29
use XoopsObject;
30
use Xmf\Module\Helper\Permission;
31
use XoopsDatabaseFactory;
32
use XoopsModules\Yogurt\Form\AudioForm;
33
34
// Audio.php,v 1
35
//  ---------------------------------------------------------------- //
36
// Author: Bruno Barthez                                               //
37
// ----------------------------------------------------------------- //
38
39
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
40
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
41
42
/**
43
 * Class Audio
44
 * $this class is responsible for providing data access mechanisms to the data source
45
 * of XOOPS user class objects.
46
 */
47
class Audio extends XoopsObject
48
{
49
    public $db;
50
    public $helper;
51
    public $permHelper;
52
53
    // constructor
54
55
    /**
56
     * Audio constructor.
57
     * @param null|int|array $id
58
     */
59
    public function __construct($id = null)
60
    {
61
        /** @var Helper $helper */
62
        $this->helper     = Helper::getInstance();
63
        $this->permHelper = new Permission();
64
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
65
        $this->initVar('audio_id', \XOBJ_DTYPE_INT, null, false, 10);
66
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false);
67
        $this->initVar('author', \XOBJ_DTYPE_TXTBOX, null, false);
68
        $this->initVar('filename', \XOBJ_DTYPE_TXTBOX, null, false);
69
        $this->initVar('uid_owner', \XOBJ_DTYPE_INT, null, false, 10);
70
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
71
        $this->initVar('date_updated', \XOBJ_DTYPE_INT, 0, false);
72
        if (null !== ($id)) {
73
            if (\is_array($id)) {
74
                $this->assignVars($id);
75
            } else {
76
                $this->load((int)$id);
77
            }
78
        } else {
79
            $this->setNew();
80
        }
81
    }
82
83
    /**
84
     * @param int $id
85
     */
86
    public function load($id)
87
    {
88
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_audios') . ' WHERE audio_id=' . $id;
89
        $myrow = $this->db->fetchArray($this->db->query($sql));
90
        $this->assignVars($myrow);
91
        if (!$myrow) {
92
            $this->setNew();
93
        }
94
    }
95
96
    /**
97
     * @param array  $criteria
98
     * @param bool   $asobject
99
     * @param string $sort
100
     * @param string $order
101
     * @param int    $limit
102
     * @param int    $start
103
     * @return array
104
     */
105
    public function getAllAudios(
106
        $criteria = [],
107
        $asobject = false,
108
        $sort = 'audio_id',
109
        $order = 'ASC',
110
        $limit = 0,
111
        $start = 0
112
    ) {
113
        $db          = XoopsDatabaseFactory::getDatabaseConnection();
114
        $ret         = [];
115
        $whereQuery = '';
116
        if (\is_array($criteria) && \count($criteria) > 0) {
117
            $whereQuery = ' WHERE';
118
            foreach ($criteria as $c) {
119
                $whereQuery .= " ${c} AND";
120
            }
121
            $whereQuery = mb_substr($whereQuery, 0, -4);
122
        } elseif (!\is_array($criteria) && $criteria) {
123
            $whereQuery = ' WHERE ' . $criteria;
124
        }
125
        if (!$asobject) {
126
            $sql    = 'SELECT audio_id FROM ' . $db->prefix('yogurt_audios') . "${whereQuery} ORDER BY ${sort} ${order}";
127
            $result = $db->query($sql, $limit, $start);
128
            while (false !== ($myrow = $db->fetchArray($result))) {
129
                $ret[] = $myrow['yogurt_audio_id'];
130
            }
131
        } else {
132
            $sql    = 'SELECT * FROM ' . $db->prefix('yogurt_audios') . "${whereQuery} ORDER BY ${sort} ${order}";
133
            $result = $db->query($sql, $limit, $start);
134
            while (false !== ($myrow = $db->fetchArray($result))) {
135
                $ret[] = new self($myrow);
136
            }
137
        }
138
139
        return $ret;
140
    }
141
142
    /**
143
     * Get form
144
     *
145
     * @return AudioForm
146
     */
147
    public function getForm()
148
    {
149
        return new Form\AudioForm($this);
150
    }
151
152
    /**
153
     * @return array|null
154
     */
155
    public function getGroupsRead()
156
    {
157
        //$permHelper = new \Xmf\Module\Helper\Permission();
158
        return $this->permHelper->getGroupsForItem(
159
            'sbcolumns_read',
160
            $this->getVar('audio_id')
161
        );
162
    }
163
164
    /**
165
     * @return array|null
166
     */
167
    public function getGroupsSubmit()
168
    {
169
        //$permHelper = new \Xmf\Module\Helper\Permission();
170
        return $this->permHelper->getGroupsForItem(
171
            'sbcolumns_submit',
172
            $this->getVar('audio_id')
173
        );
174
    }
175
176
    /**
177
     * @return array|null
178
     */
179
    public function getGroupsModeration()
180
    {
181
        //$permHelper = new \Xmf\Module\Helper\Permission();
182
        return $this->permHelper->getGroupsForItem(
183
            'sbcolumns_moderation',
184
            $this->getVar('audio_id')
185
        );
186
    }
187
}
188