<?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 . ";");

      }
}
