<?php
class Label {
    private $id = NULL;
    private $name = '';
    private $path = '';

    public function __construct($id, $name, $path) {
        $this->id = $id;
        $this->name = $name;
        $this->path = $path;
    }

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

    public function getName() {
        return $this->name;
    }

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

   /**
    * Get all Labels
    *
    *
    * @return Array(Label)  Array with all Labels
    *
    */

    public static function getAllLabels() {
        global $db;

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

    	foreach($labels as $label) {
    		$return[] = new Label($label->id, $label->name, $label->path);
    	}

    	return $return;
    }

    /**
     * Get a Label by ID
     *
     *
     * @param int $labelId  Label ID
     *
     * @return Label  Selected Label object
     *
     */

    public static function getLabelById($labelId) {
        global $db;

        $return = array();
        $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `id` = " . $labelId . ";");

        foreach($labels as $label) {
            $return[] = new Label($label->id, $label->name, $label->path);
        }

        if(sizeof($return) > 1) {
            // Error handling!
        }

        return $return[0];
    }

    /**
     * Get a Label by FS-path
     *
     *
     * @param string $path  path, relative to ~/documents-folder
     *
     * @return Label  Selected Label
     *
     */

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

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

        foreach($labels as $label) {
            $return[] = new Label($label->id, $label->name, $label->path);
        }

        if(sizeof($return) > 1) {
            // Error handling!
        }

        return $return[0];
    }

    /**
     * Get a Label by Name
     *
     *
     * @param string $labelName  Name of wanted Label
     *
     * @return Label  Selected Label
     *
     */

    public static function getLabelByName($labelName) {
        global $db;

        $return = array();
        $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `name` = '" . $labelName . "';");

        foreach($labels as $label) {
            $return[] = new Label($label->id, $label->name, $label->path);
        }

        if(sizeof($return) > 1) {
            // Error handling!
        }

        return $return[0];
    }

    /**
     * Add new Label to DB
     *
     * @param string $name  Name of Label
     * @param string $path  FS-Path of Label
     *
     * @return void
     *
     */

     public static function addLabel($name, $path) {
         global $db;
         global $CONFIG;

         $db->insertQuery("INSERT INTO `labels`(`name`, `path`) VALUES ('" . $name . "', '" . $path . "')");

         if(!file_exists($CONFIG['documentPath'] . $path) || !is_dir($CONFIG['documentPath'] . $path)) {
             mkdir($CONFIG['documentPath'] . $path);
         }

     }

     /**
     * Remove a Label by Label-ID
     *
     * @param int $labelId  ID of Label
     *
     * @return void
     *
     */

     public static function removeLabel($labelId) {
         global $db;

         $db->removeQuery("DELETE FROM `labels` WHERE `id` = " . $labelId . ";");
     }
}


?>
