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

Audio::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
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('uid_owner', \XOBJ_DTYPE_INT, null, false, 10);
67
        $this->initVar('author', \XOBJ_DTYPE_TXTBOX, null, false);
68
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false);
69
        $this->initVar('description', \XOBJ_DTYPE_OTHER, null, false);
70
        $this->initVar('filename', \XOBJ_DTYPE_TXTBOX, null, false);
71
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
72
        $this->initVar('date_updated', \XOBJ_DTYPE_INT, 0, false);
73
        if (null !== ($id)) {
74
            if (\is_array($id)) {
75
                $this->assignVars($id);
76
            } else {
77
                $this->load((int)$id);
78
            }
79
        } else {
80
            $this->setNew();
81
        }
82
    }
83
84
    /**
85
     * @param int $id
86
     */
87
    public function load($id)
88
    {
89
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_audios') . ' WHERE audio_id=' . $id;
90
        $myrow = $this->db->fetchArray($this->db->query($sql));
91
        $this->assignVars($myrow);
92
        if (!$myrow) {
93
            $this->setNew();
94
        }
95
    }
96
97
    /**
98
     * @param array  $criteria
99
     * @param bool   $asobject
100
     * @param string $sort
101
     * @param string $order
102
     * @param int    $limit
103
     * @param int    $start
104
     * @return array
105
     */
106
    public function getAllAudios(
107
        $criteria = [],
108
        $asobject = false,
109
        $sort = 'audio_id',
110
        $order = 'ASC',
111
        $limit = 0,
112
        $start = 0
113
    ) {
114
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
115
        $ret        = [];
116
        $whereQuery = '';
117
        if (\is_array($criteria) && \count($criteria) > 0) {
118
            $whereQuery = ' WHERE';
119
            foreach ($criteria as $c) {
120
                $whereQuery .= " ${c} AND";
121
            }
122
            $whereQuery = mb_substr($whereQuery, 0, -4);
123
        } elseif (!\is_array($criteria) && $criteria) {
124
            $whereQuery = ' WHERE ' . $criteria;
125
        }
126
        if (!$asobject) {
127
            $sql    = 'SELECT audio_id FROM ' . $db->prefix('yogurt_audios') . "${whereQuery} ORDER BY ${sort} ${order}";
128
            $result = $db->query($sql, $limit, $start);
129
            while (false !== ($myrow = $db->fetchArray($result))) {
130
                $ret[] = $myrow['yogurt_audio_id'];
131
            }
132
        } else {
133
            $sql    = 'SELECT * FROM ' . $db->prefix('yogurt_audios') . "${whereQuery} ORDER BY ${sort} ${order}";
134
            $result = $db->query($sql, $limit, $start);
135
            while (false !== ($myrow = $db->fetchArray($result))) {
136
                $ret[] = new self($myrow);
137
            }
138
        }
139
140
        return $ret;
141
    }
142
143
    /**
144
     * Get form
145
     *
146
     * @return AudioForm
147
     */
148
    public function getForm()
149
    {
150
        return new Form\AudioForm($this);
151
    }
152
153
    /**
154
     * @return array|null
155
     */
156
    public function getGroupsRead()
157
    {
158
        //$permHelper = new \Xmf\Module\Helper\Permission();
159
        return $this->permHelper->getGroupsForItem(
160
            'sbcolumns_read',
161
            $this->getVar('audio_id')
162
        );
163
    }
164
165
    /**
166
     * @return array|null
167
     */
168
    public function getGroupsSubmit()
169
    {
170
        //$permHelper = new \Xmf\Module\Helper\Permission();
171
        return $this->permHelper->getGroupsForItem(
172
            'sbcolumns_submit',
173
            $this->getVar('audio_id')
174
        );
175
    }
176
177
    /**
178
     * @return array|null
179
     */
180
    public function getGroupsModeration()
181
    {
182
        //$permHelper = new \Xmf\Module\Helper\Permission();
183
        return $this->permHelper->getGroupsForItem(
184
            'sbcolumns_moderation',
185
            $this->getVar('audio_id')
186
        );
187
    }
188
}
189