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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 21:45:26 +00:00
|
|
|
namespace OCA\Epubreader\Db;
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
use OCA\Epubreader\Utility\Time;
|
2020-04-21 20:37:42 +00:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
class BookmarkMapper extends ReaderMapper {
|
|
|
|
|
2023-06-16 19:20:03 +00:00
|
|
|
private string $userId;
|
2023-06-16 14:58:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDbConnection $db
|
2023-06-16 19:20:03 +00:00
|
|
|
* @param string $UserId
|
2023-06-16 14:58:23 +00:00
|
|
|
* @param Time $time
|
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function __construct(IDBConnection $db, string $UserId, Time $time) {
|
2023-06-16 14:58:23 +00:00
|
|
|
parent::__construct($db, 'reader_bookmarks', Bookmark::class, $time);
|
|
|
|
$this->userId = $UserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get bookmarks for $fileId+$userId(+$name)
|
2023-06-16 19:20:03 +00:00
|
|
|
* @param int $fileId
|
|
|
|
* @param ?string $name
|
|
|
|
* @param ?string $type
|
|
|
|
*
|
|
|
|
* @return ReaderEntity[]
|
2023-06-16 14:58:23 +00:00
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function get(int $fileId, ?string $name = null, ?string $type = null): array {
|
2023-06-16 14:58:23 +00:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from($this->getTableName())
|
|
|
|
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId)))
|
|
|
|
->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId)));
|
|
|
|
|
|
|
|
if ($type !== null) {
|
|
|
|
$query->andWhere($query->expr()->eq('type', $query->createNamedParameter($type)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name !== null) {
|
|
|
|
$query->andWhere($query->expr()->eq('name', $query->createNamedParameter($name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findEntities($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief write bookmark to database
|
|
|
|
*
|
|
|
|
* @param int $fileId
|
2023-06-17 22:08:38 +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
|
|
|
* @return ReaderEntity the newly created or updated bookmark
|
2023-06-16 14:58:23 +00:00
|
|
|
*/
|
2023-06-17 22:08:38 +00:00
|
|
|
public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): ReaderEntity {
|
2023-06-16 19:20:03 +00:00
|
|
|
/** @var Bookmark[] $result */
|
2023-06-16 14:58:23 +00:00
|
|
|
$result = $this->get($fileId, $name);
|
|
|
|
|
|
|
|
if(empty($result)) {
|
|
|
|
// anonymous bookmarks are named after their contents
|
2023-06-17 22:08:38 +00:00
|
|
|
if (empty($name)) {
|
2023-06-16 14:58:23 +00:00
|
|
|
$name = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default type is "bookmark"
|
|
|
|
if (null === $type) {
|
|
|
|
$type = "bookmark";
|
|
|
|
}
|
|
|
|
|
|
|
|
$bookmark = new Bookmark();
|
|
|
|
$bookmark->setFileId($fileId);
|
|
|
|
$bookmark->setUserId($this->userId);
|
|
|
|
$bookmark->setType($type);
|
|
|
|
$bookmark->setName($name);
|
|
|
|
$bookmark->setValue($value);
|
2023-06-16 19:20:03 +00:00
|
|
|
$bookmark->setContent($content ?? '');
|
2023-06-16 14:58:23 +00:00
|
|
|
|
|
|
|
$this->insert($bookmark);
|
|
|
|
} else {
|
|
|
|
$bookmark = $result[0];
|
|
|
|
$bookmark->setValue($value);
|
2023-06-16 19:20:03 +00:00
|
|
|
$bookmark->setContent($content ?? '');
|
2023-06-16 14:58:23 +00:00
|
|
|
|
|
|
|
$this->update($bookmark);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $bookmark;
|
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
}
|