Skip to content
Snippets Groups Projects
Verified Commit c534fe9f authored by CARLIER Maxime's avatar CARLIER Maxime
Browse files

Refactor ViewAnnonceur Controller for Better Efficiency and Readability

parent dadc3c42
No related branches found
No related tags found
No related merge requests found
<?php <?php
/**
* Created by PhpStorm.
* User: ponicorn
* Date: 26/01/15
* Time: 00:25
*/
namespace controller; namespace controller;
...@@ -14,38 +8,27 @@ use model\Photo; ...@@ -14,38 +8,27 @@ use model\Photo;
class viewAnnonceur class viewAnnonceur
{ {
public function __construct() public function afficherAnnonceur($twig, $chemin, $n, $cat)
{ {
} $annonceur = Annonceur::find($n);
if (!$annonceur) {
function afficherAnnonceur($twig, $menu, $chemin, $n, $cat)
{
$this->annonceur = annonceur::find($n);
if (!isset($this->annonceur)) {
echo "404"; echo "404";
return; return;
} }
$tmp = annonce::where('id_annonceur', '=', $n)->get(); $annonces = Annonce::where('id_annonceur', '=', $n)->get()->map(function ($annonce) use ($chemin) {
$annonce->nb_photo = Photo::where('id_annonce', '=', $annonce->id_annonce)->count();
$annonce->url_photo = $annonce->nb_photo > 0
? Photo::where('id_annonce', '=', $annonce->id_annonce)->first()->url_photo
: $chemin . '/img/noimg.png';
$annonces = []; return $annonce;
foreach ($tmp as $a) { });
$a->nb_photo = Photo::where('id_annonce', '=', $a->id_annonce)->count();
if ($a->nb_photo > 0) {
$a->url_photo = Photo::select('url_photo')
->where('id_annonce', '=', $a->id_annonce)
->first()->url_photo;
} else {
$a->url_photo = $chemin . '/img/noimg.png';
}
$annonces[] = $a; echo $twig->loadTemplate("annonceur.html.twig")->render([
} 'annonceur' => $annonceur,
$template = $twig->loadTemplate("annonceur.html.twig"); "chemin" => $chemin,
echo $template->render([ "annonces" => $annonces,
'nom' => $this->annonceur, "categories" => $cat
"chemin" => $chemin, ]);
"annonces" => $annonces,
"categories" => $cat
]);
} }
} }
\ No newline at end of file
...@@ -112,7 +112,7 @@ $app->post('/search/', function () use ($app, $twig, $menu, $chemin, $cat) { ...@@ -112,7 +112,7 @@ $app->post('/search/', function () use ($app, $twig, $menu, $chemin, $cat) {
$app->get('/annonceur/:n', function ($n) use ($twig, $menu, $chemin, $cat) { $app->get('/annonceur/:n', function ($n) use ($twig, $menu, $chemin, $cat) {
$annonceur = new viewAnnonceur(); $annonceur = new viewAnnonceur();
$annonceur->afficherAnnonceur($twig, $menu, $chemin, $n, $cat->getCategories()); $annonceur->afficherAnnonceur($twig, $chemin, $n, $cat->getCategories());
}); });
$app->get('/del/:n', function ($n) use ($twig, $menu, $chemin) { $app->get('/del/:n', function ($n) use ($twig, $menu, $chemin) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment