<?php

class Mail {
    private $id                 = NULL;
    private $mailSender         = NULL;
    private $subject            = NULL;
    private $mailboxFolderID    = NULL;
    private $mailUID            = NULL;


    public function __construct($id, $mailSender, $subject, $mailboxFolderID, $mailUID) {
        $this->id               = $id;
        $this->mailSender       = $mailSender;
        $this->subject          = $subject;
        $this->mailboxFolderID  = $mailboxFolderID;
        $this->mailUID          = $mailUID;

    }

    /**
     * Get the value of ID
     *
     *
     * @return int
     *
     */

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

    /**
     * Set the value of ID
     *
     *
     * @param int id
     *
     */

    public function setID($id) {
        $this->id = $id;
     }

    /**
     * Get the value of Mail Sender
     *
     *
     * @return string
     *
     */

    public function getMailSender() {
        return $this->mailSender;
    }

    /**
     * Set the value of Mail Sender
     *
     *
     * @param string mailSender
     *
     */

    public function setMailSender($mailSender) {
        $this->mailSender = $mailSender;
     }

    /**
     * Get the value of Subject
     *
     *
     * @return string
     *
     */

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

    /**
     * Set the value of Subject
     *
     *
     * @param string subject
     *
     */

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

    /**
     * Get the value of Mailbox Folder ID
     *
     *
     * @return int
     *
     */

    public function getMailboxFolderID() {
        return $this->mailboxFolderID;
    }

    /**
     * Set the value of Mailbox Folder ID
     *
     *
     * @param int mailboxFolderID
     *
     */

    public function setMailboxFolderID($mailboxFolderID) {
        $this->mailboxFolderID = $mailboxFolderID;
     }

    /**
     * Get the value of Mail UID
     *
     *
     * @return int
     *
     */

    public function getMailUID() {
        return $this->mailUID;
    }

    /**
     * Set the value of Mail
     *
     *
     * @param int mailUID
     *
     */

    public function setMailUID($mailUID) {
        $this->mailUID = $mailUID;
     }

     public function getMailRecipient() {
         global $db;

         return $db->selectStringQuery("SELECT `mailboxes`.`username` FROM `mailboxes` WHERE `mailboxes`.`id` = (SELECT `mailbox-folders`.`mailbox_id` FROM `mailbox-folders`WHERE `mailbox-folders`.`id` = (SELECT `mails`.`mailbox_folder_id` FROM `mails` WHERE `mails`.`id` = " . $this->getID() . "));");
     }

     /**
     * Save Mail to DB
     *
     *
     * @return void
     *
     */

     public function save() {
         global $db;

         if($this->getID() == NULL) {
             $this->setID("'NULL'");
         }

         $db->insertQuery("INSERT INTO `mails` (`id`, `mail_sender`, `subject`, `mailbox_folder_id`, `mail_uid`) VALUES (" . $this->getID() . ", '" . $this->getMailSender() . "', '" . $this->getSubject() . "', " . $this->getMailboxFolderID() . ", " . $this->getMailUID() . ") ON DUPLICATE KEY UPDATE `mail_sender` = '" . $this->getMailSender() . "', `subject` = '" . $this->getSubject() . "', `mailbox_folder_id` = " . $this->getMailboxFolderID() . ", `mail_uid` = " . $this->getMailUID() . ";");

         $this->setID(NULL); // get ID from DB

         echo "<br><br>";
     }

     /**
     * Get Mail by Mail-ID
     *
     * @param int $mailID  ID of Mail
     *
     * @return Mail
     *
     */

     public static function getMailByMailID($mailID) {
         global $db;

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

         $return = array();

         if(!$mails) {
             return false;
         }

         foreach($mails as $mail) {
             $return[] = new Mail($mail->id, $mail->mail_sender, $mail->subject, $mail->mailbox_folder_id, $mail->mail_uid);
         }

         return $return[0];
     }

     /**
     * Get Mail by Mail information
     *
     * @param int $mailboxFolder  ID of mailbox-Folder
     * @param int $mailUID  UID of Mail
     *
     * @return Mail/bool  Selected Mail, or false if not found
     *
     */

     public static function getMailByMailInfo($mailboxFolder, $mailUID) {
         global $db;

         $mails = $db->selectQuery("SELECT * FROM `mails` WHERE `mailbox_folder_id` = " . $mailboxFolder . " AND `mail_uid` = " . $mailUID . ";");

         $return = array();

         if(!$mails) {
             return false;
         }

         foreach($mails as $mail) {
             $return[] = new Mail($mail->id, $mail->mail_sender, $mail->subject, $mail->mailbox_folder_id, $mail->mail_uid);
         }

         return $return[0];
     }

     /**
     * Get Mails by Label ID
     *
     * @param int $labelID  ID of Label
     *
     * @return Array(Mail)  Array with selected Mails
     *
     */

     public static function getMailsByLabelID($labelID) {
         global $db;

         $mails = $db->selectQuery("SELECT `mails`.* FROM `mails` JOIN `mailbox-folders` ON `mailbox-folders`.id = `mails`.mailbox_folder_id WHERE `mailbox-folders`.label_id = " . $labelID . ";");

         if(!$mails) {
             return false;
         }

         $return = array();

         foreach($mails as $mail) {
             $return[] = new Mail($mail->id, $mail->mail_sender, $mail->subject, $mail->mailbox_folder_id, $mail->mail_uid);
         }

         return $return;
     }

}

?>
