<?php

include_once './includes/users/Member.inc.php';

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

include_once './includes/utils/InputSanitizer.inc.php';
include_once './includes/utils/DateFormatter.inc.php';
include_once './includes/utils/DateParser.inc.php';

/**
 * This class represents an article on the news page.
 *
 * An article consists of an author, a text, a date of privateation,
 * a date of writing, a text and an abstract.
 *
 * @author Thomas Schwery
 */
class Article {

    /**
     * An unique identifier for the article.
     * @var int
     */
    private $identifier;

    /**
     * A text summarising the article.
     * @var string
     */
    private $abstract;

    /**
     * The identifier of the user writing the article.
     * @var int
     */
    private $authorId;

    /**
     * The content of the article.
     * @var string
     */
    private $text;

    /**
     * The date of the article.
     * @var int
     */
    private $date;

    /**
     * The date at which the article was written.
     * @var int
     */
    private $writingDate;

    /**
     * True if the Article needs saving, false otherwise.
     * @var bool
     */
    private $needSaving;

    public static function Get($identifier) {
        $newArticle = new Article($identifier);

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

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

        $result = $result[0];

        if (!$result) {
            return false;
        }

        $newArticle->abstract = $result['abstract'];
        $newArticle->text = $result['text'];
        $newArticle->authorId = $result['authorId'];
        $newArticle->date = DateParser::parseSQL($result['publicationDate']);
        $newArticle->writingDate = DateParser::parseSQL($result['writingDate']);

        $newArticle->needSaving = false;

        return $newArticle;
    }

    public static function Create($writingDate, $publicationDate, $authorId, $abstract, $text) {

        $text = InputSanitizer::Sanitize($text);
        $abstract = InputSanitizer::Sanitize($abstract);
        $publicationDate = InputSanitizer::Number($publicationDate);
        $writingDate = InputSanitizer::Number($writingDate);
        $authorId = InputSanitizer::Number($authorId);

        $publicationDate = DateFormatter::formatSQL($publicationDate);
        $writingDate = DateFormatter::formatSQL($writingDate);

        $sql = "INSERT INTO `news` (
            `identifier`, `writingDate`, `publicationDate`, `authorId`, `abstract`, `text`)
            VALUES (
            NULL, '$writingDate', '$publicationDate', '$authorId', '$abstract', '$text');";

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

        $sql = "SELECT identifier FROM `news` WHERE `writingDate` = '$writingDate' LIMIT 1;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

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

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

    /**
     * Removes the article corresponding to the given identifier.
     *
     * @param int $identifier
     * @return int/bool
     */
    public static function Remove($identifier) {
        $sql = "DELETE FROM `news` WHERE `news`.`identifier` = $identifier;";

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

        return $result;
    }

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

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

    /**
     * Saves the article in the database
     */
    private function save() {

        $writingDate = DateFormatter::formatSQL($this->writingDate);
        $publicationDate = DateFormatter::formatSQL($this->date);

        $sql = "UPDATE `news` SET
            `writingDate` = '$writingDate',
            `publicationDate` = '$publicationDate',
            `authorId` = '$this->authorId',
            `abstract` = '$this->abstract',
            `text` = '$this->text'
            WHERE `identifier` = $this->identifier;";

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

    public function setContent($abstract, $text, $date) {
        
        $text = InputSanitizer::Sanitize($text);
        $abstract = InputSanitizer::Sanitize($abstract);
        $date = InputSanitizer::Number($date);

        $this->abstract = $abstract;
        $this->text = $text;
        $this->date = $date;
        $this->writingDate = time();
        $this->needSaving = true;
    }

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

    public function getText() {
        return $this->text;
    }

    public function getAbstract() {
        return $this->abstract;
    }

    public function getAuthor() {
        $user = Member::Get($this->authorId);
        return $user->getFullName();
    }

    public function getAuthorId() {
        return $this->authorId;
    }

    public function getDate() {
        return $this->date;
    }

    public function getWritingDate() {
        return $this->writingDate;
    }

    public function setAuthorId($authorId) {
        $authorId = InputSanitizer::Number($authorId);
        $this->authorId = $authorId;
        $this->needSaving = true;
    }
}
?>
