diff --git a/controller/viewAnnonceur.php b/controller/viewAnnonceur.php index 39b7cc96e1f2cf82b83453d0e296e0da0db001d2..5f9260dfc444015f581d2bdfd025f296c83fc51a 100644 --- a/controller/viewAnnonceur.php +++ b/controller/viewAnnonceur.php @@ -1,10 +1,4 @@ <?php -/** - * Created by PhpStorm. - * User: ponicorn - * Date: 26/01/15 - * Time: 00:25 - */ namespace controller; @@ -14,38 +8,27 @@ use model\Photo; class viewAnnonceur { - public function __construct() + public function afficherAnnonceur($twig, $chemin, $n, $cat) { - } - - function afficherAnnonceur($twig, $menu, $chemin, $n, $cat) - { - $this->annonceur = annonceur::find($n); - if (!isset($this->annonceur)) { + $annonceur = Annonceur::find($n); + if (!$annonceur) { echo "404"; 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 = []; - 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'; - } + return $annonce; + }); - $annonces[] = $a; - } - $template = $twig->loadTemplate("annonceur.html.twig"); - echo $template->render([ - 'nom' => $this->annonceur, - "chemin" => $chemin, - "annonces" => $annonces, - "categories" => $cat - ]); + echo $twig->loadTemplate("annonceur.html.twig")->render([ + 'annonceur' => $annonceur, + "chemin" => $chemin, + "annonces" => $annonces, + "categories" => $cat + ]); } } \ No newline at end of file diff --git a/public/index.php b/public/index.php index b9691dec724c5ad4e318fc62dc5675a04338ae4d..5d00b30d318b2e1db21e3bec60ba235eb907e2c7 100644 --- a/public/index.php +++ b/public/index.php @@ -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) { $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) {