diff --git a/class/foreignfield.php b/class/foreignfield.php new file mode 100644 index 0000000000000000000000000000000000000000..12fab0305b8f228a0cb674fe0fe5b468178d8be4 --- /dev/null +++ b/class/foreignfield.php @@ -0,0 +1,51 @@ +<?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; + $cl = ucfirst($name); + $this->choices = $cl::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 932ce1815d21b2191ded4895e9040b9c5f42e6f7..e9ca9d41efe0b67c6272b43a7072d1cefcdde9a3 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 8cc3e75657ee382c57bea950336a145e2f83bd19..48522dd66fc05df838fdca0c037c103676374418 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 id() {