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 Chandler\Database\DatabaseConnection;
|
|
|
|
use openvk\Web\Models\Entities\Note;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
|
|
|
|
class Notes
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $notes;
|
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->notes = $this->context->table("notes");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
private function toNote(?ActiveRow $ar): ?Note
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
return is_null($ar) ? null : new Note($ar);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function get(int $id): ?Note
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
return $this->toNote($this->notes->get($id));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUserNotes(User $user, int $page = 1, ?int $perPage = null, string $sort = "DESC"): \Traversable
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$perPage ??= OPENVK_DEFAULT_PER_PAGE;
|
|
|
|
foreach ($this->notes->where("owner", $user->getId())->where("deleted", 0)->order("created $sort")->page($page, $perPage) as $album) {
|
2020-06-07 19:04:43 +03:00
|
|
|
yield new Note($album);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2021-11-15 14:00:49 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getNoteById(int $owner, int $note): ?Note
|
2021-11-15 14:00:49 +03:00
|
|
|
{
|
|
|
|
$note = $this->notes->where(['owner' => $owner, 'virtual_id' => $note])->fetch();
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!is_null($note)) {
|
2021-11-15 14:00:49 +03:00
|
|
|
return new Note($note);
|
2025-01-31 18:20:13 +03:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2021-11-15 14:00:49 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUserNotesCount(User $user): int
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2020-06-24 23:20:25 +03:00
|
|
|
return sizeof($this->notes->where("owner", $user->getId())->where("deleted", 0));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
}
|