Ishot::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 19
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
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
 * @category        Module
21
 * @copyright       {@link https://xoops.org/ XOOPS Project}
22
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
23
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
24
 */
25
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
26
27
/**
28
 * Ishot class.
29
 * $this class is responsible for providing data access mechanisms to the data source
30
 * of XOOPS user class objects.
31
 */
32
class Ishot extends XoopsObject
33
{
34
    public \XoopsDatabase $db;
35
    public Helper         $helper;
36
    public Permission     $permHelper;
37
    public                $cod_ishot;
38
    public $uid_voter;
39
    public $uid_voted;
40
    public $ishot;
41
    public $date_created;
42
    // constructor
43
44
    /**
45
     * Ishot constructor.
46
     * @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...
47
     */
48
    public function __construct($id = null)
49
    {
50
        /** @var Helper $helper */
51
        $this->helper     = Helper::getInstance();
52
        $this->permHelper = new Permission();
53
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
54
        $this->initVar('cod_ishot', \XOBJ_DTYPE_INT, null, false, 10);
55
        $this->initVar('uid_voter', \XOBJ_DTYPE_INT, null, false, 10);
56
        $this->initVar('uid_voted', \XOBJ_DTYPE_INT, null, false, 10);
57
        $this->initVar('ishot', \XOBJ_DTYPE_INT, null, false, 10);
58
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
59
        if (!empty($id)) {
60
            if (\is_array($id)) {
61
                $this->assignVars($id);
62
            } else {
63
                $this->load((int)$id);
64
            }
65
        } else {
66
            $this->setNew();
67
        }
68
    }
69
70
    /**
71
     * @param $id
72
     */
73
    public function load($id): void
74
    {
75
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_ishot') . ' WHERE cod_ishot=' . $id;
76
        $myrow = $this->db->fetchArray($this->db->query($sql));
77
        $this->assignVars($myrow);
78
        if (!$myrow) {
79
            $this->setNew();
80
        }
81
    }
82
83
    /**
84
     * @param array  $criteria
85
     * @param bool   $asobject
86
     * @param string $sort
87
     * @param string $order
88
     * @param int    $limit
89
     * @param int    $start
90
     * @return array
91
     */
92
    public function getAllHots(
93
        $criteria = [],
94
        $asobject = false,
95
        $sort = 'cod_ishot',
96
        $order = 'ASC',
97
        $limit = 0,
98
        $start = 0
99
    ) {
100
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
101
        $ret        = [];
102
        $whereQuery = '';
103
        if (\is_array($criteria) && \count($criteria) > 0) {
104
            $whereQuery = ' WHERE';
105
            foreach ($criteria as $c) {
106
                $whereQuery .= " {$c} AND";
107
            }
108
            $whereQuery = mb_substr($whereQuery, 0, -4);
109
        } elseif (!\is_array($criteria) && $criteria) {
110
            $whereQuery = ' WHERE ' . $criteria;
111
        }
112
        if ($asobject) {
113
            $sql    = 'SELECT * FROM ' . $db->prefix('suico_ishot') . "{$whereQuery} ORDER BY {$sort} {$order}";
114
            $result = $db->query($sql, $limit, $start);
115
            while (false !== ($myrow = $db->fetchArray($result))) {
116
                $ret[] = new self($myrow);
117
            }
118
        } else {
119
            $sql    = 'SELECT cod_ishot FROM ' . $db->prefix(
120
                    'suico_ishot'
121
                ) . "{$whereQuery} ORDER BY {$sort} {$order}";
122
            $result = $db->query($sql, $limit, $start);
123
            while (false !== ($myrow = $db->fetchArray($result))) {
124
                $ret[] = $myrow['suico_ishot_id'];
125
            }
126
        }
127
128
        return $ret;
129
    }
130
}
131