<?php
namespace App\Person;
use PDO;
use App\Core\AbstractRepository;

class WohnorteRepository extends AbstractRepository
{

  public function getTableName() {
    return "gen_wohnort";
  }

  public function getModelName() {
    return "App\\Person\\WohnorteModel";
  }

  public function allByMember($id) {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $strSQL = "SELECT * FROM gen_member
               LEFT JOIN gen_wohnort ON gen_wohnort.adr_id_f = gen_member.member_id
               LEFT JOIN gen_adresse ON gen_wohnort.adr_id_f = gen_adresse.adr_id
               WHERE (gen_wohnort.member_id_f = $id) ORDER BY ort_von DESC";
    $stmt = $this->pdo->query($strSQL);
    $wohnorte = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
    return $wohnorte;
  }

  public function allByMemberId($id) {
      $table = $this->getTableName();
      $model = $this->getModelName();

      $strSQL = "SELECT * FROM `$table` WHERE (member_id_f = $id)
                                        ORDER BY ort_von";
      $stmt = $this->pdo->query($strSQL);
      $orte = $stmt->fetchALL(PDO::FETCH_CLASS, $model);
      return $orte;
  }

  public function insertForPlace() {
      $table = $this->getTableName();
      $model = $this->getModelName();

      $ort_id = test_input($_POST['ort_id']);
      $member_id_f = test_input($_POST['member_id_f']);
      $adr_id_f = test_input($_POST['adr_id_f']);
      $ort_von = test_input($_POST['ort_von']);
      $ort_bis = test_input($_POST['ort_bis']);

      if (test_input($_POST['submit']) == "submit") {

        $strSQL = "INSERT INTO gen_wohnort (member_id_f, adr_id_f, ort_von, ort_bis)
                   VALUES ('".$member_id_f."', '".$adr_id_f."', '".$ort_von."', '".$ort_bis."')";
      }
      if (test_input($_POST['submit']) == "update") {
        $strSQL = "UPDATE gen_wohnort
                    SET member_id_f='".$member_id_f."', adr_id_f='".$adr_id_f."', ort_von='".$ort_von."', ort_bis='".$ort_bis."'
                    WHERE ort_id ='".$ort_id."'";
      }

      $stmt = $this->pdo->prepare($strSQL);
      $stmt->execute();
  }

}

 ?>
