2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
namespace openvk\Web\Models\Repositories;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
use openvk\Web\Models\Entities\Application;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
|
|
|
|
class Applications
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $apps;
|
|
|
|
private $appRels;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function __construct()
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->apps = $this->context->table("apps");
|
|
|
|
$this->appRels = $this->context->table("app_users");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
private function toApp(?ActiveRow $ar): ?Application
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
return is_null($ar) ? null : new Application($ar);
|
2022-08-20 21:07:54 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function get(int $id): ?Application
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
return $this->toApp($this->apps->get($id));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getList(int $page = 1, ?int $perPage = null): \Traversable
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
2022-08-20 21:07:54 +03:00
|
|
|
$apps = $this->apps->where("enabled", 1)->page($page, $perPage);
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($apps as $app) {
|
2022-08-20 21:07:54 +03:00
|
|
|
yield new Application($app);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2022-08-20 21:07:54 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getListCount(): int
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
return sizeof($this->apps->where("enabled", 1));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getByOwner(User $owner, int $page = 1, ?int $perPage = null): \Traversable
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
2022-08-20 21:07:54 +03:00
|
|
|
$apps = $this->apps->where("owner", $owner->getId())->page($page, $perPage);
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($apps as $app) {
|
2022-08-20 21:07:54 +03:00
|
|
|
yield new Application($app);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2022-08-20 21:07:54 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getOwnCount(User $owner): int
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
return sizeof($this->apps->where("owner", $owner->getId()));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getInstalled(User $user, int $page = 1, ?int $perPage = null): \Traversable
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
2022-08-20 21:07:54 +03:00
|
|
|
$apps = $this->appRels->where("user", $user->getId())->page($page, $perPage);
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($apps as $appRel) {
|
2022-08-20 21:07:54 +03:00
|
|
|
yield $this->get($appRel->app);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2022-08-20 21:07:54 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getInstalledCount(User $user): int
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
return sizeof($this->appRels->where("user", $user->getId()));
|
|
|
|
}
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function find(string $query = "", array $params = [], array $order = ['type' => 'id', 'invert' => false]): Util\EntityStream
|
2023-06-10 18:54:02 +03:00
|
|
|
{
|
2024-10-25 16:28:35 +03:00
|
|
|
$query = "%$query%";
|
2023-06-10 18:54:02 +03:00
|
|
|
$result = $this->apps->where("CONCAT_WS(' ', name, description) LIKE ?", $query)->where("enabled", 1);
|
2024-10-25 16:28:35 +03:00
|
|
|
$order_str = 'id';
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
switch ($order['type']) {
|
2024-10-25 16:28:35 +03:00
|
|
|
case 'id':
|
|
|
|
$order_str = 'id ' . ($order['invert'] ? 'ASC' : 'DESC');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($order_str) {
|
2024-10-25 16:28:35 +03:00
|
|
|
$result->order($order_str);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2024-10-25 16:28:35 +03:00
|
|
|
return new Util\EntityStream("Application", $result);
|
2023-06-10 18:54:02 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|