<?php

// ===========
// Notes Class
// ===========

class Notes extends Base {

    // Constants

    const VALID_TYPES = array("User");

    // Variables

    private $current_user_id = "";
    private $current_user_name = "";
    private $current_user_email = "";

    // =================
    // Class Constructor
    // =================

    public function __construct() {

        set_error_handler(array($this, "displayError"));

        date_default_timezone_set(self::TIMEZONE);

        $this->current_user_id = $_SESSION[self::USER_ID] ?? "";
        $this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
        $this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";
    }

    // ========
    // Add Note
    // ========

    public function addNote() {

        $step = filter_input(INPUT_POST, "step") ?: "prompt";

        switch ($step) {

            case "prompt":

                $note_type = filter_input(INPUT_POST, "note_type") ?: "";
                $note_reference = filter_input(INPUT_POST, "note_reference", FILTER_SANITIZE_NUMBER_INT) ?: 0;

                if (!in_array($note_type, self::VALID_TYPES) or empty($note_reference)) {
                    $this->logError("Invalid Note Type ({$note_type} or Reference {$note_reference} " . __METHOD__);
                }

                $XSLParms["NOTE_TYPE"] = $note_type;
                $XSLParms["NOTE_REFERENCE"] = $note_reference;

                $content = $this->applyXSL("", $this->getXSL("addNote"), $XSLParms);

                $this->displayContent($content);

                break;

            case "add":

                $note_type = filter_input(INPUT_POST, "note_type") ?: "";
                $note_reference = filter_input(INPUT_POST, "note_reference", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $note_text = filter_input(INPUT_POST, "note_text") ?: "";

                $SQL = "insert into notes
          
                         (  note_type,
                            note_reference,
                            note_text,
                            note_origin )
                               
                  VALUES ( :note_type,
                           :note_reference,
                           :note_text,
                           :note_origin )";

                $statement = $this->connect()->prepare($SQL);

                $statement->bindValue(":note_type", $note_type);
                $statement->bindValue(":note_reference", $note_reference);
                $statement->bindValue(":note_text", $note_text);
                $statement->bindValue(":note_origin", $this->current_user_name);

                $statement->execute();

                break;

            default:

                $this->displayHome();
        }
    }

    // ===========
    // Delete Note
    // ===========

    public function deleteNote() {

        $note_serial = filter_input(INPUT_POST, "note_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;

        $note = $this->getNote($note_serial);

        if ($note->count() == 0) {
            $this->logError("Invalid Note ({$note_serial}). " . __METHOD__);
        }

        $SQL = "delete from notes
             where note_serial = {$note_serial}";

        $this->executeSQL($SQL);
    }

    // =========
    // Edit Note
    // =========

    public function editNote() {

        $step = filter_input(INPUT_POST, "step") ?: "prompt";

        switch ($step) {

            case "prompt":

                $note_serial = filter_input(INPUT_POST, "note_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;

                $note = $this->getNote($note_serial);

                if ($note->count() == 0) {
                    $this->logError("Invalid Note ({$note_serial}). " . __METHOD__);
                }

                $XML = $this->mergeXML($note, "<XML/>");

                $content = $this->applyXSL($XML, $this->getXSL("editNote"));

                $this->displayContent($content);

                break;

            case "update":

                $note_serial = filter_input(INPUT_POST, "note_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
                $note_text = filter_input(INPUT_POST, "note_text") ?: "";

                $SQL = "update notes
                   set note_text   = :note_text,
                       note_origin = :note_origin
                 where note_serial = :note_serial";

                $statement = $this->connect()->prepare($SQL);

                $statement->bindValue(":note_serial", $note_serial);
                $statement->bindValue(":note_text", $note_text);
                $statement->bindValue(":note_origin", $this->current_user_name);

                $statement->execute();

                break;

            default:

                $this->displayHome();
        }
    }

    // ====================
    // Return a Note Object
    // ====================

    public function getNote($note_serial = 0) {

        $SQL = "select view_notes.*
              from view_notes
             where note_serial = {$note_serial}";

        return $this->getTable("notes", $SQL);
    }

    // =====================
    // Return a Notes Object
    // =====================

    public function getNotes($note_type = "", $note_reference = 0) {

        $SQL = "select view_notes.*
              from view_notes
             where note_type      = '{$note_type}'
               and note_reference =  {$note_reference}
             order by note_timestamp asc";

        return $this->getTable("notes", $SQL);
    }

    // =====================
    // Return a Notes Object
    // =====================

    public function getAllNotes($note_reference = 0) {

        $SQL = "select view_notes.*
              from view_notes
             where note_reference = {$note_reference}
             order by note_timestamp asc";

//        $this->debug($SQL);

        return $this->getTable("notes", $SQL);
    }

    // ==============
    // Add Note Quick
    // ==============

    public function addNoteQuick($note_type = "", $note_reference = 0, $note_text = "") {

        $SQL = "insert into notes
          
                         (  note_type,
                            note_reference,
                            note_text,
                            note_origin )
                               
                  VALUES ( :note_type,
                           :note_reference,
                           :note_text,
                           :note_origin )";

        $statement = $this->connect()->prepare($SQL);

        $statement->bindValue(":note_type", $note_type);
        $statement->bindValue(":note_reference", $note_reference);
        $statement->bindValue(":note_text", $note_text);
        $statement->bindValue(":note_origin", $this->current_user_name);

        $statement->execute();
    }
}

?>
