From cfcdc3a30583ea27671d2e192b79d93b1d2511dd Mon Sep 17 00:00:00 2001
From: Matthieu Guffroy <mattgu74@gmail.com>
Date: Thu, 19 Jun 2014 02:50:14 +0200
Subject: [PATCH] filter view

---
 class/listview.php  |  4 ++--
 class/objet.php     | 16 ++++++++++------
 controller/fact.php |  2 +-
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/class/listview.php b/class/listview.php
index ad78961..4c66fea 100644
--- a/class/listview.php
+++ b/class/listview.php
@@ -6,10 +6,10 @@ class ListView
     protected $objArray;
     public $class;
 
-    public function __construct($class, $title, $id=null)
+    public function __construct($class, $title, $arr=Array())
     {
         $this->title = $title;
-        $this->objArray = $class::getAll($id);
+        $this->objArray = $class::getAll($arr);
         $this->class = $class;
     }
 
diff --git a/class/objet.php b/class/objet.php
index 9198e58..b5f3f1d 100644
--- a/class/objet.php
+++ b/class/objet.php
@@ -23,18 +23,22 @@ class Objet
         return $dbName;
     }
 
-    public static function getAll($id = null)
+    public static function getAll($arr = array())
     {
         $className = get_called_class();
         $dbName = self::dbName();
         $bdd = new Db();
         $result = Array();
-        if($id == null) {
+        if(count($arr) == 0) {
             $requete_prepare = $bdd->db->prepare("SELECT * FROM ".$dbName); // on prépare notre requête
             $requete_prepare->execute();
         } else {
-            $requete_prepare = $bdd->db->prepare("SELECT * FROM ".$dbName." WHERE id = :id");
-            $requete_prepare->execute(array("id" => $id));
+            $req = "SELECT * FROM ".$dbName." WHERE 1=1";
+            foreach($arr as $k => $a) {
+                $req .= " AND $k = :$k";
+            }
+            $requete_prepare = $bdd->db->prepare($req);
+            $requete_prepare->execute($arr);
         }
 
         while($ligne = $requete_prepare->fetch(PDO::FETCH_ASSOC)) {
@@ -160,7 +164,7 @@ class Objet
         return self::dbName()." #".$this->{$this->_primaryAttr};
     }
 
-    public function getList($title, $id=null) {
-        return new ListView(get_called_class(), $title, $id);
+    public function getList($title, $arr=array()) {
+        return new ListView(get_called_class(), $title, $arr);
     }
 }
diff --git a/controller/fact.php b/controller/fact.php
index 40a08d4..06152d7 100644
--- a/controller/fact.php
+++ b/controller/fact.php
@@ -47,7 +47,7 @@ switch($action) {
 		echo ' <a href="'.$base_url.$page.'/addProduit'.'?factId='.$_GET["id"].'" type="button" class="btn btn-success">Ajouter un Produit</a>';
 		echo ' <a href="'.$base_url.$page.'/addOrd'.'?factId='.$_GET["id"].'" type="button" class="btn btn-success">Ajouter une Ordonnance</a>';
         echo '</div>';
-        $list = LigneFacture::getList("Détails de la facture", $_GET["id"]);
+        $list = LigneFacture::getList("Détails de la facture", array("facture" => $_GET["id"]));
         include 'view/list.php';
 		break;
 	default:
-- 
GitLab