This repository has been archived on 2024-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
epubreader/lib/Service/BookmarkService.php

106 lines
2.6 KiB
PHP
Raw Normal View History

2020-04-21 20:37:42 +00:00
<?php
/**
* @author Frank de Lange
* @copyright 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Epubreader\Service;
2020-04-21 20:37:42 +00:00
use OCA\Epubreader\Db\BookmarkMapper;
2023-06-16 19:20:03 +00:00
use OCA\Epubreader\Db\ReaderEntity;
2020-04-21 20:37:42 +00:00
class BookmarkService extends Service {
2023-06-16 14:58:23 +00:00
// "bookmark" name to use for the cursor (current reading position)
public const CURSOR = '__CURSOR__';
public const bookmark_type = 'bookmark';
2020-04-21 20:37:42 +00:00
2023-06-16 19:20:03 +00:00
private BookmarkMapper $bookmarkMapper;
2020-04-21 20:37:42 +00:00
2023-06-16 19:20:03 +00:00
public function __construct(BookmarkMapper $bookmarkMapper) {
2023-06-16 14:58:23 +00:00
parent::__construct($bookmarkMapper);
$this->bookmarkMapper = $bookmarkMapper;
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief get bookmark
*
* bookmark type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc
*
* @param int $fileId
2023-06-16 19:20:03 +00:00
* @param ?string $name
* @param ?string $type
2023-06-16 14:58:23 +00:00
*/
2023-06-16 19:20:03 +00:00
public function get($fileId, ?string $name = null, ?string $type = null): array {
2023-06-16 14:58:23 +00:00
$result = $this->bookmarkMapper->get($fileId, $name, $type);
return array_map(
2023-06-16 19:20:03 +00:00
function (ReaderEntity $entity) {
2023-06-16 14:58:23 +00:00
return $entity->toService();
}, $result);
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief write bookmark
*
* position type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc
*
* @param int $fileId
2023-06-16 19:20:03 +00:00
* @param ?string $name
2023-06-16 14:58:23 +00:00
* @param string $value
2023-06-16 19:20:03 +00:00
* @param ?string $type
* @param ?string $content
2023-06-16 14:58:23 +00:00
*/
2023-06-16 19:20:03 +00:00
public function set(int $fileId, ?string $name = null, string $value, ?string $type = null, ?string $content = null): ReaderEntity {
2023-06-16 14:58:23 +00:00
return $this->bookmarkMapper->set($fileId, $name, $value, $type, $content);
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief get cursor (current position in book)
*
* @param int $fileId
*/
2023-06-16 19:20:03 +00:00
public function getCursor(int $fileId): array {
2023-06-16 14:58:23 +00:00
$result = $this->get($fileId, static::CURSOR);
if (count($result) === 1) {
return $result[0];
}
2023-06-16 19:20:03 +00:00
return [];
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief set cursor (current position in book)
*
* @param int $fileId
* @param string $value
*/
2023-06-16 19:20:03 +00:00
public function setCursor(int $fileId, string $value): ReaderEntity {
2023-06-16 14:58:23 +00:00
return $this->bookmarkMapper->set($fileId, static::CURSOR, $value, static::bookmark_type);
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief delete bookmark
*
* @param int $fileId
2023-06-16 19:20:03 +00:00
* @param ?string $name
* @param ?string $type
2023-06-16 14:58:23 +00:00
*/
2023-06-16 19:20:03 +00:00
public function delete($fileId, ?string $name = null, ?string $type = null): void {
2023-06-16 14:58:23 +00:00
foreach ($this->bookmarkMapper->get($fileId, $name, $type) as $bookmark) {
$this->bookmarkMapper->delete($bookmark);
}
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief delete cursor
*
* @param int $fileId
*/
2023-06-16 19:20:03 +00:00
public function deleteCursor(int $fileId): void {
2023-06-16 14:58:23 +00:00
$this->delete($fileId, static::CURSOR, static::bookmark_type);
}
2020-04-21 20:37:42 +00:00
}