<?php

class Init {

    const HOMEURL = 'https://localhost/robertos/';
    const IMAGEURL = 'https://localhost/robertos/uploads/';

}

class Functions {

    public function setCookie($name, $value) {
        $cookie_name = $name;
        $cookie_value = $value;
        setcookie($cookie_name, $cookie_value, time() + (604800 * 30), "/"); // 7 days
    }

    public function deleteAllCookies() {
        if (isset($_SERVER['HTTP_COOKIE'])) {
            $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
            foreach ($cookies as $cookie) {
                $parts = explode('=', $cookie);
                $name = trim($parts[0]);
                setcookie($name, '', time() - 1000);
                setcookie($name, '', time() - 1000, '/');
            }
        }
    }

    public function getPoiTypesImage($type) {
        switch ($type) {
            case 1: $thumbnailURL = Init::HOMEURL . "images/pharmacy.png";
                break;
            case 2: $thumbnailURL = Init::HOMEURL . "images/hospital.png";
                break;
            case 3: $thumbnailURL = Init::HOMEURL . "images/post.png";
                break;
            case 4: $thumbnailURL = Init::HOMEURL . "images/exchange.png";
                break;
            default: $thumbnailURL = Init::HOMEURL . "images/imageNotAvailable.jpg";
        }
        return $thumbnailURL;
    }

    public function getGrid($row, $type) {
        $output = "";
        if (isset($row['imageURL'])) {
            if ($row['imageURL'] == "") {
                $thumbnailURL = Init::HOMEURL . "images/imageNotAvailable.jpg";
            } else {
                $thumbnailURL = Init::IMAGEURL . $row['imageURL'];
            }
        } else {
            if (isset($row['typeId'])) {
                $thumbnailURL = Functions::getPoiTypesImage($row['typeId']);
            } else {
                $thumbnailURL = Init::HOMEURL . "images/imageNotAvailable.jpg";
            }
        }
        $output = "<div class='thumb'>
                        <a href='" . Init::HOMEURL . $type . "/" . $row['Id'] . "' title='" . $row['Name'] . "'>
                            <p class='thumbnail'>
                                <img src='" . $thumbnailURL . "' alt='" . $row['Name'] . "'>
                  ";

        if (isset($row['Recommended'])) {
            if ($row['Recommended']) {
                $output .= "<img src='" . Init::HOMEURL . "images/medal.png' alt='Recommended' class='recommended'>";
            }
        }

        $output .= "        </p>
                            <p class='thumbnailName'>" . $row['Name'] . "</p>
                        </a>
                    </div>";

        return $output;
    }

    public function testDestination($destIdNotParsed) {
        $destId = filter_var($destIdNotParsed, FILTER_SANITIZE_NUMBER_INT);
        $db = new Db();
        $result = $db->select("SELECT count(*) as NumRows FROM destinations WHERE Id='" . $destId . "'");
        if ($result[0]["NumRows"] == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function getDestinationNameById($destIdNotParsed) {
        $destId = filter_var($destIdNotParsed, FILTER_SANITIZE_NUMBER_INT);
        $db = new Db();
        $result = $db->select("SELECT Name FROM destinations WHERE Id='" . $destId . "'");
        return $result[0]["Name"];
    }

}

class Db {

    protected static $connection;

    public function connect() {
        if (!isset(self::$connection)) {
            $config = parse_ini_file('config.ini');
            self::$connection = new mysqli($config['host'], $config['username'], $config['password'], $config['dbname']);
        }

        if (self::$connection === false) {
            return false;
        } else {
            self::$connection->set_charset("utf8");
        }

        return self::$connection;
    }

    public function query($query) {
        $connection = $this->connect();
        $result = $connection->query($query);
        return $result;
    }

    public function select($query) {
        $rows = array();
        $result = $this->query($query);
        if ($result === false) {
            return false;
        }

        while ($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }

    public function error() {
        $connection = $this->connect();
        return $connection->error;
    }

    public function quote($value) {
        $connection = $this->connect();
        return "'" . $connection->real_escape_string($value) . "'";
    }

}
