Selaa lähdekoodia

\#61: Buttons ausgelagert

Moritz Schmidt 10 vuotta sitten
vanhempi
commit
55901de236
5 muutettua tiedostoa jossa 208 lisäystä ja 75 poistoa
  1. 29 2
      ajax.php
  2. 1 1
      includes/database.inc.php
  3. 31 0
      includes/mailbox.inc.php
  4. 37 0
      includes/mailboxfolder.inc.php
  5. 110 72
      scripts/custom.js

+ 29 - 2
ajax.php

@@ -102,7 +102,8 @@ switch($_REQUEST['action']) {
                     "value" => MailboxFolder::getMailboxFolderNameFromId($_GET['mfId'])
                 )
             ),
-            "title"     => "Mailkonto bearbeiten"
+            "title"     => "Mailkonto bearbeiten",
+            "mailboxFolderID"   => $_GET['mfId']
         );
 
         foreach($allMailBoxes as $mailBox) {
@@ -213,6 +214,32 @@ switch($_REQUEST['action']) {
         header("Status: 200 OK");
         Label::removeLabel($_REQUEST['labelId']);
 
+        // TODO: get status by DB (See: #40, #43)
+
+        $return = array(
+            "status"    => "OK"
+        );
+
+        echo json_encode($return);
+        break;
+    case 'saveMailFolder':
+        header("Status: 200 OK");
+        MailboxFolder::updateMailboxFolder($_REQUEST['mailboxFolderID'], $_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getId(), $_REQUEST['labelID']);
+
+        // TODO: get status by DB (See: #40, #43)
+
+        $return = array(
+            "status"    => "OK"
+        );
+
+        echo json_encode($return);
+        break;
+    case 'saveNewMailFolder':
+        header("Status: 200 OK");
+        MailboxFolder::addMailboxFolder($_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getId(), $_REQUEST['labelID']);
+
+        // TODO: get status by DB (See: #40, #43)
+
         $return = array(
             "status"    => "OK"
         );
@@ -220,7 +247,7 @@ switch($_REQUEST['action']) {
         echo json_encode($return);
         break;
     case 'debugTest': // for testing single methods etc.
-        pa(Document::getDefaultDraft());
+        pa(MailBox::getMailboxByUsername($_REQUEST['account'])->getId());
         break;
     default:
         header("Status: 400 No Action Defined");

+ 1 - 1
includes/database.inc.php

@@ -70,7 +70,7 @@ class Database {
 	public function updateQuery($query) {
 		$stmt = $this->handle->prepare($query);
 		if(!$stmt) {
-			pa($this->handle);
+			pa($this->handle); // TODO: better error
 		} else {
 			$stmt->execute();
 			$stmt->close();

+ 31 - 0
includes/mailbox.inc.php

@@ -221,5 +221,36 @@ class Mailbox {
         return $mailbox;
     }
 
+    /**
+     * Description
+     *
+     * @param type $name  description
+     *
+     * @return type  the integer of the set mode used. FALSE if foo
+     *
+     */
+
+     public static function getMailboxByUsername($mailboxUsername) {
+         global $db;
+
+         $mailbox = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `username` = '" . $mailboxUsername . "';");
+
+         $useSsl = '';
+         $noValidCert = '';
+
+         if($mailbox[0]->use_ssl) {
+             $useSsl = '/ssl';
+         }
+
+         if(!$mailbox[0]->valid_ssl) {
+             $noValidCert = '/novalidate-cert';
+         }
+
+         // TODO: sizeof > < 0 etc.
+
+         return new Mailbox($mailbox[0]->id, $mailbox[0]->server, $mailbox[0]->port, $mailbox[0]->protocol, $useSsl, $noValidCert, $mailbox[0]->username, $mailbox[0]->password, false);
+
+     }
+
 
 }

+ 37 - 0
includes/mailboxfolder.inc.php

@@ -102,4 +102,41 @@ class MailboxFolder {
         $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 . ";");
+
+      }
 }

+ 110 - 72
scripts/custom.js

@@ -1,5 +1,7 @@
 $(document).ready(function() {
 
+    var lastOpenedBoxData; // Stores JSON-data of last opened editBox
+
     function getUrlGetParameter(val) {
         var result = "Not found",
             tmp = [];
@@ -21,72 +23,85 @@ $(document).ready(function() {
         element.children(".spinner").remove();
     }
 
-    function loadBox(element, callback) {
-
-        $(document).on("click", element, function(e) {
-
-            e.preventDefault();
+    function loadBox(element, boxType, saveID, callback) {
+
+        var saveButtonIDs = {
+            saveMailFolder: {id: "save-mail-folder", text: "Mailkonto speichern"},
+            saveDocument: {id: "save-document", text: "Dokument speichern"},
+            saveLabel: {id: "save-label", text: "Label speichern"}
+        };
+
+        if(boxType == "editBox") {
+
+            $(document).on("click", element, function(e) {
+
+                e.preventDefault();
+
+                $.getJSON($(this).attr('href'), function(r) {
+
+                    lastOpenedBoxData = r;
+
+                    try {
+                        var optionsContainer = document.createElement('div');
+
+                        $.each(r['options'], function(i) {
+                            switch(this.type) {
+                                case 'text':
+                                    $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
+                                    break;
+                                case 'select':
+                                    selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
+                                    if(typeof this['values'] !== "undefined" && this['values']) {
+                                        this['values'].forEach(function(item) { // TODO: jQuery-each?
+                                            if(r['options'][0]['value'] == item) {
+                                                selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
+                                            } else {
+                                                selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
+                                            }
+                                        });
+                                    }
+
+                                    selectHtml += "</select>";
+                                    $(optionsContainer).append(this.name +  selectHtml + "<br><br>");
+                                    break;
+                                default:
+                                    break;
+                            }
+                        });
 
-            $.get($(this).attr('href'), function(r) {
-
-                try {
-                    editBox = $.parseJSON(r);
-                    var optionsContainer = document.createElement('div');
-
-                    $.each(editBox['options'], function(i) {
-                        switch(this.type) {
-                            case 'text':
-                                $(optionsContainer).append(this.name + "<input id=\"editbox-input-" + this.name + "\" type=\"text\" value=\"" + this.value + "\" class=\"editbox-input\" /><br><br>");
-                                break;
-                            case 'select':
-                                selectHtml = "<select id=\"editbox-input-" + this.name + "\" class=\"editbox-input\">";
-                                if(typeof this['values'] !== "undefined" && this['values']) {
-                                    this['values'].forEach(function(item) { // TODO: jQuery-each?
-                                        if(editBox['options'][0]['value'] == item) {
-                                            selectHtml += "<option selected value=\"" + item + "\">" + item + "</option>"
-                                        } else {
-                                            selectHtml += "<option value=\"" + item + "\">" + item + "</option>"
-                                        }
-                                    });
+                        $.fancybox({
+                            maxWidth	: 800,
+                            maxHeight	: 600,
+                            fitToView	: true,
+                            width		: '70%',
+                            height		: '70%',
+                            autoSize	: false,
+                            title       : "<h3>" + r['title'] + "</h3><hr>",
+                            content     : optionsContainer.innerHTML,
+                            helpers     : {
+                                title: {
+                                    type    : 'inside',
+                                    position: 'top'
                                 }
-
-                                selectHtml += "</select>";
-                                $(optionsContainer).append(this.name +  selectHtml + "<br><br>");
-                                break;
-                            default:
-                                break;
-                        }
-                    });
-
-                    $.fancybox({
-                        maxWidth	: 800,
-                        maxHeight	: 600,
-                        fitToView	: true,
-                        width		: '70%',
-                        height		: '70%',
-                        autoSize	: false,
-                        title       : "<h3>" + editBox['title'] + "</h3><hr>",
-                        content     : optionsContainer.innerHTML,
-                        helpers     : {
-                            title: {
-                                type    : 'inside',
-                                position: 'top'
+                            },
+                            afterShow   : function() {
+                                callback();
+                                $('.fancybox-inner').append("<a href=\"#\"type=\"button\" id=\"" + saveButtonIDs[saveID]['id'] + "\" class=\"btn btn-success save-button\">" + saveButtonIDs[saveID]['text'] + "</a>");
                             }
-                        },
-                        afterShow   : callback
-                    });
-                } catch(e) {
-                    console.log(e); // DBG
-                    var n = noty({
-                        layout  : 'topCenter',
-                        text    : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
-                        type    : 'error',
-                        timeout : 5000
                         });
-                }
-            });
+                    } catch(e) {
+                        console.log(e); // DBG
+                        var n = noty({
+                            layout  : 'topCenter',
+                            text    : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
+                            type    : 'error',
+                            timeout : 5000
+                            });
+                    }
+                });
 
-        });
+            });
+        }
     }
 
     function getMailboxStatus() {
@@ -119,8 +134,34 @@ $(document).ready(function() {
         });
     }
 
+    function manageLabelListener() {
+        $(document).on("click", "#save-mail-folder", function(e) {
+            e.preventDefault();
+
+            data = {
+                mailboxFolderID: lastOpenedBoxData['mailboxFolderID'],
+                account: $("#editbox-input-account").val(),
+                folder: $("#editbox-input-folder").val(),
+                labelID: getUrlGetParameter("labelId")
+            };
+
+            $.getJSON("ajax.php?action=saveMailFolder", data, function(r) {
+                if(r['status'] == "OK") {
+                    location.reload();
+                } else {
+                    var n = noty({
+                        layout  : 'topCenter',
+                        text    : 'Irgendwas ist schief gelaufen.<br>Bitte probieren Sie es später noch einmal.',
+                        type    : 'error',
+                        timeout : 5000
+                        });
+                }
+            });
+        });
+    }
+
     function manageLabelsListener() {
-        $(document).on("click", "#save-new-label", function(e) {
+        $(document).on("click", "#save-label", function(e) {
 
             e.preventDefault();
 
@@ -175,7 +216,7 @@ $(document).ready(function() {
             reloadDraftVars();
         });
 
-        $(document).on("click", "#save-new-document", function(e) {
+        $(document).on("click", "#save-document", function(e) {
             e.preventDefault();
 
             data = {
@@ -197,6 +238,7 @@ $(document).ready(function() {
 
     function reloadDraftVars() {
         $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
+
             draftVarsContainer = $("#draft-vars");
 
             if(draftVarsContainer.length > 0) {
@@ -204,17 +246,12 @@ $(document).ready(function() {
                 draftVarsContainer.remove();
             }
 
-            $(".fancybox-inner").append("<div id=\"draft-vars\"></div>")
+            $(".fancybox-inner .save-button").before("<div id=\"draft-vars\"></div>");
 
             r.forEach(function(item) {
-                //$(".fancybox-inner #draft-vars").append(item + "<input id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\" type=\"text\" /><br><br>");
-
                 $(".fancybox-inner #draft-vars").append(item + "<textarea id=\"editbox-draft-vars-" + item + "\" class=\"editbox-draft-vars\"></textarea><br><br><br>");
 
             })
-
-            $(".fancybox-inner #draft-vars").append("<a href=\"#\"type=\"button\" id=\"save-new-document\" class=\"btn btn-success\">Dokument speichern</a>");
-
         });
     }
 
@@ -420,14 +457,15 @@ $(document).ready(function() {
             settingsListener();
             break;
         case 'manage-label':
-            loadBox('.manage-mailboxfolder', function() {});
+            loadBox('.manage-mailboxfolder', "editBox", "saveMailFolder", function() {});
+            manageLabelListener();
             break;
         case 'manage-labels':
-            loadBox('#add-label', function() { $('.fancybox-inner').append("<a href=\"#\"type=\"button\" id=\"save-new-label\" class=\"btn btn-success\">Label speichern</a>"); });
+            loadBox('#add-label', "editBox", "saveLabel", function() {});
             manageLabelsListener();
             break;
         case 'label':
-            loadBox('#new-document', function() { reloadDraftVars(); });
+            loadBox('#new-document', "editBox", "saveDocument", function() { reloadDraftVars(); }); // TODO: Callback auf reloadDraftVars setzen (ohne ();)
             newDocumentListener();
             break;
         default: