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/Db/PreferenceMapper.php

107 lines
2.9 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\Db;
2020-04-21 20:37:42 +00:00
2023-05-12 13:20:20 +00:00
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\Epubreader\Utility\Time;
2020-04-21 20:37:42 +00:00
use OCP\IDBConnection;
class PreferenceMapper extends ReaderMapper {
public function __construct(IDBConnection $db, $UserId, Time $time) {
2020-10-31 20:05:16 +00:00
parent::__construct($db, 'reader_prefs', Preference::class, $time);
2020-04-21 20:37:42 +00:00
$this->userId = $UserId;
}
/**
* @brief get preferences for $scope+$fileId+$userId(+$name)
*
* @param string $scope
* @param int $fileId
* @param string $name
* @return array
*/
public function get($scope, $fileId, $name=null) {
2023-05-12 13:20:20 +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)))
->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId)));
if (!empty($name)) {
$query->andWhere($query->expr()->eq('name', $query->createNamedParameter($name)));
2020-04-21 20:37:42 +00:00
}
2023-05-12 13:20:20 +00:00
return $this->findEntities($query);
2020-04-21 20:37:42 +00:00
}
/**
* @brief write preference to database
*
* @param string $scope
* @param int $fileId
* @param string $name
* @param string $value
*
* @return Preference the newly created or updated preference
*/
public function set($scope, $fileId, $name, $value) {
$result = $this->get($scope, $fileId, $name);
if(empty($result)) {
$preference = new Preference();
$preference->setScope($scope);
$preference->setFileId($fileId);
$preference->setUserId($this->userId);
$preference->setName($name);
$preference->setValue($value);
$this->insert($preference);
} else {
$preference = $result[0];
$preference->setValue($value);
$this->update($preference);
}
return $preference;
}
/* currently not used*/
public function deleteForFileId($fileId) {
2020-10-31 20:05:16 +00:00
$sql = "SELECT * FROM `*PREFIX*reader_prefs` WHERE file_id=?";
2020-04-21 20:37:42 +00:00
$args = [ $fileId ];
array_map(
function($entity) {
$this->delete($entity);
}, $this->findEntities($sql, $args)
);
}
/* currently not used*/
public function deleteForUserId($userId) {
2020-10-31 20:05:16 +00:00
$sql = "SELECT * FROM `*PREFIX*reader_prefs` WHERE user_id=?";
2020-04-21 20:37:42 +00:00
$args = [ $userId ];
array_map(
function($entity) {
$this->delete($entity);
}, $this->findEntities($sql, $args)
);
}
}