<?php

class Document {
    private $id         =   NULL;
    private $fileName   =   NULL;
    private $path       =   NULL;
    private $labelID    =   NULL;
    private $draft      =   NULL;
    private $created    =   NULL;
    private $lastChange =   NULL;

    public function __construct($id, $fileName, $path, $labelID, $draft, $created, $lastChange) {
        $this->id           = $id;
        $this->fileName     = $fileName;
        $this->path         = $path;
        $this->labelID      = $labelID;
        $this->draft        = $draft;
        $this->created      = $created;
        $this->lastChange   = $lastChange;
    }

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

    public function getFileName() {
        return $this->fileName;
    }

    public function getPath() {
        return $this->path;
    }

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

    public function getDraft() {
        return $this->draft;
    }

    public function getCreated() {
        return $this->created;
    }

    public function getLastChange() {
        return $this->lastChange;
    }

    /**
     * Get all Documents
     *
     * @return Array(Document)  Array with all Documents
     *
     */

     public static function getAllDocuments() {
         global $db;

         $return = array();
         $documents = $db->selectQuery("SELECT * FROM `documents`;");

         if(!$documents) {
             return false;
         }

         foreach($documents as $document) {
             $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
         }

         return $return;
     }

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

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

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

        if(!$documents) {
            return false;
        }

        foreach($documents as $document) {
            $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
        }

        return $return;
    }

    /**
     * Get Documents by FS-Path
     *
     *
     * @param string $path  path, relative to ~/documents-folder
     *
     * @return Array(Document)  Array with selected Document(s)
     *
     */

    public static function getDocumentsByLabelPath($path) {
        global $db;

    	$return = array();
    	$documents = $db->selectQuery("SELECT documents.* FROM `documents` JOIN `labels` ON labels.id = documents.label_id WHERE labels.path = '" . $path . "';");

    	foreach($documents as $document) {
    		$return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change);
    	}

    	return $return;
    }

    /**
     * Insert new Document into DB
     *
     *
     * @param string    $type       Type of Document
     * @param string    $fileName   Name of File/Subject of Mail
     * @param string    $path       Path relative to Label-Home-Folder / Mailaccount
     * @param int       $labelID    ID of Label
     * @param string    $draft      Name of used draft
     * @param string $created    Date of creation
     * @param string $lastChange Date of last change
     * @param int       $mailUid    UID of mail, 0 for Files
     * @param int       $mailAcc    ID of MailAccount. Default: -1 (For Files)
     *
     * @return void
     *
     */

    public static function addDocument($type, $fileName, $path, $labelID, $draft, $created, $lastChange) {
        global $db;

        $query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelID . ", '" . $draft . "', '" . $created . "', '" . $lastChange . "');";
        $db->insertQuery($query);
    }

    /**
     * Remove a Document from DB by documentID
     *
     * @param int $documentID  ID of Document
     *
     * @return void
     *
     */

    public static function removeDocumentByID($documentID) {
        global $db;

        $db->removeQuery("DELETE FROM `documents` WHERE `id` = " . $documentID . ";");

    }

    /**
     * Get all Drafts
     *
     *
     * @return Array  Array with Draft attributes
     *
     */

     public static function getAllDrafts() {
         global $db;
         $return = array();

         $drafts = $db->selectQuery("SELECT * FROM `drafts`;");

         foreach($drafts as $draft) {
             $return[] = $draft->filename;
         }

         return $return;
     }

     /**
     * Get the default Draft
     *
     *
     * @return Array  Array with Draft attributes
     *
     */

     public static function getDefaultDraft() {
         global $db;

         $draft = $db->selectQuery("SELECT * FROM `drafts` WHERE `default` = 1;");

         return $draft[0];

     }

}

?>
