<?php

class Reminder {
    private $id             = NULL;
    private $userID         = NULL;
    private $reminderDate   = NULL;
    private $remindedYet    = NULL;
    private $subject        = NULL;

    public function __construct($id, $userID, $reminderDate, $remindedYet, $subject) {
        $this->id           = $id;
        $this->userID       = $userID;
        $this->reminderDate = $reminderDate;
        $this->remindedYet  = $remindedYet;
        $this->subject      = $subject;
    }

    public function getID() {
        return $this->id;
    }

    public function getUserID() {
        return $this->userID;
    }

    public function getReminderDate() {
        return $this->reminderDate;
    }

    public function setReminderDate($reminderDate) {
        $this->reminderDate = $reminderDate;
    }

    public function getRemindedYet() {
        return $this->remindedYet;
    }

    public function setRemindedYet($remindedYet) {
        $this->remindedYet = $remindedYet;
    }

    public function getSubject() {
        return $this->subject;
    }

    public function setSubject($subject) {
        $this->subject = $setSubject;
    }

    public function save() {
        global $db;

        $db->updateQuery("UPDATE `reminders` SET `user_id` = " . $this->userID . ", `reminder_date` = '" . $this->reminderDate . "', `reminded_yet` = " . $this->remindedYet . " WHERE `id` = " . $this->id . ";");
    }

    /**
     * Add Reminder to DB
     *
     * @param int $userID  ID of User
     * @param string $reminderDate  Date of Reminder
     *
     * @return void
     *
     */

     public static function addReminder($userID, $reminderDate, $subject) {
         global $db;

         $db->insertQuery("INSERT INTO `reminders`(`user_id`, `reminder_date`, `reminded_yet`, `subject`) VALUES (" . $userID . ", '" . $reminderDate . "', 0, '" . $subject . "');");

     }

     /**
     * Description
     *
     * @param type $name  description
     *
     * @return type  the integer of the set mode used. FALSE if foo
     *
     */

     public static function getLastReminder() {
         global $db;

         $lastID = $db->selectStringQuery("SELECT MAX(id) FROM `reminders`;");

         return Reminder::getReminderByID($lastID);
     }

    /**
     * Get Reminder by Reminder-ID
     *
     * @param int $reminderID  ID of Reminder
     *
     * @return Reminder  Selected Reminder
     *
     */

    public static function getReminderByID($reminderID) {
        global $db;

        $reminders = $db->selectQuery("SELECT * FROM `reminders` WHERE `id` = " . $reminderID . ";");

        $return = array();

        foreach($reminders as $reminder) {
            $return[] = new Reminder($reminder->id, $reminder->user_id, $reminder->reminder_date, $reminder->reminded_yet, $reminder->subject);
        }

        return $return[0];
    }

    /**
     * Get upcoming Reminders by User-ID
     *
     * @param int $userID  ID of User
     *
     * @return Array(Reminder)  Array with selected Reminders
     *
     */

    public static function getUpcomingRemindersByUserID($userID) {
        global $db;

        $reminders = $db->selectQuery("SELECT * FROM `reminders` WHERE `user_id` = " . $userID . " AND `reminder_date`< CURRENT_TIMESTAMP AND `reminded_yet` = 0;");

        $return = array();

        if(!$reminders) {
            return false;
        }

        foreach($reminders as $reminder) {
            $return[] = new Reminder($reminder->id, $reminder->user_id, $reminder->reminder_date, $reminder->reminded_yet, $reminder->subject);
        }

        return $return;
    }

}

?>
