Prechádzať zdrojové kódy

\#52: editBox kann jetzt verschiedene input-typen verarbeiten
\#53: CSS ausgelagert
\#51: Funktioniert an sich, mehrere Zeilen müssen aber noch implementiert werden (\#55)

Moritz Schmidt 10 rokov pred
rodič
commit
aa79fe537c
6 zmenil súbory, kde vykonal 152 pridanie a 13 odobranie
  1. 33 4
      ajax.php
  2. BIN
      drafts/Brief.docx
  3. BIN
      drafts/Rechnung.odt
  4. 41 0
      includes/document.inc.php
  5. 72 8
      scripts/custom.js
  6. 6 1
      styles/style.css

+ 33 - 4
ajax.php

@@ -123,14 +123,15 @@ switch($_REQUEST['action']) {
         $editBox = array(
             "options"   => array(
                 array(
-                    "type"  => "select",
-                    "name"  => "vorlage",
-                    "value" => "Rechnung.odt" // TODO: get default value
+                    "type"      => "select",
+                    "name"      => "vorlage",
+                    "value"     => Document::getDefaultDraft()->filename,
+                    "values"    => Document::getAllDrafts()
                 ),
                 array(
                     "type"  => "text",
                     "name"  => "filename",
-                    "value" => "Neue-Rechnung-DATUM.odt" // TODO: get value based on draft
+                    "value" => str_replace(".docx", "", Document::getDefaultDraft()->filename) . "_" . date("d_m_Y") . ".docx" // TODO: get value based on draft
                 )
             ),
             "title" => "Neues Dokument"
@@ -138,6 +139,15 @@ switch($_REQUEST['action']) {
 
         echo json_encode($editBox);
         break;
+    case 'getDraftVars':
+        header("Status: 200 OK");
+
+        // TODO: handle no draft given
+
+        $documentHandle = new \PhpOffice\PhpWord\TemplateProcessor('drafts/' . $_REQUEST['draft']);
+        $templateVars = $documentHandle->getVariables();
+        echo json_encode(array_values($templateVars));
+        break;
     case 'getMailboxStatus':
         $mailbox = Mailbox::getMailboxById($_REQUEST['mailboxId'], true);
         $mailboxStatus = array(
@@ -145,6 +155,25 @@ switch($_REQUEST['action']) {
         );
         echo json_encode($mailboxStatus);
         break;
+    case 'saveNewDocument':
+        header("Status: 200 OK");
+
+        $newDocument = new DocumentHandler($_REQUEST['filename'], $_REQUEST['draft']);
+
+        foreach($_REQUEST['draftVars'] as $key => $draftVar) {
+            $newDocument->setVal($key, htmlspecialchars($draftVar));
+        }
+
+        $newDocument->saveFile();
+
+        echo json_encode(array("ok"));
+
+        //pa($_REQUEST);
+
+        break;
+    case 'debugTest': // for testing single methods etc.
+        pa(Document::getDefaultDraft());
+        break;
     default:
         header("Status: 400 No Action Defined");
         echo 'error';

BIN
drafts/Brief.docx


BIN
drafts/Rechnung.odt


+ 41 - 0
includes/document.inc.php

@@ -146,10 +146,51 @@ class Document {
 
     public static function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange, $mailUid, $mailAcc = -1) {
         global $db;
+
         $query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`, `mail_uid`, `mail_acc`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ", " . $mailUid . ", " . $mailAcc . ");";
         $db->insertQuery($query);
     }
 
+    /**
+     * 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;");
+
+         // TODO: > 1, < 1 checking
+
+         return $draft[0];
+
+     }
+
 }
 
 ?>

+ 72 - 8
scripts/custom.js

@@ -21,7 +21,7 @@ $(document).ready(function() {
         element.children(".spinner").remove();
     }
 
-    function loadBox(element) {
+    function loadBox(element, callback) {
 
         $(document).on("click", element, function(e) {
 
@@ -34,7 +34,25 @@ $(document).ready(function() {
                     var optionsContainer = document.createElement('div');
 
                     $.each(editBox['options'], function(i) {
-                        $(optionsContainer).append(this.name + "<input type=\"" + this.type + "\" value=\"" + this.value + "\" style=\"float: right;\"/><br><br>")
+                        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\">";
+                                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>"
+                                    }
+                                });
+                                selectHtml += "</select>";
+                                $(optionsContainer).append(this.name +  selectHtml + "<br><br>");
+                                break;
+                            default:
+                                break;
+                        }
                     });
 
                     $.fancybox({
@@ -44,14 +62,15 @@ $(document).ready(function() {
                         width		: '70%',
                         height		: '70%',
                         autoSize	: false,
-                        title   : "<h3>" + editBox['title'] + "</h3><hr>",
-                        content : optionsContainer.innerHTML,
-                        helpers : {
+                        title       : "<h3>" + editBox['title'] + "</h3><hr>",
+                        content     : optionsContainer.innerHTML,
+                        helpers     : {
                             title: {
                                 type    : 'inside',
                                 position: 'top'
                             }
-                        }
+                        },
+                        afterShow   : callback
                     });
                 } catch(e) {
                     console.log(e); // DBG
@@ -86,6 +105,50 @@ $(document).ready(function() {
         });
     }
 
+    function newDocumentListener() {
+        $(document).on("change", ".fancybox-inner select", function(e) {
+            reloadDraftVars();
+        });
+
+        $(document).on("click", "#save-new-document", function(e) {
+            e.preventDefault();
+
+            data = {
+                draft: $("#editbox-input-vorlage").val(),
+                filename: $("#editbox-input-filename").val(),
+                draftVars: {}
+            }
+
+            $(".editbox-draft-vars").each(function(i) {
+                data['draftVars'][$(this).attr("id").replace("editbox-draft-vars-", "")] = $(this).val();
+            });
+
+            $.getJSON("ajax.php?action=saveNewDocument", data, function(r) {
+                console.log(r);
+            });
+        });
+    }
+
+    function reloadDraftVars() {
+        $.getJSON("ajax.php?action=getDraftVars&draft=" + $(".fancybox-inner select").val(), function(r) {
+            draftVarsContainer = $("#draft-vars");
+
+            if(draftVarsContainer.length > 0) {
+                console.log(draftVarsContainer);
+                draftVarsContainer.remove();
+            }
+
+            $(".fancybox-inner").append("<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("<a href=\"#\"type=\"button\" id=\"save-new-document\" class=\"btn btn-success\">Dokument speichern</a>");
+
+        });
+    }
+
     $.fn.editable.defaults.mode = 'inline';
     $.fn.editableform.buttons =
   '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
@@ -287,10 +350,11 @@ $(document).ready(function() {
             getMailboxStatus();
             break;
         case 'manage-label':
-            loadBox('.manage-mailboxfolder');
+            loadBox('.manage-mailboxfolder', function() {});
             break;
         case 'label':
-            loadBox('#new-document');
+            loadBox('#new-document', function() { reloadDraftVars(); });
+            newDocumentListener();
             break;
         default:
             break;

+ 6 - 1
styles/style.css

@@ -62,6 +62,11 @@ table td {
 	background-color: #f5f5f5;
 }*/
 
+.editbox-input, .editbox-draft-vars {
+	float: right;
+}
+
+
 /* Spinner (http://tobiasahlin.com/spinkit/) */
 
 .spinner {
@@ -109,7 +114,7 @@ table td {
 		transform: scale(0.0);
 		-webkit-transform: scale(0.0);
 	}
-	
+
 	40% {
 		transform: scale(1.0);
 		-webkit-transform: scale(1.0);