From b54dc94a6f7d10f9d5d1d9094f850854fc39a603 Mon Sep 17 00:00:00 2001 From: Matthieu Guffroy <mattgu74@gmail.com> Date: Wed, 18 Jun 2014 14:50:53 +0200 Subject: [PATCH] implement foreignfield --- class/foreignfield.php | 50 ++++++++++++++++++++++++++++++++++++++++++ class/objet.php | 4 ++++ class/race.php | 4 ++++ 3 files changed, 58 insertions(+) create mode 100644 class/foreignfield.php diff --git a/class/foreignfield.php b/class/foreignfield.php new file mode 100644 index 0000000..0ba22c1 --- /dev/null +++ b/class/foreignfield.php @@ -0,0 +1,50 @@ +<?php + +class ForeignField { + protected $name; + protected $value; + protected $primary; + protected $choices; + + public function __construct($name, &$value, $primary=false) { + $this->name = $name; + $this->value =& $value; + $this->primary = $primary; + $this->choices = ucfirst($name)::getAll(); + } + + 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"; } + + $html = '<div class="form-group"> + <label for="form'.$this->name.'">'.ucfirst($this->name).'</label> + <select class="form-control" name="'.$this->name.'">'; + foreach ($this->choices as $value) { + if($this->value == $value->{$value->$_primaryAttr}()) { $selected = "selected" } else { $selected = "" } + $html .= "<option value=".$value->{$value->$_primaryAttr}()." ".$selected." >".$value->str()."</option>"; + } + + $html.= '</select> + </div>'; + return $html; + } + + /* + Load the data from $_POST + 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/objet.php b/class/objet.php index 16527c3..3ef7c64 100644 --- a/class/objet.php +++ b/class/objet.php @@ -136,4 +136,8 @@ class Objet $ligne = $requete_prepare->fetch(PDO::FETCH_ASSOC); $this->fromDb($ligne); } + + public function str() { + return self::dbName()." #".$this->{$this->_primaryAttr}; + } } \ No newline at end of file diff --git a/class/race.php b/class/race.php index 769ae15..70626e0 100644 --- a/class/race.php +++ b/class/race.php @@ -6,6 +6,10 @@ class Race extends Objet protected $nom; protected $espece; protected $prix_intervention; + public $_specialFields = array( + "espece" => array( + "t" => "ForeignField" + )); public function nom() { -- GitLab