This repository has been archived on 2023-12-29. You can view files and clone it, but cannot push or open issues or pull requests.
dogstats/web/models/User.model.php

74 lines
2.3 KiB
PHP

<?php
class User extends Model {
protected $dbTable = "users";
protected $dbFields = Array (
'uuid' => ['type'=>'text','required','unique','autoValMethod'=>'gen_ulid'],
'password' => ['type'=>'text'],
'registered' => ['type'=>'datetime','required','autoValMethod'=>'getDateTime'],
'email' => ['type'=>'email','unique'],
'firstname' => ['type'=>'text'],
'lastname' => ['type'=>'text'],
'birthday' => ['type' => 'text'],
'club' => ['type'=>'text'],
'last_login' => ['type'=>'datetime','required','unique','autoValMethod'=>'getDateTime'],
'token' => ['type'=>'text','required','unique','autoValMethod'=>'uuid4'],
'timezone' => ['type'=>'int'],
'dogs' => ['type'=> 'array','default'=>[]],
'tournaments' =>['type'=> 'array','default'=>[]],
'photo' => ['type'=>'text','default'=>'https://pictshare.net/pj7vzx.jpg'],
'theme' => ['type'=>'text','default'=>'light'], //light/dark
'active' => ['type'=>'int','default'=>0]
);
protected $hidden = ['password','token'];
function login()
{
if(!$this->id) return false;
$this->load();
$this->last_login = $this->getDateTime();
$_SESSION['user'] = $this;
$_SESSION['userid'] = $this->id;
$_SESSION['user']->save();
}
function logout()
{
if(!$this->id) return false;
unset($_SESSION['user']);
}
function removeDog($dogid)
{
if(!$this->id) return false;
if (in_array($dogid, $this->data['dogs']))
{
$this->data['dogs'] = array_diff($this->data['dogs'],[$dogid]);
$this->save();
}
}
function getAll($filtered = true)
{
$keys = $GLOBALS['redis']->keys($this->dbTable.':*');
$users = [];
foreach($keys as $key)
{
$id = end(explode(':',$key));
$u = new User();
$u->load($id);
if($filtered===true)
$thisdata = $u->getDataFiltered();
else
$thisdata = $u->data;
$thisdata['id'] = $id;
$users[] = $thisdata;
}
return $users;
}
}