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
|
|
|
|
2020-04-21 21:45:26 +00:00
|
|
|
use OCA\Epubreader\Utility\Time;
|
2020-04-21 20:37:42 +00:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
2023-08-02 20:04:03 +00:00
|
|
|
class PreferenceMapper extends ReaderMapper
|
|
|
|
{
|
2023-06-16 19:20:03 +00:00
|
|
|
private string $userId;
|
2023-06-16 14:58:23 +00:00
|
|
|
|
2023-08-02 20:04:03 +00:00
|
|
|
public function __construct(IDBConnection $db, string $UserId, Time $time)
|
|
|
|
{
|
2023-06-16 14:58:23 +00:00
|
|
|
parent::__construct($db, 'reader_prefs', Preference::class, $time);
|
|
|
|
$this->userId = $UserId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get preferences for $scope+$fileId+$userId(+$name)
|
|
|
|
*
|
2023-06-16 19:20:03 +00:00
|
|
|
* @return ReaderEntity[]
|
2023-06-16 14:58:23 +00:00
|
|
|
*/
|
2023-08-02 20:04:03 +00:00
|
|
|
public function get(string $scope, int $fileId, ?string $name = null): array
|
|
|
|
{
|
2023-06-16 14:58:23 +00:00
|
|
|
$query = $this->db->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from($this->getTableName())
|
|
|
|
->where($query->expr()->eq('scope', $query->createNamedParameter($scope)))
|
|
|
|
->andWhere($query->expr()->eq('file_id', $query->createNamedParameter($fileId)))
|
2023-08-02 20:04:03 +00:00
|
|
|
->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId)))
|
|
|
|
;
|
2023-06-16 14:58:23 +00:00
|
|
|
|
|
|
|
if (!empty($name)) {
|
|
|
|
$query->andWhere($query->expr()->eq('name', $query->createNamedParameter($name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->findEntities($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief write preference to database
|
|
|
|
*
|
2023-06-16 19:20:03 +00:00
|
|
|
* @return ReaderEntity the newly created or updated preference
|
2023-06-16 14:58:23 +00:00
|
|
|
*/
|
2023-08-02 20:04:03 +00:00
|
|
|
public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity
|
|
|
|
{
|
2023-06-16 14:58:23 +00:00
|
|
|
$result = $this->get($scope, $fileId, $name);
|
|
|
|
|
2023-08-02 20:04:03 +00:00
|
|
|
if (empty($result)) {
|
2023-06-16 14:58:23 +00:00
|
|
|
$preference = new Preference();
|
|
|
|
$preference->setScope($scope);
|
|
|
|
$preference->setFileId($fileId);
|
|
|
|
$preference->setUserId($this->userId);
|
|
|
|
$preference->setName($name);
|
|
|
|
$preference->setValue($value);
|
|
|
|
|
|
|
|
$this->insert($preference);
|
2023-06-17 22:26:54 +00:00
|
|
|
} elseif ($result[0] instanceof Preference) {
|
2023-06-16 14:58:23 +00:00
|
|
|
$preference = $result[0];
|
|
|
|
$preference->setValue($value);
|
|
|
|
|
|
|
|
$this->update($preference);
|
2023-06-17 22:26:54 +00:00
|
|
|
} else {
|
|
|
|
$preference = new Preference();
|
2023-06-16 14:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $preference;
|
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
}
|