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([
|
2023-11-16 19:44:12 +03:00
|
|
|
|
"wall" => $user,
|
|
|
|
|
"pinned" => false,
|
|
|
|
|
"deleted" => false,
|
|
|
|
|
"suggested" => 0,
|
2021-09-20 15:19:15 +03:00
|
|
|
|
])->order("created DESC")->limit($perPage, $offset);
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
|
yield new Post($post);
|
|
|
|
|
}
|
2023-11-16 19:44:12 +03:00
|
|
|
|
|
|
|
|
|
function getOwnersPostsFromWall(int $user, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
|
|
|
|
|
{
|
|
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
|
$offset ??= $perPage * ($page - 1);
|
|
|
|
|
|
|
|
|
|
$sel = $this->posts->where([
|
|
|
|
|
"wall" => $user,
|
|
|
|
|
"deleted" => false,
|
|
|
|
|
"suggested" => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if($user > 0)
|
|
|
|
|
$sel->where("owner", $user);
|
|
|
|
|
else
|
|
|
|
|
$sel->where("flags !=", 0);
|
|
|
|
|
|
|
|
|
|
$sel->order("created DESC")->limit($perPage, $offset);
|
|
|
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
|
yield new Post($post);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOthersPostsFromWall(int $user, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
|
|
|
|
|
{
|
|
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
|
$offset ??= $perPage * ($page - 1);
|
|
|
|
|
|
|
|
|
|
$sel = $this->posts->where([
|
|
|
|
|
"wall" => $user,
|
|
|
|
|
"deleted" => false,
|
|
|
|
|
"suggested" => 0,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if($user > 0)
|
|
|
|
|
$sel->where("owner !=", $user);
|
|
|
|
|
else
|
|
|
|
|
$sel->where("flags", 0);
|
|
|
|
|
|
|
|
|
|
$sel->order("created DESC")->limit($perPage, $offset);
|
|
|
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
|
yield new Post($post);
|
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
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")
|
2023-11-16 19:44:12 +03:00
|
|
|
|
->where("suggested", 0)
|
2020-06-07 19:04:43 +03:00
|
|
|
|
->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%")
|
2023-11-16 19:44:12 +03:00
|
|
|
|
->where("deleted", 0)
|
|
|
|
|
->where("suggested", 0);
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
return sizeof($sel);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 19:44:12 +03:00
|
|
|
|
function getPostById(int $wall, int $post, bool $forceSuggestion = false): ?Post
|
2020-06-07 19:04:43 +03:00
|
|
|
|
{
|
2023-11-16 19:44:12 +03:00
|
|
|
|
$post = $this->posts->where(['wall' => $wall, 'virtual_id' => $post]);
|
|
|
|
|
|
|
|
|
|
if(!$forceSuggestion) {
|
|
|
|
|
$post->where("suggested", 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$post = $post->fetch();
|
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
|
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-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->posts->where("content LIKE ?", "%$query%")->where("deleted", 0)->where("suggested", 0);
|
|
|
|
|
$order_str = 'id';
|
|
|
|
|
|
|
|
|
|
switch($order['type']) {
|
|
|
|
|
case 'id':
|
|
|
|
|
$order_str = 'created ' . ($order['invert'] ? 'ASC' : 'DESC');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach($params as $paramName => $paramValue) {
|
|
|
|
|
if(is_null($paramValue) || $paramValue == '') continue;
|
|
|
|
|
|
|
|
|
|
switch($paramName) {
|
|
|
|
|
case "before":
|
|
|
|
|
$result->where("created < ?", $paramValue);
|
|
|
|
|
break;
|
|
|
|
|
case "after":
|
|
|
|
|
$result->where("created > ?", $paramValue);
|
|
|
|
|
break;
|
|
|
|
|
/*case 'die_in_agony':
|
|
|
|
|
$result->where("nsfw", 1);
|
|
|
|
|
break;
|
|
|
|
|
case 'ads':
|
|
|
|
|
$result->where("ad", 1);
|
|
|
|
|
break;*/
|
2024-10-21 22:01:11 +03:00
|
|
|
|
# БУДЬ МАКСИМАЛЬНО АККУРАТЕН С ДАННЫМ ПАРАМЕТРОМ
|
2024-10-18 22:29:40 +03:00
|
|
|
|
case 'from_me':
|
|
|
|
|
$result->where("owner", $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);
|
2023-06-10 18:54:02 +03:00
|
|
|
|
|
2024-10-18 22:29:40 +03:00
|
|
|
|
return new Util\EntityStream("Post", $result);
|
2023-06-10 18:54:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
|
function getPostCountOnUserWall(int $user): int
|
|
|
|
|
{
|
2023-11-16 19:44:12 +03:00
|
|
|
|
return sizeof($this->posts->where(["wall" => $user, "deleted" => 0, "suggested" => 0]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOwnersCountOnUserWall(int $user): int
|
|
|
|
|
{
|
|
|
|
|
if($user > 0)
|
|
|
|
|
return sizeof($this->posts->where(["wall" => $user, "deleted" => 0, "owner" => $user]));
|
|
|
|
|
else
|
|
|
|
|
return sizeof($this->posts->where(["wall" => $user, "deleted" => 0, "suggested" => 0])->where("flags !=", 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOthersCountOnUserWall(int $user): int
|
|
|
|
|
{
|
|
|
|
|
if($user > 0)
|
|
|
|
|
return sizeof($this->posts->where(["wall" => $user, "deleted" => 0])->where("owner !=", $user));
|
|
|
|
|
else
|
|
|
|
|
return sizeof($this->posts->where(["wall" => $user, "deleted" => 0, "suggested" => 0])->where("flags", 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSuggestedPosts(int $club, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
|
|
|
|
|
{
|
|
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
|
$offset ??= $perPage * ($page - 1);
|
|
|
|
|
|
|
|
|
|
$sel = $this->posts
|
|
|
|
|
->where("deleted", 0)
|
|
|
|
|
->where("wall", $club * -1)
|
|
|
|
|
->order("created DESC")
|
|
|
|
|
->where("suggested", 1)
|
|
|
|
|
->limit($perPage, $offset);
|
|
|
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
|
yield new Post($post);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSuggestedPostsCount(int $club)
|
|
|
|
|
{
|
|
|
|
|
return sizeof($this->posts->where(["wall" => $club * -1, "deleted" => 0, "suggested" => 1]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSuggestedPostsByUser(int $club, int $user, int $page = 1, ?int $perPage = NULL, ?int $offset = NULL): \Traversable
|
|
|
|
|
{
|
|
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
|
$offset ??= $perPage * ($page - 1);
|
|
|
|
|
|
|
|
|
|
$sel = $this->posts
|
|
|
|
|
->where("deleted", 0)
|
|
|
|
|
->where("wall", $club * -1)
|
|
|
|
|
->where("owner", $user)
|
|
|
|
|
->order("created DESC")
|
|
|
|
|
->where("suggested", 1)
|
|
|
|
|
->limit($perPage, $offset);
|
|
|
|
|
|
|
|
|
|
foreach($sel as $post)
|
|
|
|
|
yield new Post($post);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSuggestedPostsCountByUser(int $club, int $user): int
|
|
|
|
|
{
|
|
|
|
|
return sizeof($this->posts->where(["wall" => $club * -1, "deleted" => 0, "suggested" => 1, "owner" => $user]));
|
2020-06-07 19:04:43 +03:00
|
|
|
|
}
|
2022-01-03 18:31:55 +03:00
|
|
|
|
|
|
|
|
|
function getCount(): int
|
|
|
|
|
{
|
2023-12-10 21:55:28 +03:00
|
|
|
|
return (clone $this->posts)->count('*');
|
2022-01-03 18:31:55 +03:00
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
}
|