<?php

include_once './includes/database/MySQLDatabase.inc.php';

/**
 * This class represents an entry in the menu.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class MenuEntry {

    private $name;

    private $hint;

    private $url;

    private $identifier;

    private $needSaving;

    private $menuId;

    private $weight;


    public static function Get($identifier) {
        $newEntry = new MenuEntry($identifier);

        $sql = "SELECT * FROM menuEntries WHERE identifier = $identifier LIMIT 1;";

        $result = MySQLDatabase::getInstance()->runRequest($sql);

        $result = $result[0];

        if (!$result) {
            return false;
        }

        $newEntry->name = $result['name'];
        $newEntry->hint = $result['hint'];
        $newEntry->url = $result['url'];
        $newEntry->menuId = $result['menuId'];
        $newEntry->weight = $result['weight'];

        $newEntry->needsSaving = false;

        return $newEntry;
    }

    public static function Create($name, $hint, $url, $menuId, $weight) {

        $name = InputSanitizer::Sanitize($name);
        $hint = InputSanitizer::Sanitize($hint);
        $url = InputSanitizer::Sanitize($url);
        $menuId = InputSanitizer::Number($menuId);
        $weight = InputSanitizer::Number($weight);

        $sql = "INSERT INTO `menuEntries` (
            `identifier`, `name`, `hint`, `url`, `menuId`, `weight`)
            VALUES (
            NULL, '$name', '$hint', '$url', $menuId, $weight);";

        $result = MySQLDatabase::getInstance()->runOperation($sql);

        $sql = "SELECT identifier FROM menuEntries WHERE
            name = $name AND hint = $hint AND url = $url AND menuId = $menuId LIMIT 1;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        $identifier = $result[0]['identifier'];

        $newArticle = Article::Get($identifier);
        return $newArticle;
    }

    public static function Remove($identifier) {
        $sql = "DELETE FROM `menuEntries` WHERE `identifier` = $identifier;";

        $result = MySQLDatabase::getInstance()->runOperation($sql);

        return $result;
    }

    private function __construct($identifier) {
        $this->identifier = $identifier;
    }

    public function __destruct() {
        if ($this->needSaving) {
            $this->save();
        }
    }

    private function save() {
        $sql = "UPDATE `menuEntries` SET
            `name` = '$this->name',
            `hint` = '$this->hint',
            `url` = '$this->url',
            `menuId` = '$this->menuId',
            `weight` = '$this->weight'
            WHERE `identifier` = $this->identifier;";

        $result = MySQLDatabase::getInstance()->runOperation($sql);
    }

    public function getName() {
        /*
        $url = $this->url;

        if (@include_once './pages/' . $url . 'Page.inc.php') {
            $pageClass = $url . 'Page';
            $page = new $pageClass;
            $title = $page->getStaticTitle();
        } else {
         *
        */
        $title = $this->name;

        return $title;
    }

    public function getHint() {
        return $this->hint;
    }

    public function getUrl() {
        return $this->url;
    }

    public function getMenuId() {
        return $this->menuId;
    }

    public function getIdentifier() {
        return $this->identifier;
    }

    public function getWeight() {
        return $this->weight;
    }

    public function setContent($name, $hint, $url, $menuId, $weight) {

        $name = InputSanitizer::Sanitize($name);
        $hint = InputSanitizer::Sanitize($hint);
        $url = InputSanitizer::Sanitize($url);
        $menuId = InputSanitizer::Number($menuId);
        $weight = InputSanitizer::Number($weight);

        $this->name = $name;
        $this->hint = $hint;
        $this->url = $url;
        $this->menuId = $menuId;
        $this->weight = $weight;

        $this->needSaving = true;
    }
}
?>
