2026-03-14 21:45:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-03-18 22:20:20 +01:00
|
|
|
namespace MintyPHP\Module\Bookmarks\Repository;
|
2026-03-14 21:45:58 +01:00
|
|
|
|
|
|
|
|
use MintyPHP\DB;
|
2026-03-17 22:58:07 +01:00
|
|
|
use MintyPHP\Repository\Support\RepoQuery;
|
|
|
|
|
use MintyPHP\Repository\Support\RepositoryArrayHelper;
|
2026-03-14 21:45:58 +01:00
|
|
|
|
2026-03-26 12:10:05 +01:00
|
|
|
class BookmarkGroupRepository
|
2026-03-14 21:45:58 +01:00
|
|
|
{
|
2026-03-26 12:10:05 +01:00
|
|
|
use SortOrderTrait;
|
|
|
|
|
|
|
|
|
|
protected function sortOrderTableName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'user_bookmark_groups';
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-14 21:45:58 +01:00
|
|
|
private function unwrapList(mixed $rows): array
|
|
|
|
|
{
|
2026-03-17 22:58:07 +01:00
|
|
|
return RepositoryArrayHelper::unwrapList($rows, 'user_bookmark_groups');
|
2026-03-14 21:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 22:58:07 +01:00
|
|
|
public function listByUser(int $userId, array $options = []): array
|
2026-03-14 21:45:58 +01:00
|
|
|
{
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2026-03-17 22:58:07 +01:00
|
|
|
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($options, 100, 1, 100);
|
2026-03-14 21:45:58 +01:00
|
|
|
$rows = DB::select(
|
2026-03-17 22:58:07 +01:00
|
|
|
'select id, user_id, name, icon, sort_order, created, modified from user_bookmark_groups where user_id = ? order by sort_order, id limit ? offset ?',
|
|
|
|
|
(string) $userId,
|
|
|
|
|
(string) $limit,
|
|
|
|
|
(string) $offset
|
2026-03-14 21:45:58 +01:00
|
|
|
);
|
|
|
|
|
return $this->unwrapList($rows);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function countByUser(int $userId): int
|
|
|
|
|
{
|
|
|
|
|
if ($userId <= 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
$count = DB::selectValue(
|
|
|
|
|
'select count(*) from user_bookmark_groups where user_id = ?',
|
|
|
|
|
(string) $userId
|
|
|
|
|
);
|
|
|
|
|
return $count ? (int) $count : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function findById(int $id, int $userId): array|false
|
|
|
|
|
{
|
|
|
|
|
if ($id <= 0 || $userId <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$rows = DB::select(
|
|
|
|
|
'select id, user_id, name, icon, sort_order, created, modified from user_bookmark_groups where id = ? and user_id = ?',
|
|
|
|
|
(string) $id,
|
|
|
|
|
(string) $userId
|
|
|
|
|
);
|
|
|
|
|
$list = $this->unwrapList($rows);
|
|
|
|
|
return $list[0] ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function create(array $data): int|false
|
|
|
|
|
{
|
|
|
|
|
$userId = (int) ($data['user_id'] ?? 0);
|
|
|
|
|
$name = trim((string) ($data['name'] ?? ''));
|
|
|
|
|
$icon = trim((string) ($data['icon'] ?? 'bi-folder'));
|
|
|
|
|
$sortOrder = (int) ($data['sort_order'] ?? 0);
|
|
|
|
|
if ($userId <= 0 || $name === '' || $icon === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return DB::insert(
|
|
|
|
|
'insert into user_bookmark_groups (user_id, name, icon, sort_order, created) values (?,?,?,?,NOW())',
|
|
|
|
|
(string) $userId,
|
|
|
|
|
$name,
|
|
|
|
|
$icon,
|
|
|
|
|
(string) $sortOrder
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function update(int $id, int $userId, array $data): bool
|
|
|
|
|
{
|
|
|
|
|
if ($id <= 0 || $userId <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sets = [];
|
|
|
|
|
$params = [];
|
|
|
|
|
|
|
|
|
|
if (array_key_exists('name', $data)) {
|
|
|
|
|
$name = trim((string) $data['name']);
|
|
|
|
|
if ($name === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$sets[] = 'name = ?';
|
|
|
|
|
$params[] = $name;
|
|
|
|
|
}
|
|
|
|
|
if (array_key_exists('icon', $data)) {
|
|
|
|
|
$icon = trim((string) $data['icon']);
|
|
|
|
|
if ($icon === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$sets[] = 'icon = ?';
|
|
|
|
|
$params[] = $icon;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($sets === []) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params[] = (string) $id;
|
|
|
|
|
$params[] = (string) $userId;
|
|
|
|
|
|
|
|
|
|
$affected = DB::update(
|
|
|
|
|
'update user_bookmark_groups set ' . implode(', ', $sets) . ' where id = ? and user_id = ?',
|
|
|
|
|
...$params
|
|
|
|
|
);
|
|
|
|
|
return $affected !== false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function delete(int $id, int $userId): bool
|
|
|
|
|
{
|
|
|
|
|
if ($id <= 0 || $userId <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$deleted = DB::delete(
|
|
|
|
|
'delete from user_bookmark_groups where id = ? and user_id = ?',
|
|
|
|
|
(string) $id,
|
|
|
|
|
(string) $userId
|
|
|
|
|
);
|
|
|
|
|
return $deleted !== false && (int) $deleted > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|