<?php

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

include_once './includes/news/Article.inc.php';

include_once './includes/utils/MapperUtils.inc.php';

/**
 * This class allows to get lists of Articles.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class ArticleLister {

    /**
     * Returns all the available articles.
     *
     * @return array(Article)
     */
    public static function getAllArticles() {
        return ArticleLister::getArticles("");
    }

    /**
     * Returns all the available articles between the given timestamps.
     *
     * @var int $from   The start of the intervall (timestamp)
     * @var int $to     The end of the intervall (timestamp)
     *
     * @return array(Article)
     */
    public static function getArticleByDate($from, $to) {
        $from = DateFormatter::formatSQL($from);
        $to = DateFormatter::formatSQL($to);
        return ArticleLister::getArticles("WHERE publicationDate BETWEEN '$from' AND '$to'");
    }

    /**
     * Returns all the available articles from the given user.
     *
     * @var Member $author The author to filter by
     *
     * @return array(Article)
     */
    public static function getArticleByAuthor($author) {
        $authorId = $author->getIdentifier();
        return ArticleLister::getArticles("WHERE authorId = $authorId");
    }

    /**
     * Returns all articles corresponding to the restriction.
     *
     * @param string $restriction   The filter to apply
     *
     * @return array(Article)
     */
    private static function getArticles($restriction) {
        $sql = "SELECT identifier FROM news $restriction;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        if ($result) {
            $result1 = array_map("mapIdentifierToArticle", $result);
            return $result1;
        } else {
            return array();
        }
        
    }
}
?>
