Merge pull request #22679 from shariquerik/conditional-webform-list-view

This commit is contained in:
Shariq Ansari 2023-10-10 15:47:27 +05:30 committed by GitHub
commit 009d9e010b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 135 additions and 30 deletions

View file

@ -36,7 +36,7 @@ frappe.ui.FilterGroup = class {
}
hide_popover() {
this.filter_button.popover("hide");
this.filter_button?.popover("hide");
}
init_filter_popover() {

View file

@ -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: {

View file

@ -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,
},

View file

@ -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) {
@ -174,6 +175,113 @@ 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) {
let wrapper = $(frm.get_field("condition_json").wrapper).empty();
let table = $(`
<style>
.table-bordered th, .table-bordered td {
border: none;
border-right: 1px solid var(--border-color);
}
.table-bordered td {
border-top: 1px solid var(--border-color);
}
.table thead th {
border-bottom: none;
font-weight: var(--weight-regular);
}
tr th:last-child, tr td:last-child{
border-right: none;
}
thead {
font-size: var(--text-sm);
color: var(--gray-600);
background-color: var(--subtle-fg);
}
thead th:first-child {
border-top-left-radius: 9px;
}
thead th:last-child {
border-top-right-radius: 9px;
}
</style>
<table class="table table-bordered" style="cursor:pointer; margin:0px; border-radius: 10px; border-spacing: 0; border-collapse: separate;">
<thead>
<tr>
<th>${__("Filter")}</th>
<th style="width: 20%">${__("Condition")}</th>
<th>${__("Value")}</th>
</tr>
</thead>
<tbody></tbody>
</table>`).appendTo(wrapper);
$(`<p class="text-muted small mt-2">${__("Click table to edit")}</p>`).appendTo(wrapper);
let filters = JSON.parse(frm.doc.condition_json || "[]");
let filters_set = false;
let 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;
}
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();
let filters = frm.filter_group.get_filters();
frm.set_value("condition_json", JSON.stringify(filters));
frm.trigger("render_condition_table");
}
},
primary_action_label: "Set",
});
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", {

View file

@ -31,10 +31,9 @@
"allow_incomplete",
"section_break_2",
"max_attachment_size",
"section_break_xzqr",
"condition",
"column_break_tjgl",
"condition_section",
"condition_description",
"condition_json",
"section_break_3",
"list_setting_message",
"show_list",
@ -374,32 +373,27 @@
"fieldtype": "Check",
"label": "Anonymous"
},
{
"fieldname": "condition",
"fieldtype": "Code",
"label": "Condition",
"max_height": "150px"
},
{
"fieldname": "section_break_xzqr",
"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"
"options": "<p>Multiple webforms can be created for a single doctype. Add filters 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 filter <b>year = 2023</b></p>\n"
},
{
"fieldname": "condition_section",
"fieldtype": "Section Break"
},
{
"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 12:51:18.341792",
"modified_by": "Administrator",
"module": "Website",
"name": "Web Form",

View file

@ -200,16 +200,12 @@ def get_context(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