openvk/Web/Models/Repositories/Clubs.php

138 lines
4.1 KiB
PHP
Raw Normal View History

2020-06-07 19:04:43 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
2023-05-21 18:38:39 +03:00
use openvk\Web\Models\Entities\{Club, Manager};
use openvk\Web\Models\Repositories\{Aliases, Users};
2020-06-07 19:04:43 +03:00
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
class Clubs
{
private $context;
private $clubs;
2023-05-21 18:38:39 +03:00
private $coadmins;
2020-06-07 19:04:43 +03:00
function __construct()
{
2023-05-21 18:38:39 +03:00
$this->context = DatabaseConnection::i()->getContext();
$this->clubs = $this->context->table("groups");
$this->coadmins = $this->context->table("group_coadmins");
2020-06-07 19:04:43 +03:00
}
private function toClub(?ActiveRow $ar): ?Club
{
return is_null($ar) ? NULL : new Club($ar);
}
function getByShortURL(string $url): ?Club
{
$shortcode = $this->toClub($this->clubs->where("shortcode", $url)->fetch());
if ($shortcode)
return $shortcode;
$alias = (new Aliases)->getByShortcode($url);
if (!$alias) return NULL;
if ($alias->getType() !== "club") return NULL;
return $alias->getClub();
2020-06-07 19:04:43 +03:00
}
function get(int $id): ?Club
{
return $this->toClub($this->clubs->get($id));
}
function getByIds(array $ids = []): array
{
$clubs = $this->clubs->select('*')->where('id IN (?)', $ids);
$clubs_array = [];
foreach($clubs as $club) {
$clubs_array[] = $this->toClub($club);
}
return $clubs_array;
}
function find(string $query, array $params = [], array $order = ['type' => 'id', 'invert' => false], int $page = 1, ?int $perPage = NULL): \Traversable
2020-06-07 19:04:43 +03:00
{
$query = "%$query%";
$result = $this->clubs;
$order_str = 'id';
switch($order['type']) {
2024-12-22 15:07:26 +03:00
default:
case 'id':
$order_str = 'id ' . ($order['invert'] ? 'ASC' : 'DESC');
break;
2024-12-22 15:07:26 +03:00
case 'subs':
$sql_query = <<<EOF
SELECT `groups`.`id`, `subs`
FROM `groups`
LEFT JOIN
(SELECT `target`, COUNT(`follower`) as `subs`
FROM `subscriptions`
WHERE `model` = "openvk\\\\Web\\\\Models\\\\Entities\\\\Club"
GROUP BY `target`) `subscriptions` ON `subscriptions`.`target` = `groups`.`id`
WHERE `groups`.`name` LIKE ? OR `groups`.`about` LIKE ?
ORDER BY `subs` DESC
LIMIT ? OFFSET ?;
EOF;
return new Util\RawEntityStream("Club", $sql_query, $query, $query);
}
$result = $result->where("name LIKE ? OR about LIKE ?", $query, $query);
if($order_str)
$result->order($order_str);
return new Util\EntityStream("Club", $result);
2020-06-07 19:04:43 +03:00
}
function getCount(): int
{
return (clone $this->clubs)->count('*');
}
function getPopularClubs(): \Traversable
{
// TODO rewrite
/*
2022-04-18 09:54:56 +03:00
$query = "SELECT ROW_NUMBER() OVER (ORDER BY `subscriptions` DESC) as `place`, `target` as `id`, COUNT(`follower`) as `subscriptions` FROM `subscriptions` WHERE `model` = \"openvk\\\Web\\\Models\\\Entities\\\Club\" GROUP BY `target` ORDER BY `subscriptions` DESC, `id` LIMIT 30;";
$entries = DatabaseConnection::i()->getConnection()->query($query);
foreach($entries as $entry)
yield (object) [
"place" => $entry["place"],
"club" => $this->get($entry["id"]),
"subscriptions" => $entry["subscriptions"],
];
*/
}
2023-05-21 18:38:39 +03:00
function getWriteableClubs(int $id): \Traversable
{
$result = $this->clubs->where("owner", $id);
$coadmins = $this->coadmins->where("user", $id);
foreach($result as $entry) {
yield new Club($entry);
}
foreach($coadmins as $coadmin) {
$cl = new Manager($coadmin);
yield $cl->getClub();
}
}
function getWriteableClubsCount(int $id): int
{
return sizeof($this->clubs->where("owner", $id)) + sizeof($this->coadmins->where("user", $id));
}
2020-06-07 19:04:43 +03:00
use \Nette\SmartObject;
}