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 openvk\Web\Models\Entities\Video;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class Videos
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $videos;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->videos = $this->context->table("videos");
|
|
|
|
}
|
|
|
|
|
|
|
|
function get(int $id): ?Video
|
|
|
|
{
|
|
|
|
$videos = $this->videos->get($id);
|
|
|
|
if(!$videos) return NULL;
|
|
|
|
|
|
|
|
return new Video($videos);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getByOwnerAndVID(int $owner, int $vId): ?Video
|
|
|
|
{
|
|
|
|
$videos = $this->videos->where([
|
|
|
|
"owner" => $owner,
|
|
|
|
"virtual_id" => $vId,
|
|
|
|
])->fetch();
|
|
|
|
if(!$videos) return NULL;
|
|
|
|
|
|
|
|
return new Video($videos);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getByUser(User $user, int $page = 1, ?int $perPage = NULL): \Traversable
|
|
|
|
{
|
|
|
|
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
|
2021-11-20 12:56:59 +03:00
|
|
|
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);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getUserVideosCount(User $user): int
|
|
|
|
{
|
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
|
|
|
|
2024-10-25 16:28:35 +03:00
|
|
|
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-13 20:57:49 +03:00
|
|
|
$result = $this->videos->where("CONCAT_WS(' ', name, description) LIKE ?", $query)->where("deleted", 0);
|
2024-10-25 16:28:35 +03:00
|
|
|
$order_str = 'id';
|
|
|
|
|
|
|
|
switch($order['type']) {
|
|
|
|
case 'id':
|
|
|
|
$order_str = 'id ' . ($order['invert'] ? 'ASC' : 'DESC');
|
|
|
|
break;
|
|
|
|
}
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2024-10-25 16:28:35 +03:00
|
|
|
foreach($params as $paramName => $paramValue) {
|
|
|
|
switch($paramName) {
|
|
|
|
case "before":
|
|
|
|
$result->where("created < ?", $paramValue);
|
|
|
|
break;
|
|
|
|
case "after":
|
|
|
|
$result->where("created > ?", $paramValue);
|
|
|
|
break;
|
|
|
|
case 'only_youtube':
|
|
|
|
if((int) $paramValue != 1) break;
|
|
|
|
$result->where("link != ?", 'NULL');
|
|
|
|
break;
|
2023-06-10 18:54:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 16:28:35 +03:00
|
|
|
if($order_str)
|
|
|
|
$result->order($order_str);
|
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
|
|
|
|
|
|
|
function getLastVideo(User $user)
|
|
|
|
{
|
|
|
|
$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
|
|
|
}
|