<?php

class MailboxFolder {
    private $id         = NULL;
    private $folderName = NULL;
    private $mailboxID  = NULL;
    private $labelID    = NULL;

    public function __construct($id, $folderName, $mailboxID, $labelID) {
        $this->id = $id;
        $this->folderName = $folderName;
        $this->mailboxID = $mailboxID;
        $this->labelID = $labelID;

    }

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

    public function getFolderName() {
        return $this->folderName;
    }

    public function getMailboxID() {
        return $this->mailboxID;
    }

    public function getLabelID() {
        return $this->labelID;
    }

    /**
     * Get MailboxFolder by Folder-ID
     *
     *
     * @param string $folderID  ID of folder
     *
     * @return MailboxFolder  Selected MailboxFolder
     *
     */

    public static function getMailboxFolderByID($folderID) {
        global $db;

        $return = array();
        $folders = $db->selectQuery("SELECT * FROM `mailbox-folders` WHERE `id` = '" . $folderID . "';");

        if(!$folders) {
            return false;
        }

        foreach($folders as $folder) {
            $return[] = new MailboxFolder($folder->id, $folder->folder_name, $folder->mailbox_id, $folder->label_id);
        }

        return $return[0];
    }

    /**
     * Get MailboxFolder by Folder-name
     *
     *
     * @param string $folderName  Name of folder
     *
     * @return MailboxFolder  Selected MailboxFolder
     *
     */

    public static function getMailboxFolderByName($folderName) {
        global $db;

        $return = array();
        $folders = $db->selectQuery("SELECT * FROM `mailbox-folders` WHERE `folder_name` = '" . $folderName . "';");

        if(!$folders) {
            return false;
        }

        foreach($folders as $folder) {
            $return[] = new MailboxFolder($folder->id, $folder->folder_name, $folder->mailbox_id, $folder->label_id);
        }

        return $return[0];
    }

    /**
     * Get MailboxFolder by Label ID
     *
     *
     * @param int $labelID  Label ID
     *
     * @return Array(MailboxFolder)  Array with selected MailboxFolder(s)
     *
     */

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

        $return = array();
        $folders = $db->selectQuery("SELECT * FROM `mailbox-folders` WHERE `label_id` = '" . $labelID . "';");

        if(!$folders) {
            return false;
        }

        foreach($folders as $folder) {
            $return[] = new MailboxFolder($folder->id, $folder->folder_name, $folder->mailbox_id, $folder->label_id);
        }

        return $return;
    }

    /**
     * Get MailboxFolder-Name by ID
     *
     *
     * @param int $mID  Mailbox-Folder-ID
     *
     * @return string  MailboxFolder-Name
     *
     */

    public static function getMailboxFolderNameFromID($mID) {
        global $db;

        $mailbox = $db->selectStringQuery("SELECT `folder_name` FROM `mailbox-folders` WHERE id=" . $mID);
        return $mailbox;
    }

    /**
     * Add new Mailbox-Folder
     *
     * @param string $folderName  FS-Path of folderName
     * @param int $mailboxID  ID of mailboxID
     * @param int $labelID  ID of Label
     *
     * @return void
     *
     */

     public static function addMailboxFolder($folderName, $mailboxID, $labelID) {
         global $db;

         $db->insertQuery("INSERT INTO `mailbox-folders`(`folder_name`, `mailbox_id`, `label_id`) VALUES ('" . $folderName . "', " . $mailboxID . ", " . $labelID . ")");

     }

     /**
      * Update Mailbox-Folder
      *
      * @param int $mailboxFolderID  ID of mailboxFolder
      * @param string $folderName  FS-Path of folderName
      * @param int $mailboxID  ID of mailboxID
      * @param int $labelID  ID of Label
      *
      * @return void
      *
      */

      public static function updateMailboxFolder($mailboxFolderID, $folderName, $mailboxID, $labelID) {
          global $db;

          $db->updateQuery("UPDATE `mailbox-folders` SET `folder_name` = '" . $folderName . "', `mailbox_id` = " . $mailboxID . ",`label_id` = " . $labelID . " WHERE `id` = " . $mailboxFolderID . ";");

      }
}
