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

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