diff --git a/church/church_foundations/doctype/church_belief_bible_references/church_belief_bible_references.json b/church/church_foundations/doctype/church_belief_bible_references/church_belief_bible_references.json index 51f157e..0e7dcd7 100644 --- a/church/church_foundations/doctype/church_belief_bible_references/church_belief_bible_references.json +++ b/church/church_foundations/doctype/church_belief_bible_references/church_belief_bible_references.json @@ -14,7 +14,7 @@ "fieldtype": "Link", "in_list_view": 1, "label": "Reference", - "options": "Church Bible Reference", + "options": "Church Bible Verse", "reqd": 1 } ], @@ -22,7 +22,7 @@ "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2025-10-08 23:42:32.368088", + "modified": "2025-10-27 23:01:09.260878", "modified_by": "Administrator", "module": "Church Foundations", "name": "Church Belief Bible References", diff --git a/church/church_study/doctype/church_bible_book/church_bible_book.json b/church/church_study/doctype/church_bible_book/church_bible_book.json index 44811d5..cc9c0ea 100644 --- a/church/church_study/doctype/church_bible_book/church_bible_book.json +++ b/church/church_study/doctype/church_bible_book/church_bible_book.json @@ -10,7 +10,8 @@ "field_order": [ "book", "abbreviation", - "number_of_chapters" + "number_of_chapters", + "usfm" ], "fields": [ { @@ -37,17 +38,23 @@ "fieldtype": "Int", "in_list_view": 1, "label": "Number of Chapters" + }, + { + "description": "Required for fetching references from Bible API", + "fieldname": "usfm", + "fieldtype": "Data", + "label": "Unified Standard Format Marker (USFM)" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, "links": [ { - "link_doctype": "Church Bible Reference", + "link_doctype": "Church Bible Verse", "link_fieldname": "book" } ], - "modified": "2025-10-08 23:25:55.315934", + "modified": "2025-10-28 00:52:53.324021", "modified_by": "Administrator", "module": "Church Study", "name": "Church Bible Book", diff --git a/church/church_study/doctype/church_bible_reference/church_bible_reference.js b/church/church_study/doctype/church_bible_reference/church_bible_reference.js index 6187166..29feaa7 100644 --- a/church/church_study/doctype/church_bible_reference/church_bible_reference.js +++ b/church/church_study/doctype/church_bible_reference/church_bible_reference.js @@ -1,8 +1,78 @@ -// Copyright (c) 2025, meichthys and contributors -// For license information, please see license.txt +frappe.ui.form.on("Church Bible Reference", { + import_reference_text: frm => { + fetch_bible_text(frm); + } +}); -// frappe.ui.form.on("Church Bible Reference", { -// refresh(frm) { +async function fetch_bible_text(frm) { + try { + const start_verse = await get_verse_reference(frm.doc.start_verse); + const end_verse = frm.doc.end_verse ? await get_verse_reference(frm.doc.end_verse) : null; + const translation = await get_translation(frm.doc.translation); -// }, -// }); + if (!start_verse || !translation) { + frappe.msgprint("Missing start verse or translation information."); + return; + } + + // Build passage string + let passage = `${start_verse.book} ${start_verse.chapter}:${start_verse.verse}`; + if (end_verse) { + passage += `-${end_verse.chapter}:${end_verse.verse}`; + } + + // Check if translation is supported + const translationsData = await fetch("https://bible-api.com/data") + .then(res => { + if (!res.ok) throw new Error(`Failed to fetch translation data (${res.status})`); + return res.json(); + }); + const supportedTranslations = (translationsData.translations || []).map(t => t.identifier || t.id); + const version = (translation.abbreviation || "").toLowerCase(); + + if (!supportedTranslations.includes(version)) { + frappe.msgprint(`The “${translation.abbreviation}” translation is not supported by bible-api.com. + It is likely that the version you are trying to use is copyrighted and therefore illegal to + use freely. We recommend using translations that are not bound by copyright because God's word + should not be for sale! (see + https://copy.church/initiatives/bibles/ for more details.)`, "Unsupported Translation"); + return; + } + + + const url = `https://bible-api.com/${encodeURIComponent(passage)}?translation=${version}`; + + // Fetch passage from https://bible-api.com/ + const response = await fetch(url); + if (!response.ok) throw new Error(`Failed to fetch passage (${response.status})`); + const data = await response.json(); + + frm.set_value("reference_text", data.text.trim()); + frappe.show_alert({ message: "Bible passage fetched!", indicator: "green" }, 3); + + } catch (err) { + console.error(err); + frappe.msgprint("Error fetching passage. Please ensure you have valid verse references."); + } +} + +// Fetch a Bible verse from DB +function get_verse_reference(name) { + if (!name) return Promise.resolve(null); + return frappe.db.get_doc("Church Bible Verse", name) + .then(doc => ({ book: doc.book, chapter: doc.chapter, verse: doc.verse })) + .catch(() => { + console.warn("Verse not found:", name); + return null; + }); +} + +// Fetch translation from DB +function get_translation(name) { + if (!name) return Promise.resolve(null); + return frappe.db.get_doc("Church Bible Translation", name) + .catch(() => { + console.warn("Translation not found:", name); + return null; + }); +} diff --git a/church/church_study/doctype/church_bible_reference/church_bible_reference.json b/church/church_study/doctype/church_bible_reference/church_bible_reference.json index fbd7b2c..2ff0578 100644 --- a/church/church_study/doctype/church_bible_reference/church_bible_reference.json +++ b/church/church_study/doctype/church_bible_reference/church_bible_reference.json @@ -1,54 +1,46 @@ { "actions": [], "allow_rename": 1, - "autoname": "format:{book} {chapter}:{verse} ({translation})", - "creation": "2025-09-17 21:42:32.438305", - "description": "A Bible reference (i.e John 3:16)", + "creation": "2025-10-27 22:53:12.954122", + "description": "A Bible reverence to a single verse or range of verses along with optional Translation and verse text. i.e. Matthew 10:18 (BSB)", "doctype": "DocType", - "editable_grid": 1, "engine": "InnoDB", "field_order": [ - "book", - "chapter", - "verse", - "translation" + "start_verse", + "end_verse", + "column_break_fdwn", + "translation", + "import_reference_text", + "reference_text_section", + "reference_text" ], "fields": [ { - "allow_in_quick_entry": 1, - "fieldname": "book", + "fieldname": "start_verse", "fieldtype": "Link", "in_filter": 1, "in_list_view": 1, "in_preview": 1, "in_standard_filter": 1, - "label": "Book", - "options": "Church Bible Book", - "reqd": 1, - "search_index": 1 + "label": "Start Verse", + "options": "Church Bible Verse", + "reqd": 1 }, { - "allow_in_quick_entry": 1, - "fieldname": "chapter", - "fieldtype": "Int", + "fieldname": "end_verse", + "fieldtype": "Link", "in_filter": 1, "in_list_view": 1, "in_preview": 1, "in_standard_filter": 1, - "label": "Chapter" + "label": "End Verse", + "options": "Church Bible Verse" }, { - "allow_in_quick_entry": 1, - "fieldname": "verse", - "fieldtype": "Int", - "in_filter": 1, - "in_list_view": 1, - "in_preview": 1, - "in_standard_filter": 1, - "label": "Verse" + "fieldname": "column_break_fdwn", + "fieldtype": "Column Break" }, { - "allow_in_quick_entry": 1, "fieldname": "translation", "fieldtype": "Link", "in_filter": 1, @@ -57,16 +49,31 @@ "in_standard_filter": 1, "label": "Translation", "options": "Church Bible Translation" + }, + { + "fieldname": "import_reference_text", + "fieldtype": "Button", + "label": "Import Reference Text" + }, + { + "fieldname": "reference_text_section", + "fieldtype": "Section Break" + }, + { + "fieldname": "reference_text", + "fieldtype": "Small Text", + "in_list_view": 1, + "in_preview": 1, + "label": "Reference Text" } ], "grid_page_length": 50, "index_web_pages_for_search": 1, "links": [], - "modified": "2025-10-08 23:32:28.440642", + "modified": "2025-10-28 00:38:57.907452", "modified_by": "Administrator", "module": "Church Study", "name": "Church Bible Reference", - "naming_rule": "Expression", "owner": "Administrator", "permissions": [ { @@ -85,6 +92,5 @@ "row_format": "Dynamic", "sort_field": "modified", "sort_order": "DESC", - "states": [], - "track_views": 1 + "states": [] } \ No newline at end of file diff --git a/church/church_study/doctype/church_bible_verse/__init__.py b/church/church_study/doctype/church_bible_verse/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/church/church_study/doctype/church_bible_verse/church_bible_verse.js b/church/church_study/doctype/church_bible_verse/church_bible_verse.js new file mode 100644 index 0000000..96b4189 --- /dev/null +++ b/church/church_study/doctype/church_bible_verse/church_bible_verse.js @@ -0,0 +1,8 @@ +// Copyright (c) 2025, meichthys and contributors +// For license information, please see license.txt + +// frappe.ui.form.on("Church Bible Verse", { +// refresh(frm) { + +// }, +// }); diff --git a/church/church_study/doctype/church_bible_verse/church_bible_verse.json b/church/church_study/doctype/church_bible_verse/church_bible_verse.json new file mode 100644 index 0000000..bfff3b6 --- /dev/null +++ b/church/church_study/doctype/church_bible_verse/church_bible_verse.json @@ -0,0 +1,78 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "format:{book} {chapter}:{verse}", + "creation": "2025-09-17 21:42:32.438305", + "description": "A Bible reference to a single verse (i.e John 3:16)", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "book", + "chapter", + "verse" + ], + "fields": [ + { + "allow_in_quick_entry": 1, + "fieldname": "book", + "fieldtype": "Link", + "in_filter": 1, + "in_list_view": 1, + "in_preview": 1, + "in_standard_filter": 1, + "label": "Book", + "options": "Church Bible Book", + "reqd": 1, + "search_index": 1 + }, + { + "allow_in_quick_entry": 1, + "fieldname": "chapter", + "fieldtype": "Int", + "in_filter": 1, + "in_list_view": 1, + "in_preview": 1, + "in_standard_filter": 1, + "label": "Chapter" + }, + { + "allow_in_quick_entry": 1, + "fieldname": "verse", + "fieldtype": "Int", + "in_filter": 1, + "in_list_view": 1, + "in_preview": 1, + "in_standard_filter": 1, + "label": "Verse" + } + ], + "grid_page_length": 50, + "index_web_pages_for_search": 1, + "links": [], + "modified": "2025-10-28 00:18:06.577326", + "modified_by": "Administrator", + "module": "Church Study", + "name": "Church Bible Verse", + "naming_rule": "Expression", + "owner": "Administrator", + "permissions": [ + { + "create": 1, + "delete": 1, + "email": 1, + "export": 1, + "print": 1, + "read": 1, + "report": 1, + "role": "System Manager", + "share": 1, + "write": 1 + } + ], + "row_format": "Dynamic", + "sort_field": "modified", + "sort_order": "DESC", + "states": [], + "track_views": 1 +} \ No newline at end of file diff --git a/church/church_study/doctype/church_bible_verse/church_bible_verse.py b/church/church_study/doctype/church_bible_verse/church_bible_verse.py new file mode 100644 index 0000000..509f58e --- /dev/null +++ b/church/church_study/doctype/church_bible_verse/church_bible_verse.py @@ -0,0 +1,9 @@ +# Copyright (c) 2025, meichthys and contributors +# For license information, please see license.txt + +# import frappe +from frappe.model.document import Document + + +class ChurchBibleVerse(Document): + pass diff --git a/church/church_study/doctype/church_bible_verse/test_church_bible_verse.py b/church/church_study/doctype/church_bible_verse/test_church_bible_verse.py new file mode 100644 index 0000000..938c49c --- /dev/null +++ b/church/church_study/doctype/church_bible_verse/test_church_bible_verse.py @@ -0,0 +1,9 @@ +# Copyright (c) 2025, meichthys and Contributors +# See license.txt + +# import frappe +from frappe.tests.utils import FrappeTestCase + + +class TestChurchBibleVerse(FrappeTestCase): + pass