Skip to content

Commit

Permalink
fix: conditional webform with list view enabled
Browse files Browse the repository at this point in the history
(cherry picked from commit fb9c111)
  • Loading branch information
shariquerik authored and mergify[bot] committed Oct 10, 2023
1 parent 55c0602 commit 45be3d3
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 27 deletions.
6 changes: 6 additions & 0 deletions frappe/public/js/frappe/web_form/web_form_list.js
Expand Up @@ -109,6 +109,12 @@ export default class WebFormList {
}

fetch_data() {
if (this.condition_json && JSON.parse(this.condition_json)) {
let filter = frappe.utils.get_filter_from_json(this.condition_json);
filter = frappe.utils.get_filter_as_json(filter);
this.filters = Object.assign(this.filters, JSON.parse(filter));
}

let args = {
method: "frappe.www.list.get_list_data",
args: {
Expand Down
1 change: 1 addition & 0 deletions frappe/public/js/frappe/web_form/webform_script.js
Expand Up @@ -27,6 +27,7 @@ frappe.ready(function () {
doctype: web_form_doc.doc_type,
web_form_name: web_form_doc.name,
list_columns: web_form_doc.list_columns,
condition_json: web_form_doc.condition_json,
settings: {
allow_delete: web_form_doc.allow_delete,
},
Expand Down
105 changes: 105 additions & 0 deletions frappe/website/doctype/web_form/web_form.js
Expand Up @@ -36,6 +36,7 @@ frappe.ui.form.on("Web Form", {
frm.trigger("set_fields");
frm.trigger("add_get_fields_button");
frm.trigger("add_publish_button");
frm.trigger("render_condition_table");
},

login_required: function (frm) {
Expand Down Expand Up @@ -170,6 +171,110 @@ frappe.ui.form.on("Web Form", {
allow_multiple: function (frm) {
frm.doc.allow_multiple && frm.set_value("show_list", 1);
},

before_save: function (frm) {
let static_filters = JSON.parse(frm.doc.condition_json || "[]");
frm.set_value("condition_json", JSON.stringify(static_filters));
frm.trigger("render_condition_table");
},

render_condition_table: function (frm) {
// frm.set_df_property("filters_section", "hidden", 0);
let is_document_type = true;

let wrapper = $(frm.get_field("condition_json").wrapper).empty();
let table = $(`<table class="table table-bordered" style="cursor:pointer; margin:0px;">
<thead>
<tr>
<th style="width: 20%">${__("Filter")}</th>
<th style="width: 20%">${__("Condition")}</th>
<th>${__("Value")}</th>
</tr>
</thead>
<tbody></tbody>
</table>`).appendTo(wrapper);
$(`<p class="text-muted small">${__("Click table to edit")}</p>`).appendTo(wrapper);

let filters = JSON.parse(frm.doc.condition_json || "[]");
let filters_set = false;

let fields = [];
if (is_document_type) {
fields = [
{
fieldtype: "HTML",
fieldname: "filter_area",
},
];

if (filters?.length) {
filters.forEach((filter) => {
const filter_row = $(`<tr>
<td>${filter[1]}</td>
<td>${filter[2] || ""}</td>
<td>${filter[3]}</td>
</tr>`);

table.find("tbody").append(filter_row);
});
filters_set = true;
}
} else if (frm.filters.length) {
fields = frm.filters.filter((f) => f.fieldname);
fields.map((f) => {
if (filters[f.fieldname]) {
let condition = "=";
const filter_row = $(`<tr>
<td>${f.label}</td>
<td>${condition}</td>
<td>${filters[f.fieldname] || ""}</td>
</tr>`);
table.find("tbody").append(filter_row);
if (!filters_set) filters_set = true;
}
});
}

if (!filters_set) {
const filter_row = $(`<tr><td colspan="3" class="text-muted text-center">
${__("Click to Set Filters")}</td></tr>`);
table.find("tbody").append(filter_row);
}

table.on("click", () => {
let dialog = new frappe.ui.Dialog({
title: __("Set Filters"),
fields: fields,
primary_action: function () {
let values = this.get_values();
if (values) {
this.hide();
if (is_document_type) {
let filters = frm.filter_group.get_filters();
frm.set_value("condition_json", JSON.stringify(filters));
} else {
frm.set_value("condition_json", JSON.stringify(values));
}
frm.trigger("render_condition_table");
}
},
primary_action_label: "Set",
});

if (is_document_type) {
frm.filter_group = new frappe.ui.FilterGroup({
parent: dialog.get_field("filter_area").$wrapper,
doctype: frm.doc.doc_type,
on_change: () => {},
});
filters && frm.filter_group.add_filters_to_filter_group(filters);
}

dialog.show();

dialog.set_values(filters);
});
},
});

frappe.ui.form.on("Web Form List Column", {
Expand Down
28 changes: 11 additions & 17 deletions frappe/website/doctype/web_form/web_form.json
Expand Up @@ -31,9 +31,8 @@
"allow_incomplete",
"section_break_2",
"max_attachment_size",
"section_break_xzqr",
"condition",
"column_break_tjgl",
"condition_section",
"condition_json",
"condition_description",
"section_break_3",
"list_setting_message",
Expand Down Expand Up @@ -375,31 +374,26 @@
"label": "Anonymous"
},
{
"fieldname": "condition",
"fieldtype": "Code",
"label": "Condition",
"max_height": "150px"
"fieldname": "condition_description",
"fieldtype": "HTML",
"label": "Condition Description",
"options": "<p>Multiple webforms can be created for a single doctype. Write a condition specific to this webform to display correct record after submission.</p><p>For Example:</p>\n<p>If you create a separate webform every year to capture feedback from employees add a \n field named year in doctype and add a condition <b>doc.year==\"2023\"</b></p>\n"
},
{
"fieldname": "section_break_xzqr",
"fieldname": "condition_section",
"fieldtype": "Section Break"
},
{
"fieldname": "column_break_tjgl",
"fieldtype": "Column Break"
},
{
"fieldname": "condition_description",
"fieldtype": "HTML",
"label": "Condition Description",
"options": "<p>Multiple webforms can be created for a single doctype. Write a condition specific to this webform to display correct record after submission.</p><p>For Example:</p>\n<p>If you create a separate webform every year to capture feedback from employees add a \n field named year in doctype and add a condition <b>doc.year==\"2023\"</b></p>\n"
"fieldname": "condition_json",
"fieldtype": "JSON",
"label": "Condition JSON"
}
],
"has_web_view": 1,
"icon": "icon-edit",
"is_published_field": "published",
"links": [],
"modified": "2023-06-03 19:18:56.760479",
"modified": "2023-10-10 11:31:56.609386",
"modified_by": "Administrator",
"module": "Website",
"name": "Web Form",
Expand Down
16 changes: 6 additions & 10 deletions frappe/website/doctype/web_form/web_form.py
Expand Up @@ -153,16 +153,12 @@ def get_context(self, context):
and not frappe.form_dict.name
and not frappe.form_dict.is_list
):
names = frappe.db.get_values(self.doc_type, {"owner": frappe.session.user}, pluck="name")
for name in names:
if self.condition:
doc = frappe.get_doc(self.doc_type, name)
if frappe.safe_eval(self.condition, None, {"doc": doc.as_dict()}):
context.in_view_mode = True
frappe.redirect(f"/{self.route}/{name}")
else:
context.in_view_mode = True
frappe.redirect(f"/{self.route}/{name}")
condition_json = json.loads(self.condition_json) if self.condition_json else []
condition_json.append(["owner", "=", frappe.session.user])
names = frappe.get_all(self.doc_type, filters=condition_json, pluck="name")
if names:
context.in_view_mode = True
frappe.redirect(f"/{self.route}/{names[0]}")

# Show new form when
# - User is Guest
Expand Down

0 comments on commit 45be3d3

Please sign in to comment.