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

Video::getAllVideos()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 45
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 45
rs 8.0555
c 0
b 0
f 0
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
 * @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          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use Xmf\Module\Helper\Permission;
26
use XoopsDatabaseFactory;
27
use XoopsObject;
28
29
/**
30
 * Protection against inclusion outside the site
31
 */
32
if (!\defined('XOOPS_ROOT_PATH')) {
33
    die('XOOPS root path not defined');
34
}
35
36
/**
37
 * Includes of form objects and uploader
38
 */
39
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
40
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
41
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
42
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
43
44
/**
45
 * Video class.
46
 * $this class is responsible for providing data access mechanisms to the data source
47
 * of XOOPS user class objects.
48
 */
49
class Video extends XoopsObject
50
{
51
    public $db;
52
53
    public $helper;
54
55
    public $permHelper;
56
57
    // constructor
58
59
    /**
60
     * Video constructor.
61
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
62
     */
63
64
    public function __construct($id = null)
65
    {
66
        /** @var Helper $helper */
67
68
        $this->helper = Helper::getInstance();
69
70
        $this->permHelper = new Permission();
71
72
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
73
74
        $this->initVar('video_id', \XOBJ_DTYPE_INT, null, false, 10);
75
76
        $this->initVar('uid_owner', \XOBJ_DTYPE_INT, null, false, 10);
77
78
        $this->initVar('video_desc', \XOBJ_DTYPE_OTHER, null, false);
79
80
        $this->initVar('youtube_code', \XOBJ_DTYPE_TXTBOX, null, false);
81
82
        $this->initVar('main_video', \XOBJ_DTYPE_TXTBOX, null, false);
83
84
        $this->initVar('date_created', \XOBJ_DTYPE_INT);
85
86
        $this->initVar('date_updated', \XOBJ_DTYPE_INT);
87
88
        if (!empty($id)) {
89
            if (\is_array($id)) {
90
                $this->assignVars($id);
91
            } else {
92
                $this->load((int)$id);
93
            }
94
        } else {
95
            $this->setNew();
96
        }
97
    }
98
99
    /**
100
     * @param $id
101
     */
102
103
    public function load($id)
104
    {
105
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_videos') . ' WHERE video_id=' . $id;
106
107
        $myrow = $this->db->fetchArray($this->db->query($sql));
108
109
        $this->assignVars($myrow);
110
111
        if (!$myrow) {
112
            $this->setNew();
113
        }
114
    }
115
116
    /**
117
     * @param array  $criteria
118
     * @param bool   $asobject
119
     * @param string $sort
120
     * @param string $order
121
     * @param int    $limit
122
     * @param int    $start
123
     * @return array
124
     */
125
126
    public function getAllVideos(
127
        $criteria = [],
128
        $asobject = false,
129
        $sort = 'video_id',
130
        $order = 'ASC',
131
        $limit = 0,
132
        $start = 0
133
    ) {
134
        $db = XoopsDatabaseFactory::getDatabaseConnection();
135
136
        $ret = [];
137
138
        $whereQuery = '';
139
140
        if (\is_array($criteria) && \count($criteria) > 0) {
141
            $whereQuery = ' WHERE';
142
143
            foreach ($criteria as $c) {
144
                $whereQuery .= " ${c} AND";
145
            }
146
147
            $whereQuery = mb_substr($whereQuery, 0, -4);
148
        } elseif (!\is_array($criteria) && $criteria) {
149
            $whereQuery = ' WHERE ' . $criteria;
150
        }
151
152
        if (!$asobject) {
153
            $sql = 'SELECT video_id FROM ' . $db->prefix('yogurt_videos') . "${whereQuery} ORDER BY ${sort} ${order}";
154
155
            $result = $db->query($sql, $limit, $start);
156
157
            while (false !== ($myrow = $db->fetchArray($result))) {
158
                $ret[] = $myrow['yogurt_video_id'];
159
            }
160
        } else {
161
            $sql = 'SELECT * FROM ' . $db->prefix('yogurt_videos') . "${whereQuery} ORDER BY ${sort} ${order}";
162
163
            $result = $db->query($sql, $limit, $start);
164
165
            while (false !== ($myrow = $db->fetchArray($result))) {
166
                $ret[] = new self($myrow);
167
            }
168
        }
169
170
        return $ret;
171
    }
172
173
    /**
174
     * Get form
175
     *
176
     * @return \XoopsModules\Yogurt\Form\VideoForm
177
     */
178
179
    public function getForm()
180
    {
181
        return new Form\VideoForm($this);
182
    }
183
184
    /**
185
     * @return array|null
186
     */
187
188
    public function getGroupsRead()
189
    {
190
        //$permHelper = new \Xmf\Module\Helper\Permission();
191
192
        return $this->permHelper->getGroupsForItem(
193
            'sbcolumns_read',
194
            $this->getVar('video_id')
195
        );
196
    }
197
198
    /**
199
     * @return array|null
200
     */
201
202
    public function getGroupsSubmit()
203
    {
204
        //$permHelper = new \Xmf\Module\Helper\Permission();
205
206
        return $this->permHelper->getGroupsForItem(
207
            'sbcolumns_submit',
208
            $this->getVar('video_id')
209
        );
210
    }
211
212
    /**
213
     * @return array|null
214
     */
215
216
    public function getGroupsModeration()
217
    {
218
        //$permHelper = new \Xmf\Module\Helper\Permission();
219
220
        return $this->permHelper->getGroupsForItem(
221
            'sbcolumns_moderation',
222
            $this->getVar('video_id')
223
        );
224
    }
225
}
226