2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Repositories;
|
|
|
|
use openvk\Web\Models\Entities\Postable;
|
|
|
|
use openvk\Web\Models\Entities\Comment;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class Comments
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $comments;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->comments = $this->context->table("comments");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function toComment(?ActiveRow $ar): ?Comment
|
|
|
|
{
|
|
|
|
return is_null($ar) ? NULL : new Comment($ar);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get(int $id): ?Comment
|
|
|
|
{
|
|
|
|
return $this->toComment($this->comments->get($id));
|
|
|
|
}
|
|
|
|
|
2022-07-23 15:50:00 +03:00
|
|
|
function getCommentsByTarget(Postable $target, int $page, ?int $perPage = NULL, ?string $sort = "ASC"): \Traversable
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$comments = $this->comments->where([
|
|
|
|
"model" => get_class($target),
|
|
|
|
"target" => $target->getId(),
|
|
|
|
"deleted" => false,
|
2022-07-23 15:50:00 +03:00
|
|
|
])->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE)->order("created ".$sort);;
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
foreach($comments as $comment)
|
|
|
|
yield $this->toComment($comment);
|
|
|
|
}
|
2021-11-28 14:39:42 +03:00
|
|
|
|
|
|
|
function getLastCommentsByTarget(Postable $target, ?int $count = NULL): \Traversable
|
|
|
|
{
|
|
|
|
$comments = $this->comments->where([
|
|
|
|
"model" => get_class($target),
|
|
|
|
"target" => $target->getId(),
|
|
|
|
"deleted" => false,
|
|
|
|
])->page(1, $count ?? OPENVK_DEFAULT_PER_PAGE)->order("created DESC");
|
|
|
|
|
|
|
|
$comments = array_reverse(iterator_to_array($comments));
|
|
|
|
foreach($comments as $comment)
|
|
|
|
yield $this->toComment($comment);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function getCommentsCountByTarget(Postable $target): int
|
|
|
|
{
|
|
|
|
return sizeof($this->comments->where([
|
|
|
|
"model" => get_class($target),
|
|
|
|
"target" => $target->getId(),
|
|
|
|
"deleted" => false,
|
|
|
|
]));
|
|
|
|
}
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2024-10-18 22:29:40 +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-18 22:29:40 +03:00
|
|
|
$result = $this->comments->where("content LIKE ?", "%$query%")->where("deleted", 0);
|
|
|
|
$order_str = 'id';
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2024-10-18 22:29:40 +03:00
|
|
|
switch($order['type']) {
|
|
|
|
case 'id':
|
|
|
|
$order_str = 'created ' . ($order['invert'] ? 'ASC' : 'DESC');
|
|
|
|
break;
|
|
|
|
}
|
2023-06-10 18:54:02 +03:00
|
|
|
|
2024-10-18 22:29:40 +03:00
|
|
|
foreach($params as $paramName => $paramValue) {
|
|
|
|
switch($paramName) {
|
|
|
|
case "before":
|
|
|
|
$result->where("created < ?", $paramValue);
|
|
|
|
break;
|
|
|
|
case "after":
|
|
|
|
$result->where("created > ?", $paramValue);
|
|
|
|
break;
|
2023-06-10 18:54:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 22:29:40 +03:00
|
|
|
if($order_str)
|
|
|
|
$result->order($order_str);
|
|
|
|
|
|
|
|
return new Util\EntityStream("Comment", $result);
|
2023-06-10 18:54:02 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|