diff --git a/class/foreignfield.php b/class/foreignfield.php new file mode 100644 index 0000000000000000000000000000000000000000..0ba22c126ab6dc04de629a0abd5a4b372463a5f5 --- /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 16527c332682df350d92ff95e92626a9331b15f9..3ef7c64f6ae26c935ddcb8b663bb3519eb857a9a 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 769ae15ac347e9529a2edb86b925e4eb9c64cbff..70626e08300b5d758085449ce035b43d1aadd0d6 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() {