1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace XoopsModules\Suico; |
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
|
|
|
* @category Module |
19
|
|
|
* @package suico |
20
|
|
|
* @copyright {@link https://xoops.org/ XOOPS Project} |
21
|
|
|
* @license GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
22
|
|
|
* @author Bruno Barthez, Marcello Brandão aka Suico, Mamba, LioMJ <https://xoops.org> |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
use CriteriaElement; |
26
|
|
|
use XoopsDatabase; |
27
|
|
|
use XoopsObject; |
28
|
|
|
use XoopsPersistableObjectHandler; |
29
|
|
|
|
30
|
|
|
require_once XOOPS_ROOT_PATH . '/kernel/object.php'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* suico_visitorshandler class. |
34
|
|
|
* This class provides simple mechanism for Suico\Visitors object |
35
|
|
|
*/ |
36
|
|
|
class VisitorsHandler extends XoopsPersistableObjectHandler |
37
|
|
|
{ |
38
|
|
|
public $helper; |
39
|
|
|
public $isAdmin; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* @param \XoopsDatabase|null $xoopsDatabase |
44
|
|
|
* @param \XoopsModules\Suico\Helper|null $helper |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
?XoopsDatabase $xoopsDatabase = null, |
48
|
|
|
$helper = null |
49
|
|
|
) { |
50
|
|
|
/** @var \XoopsModules\Suico\Helper $this->helper */ |
51
|
|
|
if (null === $helper) { |
52
|
|
|
$this->helper = Helper::getInstance(); |
53
|
|
|
} else { |
54
|
|
|
$this->helper = $helper; |
55
|
|
|
} |
56
|
|
|
$isAdmin = $this->helper->isUserAdmin(); |
|
|
|
|
57
|
|
|
parent::__construct($xoopsDatabase, 'suico_visitors', Visitors::class, 'visit_id', 'uname_visitor'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* create a new Groups |
62
|
|
|
* |
63
|
|
|
* @param bool $isNew flag the new objects as "new"? |
64
|
|
|
* @return \XoopsObject Groups |
65
|
|
|
*/ |
66
|
|
|
public function create( |
67
|
|
|
$isNew = true |
68
|
|
|
) { |
69
|
|
|
$obj = parent::create($isNew); |
70
|
|
|
if ($isNew) { |
71
|
|
|
$obj->setNew(); |
72
|
|
|
} else { |
73
|
|
|
$obj->unsetNew(); |
74
|
|
|
} |
75
|
|
|
$obj->helper = $this->helper; |
76
|
|
|
return $obj; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* retrieve a Suico\Visitors |
81
|
|
|
* |
82
|
|
|
* @param int $id of the Suico\Visitors |
83
|
|
|
* @param null $fields |
|
|
|
|
84
|
|
|
* @return mixed reference to the {@link suico_visitors} object, FALSE if failed |
85
|
|
|
*/ |
86
|
|
|
public function get2( |
87
|
|
|
$id = null, |
88
|
|
|
$fields = null |
|
|
|
|
89
|
|
|
) { |
90
|
|
|
$sql = 'SELECT * FROM ' . $this->db->prefix('suico_visitors') . ' WHERE visit_id=' . $id; |
91
|
|
|
if (!$result = $this->db->query($sql)) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
$numrows = $this->db->getRowsNum($result); |
95
|
|
|
if (1 === $numrows) { |
96
|
|
|
$visitors = new Visitors(); |
97
|
|
|
$visitors->assignVars($this->db->fetchArray($result)); |
98
|
|
|
return $visitors; |
99
|
|
|
} |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* insert a new Visitors in the database |
105
|
|
|
* |
106
|
|
|
* @param \XoopsObject $xoopsObject reference to the {@link Visitors} |
107
|
|
|
* object |
108
|
|
|
* @param bool $force |
109
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
110
|
|
|
*/ |
111
|
|
|
public function insert2( |
112
|
|
|
XoopsObject $xoopsObject, |
113
|
|
|
$force = false |
114
|
|
|
) { |
115
|
|
|
global $xoopsConfig; |
116
|
|
|
if (!$xoopsObject instanceof Visitors) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
if (!$xoopsObject->isDirty()) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
if (!$xoopsObject->cleanVars()) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
$visit_id = $uid_owner = $uid_visitor = ''; |
126
|
|
|
foreach ($xoopsObject->cleanVars as $k => $v) { |
127
|
|
|
${$k} = $v; |
128
|
|
|
} |
129
|
|
|
// $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
130
|
|
|
if ($xoopsObject->isNew()) { |
131
|
|
|
// ajout/modification d'un Suico\Visitors |
132
|
|
|
$xoopsObject = new Visitors(); |
133
|
|
|
$format = 'INSERT INTO %s (visit_id, uid_owner, uid_visitor, uname_visitor, date_visited)'; |
134
|
|
|
$format .= 'VALUES (%u, %u, %u, %s, %u)'; |
135
|
|
|
$sql = \sprintf( |
136
|
|
|
$format, |
137
|
|
|
$this->db->prefix('suico_visitors'), |
138
|
|
|
$visit_id, |
139
|
|
|
$uid_owner, |
140
|
|
|
$uid_visitor, |
141
|
|
|
$this->db->quoteString($uname_visitor), |
|
|
|
|
142
|
|
|
\time() |
143
|
|
|
); |
144
|
|
|
$force = true; |
145
|
|
|
} else { |
146
|
|
|
$format = 'UPDATE %s SET '; |
147
|
|
|
$format .= 'visit_id=%u, uid_owner=%u, uid_visitor=%u, uname_visitor=%s, date_visited=%u '; |
148
|
|
|
$format .= ' WHERE visit_id = %u'; |
149
|
|
|
$sql = \sprintf( |
150
|
|
|
$format, |
151
|
|
|
$this->db->prefix('suico_visitors'), |
152
|
|
|
$visit_id, |
153
|
|
|
$uid_owner, |
154
|
|
|
$uid_visitor, |
155
|
|
|
\time(), |
156
|
|
|
$this->db->quoteString($uname_visitor), |
157
|
|
|
$visit_id |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
if ($force) { |
161
|
|
|
$result = $this->db->queryF($sql); |
162
|
|
|
} else { |
163
|
|
|
$result = $this->db->query($sql); |
164
|
|
|
} |
165
|
|
|
if (!$result) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
if (empty($visit_id)) { |
169
|
|
|
$visit_id = $this->db->getInsertId(); |
170
|
|
|
} |
171
|
|
|
$xoopsObject->assignVar('visit_id', $visit_id); |
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* delete a suico_visitors from the database |
177
|
|
|
* |
178
|
|
|
* @param \XoopsObject $xoopsObject reference to the Suico\Visitors to delete |
179
|
|
|
* @param bool $force |
180
|
|
|
* @return bool FALSE if failed. |
181
|
|
|
*/ |
182
|
|
|
public function delete2( |
183
|
|
|
XoopsObject $xoopsObject, |
184
|
|
|
$force = false |
185
|
|
|
) { |
186
|
|
|
if (!$xoopsObject instanceof Visitors) { |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
$sql = \sprintf( |
190
|
|
|
'DELETE FROM %s WHERE visit_id = %u', |
191
|
|
|
$this->db->prefix('suico_visitors'), |
192
|
|
|
$xoopsObject->getVar('visit_id') |
|
|
|
|
193
|
|
|
); |
194
|
|
|
if ($force) { |
195
|
|
|
$result = $this->db->queryF($sql); |
196
|
|
|
} else { |
197
|
|
|
$result = $this->db->query($sql); |
198
|
|
|
} |
199
|
|
|
if (!$result) { |
200
|
|
|
return false; |
201
|
|
|
} |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* count suico_visitorss matching a condition |
209
|
|
|
* |
210
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
211
|
|
|
* @return int count of suico_visitorss |
212
|
|
|
*/ |
213
|
|
|
public function getCount( |
214
|
|
|
?CriteriaElement $criteriaElement = null |
215
|
|
|
) { |
216
|
|
|
$sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_visitors'); |
217
|
|
|
if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
218
|
|
|
$sql .= ' ' . $criteriaElement->renderWhere(); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
$result = $this->db->query($sql); |
221
|
|
|
if (!$result) { |
222
|
|
|
return 0; |
223
|
|
|
} |
224
|
|
|
[$count] = $this->db->fetchRow($result); |
225
|
|
|
return $count; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* delete suico_visitorss matching a set of conditions |
230
|
|
|
* |
231
|
|
|
* @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
232
|
|
|
* @param bool $force |
233
|
|
|
* @param bool $asObject |
234
|
|
|
* @return bool FALSE if deletion failed |
235
|
|
|
*/ |
236
|
|
|
public function deleteAll( |
237
|
|
|
?CriteriaElement $criteriaElement = null, |
238
|
|
|
$force = true, |
239
|
|
|
$asObject = false |
240
|
|
|
) { |
241
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix('suico_visitors'); |
242
|
|
|
if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
243
|
|
|
$sql .= ' ' . $criteriaElement->renderWhere(); |
244
|
|
|
} |
245
|
|
|
if ($force) { |
246
|
|
|
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
247
|
|
|
return false; |
248
|
|
|
} |
249
|
|
|
} elseif (!$result = $this->db->query($sql)) { |
250
|
|
|
return false; |
251
|
|
|
} |
252
|
|
|
return true; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
|
public function purgeVisits() |
259
|
|
|
{ |
260
|
|
|
$sql = 'DELETE FROM ' . $this->db->prefix( |
261
|
|
|
'suico_visitors' |
262
|
|
|
) . ' WHERE (date_visited<UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))) '; |
263
|
|
|
if (!$result = $this->db->queryF($sql)) { |
|
|
|
|
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
return true; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|