memories/lib/Db/TimelineQueryAlbums.php

52 lines
1.6 KiB
PHP
Raw Normal View History

2022-10-26 22:12:46 +00:00
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCP\IDBConnection;
trait TimelineQueryAlbums
{
protected IDBConnection $connection;
public function getAlbums(string $uid)
{
$query = $this->connection->getQueryBuilder();
// SELECT everything from albums
2022-10-26 22:48:46 +00:00
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'), 'count');
$query->select('pa.*', $count)->from('photos_albums', 'pa')->where(
2022-10-26 22:12:46 +00:00
$query->expr()->eq('user', $query->createNamedParameter($uid)),
);
2022-10-26 22:48:46 +00:00
// WHERE there are items with this tag
$query->innerJoin('pa', 'photos_albums_files', 'paf', $query->expr()->andX(
$query->expr()->eq('paf.album_id', 'pa.album_id'),
));
// WHERE these items are memories indexed photos
$query->innerJoin('paf', 'memories', 'm', $query->expr()->eq('m.fileid', 'paf.file_id'));
// WHERE these photos are in the filecache
$query->innerJoin('m', 'filecache', 'f', $query->expr()->eq('m.fileid', 'f.fileid'),);
2022-10-26 22:12:46 +00:00
// GROUP and ORDER by
2022-10-26 22:48:46 +00:00
$query->groupBy('pa.album_id');
2022-10-26 22:12:46 +00:00
$query->orderBy('pa.created', 'DESC');
$query->addOrderBy('pa.album_id', 'DESC'); // tie-breaker
// FETCH all albums
$albums = $query->executeQuery()->fetchAll();
// Post process
foreach ($albums as &$row) {
2022-10-26 22:48:46 +00:00
$row['album_id'] = (int) $row['album_id'];
$row['created'] = (int) $row['created'];
2022-10-26 22:12:46 +00:00
$row['last_added_photo'] = (int) $row['last_added_photo'];
}
return $albums;
}
}