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

Visitors::getAllVisitors()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 47
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 47
rs 8.0555
c 1
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
 * @category        Module
18
 * @package         yogurt
19
 * @copyright       {@link https://xoops.org/ XOOPS Project}
20
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
21
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
22
 */
23
 
24
25
use Xmf\Module\Helper\Permission;
26
use XoopsDatabaseFactory;
27
use XoopsObject;
28
29
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
30
31
/**
32
 * Visitors class.
33
 * $this class is responsible for providing data access mechanisms to the data source
34
 * of XOOPS user class objects.
35
 */
36
class Visitors extends XoopsObject
37
{
38
    public $db;
39
40
    public $helper;
41
42
    public $permHelper;
43
44
    // constructor
45
46
    /**
47
     * Visitors 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
51
    public function __construct($id = null)
52
    {
53
        /** @var Helper $helper */
54
55
        $this->helper = Helper::getInstance();
56
57
        $this->permHelper = new Permission();
58
59
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
60
61
        $this->initVar('cod_visit', \XOBJ_DTYPE_INT, null, false, 10);
62
63
        $this->initVar('uid_owner', \XOBJ_DTYPE_INT, null, false, 10);
64
65
        $this->initVar('uid_visitor', \XOBJ_DTYPE_INT, null, false, 10);
66
67
        $this->initVar('uname_visitor', \XOBJ_DTYPE_TXTBOX, null, false);
68
69
        $this->initVar('date_visited', \XOBJ_DTYPE_INT, 0, false);
70
71
        if (!empty($id)) {
72
            if (\is_array($id)) {
73
                $this->assignVars($id);
74
            } else {
75
                $this->load((int)$id);
76
            }
77
        } else {
78
            $this->setNew();
79
        }
80
    }
81
82
    /**
83
     * @param $id
84
     */
85
86
    public function load($id)
87
    {
88
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_visitors') . ' WHERE cod_visit=' . $id;
89
90
        $myrow = $this->db->fetchArray($this->db->query($sql));
91
92
        $this->assignVars($myrow);
93
94
        if (!$myrow) {
95
            $this->setNew();
96
        }
97
    }
98
99
    /**
100
     * @param array  $criteria
101
     * @param bool   $asobject
102
     * @param string $sort
103
     * @param string $order
104
     * @param int    $limit
105
     * @param int    $start
106
     * @return array
107
     */
108
109
    public function getAllVisitors(
110
        $criteria = [],
111
        $asobject = false,
112
        $sort = 'cod_visit',
113
        $order = 'ASC',
114
        $limit = 0,
115
        $start = 0
116
    ) {
117
        $db = XoopsDatabaseFactory::getDatabaseConnection();
118
119
        $ret = [];
120
121
        $whereQuery = '';
122
123
        if (\is_array($criteria) && \count($criteria) > 0) {
124
            $whereQuery = ' WHERE';
125
126
            foreach ($criteria as $c) {
127
                $whereQuery .= " ${c} AND";
128
            }
129
130
            $whereQuery = mb_substr($whereQuery, 0, -4);
131
        } elseif (!\is_array($criteria) && $criteria) {
132
            $whereQuery = ' WHERE ' . $criteria;
133
        }
134
135
        if (!$asobject) {
136
            $sql = 'SELECT cod_visit FROM ' . $db->prefix(
137
                    'yogurt_visitors'
138
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
139
140
            $result = $db->query($sql, $limit, $start);
141
142
            while (false !== ($myrow = $db->fetchArray($result))) {
143
                $ret[] = $myrow['yogurt_visitors_id'];
144
            }
145
        } else {
146
            $sql = 'SELECT * FROM ' . $db->prefix('yogurt_visitors') . "${whereQuery} ORDER BY ${sort} ${order}";
147
148
            $result = $db->query($sql, $limit, $start);
149
150
            while (false !== ($myrow = $db->fetchArray($result))) {
151
                $ret[] = new static($myrow);
152
            }
153
        }
154
155
        return $ret;
156
    }
157
158
    /**
159
     * Get form
160
     *
161
     * @return \XoopsModules\Yogurt\Form\VisitorsForm
162
     */
163
164
    public function getForm()
165
    {
166
        return new Form\VisitorsForm($this);
167
    }
168
169
    /**
170
     * @return array|null
171
     */
172
173
    public function getGroupsRead()
174
    {
175
        //$permHelper = new \Xmf\Module\Helper\Permission();
176
177
        return $this->permHelper->getGroupsForItem(
178
            'sbcolumns_read',
179
            $this->getVar('cod_visit')
180
        );
181
    }
182
183
    /**
184
     * @return array|null
185
     */
186
187
    public function getGroupsSubmit()
188
    {
189
        //$permHelper = new \Xmf\Module\Helper\Permission();
190
191
        return $this->permHelper->getGroupsForItem(
192
            'sbcolumns_submit',
193
            $this->getVar('cod_visit')
194
        );
195
    }
196
197
    /**
198
     * @return array|null
199
     */
200
201
    public function getGroupsModeration()
202
    {
203
        //$permHelper = new \Xmf\Module\Helper\Permission();
204
205
        return $this->permHelper->getGroupsForItem(
206
            'sbcolumns_moderation',
207
            $this->getVar('cod_visit')
208
        );
209
    }
210
}
211