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

Video::getAllVideos()   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          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 * @copyright       {@link https://xoops.org/ XOOPS Project}
24
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
25
 */
26
27
use Xmf\Module\Helper\Permission;
28
use XoopsDatabaseFactory;
29
use XoopsObject;
30
31
/**
32
 * Protection against inclusion outside the site
33
 */
34
if (!\defined('XOOPS_ROOT_PATH')) {
35
    die('XOOPS root path not defined');
36
}
37
38
/**
39
 * Includes of form objects and uploader
40
 */
41
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
42
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
43
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
44
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
45
46
/**
47
 * Video class.
48
 * $this class is responsible for providing data access mechanisms to the data source
49
 * of XOOPS user class objects.
50
 */
51
class Video extends XoopsObject
52
{
53
    public $db;
54
    public $helper;
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
    public function __construct($id = null)
64
    {
65
        /** @var Helper $helper */
66
        $this->helper     = Helper::getInstance();
67
        $this->permHelper = new Permission();
68
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
69
        $this->initVar('video_id', \XOBJ_DTYPE_INT, null, false, 10);
70
        $this->initVar('uid_owner', \XOBJ_DTYPE_INT, null, false, 10);
71
        $this->initVar('video_desc', \XOBJ_DTYPE_OTHER, null, false);
72
        $this->initVar('youtube_code', \XOBJ_DTYPE_TXTBOX, null, false);
73
        $this->initVar('main_video', \XOBJ_DTYPE_TXTBOX, null, false);
74
        $this->initVar('date_created', \XOBJ_DTYPE_INT);
75
        $this->initVar('date_updated', \XOBJ_DTYPE_INT);
76
        if (!empty($id)) {
77
            if (\is_array($id)) {
78
                $this->assignVars($id);
79
            } else {
80
                $this->load((int)$id);
81
            }
82
        } else {
83
            $this->setNew();
84
        }
85
    }
86
87
    /**
88
     * @param $id
89
     */
90
    public function load($id)
91
    {
92
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_videos') . ' WHERE video_id=' . $id;
93
        $myrow = $this->db->fetchArray($this->db->query($sql));
94
        $this->assignVars($myrow);
95
        if (!$myrow) {
96
            $this->setNew();
97
        }
98
    }
99
100
    /**
101
     * @param array  $criteria
102
     * @param bool   $asobject
103
     * @param string $sort
104
     * @param string $order
105
     * @param int    $limit
106
     * @param int    $start
107
     * @return array
108
     */
109
    public function getAllVideos(
110
        $criteria = [],
111
        $asobject = false,
112
        $sort = 'video_id',
113
        $order = 'ASC',
114
        $limit = 0,
115
        $start = 0
116
    ) {
117
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
118
        $ret        = [];
119
        $whereQuery = '';
120
        if (\is_array($criteria) && \count($criteria) > 0) {
121
            $whereQuery = ' WHERE';
122
            foreach ($criteria as $c) {
123
                $whereQuery .= " ${c} AND";
124
            }
125
            $whereQuery = mb_substr($whereQuery, 0, -4);
126
        } elseif (!\is_array($criteria) && $criteria) {
127
            $whereQuery = ' WHERE ' . $criteria;
128
        }
129
        if (!$asobject) {
130
            $sql    = 'SELECT video_id FROM ' . $db->prefix('yogurt_videos') . "${whereQuery} ORDER BY ${sort} ${order}";
131
            $result = $db->query($sql, $limit, $start);
132
            while (false !== ($myrow = $db->fetchArray($result))) {
133
                $ret[] = $myrow['yogurt_video_id'];
134
            }
135
        } else {
136
            $sql    = 'SELECT * FROM ' . $db->prefix('yogurt_videos') . "${whereQuery} ORDER BY ${sort} ${order}";
137
            $result = $db->query($sql, $limit, $start);
138
            while (false !== ($myrow = $db->fetchArray($result))) {
139
                $ret[] = new self($myrow);
140
            }
141
        }
142
143
        return $ret;
144
    }
145
146
    /**
147
     * Get form
148
     *
149
     * @return \XoopsModules\Yogurt\Form\VideoForm
150
     */
151
    public function getForm()
152
    {
153
        return new Form\VideoForm($this);
154
    }
155
156
    /**
157
     * @return array|null
158
     */
159
    public function getGroupsRead()
160
    {
161
        //$permHelper = new \Xmf\Module\Helper\Permission();
162
        return $this->permHelper->getGroupsForItem(
163
            'sbcolumns_read',
164
            $this->getVar('video_id')
165
        );
166
    }
167
168
    /**
169
     * @return array|null
170
     */
171
    public function getGroupsSubmit()
172
    {
173
        //$permHelper = new \Xmf\Module\Helper\Permission();
174
        return $this->permHelper->getGroupsForItem(
175
            'sbcolumns_submit',
176
            $this->getVar('video_id')
177
        );
178
    }
179
180
    /**
181
     * @return array|null
182
     */
183
    public function getGroupsModeration()
184
    {
185
        //$permHelper = new \Xmf\Module\Helper\Permission();
186
        return $this->permHelper->getGroupsForItem(
187
            'sbcolumns_moderation',
188
            $this->getVar('video_id')
189
        );
190
    }
191
}
192