Commit 76749833 authored by jan.koester's avatar jan.koester
Browse files

test

parent fe297bd7
Loading
Loading
Loading
Loading
+61 −19
Changes for plugins/survey/survey.cpp: 61 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -231,11 +231,29 @@ namespace blogi {
            int count = Args->database[tid]->exec(sql, res);
            for (int i = 0; i < count; ++i) {
                std::string name = res[i][0];
                std::string xml = res[i][1];
                std::string data = res[i][1];
                SurveyTemplate tpl;
                if (parseXml(xml, tpl)) {
                // Try JSON first, fall back to XML for backward compatibility
                json_object *jdata = json_tokener_parse(data.c_str());
                if (jdata && json_object_is_type(jdata, json_type_object) && templateFromJson(jdata, tpl)) {
                    json_object_put(jdata);
                    tpl.name = name;
                    _templates[name] = std::move(tpl);
                } else {
                    if (jdata) json_object_put(jdata);
                    if (parseXml(data, tpl)) {
                        tpl.name = name;
                        _templates[name] = std::move(tpl);
                        // Migrate XML to JSON in DB
                        json_object *jtpl = templateToJson(tpl);
                        const char *jsonStr = json_object_to_json_string_ext(jtpl, JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE);
                        dbpp::SQL upd;
                        dbpp::DBResult updRes;
                        std::vector<char> sbuf;
                        upd << "UPDATE survey_templates SET xml_data='" << upd.escaped(sbuf, jsonStr) << "' WHERE name='" << upd.escaped(sbuf, name.c_str()) << "';";
                        try { Args->database[tid]->exec(upd, updRes); } catch (...) {}
                        json_object_put(jtpl);
                    }
                }
            }
        }
@@ -2318,21 +2336,36 @@ namespace blogi {

            if (!sname.empty() && !sxml.empty()) {
                SurveyTemplate tpl;
                if (!parseXml(sxml, tpl)) {
                // Try JSON first, fall back to XML
                json_object *jdata = json_tokener_parse(sxml.c_str());
                bool parsed = false;
                if (jdata && json_object_is_type(jdata, json_type_object) && templateFromJson(jdata, tpl)) {
                    parsed = true;
                } else {
                    parsed = parseXml(sxml, tpl);
                }
                if (jdata) json_object_put(jdata);

                if (!parsed) {
                    json_object *jroot = json_object_new_object();
                    json_object_object_add(jroot, "title", json_object_new_string(tr(_lang, "Add Survey").c_str()));
                    json_object_object_add(jroot, "message", json_object_new_string(tr(_lang, "Invalid XML format").c_str()));
                    json_object_object_add(jroot, "message", json_object_new_string(tr(_lang, "Invalid JSON or XML format").c_str()));
                    json_object_object_add(jroot, "message_type", json_object_new_string("error"));
                    return jroot;
                }

                // Store as JSON
                json_object *jtpl = templateToJson(tpl);
                const char *jsonStr = json_object_to_json_string_ext(jtpl, JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE);

                dbpp::SQL sql;
                dbpp::DBResult res;
                std::vector<char> sbuf;
                sql << "INSERT INTO survey_templates (name, xml_data, title) VALUES('"
                    << sql.escaped(sbuf, sname.c_str()) << "','"
                    << sql.escaped(sbuf, sxml.c_str()) << "','"
                    << sql.escaped(sbuf, jsonStr) << "','"
                    << sql.escaped(sbuf, tpl.title.c_str()) << "');";
                json_object_put(jtpl);
                try {
                    Args->database[tid]->exec(sql, res);
                } catch (...) {
@@ -2381,15 +2414,16 @@ namespace blogi {

            json_object *fxml = json_object_new_object();
            json_object_object_add(fxml, "name", json_object_new_string("survey_xml"));
            json_object_object_add(fxml, "label", json_object_new_string(tr(_lang, "Survey XML").c_str()));
            json_object_object_add(fxml, "label", json_object_new_string(tr(_lang, "Survey JSON").c_str()));
            json_object_object_add(fxml, "type", json_object_new_string("textarea"));
            json_object_object_add(fxml, "placeholder", json_object_new_string(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<survey id=\"example\" title=\"My Survey\">\n"
                "  <description>Description</description>\n"
                "  <question id=\"q1\" type=\"radio\" required=\"true\">\n"
                "    <text>Your question</text>\n"
                "    <option value=\"a\">Option A</option>\n"
                "  </question>\n</survey>"));
                "{\n  \"id\": \"example\",\n  \"title\": \"My Survey\",\n"
                "  \"description\": \"Description\",\n"
                "  \"questions\": [{\n"
                "    \"id\": \"q1\", \"type\": \"radio\", \"required\": true,\n"
                "    \"text\": \"Your question\",\n"
                "    \"options\": [{\"value\": \"a\", \"label\": \"Option A\"}]\n"
                "  }]\n}"));
            json_object_array_add(jfields, fxml);

            json_object_object_add(jform, "fields", jfields);
@@ -2737,8 +2771,9 @@ namespace blogi {
                return true;
            }

            std::string xmlStr = toXml(tpl);
            json_object_put(jreq);
            // Store as JSON directly
            json_object *jtpl = templateToJson(tpl);
            const char *jsonStr = json_object_to_json_string_ext(jtpl, JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE);

            dbpp::SQL sql;
            dbpp::DBResult res;
@@ -2758,8 +2793,10 @@ namespace blogi {
            sql.clear();
            sql << "INSERT INTO survey_templates (name, xml_data, title) VALUES('"
                << sql.escaped(sbuf, sname.c_str()) << "','"
                << sql.escaped(sbuf, xmlStr.c_str()) << "','"
                << sql.escaped(sbuf, jsonStr) << "','"
                << sql.escaped(sbuf, tpl.title.c_str()) << "');";
            json_object_put(jtpl);
            json_object_put(jreq);
            try {
                Args->database[tid]->exec(sql, res);
            } catch (...) {
@@ -2898,7 +2935,8 @@ namespace blogi {
                return true;
            }

            std::string xmlStr = toXml(tpl);
            json_object *jtpl = templateToJson(tpl);
            const char *jsonStr = json_object_to_json_string_ext(jtpl, JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE);
            json_object_put(jreq);

            dbpp::SQL sql;
@@ -2910,6 +2948,7 @@ namespace blogi {
            int existing = 0;
            try { existing = Args->database[tid]->exec(sql, res); } catch (...) {}
            if (existing > 0) {
                json_object_put(jtpl);
                std::string err = "{\"status\":\"error\",\"message\":\"Survey '\" + jsonEscape(sname) + \"' already exists\"}";
                resp.setState(HTTP409);
                resp.setContentType("application/json; charset=utf-8");
@@ -2921,8 +2960,9 @@ namespace blogi {
            sql.clear();
            sql << "INSERT INTO survey_templates (name, xml_data, title) VALUES('"
                << sql.escaped(sbuf, sname.c_str()) << "','"
                << sql.escaped(sbuf, xmlStr.c_str()) << "','"
                << sql.escaped(sbuf, jsonStr) << "','"
                << sql.escaped(sbuf, tpl.title.c_str()) << "');";
            json_object_put(jtpl);
            try {
                Args->database[tid]->exec(sql, res);
            } catch (...) {
@@ -3184,7 +3224,8 @@ namespace blogi {
                    return jroot;
                }

                std::string xmlStr = toXml(tpl);
                json_object *jtpl = templateToJson(tpl);
                const char *jsonStr = json_object_to_json_string_ext(jtpl, JSON_C_TO_STRING_PLAIN | JSON_C_TO_STRING_NOSLASHESCAPE);
                json_object_put(jreq);

                // Upsert: delete + insert
@@ -3198,8 +3239,9 @@ namespace blogi {
                sql.clear();
                sql << "INSERT INTO survey_templates (name, xml_data, title) VALUES('"
                    << sql.escaped(sbuf, sname.c_str()) << "','"
                    << sql.escaped(sbuf, xmlStr.c_str()) << "','"
                    << sql.escaped(sbuf, jsonStr) << "','"
                    << sql.escaped(sbuf, tpl.title.c_str()) << "');";
                json_object_put(jtpl);
                try {
                    Args->database[tid]->exec(sql, res);
                } catch (...) {