Commit 5c1da404 authored by jan.koester's avatar jan.koester
Browse files

webedit navbar support

parent cb6c74f2
Loading
Loading
Loading
Loading
+102 −1
Changes for plugins/staticpage/static.cpp: 102 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -639,6 +639,55 @@ json_object* blogi::StaticPage::getPublishForm(const int tid){
    json_object_object_add(f_meta, "placeholder", json_object_new_string(tr(_lang,"keyword1, keyword2").c_str()));
    json_object_array_add(fields, f_meta);

    json_object *f_method = json_object_new_object();
    json_object_object_add(f_method, "key", json_object_new_string("method"));
    json_object_object_add(f_method, "label", json_object_new_string(tr(_lang,"Publish Method").c_str()));
    json_object_object_add(f_method, "type", json_object_new_string("select"));
    json_object *method_opts = json_object_new_array();
    json_object *m_default = json_object_new_object();
    json_object_object_add(m_default, "value", json_object_new_string("none"));
    json_object_object_add(m_default, "label", json_object_new_string(tr(_lang,"Only publish static page").c_str()));
    json_object_array_add(method_opts, m_default);
    json_object *m_nav = json_object_new_object();
    json_object_object_add(m_nav, "value", json_object_new_string("add_to_navbar"));
    json_object_object_add(m_nav, "label", json_object_new_string(tr(_lang,"Publish and add to navbar").c_str()));
    json_object_array_add(method_opts, m_nav);
    json_object_object_add(f_method, "options", method_opts);
    json_object_array_add(fields, f_method);

    json_object *f_navlabel = json_object_new_object();
    json_object_object_add(f_navlabel, "key", json_object_new_string("navbar_label"));
    json_object_object_add(f_navlabel, "label", json_object_new_string(tr(_lang,"Navbar Label").c_str()));
    json_object_object_add(f_navlabel, "type", json_object_new_string("text"));
    json_object_object_add(f_navlabel, "placeholder", json_object_new_string(tr(_lang,"Page title in navbar").c_str()));
    json_object_array_add(fields, f_navlabel);

    json_object *f_navbar = json_object_new_object();
    json_object_object_add(f_navbar, "key", json_object_new_string("navbar_id"));
    json_object_object_add(f_navbar, "label", json_object_new_string(tr(_lang,"Target Navbar").c_str()));
    json_object_object_add(f_navbar, "type", json_object_new_string("select"));
    json_object *navbar_opts = json_object_new_array();
    json_object *nav_auto = json_object_new_object();
    json_object_object_add(nav_auto, "value", json_object_new_string(""));
    json_object_object_add(nav_auto, "label", json_object_new_string(tr(_lang,"Auto (first navbar)").c_str()));
    json_object_array_add(navbar_opts, nav_auto);

    try {
        dbpp::SQL sql;
        dbpp::DBResult res;
        sql << "SELECT id,name FROM navbar ORDER BY id";
        int count = Args->database[tid]->exec(sql, res);
        for (int i = 0; i < count; ++i) {
            json_object *nav_opt = json_object_new_object();
            json_object_object_add(nav_opt, "value", json_object_new_string(res[i][0] ? res[i][0] : ""));
            json_object_object_add(nav_opt, "label", json_object_new_string(res[i][1] ? res[i][1] : ""));
            json_object_array_add(navbar_opts, nav_opt);
        }
    } catch (...) {}

    json_object_object_add(f_navbar, "options", navbar_opts);
    json_object_array_add(fields, f_navbar);

    return fields;
}

@@ -647,7 +696,8 @@ void blogi::StaticPage::Publish(const int tid, const std::string &html, json_obj
    std::vector<char> sbuf;
    json_object *val_obj = nullptr;

    std::string url, meta;
    std::string url, meta, method, navbarLabel;
    int navbarId = -1;

    if (params) {
        if (json_object_object_get_ex(params, "url", &val_obj)) {
@@ -658,7 +708,25 @@ void blogi::StaticPage::Publish(const int tid, const std::string &html, json_obj
            const char *v = json_object_get_string(val_obj);
            if (v) meta = v;
        }
        if (json_object_object_get_ex(params, "method", &val_obj)) {
            const char *v = json_object_get_string(val_obj);
            if (v) method = v;
        }
        if (json_object_object_get_ex(params, "navbar_label", &val_obj)) {
            const char *v = json_object_get_string(val_obj);
            if (v) navbarLabel = v;
        }
        if (json_object_object_get_ex(params, "navbar_id", &val_obj)) {
            if (json_object_is_type(val_obj, json_type_string)) {
                const char *v = json_object_get_string(val_obj);
                if (v && *v) navbarId = atoi(v);
            } else {
                navbarId = json_object_get_int(val_obj);
            }
        }
    }

    if (method.empty()) method = "none";

    if (url.empty()) {
        json_object_object_add(response, "status", json_object_new_string("error"));
@@ -705,6 +773,39 @@ void blogi::StaticPage::Publish(const int tid, const std::string &html, json_obj
            std::string fullUrl = Args->config->getprefix() + "/staticpage/" + url;
            json_object_object_add(response, "url", json_object_new_string(fullUrl.c_str()));
        }

        if (method == "add_to_navbar") {
            if (navbarId <= 0) {
                sql.clear();
                res.clear();
                sql << "SELECT id FROM navbar ORDER BY id LIMIT 1";
                if (Args->database[tid]->exec(sql, res) > 0) {
                    navbarId = atoi(res[0][0]);
                }
            }

            if (navbarId > 0) {
                std::string navUrl = Args->config->buildurl("staticpage/") + url;
                std::string navName = navbarLabel.empty() ? url : navbarLabel;

                sql.clear();
                res.clear();
                sql << "SELECT id FROM navbar_items WHERE navbar_id=" << navbarId
                    << " AND url='" << dbpp::SQL::escaped(sbuf, navUrl.c_str()) << "' LIMIT 1";
                if (Args->database[tid]->exec(sql, res) < 1) {
                    sql.clear();
                    res.clear();
                    sql << "INSERT INTO navbar_items (name,url,type,navbar_id) VALUES ('"
                        << dbpp::SQL::escaped(sbuf, navName.c_str()) << "','"
                        << dbpp::SQL::escaped(sbuf, navUrl.c_str()) << "',0,"
                        << navbarId << ")";
                    Args->database[tid]->exec(sql, res);
                }
            } else {
                json_object_object_add(response, "navbar_warning",
                    json_object_new_string(tr(_lang,"No navbar found. Static page was published without navbar entry.").c_str()));
            }
        }
    } catch (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()));