<?php

class Poi {

    // database connection and table name
    private $conn;
    private $table_name = "poi";
    // object properties
    public $Id;
    public $destinationId;
    public $typeId;
    public $Name;
    public $Description;
    public $Street;
    public $StreetNumber;
    public $Postcode;
    public $Latitude;
    public $Longtitude;
    public $imageURL;
    public $Recommended;

    public function __construct($db) {
        $this->conn = $db;
    }

    // used by select drop-down list
    public function readAll() {
        //select all data
        $query = "SELECT
                    Id, destinationId, typeId, Name, Description, Street, StreetNumber, Postcode, Latitude, Longtitude, imageURL, Recommended
                FROM
                    " . $this->table_name . "
                ORDER BY
                    Name";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    public function readOne() {
        // query to read single record
        $query = "SELECT
                    Id, destinationId, typeId, Name, Description, Street, StreetNumber, Postcode, Latitude, Longtitude, imageURL, Recommended
            FROM
                " . $this->table_name . "
            WHERE
                Id = ?
            LIMIT
                0,1";

        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // bind id of product to be updated
        $stmt->bindParam(1, $this->Id);

        // execute query
        $stmt->execute();

        // get retrieved row
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        // set values to object properties
        $this->Name = $row['Name'];
        $this->destinationId = $row['destinationId'];
        $this->typeId = $row['typeId'];
        $this->Description = $row['Description'];
        $this->Street = $row['Street'];
        $this->StreetNumber = $row['StreetNumber'];
        $this->Postcode = $row['Postcode'];
        $this->Latitude = $row['Latitude'];
        $this->Longtitude = $row['Longtitude'];
        $this->imageURL = $row['imageURL'];
        $this->Recommended = $row['Recommended'];
    }

    public function readByDestinationId() {
        //select all data

        $query = "SELECT
                     Id, destinationId, typeId, Name, Description, Street, StreetNumber, Postcode, Latitude, Longtitude, imageURL, Recommended
                FROM
                    " . $this->table_name . "
            WHERE
                destinationId = ?
            ORDER BY
                Name";

        // prepare query statement
        $stmt = $this->conn->prepare($query);

        // bind id of product to be updated
        $stmt->bindParam(1, $this->destinationId);

        $stmt->execute();
        //$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return $stmt;
    }

}

?>