XoopsModules25x /
suico
| 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 CriteriaElement; |
||
| 32 | use XoopsDatabase; |
||
| 33 | use XoopsObject; |
||
| 34 | use XoopsPersistableObjectHandler; |
||
| 35 | |||
| 36 | require_once XOOPS_ROOT_PATH . '/kernel/object.php'; |
||
| 37 | |||
| 38 | // ------------------------------------------------------------------------- |
||
| 39 | // ------------------Yogurt\Visitors user handler class ------------------- |
||
| 40 | // ------------------------------------------------------------------------- |
||
| 41 | |||
| 42 | /** |
||
| 43 | * yogurt_visitorshandler class. |
||
| 44 | * This class provides simple mecanisme for Yogurt\Visitors object |
||
| 45 | */ |
||
| 46 | class VisitorsHandler extends XoopsPersistableObjectHandler |
||
| 47 | { |
||
| 48 | public $helper; |
||
| 49 | |||
| 50 | public $isAdmin; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 55 | * @param \XoopsModules\Yogurt\Helper|null $helper |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 58 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 59 | $helper = null |
||
| 60 | ) { |
||
| 61 | /** @var \XoopsModules\Yogurt\Helper $this ->helper */ |
||
| 62 | if (null === $helper) { |
||
| 63 | $this->helper = Helper::getInstance(); |
||
| 64 | } else { |
||
| 65 | $this->helper = $helper; |
||
| 66 | } |
||
| 67 | $isAdmin = $this->helper->isUserAdmin(); |
||
| 68 | parent::__construct($xoopsDatabase, 'yogurt_visitors', Visitors::class, 'cod_visit', 'uname_visitor'); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * create a new Groups |
||
| 73 | * |
||
| 74 | * @param bool $isNew flag the new objects as "new"? |
||
| 75 | * @return \XoopsObject Groups |
||
| 76 | */ |
||
| 77 | public function create( |
||
| 78 | $isNew = true |
||
| 79 | ) { |
||
| 80 | $obj = parent::create($isNew); |
||
| 81 | if ($isNew) { |
||
| 82 | $obj->setNew(); |
||
| 83 | } else { |
||
| 84 | $obj->unsetNew(); |
||
| 85 | } |
||
| 86 | $obj->helper = $this->helper; |
||
| 87 | |||
| 88 | return $obj; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * retrieve a Yogurt\Visitors |
||
| 93 | * |
||
| 94 | * @param int $id of the Yogurt\Visitors |
||
| 95 | * @param null $fields |
||
| 96 | * @return mixed reference to the {@link yogurt_visitors} object, FALSE if failed |
||
| 97 | */ |
||
| 98 | public function get2( |
||
| 99 | $id = null, |
||
| 100 | $fields = null |
||
| 101 | ) { |
||
| 102 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_visitors') . ' WHERE cod_visit=' . $id; |
||
| 103 | if (!$result = $this->db->query($sql)) { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | $numrows = $this->db->getRowsNum($result); |
||
| 107 | if (1 === $numrows) { |
||
| 108 | $visitors = new Visitors(); |
||
| 109 | $visitors->assignVars($this->db->fetchArray($result)); |
||
| 110 | |||
| 111 | return $visitors; |
||
| 112 | } |
||
| 113 | |||
| 114 | return false; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * insert a new Visitors in the database |
||
| 119 | * |
||
| 120 | * @param \XoopsObject $xoopsObject reference to the {@link Visitors} |
||
| 121 | * object |
||
| 122 | * @param bool $force |
||
| 123 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 124 | */ |
||
| 125 | public function insert2( |
||
| 126 | XoopsObject $xoopsObject, |
||
| 127 | $force = false |
||
| 128 | ) { |
||
| 129 | global $xoopsConfig; |
||
| 130 | if (!$xoopsObject instanceof Visitors) { |
||
| 131 | return false; |
||
| 132 | } |
||
| 133 | if (!$xoopsObject->isDirty()) { |
||
| 134 | return true; |
||
| 135 | } |
||
| 136 | if (!$xoopsObject->cleanVars()) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | $cod_visit = $uid_owner = $uid_visitor = ''; |
||
| 140 | |||
| 141 | foreach ($xoopsObject->cleanVars as $k => $v) { |
||
| 142 | ${$k} = $v; |
||
| 143 | } |
||
| 144 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 145 | |||
| 146 | if ($xoopsObject->isNew()) { |
||
| 147 | // ajout/modification d'un Yogurt\Visitors |
||
| 148 | $xoopsObject = new Visitors(); |
||
| 149 | $format = 'INSERT INTO %s (cod_visit, uid_owner, uid_visitor,uname_visitor)'; |
||
| 150 | $format .= 'VALUES (%u, %u, %u, %s)'; |
||
| 151 | $sql = \sprintf( |
||
| 152 | $format, |
||
| 153 | $this->db->prefix('yogurt_visitors'), |
||
| 154 | $cod_visit, |
||
| 155 | $uid_owner, |
||
| 156 | $uid_visitor, |
||
| 157 | $this->db->quoteString($uname_visitor) |
||
| 158 | ); |
||
| 159 | $force = true; |
||
| 160 | } else { |
||
| 161 | $format = 'UPDATE %s SET '; |
||
| 162 | $format .= 'cod_visit=%u, uid_owner=%u, uid_visitor=%u, uname_visitor=%s '; |
||
| 163 | $format .= ' WHERE cod_visit = %u'; |
||
| 164 | $sql = \sprintf( |
||
| 165 | $format, |
||
| 166 | $this->db->prefix('yogurt_visitors'), |
||
| 167 | $cod_visit, |
||
| 168 | $uid_owner, |
||
| 169 | $uid_visitor, |
||
| 170 | $this->db->quoteString($uname_visitor), |
||
| 171 | $cod_visit |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | if ($force) { |
||
| 175 | $result = $this->db->queryF($sql); |
||
| 176 | } else { |
||
| 177 | $result = $this->db->query($sql); |
||
| 178 | } |
||
| 179 | if (!$result) { |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | if (empty($cod_visit)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 183 | $cod_visit = $this->db->getInsertId(); |
||
| 184 | } |
||
| 185 | $xoopsObject->assignVar('cod_visit', $cod_visit); |
||
| 186 | |||
| 187 | return true; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * delete a yogurt_visitors from the database |
||
| 192 | * |
||
| 193 | * @param \XoopsObject $xoopsObject reference to the Yogurt\Visitors to delete |
||
| 194 | * @param bool $force |
||
| 195 | * @return bool FALSE if failed. |
||
| 196 | */ |
||
| 197 | public function delete2( |
||
| 198 | XoopsObject $xoopsObject, |
||
| 199 | $force = false |
||
| 200 | ) { |
||
| 201 | if (!$xoopsObject instanceof Visitors) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | $sql = \sprintf( |
||
| 205 | 'DELETE FROM %s WHERE cod_visit = %u', |
||
| 206 | $this->db->prefix('yogurt_visitors'), |
||
| 207 | $xoopsObject->getVar('cod_visit') |
||
| 208 | ); |
||
| 209 | if ($force) { |
||
| 210 | $result = $this->db->queryF($sql); |
||
| 211 | } else { |
||
| 212 | $result = $this->db->query($sql); |
||
| 213 | } |
||
| 214 | if (!$result) { |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | |||
| 218 | return true; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * retrieve yogurt_visitorss from the database |
||
| 223 | * |
||
| 224 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met |
||
| 225 | * @param bool $id_as_key use the UID as key for the array? |
||
| 226 | * @param bool $as_object |
||
| 227 | * @return array array of {@link Visitors} objects |
||
| 228 | */ |
||
| 229 | public function &getObjects( |
||
| 230 | ?CriteriaElement $criteriaElement = null, |
||
| 231 | $id_as_key = false, |
||
| 232 | $as_object = true |
||
| 233 | ) { |
||
| 234 | $ret = []; |
||
| 235 | $limit = $start = 0; |
||
| 236 | $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_visitors'); |
||
| 237 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 238 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 239 | if ('' !== $criteriaElement->getSort()) { |
||
| 240 | $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder(); |
||
| 241 | } |
||
| 242 | $limit = $criteriaElement->getLimit(); |
||
| 243 | $start = $criteriaElement->getStart(); |
||
| 244 | } |
||
| 245 | $result = $this->db->query($sql, $limit, $start); |
||
| 246 | if (!$result) { |
||
| 247 | return $ret; |
||
| 248 | } |
||
| 249 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 250 | $visitors = new Visitors(); |
||
| 251 | $visitors->assignVars($myrow); |
||
| 252 | if (!$id_as_key) { |
||
| 253 | $ret[] = &$yogurt_visitors; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 254 | } else { |
||
| 255 | $ret[$myrow['cod_visit']] = &$yogurt_visitors; |
||
| 256 | } |
||
| 257 | unset($yogurt_visitors); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $ret; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * count yogurt_visitorss matching a condition |
||
| 265 | * |
||
| 266 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match |
||
| 267 | * @return int count of yogurt_visitorss |
||
| 268 | */ |
||
| 269 | public function getCount( |
||
| 270 | ?CriteriaElement $criteriaElement = null |
||
| 271 | ) { |
||
| 272 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_visitors'); |
||
| 273 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 274 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 275 | } |
||
| 276 | $result = $this->db->query($sql); |
||
| 277 | if (!$result) { |
||
| 278 | return 0; |
||
| 279 | } |
||
| 280 | [$count] = $this->db->fetchRow($result); |
||
| 281 | |||
| 282 | return $count; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * delete yogurt_visitorss matching a set of conditions |
||
| 287 | * |
||
| 288 | * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} |
||
| 289 | * @param bool $force |
||
| 290 | * @param bool $asObject |
||
| 291 | * @return bool FALSE if deletion failed |
||
| 292 | */ |
||
| 293 | public function deleteAll( |
||
| 294 | ?CriteriaElement $criteriaElement = null, |
||
| 295 | $force = true, |
||
| 296 | $asObject = false |
||
| 297 | ) { |
||
| 298 | $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_visitors'); |
||
| 299 | if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) { |
||
| 300 | $sql .= ' ' . $criteriaElement->renderWhere(); |
||
| 301 | } |
||
| 302 | if ($force) { |
||
| 303 | if (!$result = $this->db->queryF($sql)) { |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | } elseif (!$result = $this->db->query($sql)) { |
||
| 307 | return false; |
||
| 308 | } |
||
| 309 | |||
| 310 | return true; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return bool |
||
| 315 | */ |
||
| 316 | public function purgeVisits() |
||
| 317 | { |
||
| 318 | $sql = 'DELETE FROM ' . $this->db->prefix( |
||
| 319 | 'yogurt_visitors' |
||
| 320 | ) . ' WHERE (date_visited<(DATE_SUB(NOW(), INTERVAL 7 DAY))) '; |
||
| 321 | |||
| 322 | if (!$result = $this->db->queryF($sql)) { |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | |||
| 326 | return true; |
||
| 327 | } |
||
| 328 | } |
||
| 329 |