From 9691543ba0db231674564ecc4e7d5b9eccb26636 Mon Sep 17 00:00:00 2001
From: Florian Bogner <f.bogner@pcsg.de>
Date: Mon, 8 May 2017 17:28:31 +0200
Subject: [PATCH] feat: Added upload capacility

---
 ajax/uploadComplete.php              | 18 ++++++
 bin/controls/settings/Plugins.js     | 21 ++++++-
 bin/windows/Upload.js                | 92 ++++++++++++++++++++++++++++
 locale.xml                           | 37 +++++++++++
 src/QUI/Ckeditor/Plugins/Manager.php | 63 +++++++++++++++++++
 5 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 ajax/uploadComplete.php
 create mode 100644 bin/windows/Upload.js

diff --git a/ajax/uploadComplete.php b/ajax/uploadComplete.php
new file mode 100644
index 0000000..c67b4fa
--- /dev/null
+++ b/ajax/uploadComplete.php
@@ -0,0 +1,18 @@
+<?php
+
+QUI::$Ajax->registerFunction(
+    'package_quiqqer_ckeditor4_ajax_uploadComplete',
+    function ($File) {
+
+        $filePath = $File->getAttribute('filepath');
+
+        $Manager = new \QUI\Ckeditor\Plugins\Manager();
+        try {
+            $Manager->installPlugin($filePath);
+        } catch (\Exception $Exception) {
+            QUI::getMessagesHandler()->addError($Exception->getMessage());
+        }
+
+    },
+    array('File')
+);
\ No newline at end of file
diff --git a/bin/controls/settings/Plugins.js b/bin/controls/settings/Plugins.js
index bd9176b..bccae7a 100644
--- a/bin/controls/settings/Plugins.js
+++ b/bin/controls/settings/Plugins.js
@@ -64,6 +64,15 @@ define('package/quiqqer/ckeditor4/bin/controls/settings/Plugins', [
                         events  : {
                             onClick: this.$toggleState
                         }
+                    }, {
+                        name  : 'upload',
+                        text  : QUILocale.get(
+                            "quiqqer/ckeditor4",
+                            "editors.settings.plugins.table.button.upload"
+                        ),
+                        events: {
+                            onClick: this.$displayUpload
+                        }
                     }],
                     height     : "500",
                     columnModel: [
@@ -99,6 +108,8 @@ define('package/quiqqer/ckeditor4/bin/controls/settings/Plugins', [
 
                         var StateBtn = TableButtons.state;
 
+                        console.log(TableButtons);
+
                         var data = self.$Grid.getSelectedData()[0];
 
                         if (!data) {
@@ -271,8 +282,16 @@ define('package/quiqqer/ckeditor4/bin/controls/settings/Plugins', [
                         onError     : reject
                     });
                 });
-            }
+            },
 
+            $displayUpload: function () {
+                var self = this;
+
+                require(['package/quiqqer/ckeditor4/bin/windows/Upload'], function (Window) {
+                    new Window({}).open();
+                });
+
+            }
         }
     );
 });
\ No newline at end of file
diff --git a/bin/windows/Upload.js b/bin/windows/Upload.js
new file mode 100644
index 0000000..956723f
--- /dev/null
+++ b/bin/windows/Upload.js
@@ -0,0 +1,92 @@
+define('package/quiqqer/ckeditor4/bin/windows/Upload', [
+
+    'qui/QUI',
+    'qui/controls/windows/Confirm',
+    'Locale',
+    'controls/upload/Form'
+], function (QUI, QUIConfirm, QUILocale, UploadForm) {
+    "use strict";
+
+
+    return new Class({
+
+        Extends: QUIConfirm,
+        Type   : 'package/quiqqer/ckeditor4/bin/windows/Upload',
+
+        Binds: [
+            '$onSubmit',
+            '$onOpen',
+            '$onComplete'
+        ],
+
+        initialize: function (options) {
+            this.setAttributes({
+                title        : QUILocale.get("quiqqer/ckeditor4", "window.upload.title"),
+                icon         : 'fa fa-upload',
+                maxWidth     : 400,
+                maxHeight    : 600,
+                autoclose    : false,
+                texticon     : false,
+                cancel_button: {
+                    text     : false,
+                    textimage: 'icon-remove fa fa-remove'
+                },
+                ok_button    : {
+                    text     : QUILocale.get("quiqqer/ckeditor4", "window.upload.button.upload"),
+                    textimage: 'fa fa-upload'
+                }
+            });
+
+            this.$Upload = null;
+
+            this.parent(options);
+
+            this.addEvents({
+                onOpen: this.$onOpen,
+                submit: this.$onSubmit
+            });
+
+        },
+
+        /**
+         * Calls the ajax function to get the labels from gitlab and sets up its controls.
+         */
+        $onOpen: function () {
+            var self = this;
+
+            var Content = this.getContent();
+
+            this.$Upload = new UploadForm({
+                multible    : false,
+                sendbutton  : false,
+                cancelbutton: false,
+                events      : {
+                    onComplete: self.$onComplete
+                },
+                styles      : {
+                    height: '95%'
+                }
+            });
+
+            this.$Upload.setParam('onfinish', 'package_quiqqer_ckeditor4_ajax_uploadComplete');
+            this.$Upload.setParam('extract', 0);
+
+            this.$Upload.inject(Content);
+        },
+
+
+        $onSubmit: function () {
+            this.$Upload.submit();
+        },
+
+        $onComplete: function (Form, File, result) {
+
+            QUI.MessageHandler.addSuccess(
+                QUILocale.get("quiqqer/ckeditor4", "message.window.upload.complete")
+            );
+
+            this.close();
+        }
+
+    });
+});
diff --git a/locale.xml b/locale.xml
index 3abdb30..8366290 100644
--- a/locale.xml
+++ b/locale.xml
@@ -23,6 +23,39 @@
             <en><![CDATA[The given plugin is not active]]></en>
         </locale>
 
+        <!--Upload-->
+        <locale name="window.upload.title">
+            <de><![CDATA[Plugin hochladen]]></de>
+            <en><![CDATA[Upload plugin]]></en>
+        </locale>
+        <locale name="window.upload.button.upload">
+            <de><![CDATA[Hochladen]]></de>
+            <en><![CDATA[Upload]]></en>
+        </locale>
+        <locale name="message.window.upload.complete">
+            <de><![CDATA[Der Upload ist beendet und das Plugin wird nun installiert]]></de>
+            <en><![CDATA[The upload is complete and the plugin will now get installed]]></en>
+        </locale>
+        <locale name="exception.install.file.invalid.format">
+            <de>
+                <![CDATA[Die hochegaldene Datei hat das falsche Format oder ist beschädigt. Bitte laden Sie ein intaktes Ziparchiv hoch.]]></de>
+            <en>
+                <![CDATA[The uploaded file is either corrupted or does not have the correct format. Please upload a zip file.]]></en>
+        </locale>
+        <locale name="exception.install.file.extract.failed">
+            <de>
+                <![CDATA[Während des Entpackens des Archivs ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.]]></de>
+            <en><![CDATA[An error occured while extracteing the archive. Please try again]]></en>
+        </locale>
+        <locale name="message.plugin.install.success">
+            <de><![CDATA[Das Plugin wurde erfolgreich installiert]]></de>
+            <en><![CDATA[The plugin has been installed successfully]]></en>
+        </locale>
+        <locale name="exception.install.file.exists">
+            <de><![CDATA[Ein Plugin mit diesem Namen existiert bereits!]]></de>
+            <en><![CDATA[A plugin with the same name is installed already]]></en>
+        </locale>
+
         <!--Settings -->
         <locale name="editors.settings.plugins.table.header.state">
             <de><![CDATA[Status]]></de>
@@ -40,6 +73,10 @@
             <de><![CDATA[Deaktivieren]]></de>
             <en><![CDATA[Deactivate]]></en>
         </locale>
+        <locale name="editors.settings.plugins.table.button.upload">
+            <de><![CDATA[Hochladen]]></de>
+            <en><![CDATA[Upload]]></en>
+        </locale>
         <locale name="message.settings.plugins.activation.success">
             <de><![CDATA[Plugin erfolgreich aktiviert]]></de>
             <en><![CDATA[Plugin enabled successfully]]></en>
diff --git a/src/QUI/Ckeditor/Plugins/Manager.php b/src/QUI/Ckeditor/Plugins/Manager.php
index 19fca20..ccbd98b 100644
--- a/src/QUI/Ckeditor/Plugins/Manager.php
+++ b/src/QUI/Ckeditor/Plugins/Manager.php
@@ -3,6 +3,7 @@
 
 namespace QUI\Ckeditor\Plugins;
 
+use QUI\Archiver\Zip;
 use QUI\Exception;
 use QUI\System\Log;
 use QUI\Utils\Security\Orthos;
@@ -23,6 +24,7 @@ class Manager
 
     /**
      * List of plugins which should be installed
+     *
      * @var array
      */
     protected $blacklist = array(
@@ -187,6 +189,67 @@ class Manager
         return $result;
     }
 
+    /**
+     * Installs a plugin zip file from the given path
+     *
+     * @param $pluginpath
+     *
+     * @throws Exception
+     */
+    public function installPlugin($pluginpath)
+    {
+        # Check if file exists
+        if (!file_exists($pluginpath)) {
+            throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.not.found"));
+        }
+
+        $tmpDir = \QUI::getTemp()->createFolder();
+        copy(
+            $pluginpath,
+            $tmpDir . "/archive.zip"
+        );
+
+
+        $Zip = new \ZipArchive();
+
+        if ($Zip->open($tmpDir . "/archive.zip") === false) {
+            throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.invalid.format"));
+        }
+
+        if ($Zip->extractTo($tmpDir . "/content") === false) {
+            throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.extract.failed"));
+        }
+
+        foreach (scandir($tmpDir . "/content") as $entry) {
+            if ($entry == "." || $entry == "..") {
+                continue;
+            }
+
+
+            if (is_dir($this->installedPluginDir . "/" . $entry)) {
+                throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.exists"));
+            }
+
+            if (is_dir($this->activePluginDir . "/" . $entry)) {
+                throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.exists"));
+            }
+
+            rename(
+                $tmpDir . "/content/" . $entry,
+                $this->installedPluginDir . "/" . $entry
+            );
+        }
+
+        File::deleteDir($tmpDir);
+
+        \QUI::getMessagesHandler()->addSuccess(
+            \QUI::getLocale()->get(
+                "quiqqer/ckeditor4",
+                "message.plugin.install.success"
+            )
+        );
+    }
+
     #########################################
     #             Enable/Disable            #
     #########################################
-- 
GitLab