Commit 6ee48c11 authored by jan.koester's avatar jan.koester
Browse files

test

parent c3413df2
Loading
Loading
Loading
Loading
+446 −0
Changes for plugins/template/template.cpp: 446 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -27,6 +27,11 @@

#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <vector>
#include <set>
#include <regex>

#include <htmlpp/html.h>

@@ -36,6 +41,7 @@

#include <conf.h>
#include <plugin.h>
#include <i18n.h>


namespace blogi {
@@ -59,8 +65,364 @@ namespace blogi {
                << "type integer"
                << ");";
            Args->database[0]->exec(sql,res);

            // Auto-create empty templates for each layer found in theme HTML files
            autoCreateLayers();
        };

        bool haveSettings() override { return true; }

        json_object* Settings(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid) override {
            std::string _lang = getLang(*Args->database[tid]);
            std::string url = req.getRequestURL();
            std::string base = Args->config->buildurl("settings/template/");

            // Sub-routing
            if (url.find(base) != std::string::npos && url.length() > base.length()) {
                std::string sub = url.substr(base.length());
                // Parse query string
                std::string path = sub;
                std::string query;
                auto qpos = sub.find('?');
                if (qpos != std::string::npos) {
                    path = sub.substr(0, qpos);
                    query = sub.substr(qpos + 1);
                }

                if (path == "delete") {
                    return settingsDelete(tid, req, _lang, query);
                } else if (path == "edit") {
                    return settingsEdit(tid, req, _lang, query);
                }
            }

            return settingsIndex(tid, req, _lang);
        }

    private:
        // Extract all id="" values from an HTML file
        std::set<std::string> extractIdsFromFile(const std::string &filepath) {
            std::set<std::string> ids;
            std::ifstream f(filepath);
            if (!f.is_open()) return ids;

            std::string content((std::istreambuf_iterator<char>(f)),
                                 std::istreambuf_iterator<char>());
            f.close();

            std::regex idRegex("\\bid=[\"']([^\"']+)[\"']");
            std::sregex_iterator it(content.begin(), content.end(), idRegex);
            std::sregex_iterator end;
            for (; it != end; ++it) {
                ids.insert((*it)[1].str());
            }
            return ids;
        }

        void autoCreateLayers() {
            std::string tplDir = Args->config->gettemplate();

            // Scan index.html and mobile.html for IDs
            std::set<std::string> allIds;
            for (const char *file : {"index.html", "mobile.html"}) {
                std::string path = tplDir + "/" + file;
                auto ids = extractIdsFromFile(path);
                allIds.insert(ids.begin(), ids.end());
            }

            if (allIds.empty()) return;

            // Get existing layers from DB
            std::set<std::string> existingLayers;
            {
                dbpp::SQL sql;
                dbpp::DBResult res;
                sql << "SELECT DISTINCT layer FROM templates;";
                try {
                    int n = Args->database[0]->exec(sql, res);
                    for (int i = 0; i < n; ++i) {
                        if (res[i][0]) existingLayers.insert(res[i][0]);
                    }
                } catch (...) {}
            }

            // Skip IDs that are internal/editor-only (not useful as content layers)
            static const std::set<std::string> skipIds = {
                "banner", "copyrigt", "rendering", "toolbar", "plugin-list",
                "loading-plugins", "tpl-current", "tpl-current-name", "tpl-current-rev",
                "tpl-list", "tpl-status", "xml-import-input", "publish-panel",
                "publish-targets", "publish-form", "publish-status", "document-tree",
                "tree-root", "properties-panel", "prop-widget-type", "selected-element-id",
                "prop-status", "element-settings", "theme-palette", "theme-swatches",
                "preview", "addElementModal", "modal-title", "modalPluginName",
                "modalPluginTypeId", "modalParentUuid", "modalPosition", "modal-status",
                "modalAddButton", "view-controls", "desktopViewBtn", "mobileViewBtn",
                "maincontent", "layer1", "cookiebanner", "cookieoverlay",
                "cookiecontent", "cookiesettings", "cookiesubmit", "theme-toggle"
            };

            // Insert missing layers
            for (const auto &id : allIds) {
                if (skipIds.count(id)) continue;
                if (existingLayers.count(id)) continue;

                std::vector<char> sbuf;
                dbpp::SQL sql;
                dbpp::DBResult res;
                sql << "INSERT INTO templates (layer, html, type) VALUES ('"
                    << dbpp::SQL::escaped(sbuf, id.c_str()) << "', '', 0);";
                try {
                    Args->database[0]->exec(sql, res);
                } catch (...) {}
            }
        }

        json_object* settingsIndex(const int tid, libhttppp::HttpRequest &req, const std::string &_lang) {
            json_object *jroot = json_object_new_object();
            json_object_object_add(jroot, "title", json_object_new_string(tr(_lang, "Templates").c_str()));

            // Query all templates
            dbpp::SQL sql;
            dbpp::DBResult res;
            sql << "SELECT id, layer, type, length(html) FROM templates ORDER BY layer, type;";
            int n = 0;
            try { n = Args->database[tid]->exec(sql, res); } catch (...) {}

            // Table
            json_object *jtable = json_object_new_object();
            json_object *jheaders = json_object_new_array();
            json_object_array_add(jheaders, json_object_new_string("ID"));
            json_object_array_add(jheaders, json_object_new_string("Layer"));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang, "Device Type").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang, "Content").c_str()));
            json_object_object_add(jtable, "headers", jheaders);

            json_object *jrows = json_object_new_array();
            for (int i = 0; i < n; ++i) {
                json_object *jrow = json_object_new_object();
                json_object *jcells = json_object_new_array();
                json_object_array_add(jcells, json_object_new_string(res[i][0]));
                json_object_array_add(jcells, json_object_new_string(res[i][1]));

                int devType = atoi(res[i][2]);
                const char *devLabel = devType == 1 ? "Desktop" : devType == 2 ? "Mobile" : "All";
                json_object_array_add(jcells, json_object_new_string(tr(_lang, devLabel).c_str()));

                int htmlLen = atoi(res[i][3]);
                std::string sizeStr = htmlLen > 0 ? (std::to_string(htmlLen) + " Bytes") : tr(_lang, "Empty");
                json_object_array_add(jcells, json_object_new_string(sizeStr.c_str()));

                json_object_object_add(jrow, "cells", jcells);

                // Actions
                json_object *jactions = json_object_new_array();

                // Edit link → opens editor with this template's layer
                json_object *jedit = json_object_new_object();
                json_object_object_add(jedit, "label", json_object_new_string(tr(_lang, "Edit").c_str()));
                json_object_object_add(jedit, "url", json_object_new_string(
                    (Args->config->buildurl("settings/template/edit?id=") + std::string(res[i][0])).c_str()));
                json_object_array_add(jactions, jedit);

                // Delete
                json_object *jdel = json_object_new_object();
                json_object_object_add(jdel, "label", json_object_new_string(tr(_lang, "Delete").c_str()));
                json_object_object_add(jdel, "url", json_object_new_string(
                    (Args->config->buildurl("settings/template/delete?id=") + std::string(res[i][0])).c_str()));
                json_object_array_add(jactions, jdel);

                json_object_object_add(jrow, "actions", jactions);
                json_object_array_add(jrows, jrow);
            }
            json_object_object_add(jtable, "rows", jrows);
            json_object_object_add(jroot, "table", jtable);

            // Links: Open Editor
            json_object *jlinks = json_object_new_array();
            json_object *jleditor = json_object_new_object();
            json_object_object_add(jleditor, "label", json_object_new_string(tr(_lang, "Open Editor").c_str()));
            json_object_object_add(jleditor, "url", json_object_new_string(
                Args->config->buildurl("editor").c_str()));
            json_object_array_add(jlinks, jleditor);
            json_object_object_add(jroot, "links", jlinks);

            return jroot;
        }

        json_object* settingsDelete(const int tid, libhttppp::HttpRequest &req, const std::string &_lang, const std::string &query) {
            // Parse id from query
            std::string idStr;
            auto p = query.find("id=");
            if (p != std::string::npos) {
                idStr = query.substr(p + 3);
                auto amp = idStr.find('&');
                if (amp != std::string::npos) idStr = idStr.substr(0, amp);
            }

            json_object *jroot = json_object_new_object();

            if (!idStr.empty()) {
                int id = atoi(idStr.c_str());
                if (id > 0) {
                    try {
                        dbpp::SQL sql;
                        dbpp::DBResult res;
                        sql << "DELETE FROM templates WHERE id = " << id << ";";
                        Args->database[tid]->exec(sql, res);
                        json_object_object_add(jroot, "message", json_object_new_string(tr(_lang, "Deleted!").c_str()));
                        json_object_object_add(jroot, "message_type", json_object_new_string("success"));
                    } catch (const std::exception &e) {
                        json_object_object_add(jroot, "message", json_object_new_string(e.what()));
                        json_object_object_add(jroot, "message_type", json_object_new_string("error"));
                    }
                }
            }

            // Show list after delete
            json_object *idx = settingsIndex(tid, req, _lang);
            // Merge message into index result
            json_object *msg = nullptr, *mtype = nullptr;
            if (json_object_object_get_ex(jroot, "message", &msg)) {
                json_object_object_add(idx, "message", json_object_get(msg));
            }
            if (json_object_object_get_ex(jroot, "message_type", &mtype)) {
                json_object_object_add(idx, "message_type", json_object_get(mtype));
            }
            json_object_put(jroot);
            return idx;
        }

        json_object* settingsEdit(const int tid, libhttppp::HttpRequest &req, const std::string &_lang, const std::string &query) {
            // Parse id from query
            std::string idStr;
            auto p = query.find("id=");
            if (p != std::string::npos) {
                idStr = query.substr(p + 3);
                auto amp = idStr.find('&');
                if (amp != std::string::npos) idStr = idStr.substr(0, amp);
            }

            int id = atoi(idStr.c_str());

            // Check for form submission (POST)
            libhttppp::HttpForm form;
            form.parse(req);
            bool saved = false;
            for (const auto &cur : form.urlData()) {
                if (cur.key == "tpl_layer") {
                    std::string layer = cur.value;
                    std::string html;
                    int type = 0;
                    for (const auto &f : form.urlData()) {
                        if (f.key == "tpl_html") html = f.value;
                        if (f.key == "tpl_type") type = atoi(f.value.c_str());
                    }
                    std::vector<char> sbuf;
                    dbpp::SQL sql;
                    dbpp::DBResult res;
                    sql << "UPDATE templates SET layer = '"
                        << dbpp::SQL::escaped(sbuf, layer.c_str()) << "', html = '"
                        << dbpp::SQL::escaped(sbuf, html.c_str()) << "', type = "
                        << type << " WHERE id = " << id << ";";
                    try {
                        Args->database[tid]->exec(sql, res);
                        saved = true;
                    } catch (...) {}
                    break;
                }
            }

            json_object *jroot = json_object_new_object();
            json_object_object_add(jroot, "title", json_object_new_string(
                (tr(_lang, "Edit Template") + " #" + std::to_string(id)).c_str()));

            if (saved) {
                json_object_object_add(jroot, "message", json_object_new_string(tr(_lang, "Saved!").c_str()));
                json_object_object_add(jroot, "message_type", json_object_new_string("success"));
            }

            // Load current values
            std::string layer, html;
            int type = 0;
            {
                dbpp::SQL sql;
                dbpp::DBResult res;
                sql << "SELECT layer, html, type FROM templates WHERE id = " << id << ";";
                try {
                    int n = Args->database[tid]->exec(sql, res);
                    if (n > 0) {
                        if (res[0][0]) layer = res[0][0];
                        if (res[0][1]) html = res[0][1];
                        type = atoi(res[0][2]);
                    }
                } catch (...) {}
            }

            // Nav
            json_object *jnav = json_object_new_array();
            json_object *jback = json_object_new_object();
            json_object_object_add(jback, "label", json_object_new_string(tr(_lang, "Back to list").c_str()));
            json_object_object_add(jback, "url", json_object_new_string(
                Args->config->buildurl("settings/template").c_str()));
            json_object_array_add(jnav, jback);

            json_object *jedlink = json_object_new_object();
            json_object_object_add(jedlink, "label", json_object_new_string(tr(_lang, "Open Editor").c_str()));
            json_object_object_add(jedlink, "url", json_object_new_string(
                Args->config->buildurl("editor").c_str()));
            json_object_array_add(jnav, jedlink);
            json_object_object_add(jroot, "nav", jnav);

            // Form
            json_object *jform = json_object_new_object();
            json_object_object_add(jform, "method", json_object_new_string("POST"));
            json_object_object_add(jform, "action", json_object_new_string(
                (Args->config->buildurl("settings/template/edit?id=") + std::to_string(id)).c_str()));

            json_object *jfields = json_object_new_array();

            // Layer field
            json_object *jf1 = json_object_new_object();
            json_object_object_add(jf1, "name", json_object_new_string("tpl_layer"));
            json_object_object_add(jf1, "label", json_object_new_string("Layer (Element ID)"));
            json_object_object_add(jf1, "type", json_object_new_string("text"));
            json_object_object_add(jf1, "value", json_object_new_string(layer.c_str()));
            json_object_array_add(jfields, jf1);

            // Type field
            json_object *jf2 = json_object_new_object();
            json_object_object_add(jf2, "name", json_object_new_string("tpl_type"));
            json_object_object_add(jf2, "label", json_object_new_string(tr(_lang, "Device Type").c_str()));
            json_object_object_add(jf2, "type", json_object_new_string("select"));
            json_object *topts = json_object_new_array();
            const char *typeLabels[] = {"All", "Desktop", "Mobile"};
            for (int t = 0; t <= 2; ++t) {
                json_object *to = json_object_new_object();
                json_object_object_add(to, "value", json_object_new_string(std::to_string(t).c_str()));
                json_object_object_add(to, "label", json_object_new_string(tr(_lang, typeLabels[t]).c_str()));
                if (t == type) json_object_object_add(to, "selected", json_object_new_boolean(true));
                json_object_array_add(topts, to);
            }
            json_object_object_add(jf2, "options", topts);
            json_object_array_add(jfields, jf2);

            // HTML field
            json_object *jf3 = json_object_new_object();
            json_object_object_add(jf3, "name", json_object_new_string("tpl_html"));
            json_object_object_add(jf3, "label", json_object_new_string("HTML"));
            json_object_object_add(jf3, "type", json_object_new_string("textarea"));
            json_object_object_add(jf3, "value", json_object_new_string(html.c_str()));
            json_object_array_add(jfields, jf3);

            json_object_object_add(jform, "fields", jfields);
            json_object_object_add(jform, "submit", json_object_new_string(tr(_lang, "save").c_str()));
            json_object_object_add(jroot, "form", jform);

            return jroot;
        }

    public:

        void Rendering(const int tid,libhttppp::HttpRequest &req,libhtmlpp::HtmlElement* curpage,int type,const std::string &sessionid=""){

            dbpp::SQL sql;
@@ -105,6 +467,90 @@ namespace blogi {
            return;
        }

        bool canPublish() override {
            return true;
        }

        json_object* getPublishForm(const int tid) override {
            std::string _lang = getLang(*Args->database[tid]);
            json_object *fields = json_object_new_array();

            json_object *f_layer = json_object_new_object();
            json_object_object_add(f_layer, "key", json_object_new_string("layer"));
            json_object_object_add(f_layer, "label", json_object_new_string(tr(_lang,"Layer (Element ID)").c_str()));
            json_object_object_add(f_layer, "type", json_object_new_string("text"));
            json_object_object_add(f_layer, "required", json_object_new_boolean(1));
            json_object_object_add(f_layer, "placeholder", json_object_new_string("content"));
            json_object_array_add(fields, f_layer);

            json_object *f_type = json_object_new_object();
            json_object_object_add(f_type, "key", json_object_new_string("type"));
            json_object_object_add(f_type, "label", json_object_new_string(tr(_lang,"Device Type").c_str()));
            json_object_object_add(f_type, "type", json_object_new_string("select"));
            json_object *opts = json_object_new_array();
            json_object *opt0 = json_object_new_object();
            json_object_object_add(opt0, "value", json_object_new_string("0"));
            json_object_object_add(opt0, "label", json_object_new_string(tr(_lang,"All").c_str()));
            json_object_array_add(opts, opt0);
            json_object *opt1 = json_object_new_object();
            json_object_object_add(opt1, "value", json_object_new_string("1"));
            json_object_object_add(opt1, "label", json_object_new_string("Desktop"));
            json_object_array_add(opts, opt1);
            json_object *opt2 = json_object_new_object();
            json_object_object_add(opt2, "value", json_object_new_string("2"));
            json_object_object_add(opt2, "label", json_object_new_string("Mobile"));
            json_object_array_add(opts, opt2);
            json_object_object_add(f_type, "options", opts);
            json_object_array_add(fields, f_type);

            return fields;
        }

        void Publish(const int tid, const std::string &html, json_object *params, json_object *response) override {
            std::string _lang = getLang(*Args->database[tid]);

            std::string layer;
            int type = 0;

            if (params) {
                json_object *val_obj = nullptr;
                if (json_object_object_get_ex(params, "layer", &val_obj)) {
                    const char *v = json_object_get_string(val_obj);
                    if (v) layer = v;
                }
                if (json_object_object_get_ex(params, "type", &val_obj)) {
                    if (json_object_is_type(val_obj, json_type_string)) {
                        type = atoi(json_object_get_string(val_obj));
                    } else {
                        type = json_object_get_int(val_obj);
                    }
                }
            }

            if (layer.empty()) {
                json_object_object_add(response, "status", json_object_new_string("error"));
                json_object_object_add(response, "message", json_object_new_string(tr(_lang,"Layer is required.").c_str()));
                return;
            }

            try {
                std::vector<char> sbuf;
                dbpp::SQL sql;
                dbpp::DBResult res;
                sql << "INSERT INTO templates (layer, html, type) VALUES ('"
                    << dbpp::SQL::escaped(sbuf, layer.c_str()) << "', '"
                    << dbpp::SQL::escaped(sbuf, html.c_str()) << "', "
                    << type << ");";
                Args->database[tid]->exec(sql, res);

                json_object_object_add(response, "status", json_object_new_string("success"));
                json_object_object_add(response, "message", json_object_new_string(tr(_lang,"Published to template layer!").c_str()));
            } catch (const std::exception &e) {
                json_object_object_add(response, "status", json_object_new_string("error"));
                json_object_object_add(response, "message", json_object_new_string(e.what()));
            }
        }

    };

    EXPORT PluginApi* create() {
+24 −0
Changes for src/translations.h: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -373,6 +373,18 @@ namespace blogi {
            {"I18N_THEME_TOGGLE_LABEL", "Theme wechseln"},
            {"I18N_THEME_TO_LIGHT", "Zum hellen Modus wechseln"},
            {"I18N_THEME_TO_DARK", "Zum dunklen Modus wechseln"},
            // Plugin: template
            {"Layer (Element ID)", "Layer (Element-ID)"},
            {"Device Type", "Ger&auml;tetyp"},
            {"All", "Alle"},
            {"Layer is required.", "Layer ist erforderlich."},
            {"Published to template layer!", "In Template-Layer ver&ouml;ffentlicht!"},
            {"Templates", "Templates"},
            {"Content", "Inhalt"},
            {"Empty", "Leer"},
            {"Edit Template", "Template bearbeiten"},
            {"Open Editor", "Editor &ouml;ffnen"},
            {"Deleted!", "Gel&ouml;scht!"},
            // Plugin: gtm
            {"Google Tag Manager",   "Google Tag Manager"},
            {"This cookie is required for Tag Manager to improve the website.", "Dieses Cookie wird f&uuml;r den Tag Manager zur Verbesserung der Website ben&ouml;tigt."},
@@ -662,6 +674,18 @@ namespace blogi {
            {"I18N_THEME_TOGGLE_LABEL", "Toggle theme"},
            {"I18N_THEME_TO_LIGHT", "Switch to light mode"},
            {"I18N_THEME_TO_DARK", "Switch to dark mode"},
            // Plugin: template
            {"Layer (Element ID)", "Layer (Element ID)"},
            {"Device Type", "Device Type"},
            {"All", "All"},
            {"Layer is required.", "Layer is required."},
            {"Published to template layer!", "Published to template layer!"},
            {"Templates", "Templates"},
            {"Content", "Content"},
            {"Empty", "Empty"},
            {"Edit Template", "Edit Template"},
            {"Open Editor", "Open Editor"},
            {"Deleted!", "Deleted!"},
            // Plugin strings that need HTML entities or special formatting
            {"Do you want to remove this media type? All files of this type will also be removed!", "Do you want to remove this media type? All files of this type will also be removed!"},
            {"Do you want to remove this navigation?", "Do you want to remove this navigation?"},
+1 −0
Changes for src/webedit/CMakeLists.txt: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,3 +9,4 @@ add_subdirectory(customhtml)
add_subdirectory(section)
add_subdirectory(video)
add_subdirectory(survey)
add_subdirectory(list)
+11 −0
Changes for src/webedit/list/CMakeLists.txt: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
add_library(list SHARED list.cpp)

add_dependencies(list blogidev)

if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
    target_link_libraries(list PUBLIC blogidev kernel32.lib uuidp::uuidp htmlpp::htmlpp json-c::json-c tinyxml2::tinyxml2)
else()
    target_link_libraries(list PUBLIC blogidev dl uuidp::uuidp htmlpp::htmlpp json-c::json-c tinyxml2::tinyxml2)
endif()

install(TARGETS list DESTINATION lib/blogi/plugins/webedit)
+532 −0

File added.

Preview size limit exceeded, changes collapsed.