2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Repositories;
|
|
|
|
use openvk\Web\Models\Entities\Post;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class Posts
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $posts;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->posts = $this->context->table("posts");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function toPost(?ActiveRow $ar): ?Post
|
|
|
|
{
|
|
|
|
return is_null($ar) ? NULL : new Post($ar);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get(int $id): ?Post
|
|
|
|
{
|
|
|
|
return $this->toPost($this->posts->get($id));
|
|
|
|
}
|
|
|
|
|
2021-09-20 15:19:15 +03:00
|
|
|
function getPinnedPost(int $user): ?Post
|
|
|
|
{
|
|
|
|
$post = (clone $this->posts)->where([
|
|
|
|
"wall" => $user,
|
|
|
|
"pinned" => true,
|
|
|
|
"deleted" => false,
|
|
|
|
])->fetch();
|
|
|
|
|
|
|
|
return $this->toPost($post);
|
|
|
|
}
|
|
|
|
|
2021-11-24 22:24:44 +03:00
|
|
|
function getPostsFromUsersWall(int $user, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2021-09-20 15:19:15 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
2021-11-24 22:24:44 +03:00
|
|
|
$offset ??= $perPage * ($page - 1);
|
2021-09-20 15:19:15 +03:00
|
|
|
|
|
|
|
$pinPost = $this->getPinnedPost($user);
|
2021-11-24 22:24:44 +03:00
|
|
|
if(is_null($offset) || $offset == 0) {
|
|
|
|
if(!is_null($pinPost)) {
|
|
|
|
if($page === 1) {
|
|
|
|
$perPage--;
|
|
|
|
|
|
|
|
yield $pinPost;
|
|
|
|
} else {
|
|
|
|
$offset--;
|
|
|
|
}
|
2021-09-20 15:19:15 +03:00
|
|
|
}
|
2021-11-24 22:24:44 +03:00
|
|
|
} else if(!is_null($offset)) {
|
|
|
|
$offset--;
|
2021-09-20 15:19:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$sel = $this->posts->where([
|
|
|
|
"wall" => $user,
|
|
|
|
"pinned" => false,
|
|
|
|
"deleted" => false,
|
|
|
|
])->order("created DESC")->limit($perPage, $offset);
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
yield new Post($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPostsByHashtag(string $hashtag, int $page = 1, ?int $perPage = NULL): \Traversable
|
|
|
|
{
|
|
|
|
$hashtag = "#$hashtag";
|
|
|
|
$sel = $this->posts
|
2022-08-15 09:08:07 +03:00
|
|
|
->where("MATCH (content) AGAINST (? IN BOOLEAN MODE)", "+$hashtag")
|
2020-06-07 19:04:43 +03:00
|
|
|
->where("deleted", 0)
|
|
|
|
->order("created DESC")
|
|
|
|
->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE);
|
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
yield new Post($post);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPostCountByHashtag(string $hashtag): int
|
|
|
|
{
|
|
|
|
$hashtag = "#$hashtag";
|
|
|
|
$sel = $this->posts
|
|
|
|
->where("content LIKE ?", "%$hashtag%")
|
|
|
|
->where("deleted", 0);
|
|
|
|
|
|
|
|
return sizeof($sel);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPostById(int $wall, int $post): ?Post
|
|
|
|
{
|
|
|
|
$post = $this->posts->where(['wall' => $wall, 'virtual_id' => $post])->fetch();
|
|
|
|
if(!is_null($post))
|
|
|
|
return new Post($post);
|
|
|
|
else
|
2022-05-08 13:06:26 +03:00
|
|
|
return NULL;
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
}
|
2023-07-28 00:44:05 +03:00
|
|
|
|
|
|
|
function find(string $query = "", array $pars = [], string $sort = "id"): Util\EntityStream
|
|
|
|
{
|
|
|
|
$query = "%$query%";
|
|
|
|
|
|
|
|
$notNullParams = [];
|
|
|
|
|
|
|
|
foreach($pars as $paramName => $paramValue)
|
|
|
|
if($paramName != "before" && $paramName != "after")
|
|
|
|
$paramValue != NULL ? $notNullParams+=["$paramName" => "%$paramValue%"] : NULL;
|
|
|
|
else
|
|
|
|
$paramValue != NULL ? $notNullParams+=["$paramName" => "$paramValue"] : NULL;
|
|
|
|
|
|
|
|
$result = $this->posts->where("content LIKE ?", $query)->where("deleted", 0);
|
|
|
|
$nnparamsCount = sizeof($notNullParams);
|
|
|
|
|
|
|
|
if($nnparamsCount > 0) {
|
|
|
|
foreach($notNullParams as $paramName => $paramValue) {
|
|
|
|
switch($paramName) {
|
|
|
|
case "before":
|
|
|
|
$result->where("created < ?", $paramValue);
|
|
|
|
break;
|
|
|
|
case "after":
|
|
|
|
$result->where("created > ?", $paramValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Util\EntityStream("Post", $result->order("$sort"));
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
function getPostCountOnUserWall(int $user): int
|
|
|
|
{
|
|
|
|
return sizeof($this->posts->where(["wall" => $user, "deleted" => 0]));
|
|
|
|
}
|
2022-01-03 18:31:55 +03:00
|
|
|
|
|
|
|
function getCount(): int
|
|
|
|
{
|
|
|
|
return sizeof(clone $this->posts);
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|