memories/lib/Db/TimelineQuery.php

133 lines
4.2 KiB
PHP
Raw Normal View History

2022-08-20 02:53:21 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Exif;
use OCP\IConfig;
use OCP\IDBConnection;
class TimelineQuery {
protected IDBConnection $connection;
public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}
/**
* Process the days response
* @param array $days
*/
private function processDays(&$days) {
foreach($days as &$row) {
$row["day_id"] = intval($row["day_id"]);
$row["count"] = intval($row["count"]);
}
return $days;
}
/**
* Get the days response from the database for the timeline
* @param IConfig $config
* @param string $userId
*/
public function getDays(
IConfig &$config,
string &$user,
): array {
$sql = 'SELECT day_id, COUNT(file_id) AS count
FROM `*PREFIX*memories`
INNER JOIN `*PREFIX*filecache`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`file_id`
AND `*PREFIX*filecache`.`path` LIKE ?
WHERE user_id=?
GROUP BY day_id
ORDER BY day_id DESC';
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$rows = $this->connection->executeQuery($sql, [$path, $user], [
\PDO::PARAM_STR, \PDO::PARAM_STR,
])->fetchAll();
return $this->processDays($rows);
}
/**
* Get the days response from the database for one folder
* @param int $folderId
*/
public function getDaysFolder(int &$folderId) {
$sql = 'SELECT day_id, COUNT(file_id) AS count
FROM `*PREFIX*memories`
INNER JOIN `*PREFIX*filecache`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`file_id`
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
GROUP BY day_id
ORDER BY day_id DESC';
$rows = $this->connection->executeQuery($sql, [$folderId, $folderId], [
\PDO::PARAM_INT, \PDO::PARAM_INT,
])->fetchAll();
return $this->processDays($rows);
}
/**
* Process the single day response
* @param array $day
*/
private function processDay(&$day) {
foreach($day as &$row) {
$row["file_id"] = intval($row["file_id"]);
$row["is_video"] = intval($row["is_video"]);
if (!$row["is_video"]) {
unset($row["is_video"]);
}
}
return $day;
}
/**
* Get a day response from the database for the timeline
* @param IConfig $config
* @param string $userId
* @param int $dayId
*/
public function getDay(
IConfig &$config,
string &$user,
int &$dayId,
): array {
$sql = 'SELECT file_id, *PREFIX*filecache.etag, is_video
FROM *PREFIX*memories
INNER JOIN *PREFIX*filecache
ON *PREFIX*filecache.fileid = *PREFIX*memories.file_id
AND `*PREFIX*filecache`.`path` LIKE ?
WHERE user_id = ? AND day_id = ?
ORDER BY date_taken DESC';
$path = "files" . Exif::getPhotosPath($config, $user) . "%";
$rows = $this->connection->executeQuery($sql, [$path, $user, $dayId], [
\PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_INT,
])->fetchAll();
return $this->processDay($rows);
}
/**
* Get a day response from the database for one folder
* @param int $folderId
* @param int $dayId
*/
public function getDayFolder(
int &$folderId,
int &$dayId,
): array {
$sql = 'SELECT file_id, *PREFIX*filecache.etag, is_video
FROM `*PREFIX*memories`
INNER JOIN `*PREFIX*filecache`
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`file_id`
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
WHERE `*PREFIX*memories`.`day_id`=?';
$rows = $this->connection->executeQuery($sql, [$folderId, $folderId, $dayId], [
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT,
])->fetchAll();
return $this->processDay($rows);
}
}