From 85c041157f440aa56fe54b09671f021d9c2dc58a Mon Sep 17 00:00:00 2001
From: Matthieu Guffroy <mattgu74@gmail.com>
Date: Wed, 18 Jun 2014 10:59:02 +0200
Subject: [PATCH] some work

---
 class/boolfield.php   |  2 +-
 class/field.php       | 19 ++++++++++++++++---
 class/form.php        |  2 +-
 class/objet.php       |  1 +
 controller/animal.php |  2 +-
 view/form.php         |  2 +-
 6 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/class/boolfield.php b/class/boolfield.php
index c1407b9..71d5bc2 100644
--- a/class/boolfield.php
+++ b/class/boolfield.php
@@ -2,7 +2,7 @@
 class BoolField extends Field
 {
 
-    public function html() {
+    public function html($form) {
         $html = '<div class = "form-group">
                     <label for = "form'.$this->name.'">'.ucfirst($this->name).'</label>
                     <input type = "checkbox" class="form-control" name = "'.$this->name.'" value ="'.$this->value.'">
diff --git a/class/field.php b/class/field.php
index 283fdbe..f587932 100644
--- a/class/field.php
+++ b/class/field.php
@@ -3,16 +3,24 @@
 class Field {
 	protected $name;
 	protected $value;
-	public function __construct($name, &$value) {
+	protected $primary;
+
+	public function __construct($name, &$value, $primary=false) {
 		$this->name = $name;
 		$this->value =& $value;
+		$this->primary = $primary;
 	}
 
-	public function html() {
+	public function html($form) {
 		// Todo, if isset($_POST[$this->name]) && !$this->validate() => Show a error msg explanation for this line.
+		$disabled = "";
+
+		if($this->primary && $form->action == "Ajouter") { return ""; }
+		if($this->primary && $form->action == "Modifier") { $disabled = "disabled"; }
+
 		return '<div class="form-group">
     <label for="form'.$this->name.'">'.ucfirst($this->name).'</label>
-    <input type="text" class="form-control" name="'.$this->name.'" value="'.$this->value.'" >
+    <input type="text" class="form-control" name="'.$this->name.'" value="'.$this->value.'" '.$disabled.'>
   </div>';
 	}
 
@@ -21,6 +29,11 @@ class Field {
 		and return false, if data is not valid !
 	*/
 	public function validate() {
+		// On ne peut pas changer la valeur de la clef primaire
+		if($this->primary && $this->value) {
+			return true;
+		}
+
 		$this->value = $_POST[$this->name];
 		return true;
 	}
diff --git a/class/form.php b/class/form.php
index 0c836fb..9b80ff3 100644
--- a/class/form.php
+++ b/class/form.php
@@ -2,7 +2,7 @@
 
 class Form {
 	protected $object;
-	protected $action;
+	public $action;
 	protected $error = null;
 
 	public function __construct(&$obj) {
diff --git a/class/objet.php b/class/objet.php
index 984c3e1..323d4fb 100644
--- a/class/objet.php
+++ b/class/objet.php
@@ -52,6 +52,7 @@ class Objet
         $ret = array();
         foreach(array_keys(get_object_vars($this)) as $keyName) {
             if($keyName == "primaryAttr") { continue; }
+            if($keyName == $this->primaryAttr) { $ret[] = new Field($keyName, $this->$keyName, true); continue; }
             $ret[] = new Field($keyName, $this->$keyName);
         }
         return $ret;
diff --git a/controller/animal.php b/controller/animal.php
index 79858d4..efd52b9 100644
--- a/controller/animal.php
+++ b/controller/animal.php
@@ -18,7 +18,7 @@ switch($action) {
 							"keys" => array("nom", "proprio", "race", "poids", "genre", "sterile", "dateNaissance", "dateDeces", "taille", "code"));
 		include 'view/list.php';
 		break;
-	case "add":
+	case "addAnimal":
 		$animal = new Animal();
 		$formConf = $animal->getForm();
 		include 'view/form.php';
diff --git a/view/form.php b/view/form.php
index 2d0fc36..16d9945 100644
--- a/view/form.php
+++ b/view/form.php
@@ -7,7 +7,7 @@
     <form role="form" method="post">
         <input type="hidden" name="submitForm" value="1">
         <?php foreach($formConf->getFields() as $field): ?>
-            <?php echo $field->html(); ?>
+            <?php echo $field->html($formConf); ?>
         <?php endforeach; ?>
         <button type="submit" class="btn btn-default"><?php echo $formConf->getSubmitText(); ?></button>
     </form>
-- 
GitLab