add events filter

This commit is contained in:
Simon Vieille 2020-03-22 15:31:46 +01:00
parent 164fa8dcb9
commit 75d502af03
Signed by: deblan
GPG key ID: 03383D15A1D31745
3 changed files with 69 additions and 26 deletions

View file

@ -10,14 +10,30 @@ use App\Motion\SnapshotRepository;
class EventController extends AbstractController
{
/**
* @Route("/events", name="events")
* @Route("/events/{date}", name="events")
*/
public function events(SnapshotRepository $snapshotRepository)
public function events(SnapshotRepository $snapshotRepository, string $date = null)
{
$snapshots = $snapshotRepository->find();
$date = $date ?? date('Y-m-d');
$snapshots = $snapshotRepository->find([
'order' => 'DESC',
]);
$days = [];
foreach ($snapshots as $snapshot) {
$day = $snapshot->getDate()->format('d/m');
if (!in_array($day, $days)) {
$days[$snapshot->getDate()->format('Y-m-d')] = $day;
}
}
return $this->render('events.html.twig', [
'snapshots' => $snapshots,
'days' => $days,
'date' => $date,
]);
}
}

View file

@ -14,40 +14,55 @@ use App\Motion\Snapshot;
*/
class SnapshotRepository
{
protected Finder $finder;
protected ParameterBagInterface $parameters;
public function __construct(ParameterBagInterface $parameters)
{
$this->finder = new Finder();
$this->parameters = $parameters;
}
public function find(): array
public function find(array $criteria = []): array
{
$objects = [];
$files = $this->finder
->files()
$criteria['order'] ??= 'ASC';
$directoriesFinder = new Finder();
$directories = $directoriesFinder
->directories()
->in($this->parameters->get('motion_snapshots_directory'))
->name('/.*\.mp4$/s')
->sortByModifiedTime();
->sortByName();
foreach ($files as $file) {
$date = basename($file->getPath());
$time = str_replace(['.mp4', '-'], ['', ':'], $file->getBasename());
$directories->sortByName();
$movie = $file->getPath().'/'.$file->getBasename();
$thumbnail = str_replace('.mp4', '.jpg', $movie);
foreach ($directories as $directory) {
$moviesFinder = new Finder();
$snapshot = new Snapshot();
$snapshot
->setDate(new \DateTime($date.' '.$time))
->setMovie($movie)
->setThumbnail($thumbnail);
$files = $moviesFinder
->files()
->in($directory->getPathName())
->name('*.mp4')
->sortByName();
$objects[] = $snapshot;
$date = $directory->getFilename();
foreach ($files as $file) {
$time = str_replace(['.mp4', '-'], ['', ':'], $file->getBasename());
$thumbnail = str_replace('.mp4', '.jpg', $file->getPathName());
$snapshot = new Snapshot();
$snapshot
->setDate(new \DateTime($date.' '.$time))
->setMovie($file->getPathName())
->setThumbnail($thumbnail);
$objects[] = $snapshot;
}
}
if ($criteria['order'] === 'DESC') {
$objects = array_reverse($objects);
}
return $objects;

View file

@ -3,11 +3,23 @@
{% block title %}{{ parent() }} - Évènements{% endblock %}
{% block body %}
<div class="tabs">
<ul>
{% for key, name in days %}
<li {% if date == key %}class="is-active"{% endif %}>
<a href="{{ path('events', {date: key}) }}">{{ name }}</a>
</li>
{% endfor %}
</ul>
</div>
<div class="container">
{% for snapshot in snapshots|reverse %}
<a href="{{ asset(snapshot.movie) }}" class="snapshot-link">
<img src="{{ snapshot.thumbnail }}" alt="{{ snapshot.date.format('d/m/Y H:i:s') }}" class="box">
</a>
{% for snapshot in snapshots %}
{% if date == snapshot.date.format('Y-m-d') %}
<a href="{{ asset(snapshot.movie) }}" class="snapshot-link">
<img src="{{ snapshot.thumbnail }}" alt="{{ snapshot.date.format('d/m/Y H:i:s') }}" class="box">
</a>
{% endif %}
{% endfor %}
</div>