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 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', [ return $this->render('events.html.twig', [
'snapshots' => $snapshots, 'snapshots' => $snapshots,
'days' => $days,
'date' => $date,
]); ]);
} }
} }

View file

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

View file

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