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

Ishot::getAllyogurt_ishots()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 37
rs 8.0555
cc 9
nc 12
nop 6
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Yogurt;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
use Xmf\Module\Helper\Permission;
16
use XoopsDatabaseFactory;
17
use XoopsObject;
18
19
/**
20
 * @copyright    XOOPS Project https://xoops.org/
21
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author       Marcello Brandão aka  Suico
23
 * @author       XOOPS Development Team
24
 * @since
25
 */
26
if (!\defined(
27
    'XOOPS_ROOT_PATH'
28
)) {
29
    exit();
30
}
31
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
32
33
/**
34
 * Ishot class.
35
 * $this class is responsible for providing data access mechanisms to the data source
36
 * of XOOPS user class objects.
37
 */
38
class Ishot extends XoopsObject
39
{
40
    public $db;
41
    public $helper;
42
    public $permHelper;
43
44
    // constructor
45
46
    /**
47
     * Ishot constructor.
48
     * @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...
49
     */
50
    public function __construct($id = null)
51
    {
52
        /** @var Helper $helper */
53
        $this->helper     = Helper::getInstance();
54
        $this->permHelper = new Permission();
55
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
56
        $this->initVar('cod_ishot', \XOBJ_DTYPE_INT, null, false, 10);
57
        $this->initVar('uid_voter', \XOBJ_DTYPE_INT, null, false, 10);
58
        $this->initVar('uid_voted', \XOBJ_DTYPE_INT, null, false, 10);
59
        $this->initVar('ishot', \XOBJ_DTYPE_INT, null, false, 10);
60
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
61
        if (!empty($id)) {
62
            if (\is_array($id)) {
63
                $this->assignVars($id);
64
            } else {
65
                $this->load((int)$id);
66
            }
67
        } else {
68
            $this->setNew();
69
        }
70
    }
71
72
    /**
73
     * @param $id
74
     */
75
    public function load($id)
76
    {
77
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_ishot') . ' WHERE cod_ishot=' . $id;
78
        $myrow = $this->db->fetchArray($this->db->query($sql));
79
        $this->assignVars($myrow);
80
        if (!$myrow) {
81
            $this->setNew();
82
        }
83
    }
84
85
    /**
86
     * @param array  $criteria
87
     * @param bool   $asobject
88
     * @param string $sort
89
     * @param string $order
90
     * @param int    $limit
91
     * @param int    $start
92
     * @return array
93
     */
94
    public function getAllHots(
95
        $criteria = [],
96
        $asobject = false,
97
        $sort = 'cod_ishot',
98
        $order = 'ASC',
99
        $limit = 0,
100
        $start = 0
101
    ) {
102
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
103
        $ret        = [];
104
        $whereQuery = '';
105
        if (\is_array($criteria) && \count($criteria) > 0) {
106
            $whereQuery = ' WHERE';
107
            foreach ($criteria as $c) {
108
                $whereQuery .= " ${c} AND";
109
            }
110
            $whereQuery = mb_substr($whereQuery, 0, -4);
111
        } elseif (!\is_array($criteria) && $criteria) {
112
            $whereQuery = ' WHERE ' . $criteria;
113
        }
114
        if (!$asobject) {
115
            $sql    = 'SELECT cod_ishot FROM ' . $db->prefix(
116
                    'yogurt_ishot'
117
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
118
            $result = $db->query($sql, $limit, $start);
119
            while (false !== ($myrow = $db->fetchArray($result))) {
120
                $ret[] = $myrow['yogurt_ishot_id'];
121
            }
122
        } else {
123
            $sql    = 'SELECT * FROM ' . $db->prefix('yogurt_ishot') . "${whereQuery} ORDER BY ${sort} ${order}";
124
            $result = $db->query($sql, $limit, $start);
125
            while (false !== ($myrow = $db->fetchArray($result))) {
126
                $ret[] = new self($myrow);
127
            }
128
        }
129
130
        return $ret;
131
    }
132
}
133