openvk/Web/Models/Repositories/Users.php

58 lines
1.5 KiB
PHP
Raw Normal View History

2020-06-07 19:04:43 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
use openvk\Web\Models\Entities\User;
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
use Chandler\Security\User as ChandlerUser;
class Users
{
private $context;
private $users;
function __construct()
{
$this->context = DatabaseConnection::i()->getContext();
$this->users = $this->context->table("profiles");
}
private function toUser(?ActiveRow $ar): ?User
{
return is_null($ar) ? NULL : new User($ar);
}
function get(int $id): ?User
{
return $this->toUser($this->users->get($id));
}
function getByShortURL(string $url): ?User
{
return $this->toUser($this->users->where("shortcode", $url)->fetch());
}
function getByChandlerUser(ChandlerUser $user): ?User
{
return $this->toUser($this->users->where("user", $user->getId())->fetch());
}
2020-11-22 13:29:27 +03:00
function find(string $query): Util\EntityStream
2020-06-07 19:04:43 +03:00
{
2020-11-22 13:29:27 +03:00
$query = "%$query%";
$result = $this->users->where("CONCAT_WS(' ', first_name, last_name) LIKE ?", $query);
2020-08-01 17:27:10 +03:00
return new Util\EntityStream("User", $result);
2020-06-07 19:04:43 +03:00
}
function getStatistics(): object
{
return (object) [
"all" => sizeof(clone $this->users),
"active" => sizeof((clone $this->users)->where("online > 0")),
"online" => sizeof((clone $this->users)->where("online >= ?", time() - 900)),
];
}
use \Nette\SmartObject;
}