Commit 8d14bf5d authored by jan.koester's avatar jan.koester
Browse files

test

parent 91b9852a
Loading
Loading
Loading
Loading
+104 −21
Changes for plugins/survey/survey.cpp: 104 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <filesystem>

@@ -51,6 +52,7 @@ namespace blogi {
    struct SurveyOption {
        std::string value;
        std::string label;
        std::string excludes;  // comma-separated question IDs to skip when selected
    };

    struct SurveyQuestion {
@@ -240,6 +242,8 @@ namespace blogi {
                    const char *val = optEl->Attribute("value");
                    opt.value = val ? val : "";
                    opt.label = optEl->GetText() ? optEl->GetText() : "";
                    const char *exc = optEl->Attribute("excludes");
                    if (exc) opt.excludes = exc;
                    q.options.push_back(std::move(opt));
                }
                tpl.questions.push_back(std::move(q));
@@ -349,7 +353,43 @@ namespace blogi {
                    return "Company is required";
            }

            // Build set of excluded question IDs based on selected answers
            std::set<std::string> excludedIds;
            for (const auto &qq : tpl.questions) {
                json_object *f = nullptr;
                if (!json_object_object_get_ex(root, qq.id.c_str(), &f) || !f) continue;

                auto collectExcludes = [&](const std::string &selectedValue) {
                    for (const auto &opt : qq.options) {
                        if (opt.value == selectedValue && !opt.excludes.empty()) {
                            std::istringstream ss(opt.excludes);
                            std::string tok;
                            while (std::getline(ss, tok, ',')) {
                                size_t start = tok.find_first_not_of(' ');
                                size_t end = tok.find_last_not_of(' ');
                                if (start != std::string::npos)
                                    excludedIds.insert(tok.substr(start, end - start + 1));
                            }
                        }
                    }
                };

                if ((qq.type == "radio" || qq.type == "select") && json_object_is_type(f, json_type_string)) {
                    collectExcludes(json_object_get_string(f));
                } else if (qq.type == "checkbox" && json_object_is_type(f, json_type_array)) {
                    size_t len = json_object_array_length(f);
                    for (size_t i = 0; i < len; ++i) {
                        json_object *item = json_object_array_get_idx(f, i);
                        if (json_object_is_type(item, json_type_string))
                            collectExcludes(json_object_get_string(item));
                    }
                }
            }

            for (const auto &q : tpl.questions) {
                // Skip excluded questions
                if (excludedIds.count(q.id)) continue;

                json_object *field = nullptr;
                bool has = json_object_object_get_ex(root, q.id.c_str(), &field);

@@ -1109,23 +1149,33 @@ namespace blogi {
            int stepIdx = 1;
            for (const auto &q : tpl.questions) {
                html << "<div class=\"wizard-step\" data-step=\"" << stepIdx
                     << "\" data-question-id=\"" << q.id
                     << "\" data-required=\"" << (q.required ? "true" : "false") << "\">"
                     << "<div class=\"step-counter\">" << tr(_lang, "Step") << " " << (stepIdx + 1) << " / " << totalSteps << "</div>"
                     << "<div class=\"question\">"
                     << "<div class=\"question-text" << (q.required ? " required" : "") << "\">" << q.text << "</div>";

                if (q.type == "radio") {
                    for (const auto &opt : q.options)
                    for (const auto &opt : q.options) {
                        html << "<div class=\"option\"><label><input type=\"radio\" name=\"" << q.id
                             << "\" value=\"" << opt.value << "\"/>" << opt.label << "</label></div>";
                             << "\" value=\"" << opt.value << "\"";
                        if (!opt.excludes.empty()) html << " data-excludes=\"" << opt.excludes << "\"";
                        html << "/>" << opt.label << "</label></div>";
                    }
                } else if (q.type == "checkbox") {
                    for (const auto &opt : q.options)
                    for (const auto &opt : q.options) {
                        html << "<div class=\"option\"><label><input type=\"checkbox\" name=\"" << q.id
                             << "\" value=\"" << opt.value << "\"/>" << opt.label << "</label></div>";
                             << "\" value=\"" << opt.value << "\"";
                        if (!opt.excludes.empty()) html << " data-excludes=\"" << opt.excludes << "\"";
                        html << "/>" << opt.label << "</label></div>";
                    }
                } else if (q.type == "select") {
                    html << "<select name=\"" << q.id << "\"><option value=\"\">-- Select --</option>";
                    for (const auto &opt : q.options)
                        html << "<option value=\"" << opt.value << "\">" << opt.label << "</option>";
                    for (const auto &opt : q.options) {
                        html << "<option value=\"" << opt.value << "\"";
                        if (!opt.excludes.empty()) html << " data-excludes=\"" << opt.excludes << "\"";
                        html << ">" << opt.label << "</option>";
                    }
                    html << "</select>";
                } else if (q.type == "rating") {
                    html << "<div class=\"rating-group\">";
@@ -1182,7 +1232,7 @@ namespace blogi {
                 << ".step-counter{color:var(--blogi-text-muted,#6b6b6b);font-size:.9em;margin-bottom:8px;}"
                 << ".question{margin-bottom:20px;padding:16px;border:1px solid var(--blogi-border,#eaeaea);border-radius:8px;background:var(--blogi-bg-alt,#fafafa);}"
                 << ".question-text{font-weight:600;margin-bottom:12px;}"
                 << ".required::after{content:' *';color:#F36947;}"
                 << ".required::after{content:' *';color:var(--blogi-error,#F36947);}"
                 << ".option{margin:6px 0;}.option label{cursor:pointer;}"
                 << ".option input{margin-right:8px;}"
                 << ".question textarea,.question select{width:100%;padding:10px;border:1px solid var(--blogi-border,#ddd);border-radius:6px;font-size:1em;}"
@@ -1190,41 +1240,70 @@ namespace blogi {
                 << ".rating-group{display:flex;gap:8px;flex-wrap:wrap;}"
                 << ".rating-group label{padding:8px 16px;border:2px solid var(--blogi-border,#ddd);border-radius:8px;cursor:pointer;transition:.2s;}"
                 << ".rating-group input{display:none;}"
                 << ".rating-group label:has(input:checked){background:var(--blogi-accent,#84CB3A);color:#fff;border-color:var(--blogi-accent,#84CB3A);}"
                 << ".rating-group label:has(input:checked){background:var(--blogi-accent,#84CB3A);color:var(--blogi-surface,#fff);border-color:var(--blogi-accent,#84CB3A);}"
                 << ".wizard-nav{display:flex;justify-content:space-between;margin-top:16px;gap:12px;}"
                 << ".wizard-btn{padding:10px 24px;border-radius:999px;font-weight:700;cursor:pointer;border:none;transition:.2s;}"
                 << ".btn-prev{background:var(--blogi-border,#eaeaea);color:var(--blogi-text,#1a1a1a);}"
                 << ".btn-next{background:var(--blogi-accent,#84CB3A);color:#fff;}"
                 << ".btn-submit{background:linear-gradient(135deg,var(--blogi-accent,#84CB3A),#F36947);color:#fff;}"
                 << ".btn-next{background:var(--blogi-accent,#84CB3A);color:var(--blogi-surface,#fff);}"
                 << ".btn-submit{background:linear-gradient(135deg,var(--blogi-accent,#84CB3A),var(--blogi-error,#F36947));color:var(--blogi-surface,#fff);}"
                 << ".wizard-btn:hover{transform:translateY(-1px);box-shadow:0 4px 12px rgba(0,0,0,.1);}"
                 << ".step-error{color:#F36947;font-size:.9em;margin-top:8px;display:none;}"
                 << ".step-error{color:var(--blogi-error,#F36947);font-size:.9em;margin-top:8px;display:none;}"
                 << ".step-error.show{display:block;}"
                 << "#surveyMessage{padding:16px;border-radius:8px;margin-top:16px;display:none;}"
                 << "#surveyMessage.success{background:#e8f5e9;color:#2e7d32;display:block;}"
                 << "#surveyMessage.error{background:#fbe9e7;color:#c62828;display:block;}"
                 << ".file-upload-area{border:2px dashed #ccc;border-radius:10px;padding:30px;text-align:center;cursor:pointer;transition:.3s;background:#fefefe;}"
                 << ".file-upload-area:hover,.file-upload-area.dragover{border-color:var(--blogi-accent,#84CB3A);background:#f0f9e8;}"
                 << "#surveyMessage.success{background:var(--blogi-success-bg,#e8f5e9);color:var(--blogi-success-text,#2e7d32);display:block;}"
                 << "#surveyMessage.error{background:var(--blogi-error-bg,#fbe9e7);color:var(--blogi-error-text,#c62828);display:block;}"
                 << ".file-upload-area{border:2px dashed var(--blogi-border,#ccc);border-radius:10px;padding:30px;text-align:center;cursor:pointer;transition:.3s;background:var(--blogi-surface,#fefefe);}"
                 << ".file-upload-area:hover,.file-upload-area.dragover{border-color:var(--blogi-accent,#84CB3A);background:var(--blogi-bg-alt,#f0f9e8);}"
                 << ".file-upload-area input[type=file]{display:none;}"
                 << ".upload-icon{font-size:2em;margin-bottom:8px;}"
                 << ".upload-text{color:var(--blogi-text-muted,#6b6b6b);}"
                 << ".file-hint{margin-top:8px;font-size:.85em;color:var(--blogi-text-muted,#6b6b6b);}"
                 << ".image-list{margin-top:12px;display:grid;grid-template-columns:repeat(auto-fill,minmax(140px,1fr));gap:12px;}"
                 << ".image-item{border:1px solid #eaeaea;border-radius:8px;padding:8px;background:#fff;}"
                 << ".image-item{border:1px solid var(--blogi-border,#eaeaea);border-radius:8px;padding:8px;background:var(--blogi-surface,#fff);}"
                 << ".image-item img{width:100%;height:100px;object-fit:cover;border-radius:6px;}"
                 << ".image-item .image-name{font-size:.8em;margin-top:6px;color:#444;word-break:break-all;}"
                 << ".image-item .file-remove{margin-top:6px;width:100%;background:#F36947;color:#fff;border:none;padding:6px 10px;border-radius:999px;cursor:pointer;font-size:.85em;}"
                 << ".image-item .image-name{font-size:.8em;margin-top:6px;color:var(--blogi-text-muted,#444);word-break:break-all;}"
                 << ".image-item .file-remove{margin-top:6px;width:100%;background:var(--blogi-error,#F36947);color:var(--blogi-surface,#fff);border:none;padding:6px 10px;border-radius:999px;cursor:pointer;font-size:.85em;}"
                 << "</style>";

            // Inline JS
            html << "<script>"
                 << "var currentStep=0,totalSteps=" << totalSteps << ";"
                 << "var imageData={},imageIdSeq=0;"
                 // exclusion logic
                 << "function getExcludedQuestions(){"
                 << "var excluded={};"
                 << "var checked=document.querySelectorAll('#surveyForm input[type=radio]:checked,#surveyForm input[type=checkbox]:checked');"
                 << "for(var i=0;i<checked.length;i++){var exc=checked[i].getAttribute('data-excludes');"
                 << "if(exc){var ids=exc.split(',');for(var j=0;j<ids.length;j++){var id=ids[j].trim();if(id)excluded[id]=true;}}}"
                 << "var sels=document.querySelectorAll('#surveyForm select');"
                 << "for(var i=0;i<sels.length;i++){var s=sels[i];if(!s.value)continue;"
                 << "var o=s.querySelector('option[value=\"'+s.value+'\"]');"
                 << "if(o){var exc=o.getAttribute('data-excludes');"
                 << "if(exc){var ids=exc.split(',');for(var j=0;j<ids.length;j++){var id=ids[j].trim();if(id)excluded[id]=true;}}}}"
                 << "return excluded;}"
                 << "function isStepExcluded(n){"
                 << "var step=document.querySelectorAll('.wizard-step')[n];"
                 << "if(!step)return false;"
                 << "var qid=step.getAttribute('data-question-id');"
                 << "if(!qid)return false;"
                 << "return !!getExcludedQuestions()[qid];}"
                 << "function nextVisibleStep(from){"
                 << "for(var i=from+1;i<totalSteps;i++){if(!isStepExcluded(i))return i;}return -1;}"
                 << "function prevVisibleStep(from){"
                 << "for(var i=from-1;i>=0;i--){if(!isStepExcluded(i))return i;}return -1;}"
                 << "function updateStepNav(n){"
                 << "var step=document.querySelectorAll('.wizard-step')[n];"
                 << "if(!step)return;var nav=step.querySelector('.wizard-nav');if(!nav)return;"
                 << "var nxt=nextVisibleStep(n);"
                 << "var btnNext=nav.querySelector('.btn-next'),btnSubmit=nav.querySelector('.btn-submit');"
                 << "if(nxt===-1){if(btnNext){btnNext.classList.remove('btn-next');btnNext.classList.add('btn-submit');btnNext.textContent='" << tr(_lang, "Submit") << "';}}"
                 << "else{if(btnSubmit&&!nav.querySelector('.btn-next')){btnSubmit.classList.remove('btn-submit');btnSubmit.classList.add('btn-next');btnSubmit.textContent='" << tr(_lang, "Next") << "';}}}"
                 // showStep
                 << "function showStep(n){"
                 << "var s=document.querySelectorAll('.wizard-step'),d=document.querySelectorAll('.step-dot');"
                 << "for(var i=0;i<s.length;i++){s[i].classList.remove('active');d[i].classList.remove('active');"
                 << "if(i<n)d[i].classList.add('done');else d[i].classList.remove('done');}"
                 << "s[n].classList.add('active');d[n].classList.add('active');currentStep=n;}"
                 << "s[n].classList.add('active');d[n].classList.add('active');currentStep=n;updateStepNav(n);}"
                 // validateStep
                 << "function validateStep(n){"
                 << "var step=document.querySelectorAll('.wizard-step')[n];"
@@ -1247,8 +1326,8 @@ namespace blogi {
                 << "var list=imageData[fileInput.name];if(!list||list.length===0){err.classList.add('show');return false;}}"
                 << "err.classList.remove('show');return true;}"
                 // wizard nav
                 << "function wizardNext(){if(!validateStep(currentStep))return;if(currentStep<totalSteps-1)showStep(currentStep+1);}"
                 << "function wizardPrev(){if(currentStep>0)showStep(currentStep-1);}"
                 << "function wizardNext(){if(!validateStep(currentStep))return;var nxt=nextVisibleStep(currentStep);if(nxt!==-1)showStep(nxt);}"
                 << "function wizardPrev(){var prv=prevVisibleStep(currentStep);if(prv!==-1)showStep(prv);}"
                 // image upload helpers
                 << "function ensureImageList(name){if(!imageData[name])imageData[name]=[];return imageData[name];}"
                 << "function renderImageList(name){"
@@ -1304,6 +1383,10 @@ namespace blogi {
                 << "for(var key in imageData){if(!imageData.hasOwnProperty(key))continue;"
                 << "var list=imageData[key];if(list&&list.length>0){"
                 << "data[key]=[];for(var i=0;i<list.length;i++)data[key].push(list[i].data);}}"
                 // add excluded question IDs
                 << "var excl=getExcludedQuestions(),exclArr=[];"
                 << "for(var k in excl){if(excl.hasOwnProperty(k))exclArr.push(k);}"
                 << "if(exclArr.length>0)data['_excluded']=exclArr;"
                 << "var xhr=new XMLHttpRequest();"
                 << "xhr.open('POST','" << jsonEscape(submitUrl) << "',true);"
                 << "xhr.setRequestHeader('Content-Type','application/json');"