2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
namespace openvk\Web\Models\Repositories;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use openvk\Web\Models\Entities\Video;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class Videos
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $videos;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function __construct()
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->videos = $this->context->table("videos");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function get(int $id): ?Video
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$videos = $this->videos->get($id);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$videos) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
return new Video($videos);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getByOwnerAndVID(int $owner, int $vId): ?Video
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$videos = $this->videos->where([
|
|
|
|
"owner" => $owner,
|
|
|
|
"virtual_id" => $vId,
|
|
|
|
])->fetch();
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$videos) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
return new Video($videos);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getByUser(User $user, int $page = 1, ?int $perPage = null): \Traversable
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
foreach ($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])->page($page, $perPage)->order("created DESC") as $video) {
|
2020-06-24 18:25:43 +03:00
|
|
|
yield new Video($video);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getByUserLimit(User $user, int $offset = 0, int $limit = 10): \Traversable
|
2024-11-17 13:03:49 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
foreach ($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])->limit($limit, $offset)->order("created DESC") as $video) {
|
2024-11-17 13:03:49 +03:00
|
|
|
yield new Video($video);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUserVideosCount(User $user): int
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2021-10-17 12:03:31 +03:00
|
|
|
return sizeof($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0]));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
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%";
|
2024-11-17 13:03:49 +03:00
|
|
|
$result = $this->videos->where("CONCAT_WS(' ', name, description) LIKE ?", $query)->where("deleted", 0)->where("unlisted", 0);
|
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;
|
|
|
|
}
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($params as $paramName => $paramValue) {
|
|
|
|
switch ($paramName) {
|
2024-10-25 16:28:35 +03:00
|
|
|
case "before":
|
|
|
|
$result->where("created < ?", $paramValue);
|
|
|
|
break;
|
|
|
|
case "after":
|
|
|
|
$result->where("created > ?", $paramValue);
|
|
|
|
break;
|
|
|
|
case 'only_youtube':
|
2025-01-31 18:20:13 +03:00
|
|
|
if ((int) $paramValue != 1) {
|
|
|
|
break;
|
|
|
|
}
|
2024-10-25 16:28:35 +03:00
|
|
|
$result->where("link != ?", 'NULL');
|
|
|
|
break;
|
2023-06-10 18:54:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2024-10-25 16:28:35 +03:00
|
|
|
return new Util\EntityStream("Video", $result);
|
2023-06-10 18:54:02 +03:00
|
|
|
}
|
2023-11-15 11:41:18 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getLastVideo(User $user)
|
2023-11-15 11:41:18 +03:00
|
|
|
{
|
|
|
|
$video = $this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])->order("id DESC")->fetch();
|
|
|
|
|
|
|
|
return new Video($video);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|