feat(17-04): FAQ plugin tracer, admin FAQ with sanitized question repeater

- golem15.faq plugin (Requires golem15.translate), table golem15_faq_faqs with the PHP columns
- FAQ model with jsonable content and the translate Locale relation
- admin controller golem15.faq.faqs: name, slug, locale, active switch, richeditor description, question repeater
- navigation FAQ (order 500), permission golem15.faq.faqs, en and pl phrasebooks
- TestFaqAdminSmoke: answers stored sanitized with a notice under content.1.answer
This commit is contained in:
2026-10-12 17:24:52 +02:00
commit 1c5491232a
22 changed files with 1370 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
// Package controllers holds the FAQ plugin's admin controllers.
package controllers
import (
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/pact"
)
// AdminControllers is the plugin's admin controller list. app returns the
// booted application; it is asked on every call, so a controller never keeps
// a database handle of its own.
func AdminControllers(app func() *backpack.App) []pact.AdminController {
return []pact.AdminController{
faqsAdminController{app: app},
}
}

79
controllers/faqs.go Normal file
View File

@@ -0,0 +1,79 @@
package controllers
import (
"context"
"git.golem15.com/golem15/sm-faq-plugin/models"
translate "git.golem15.com/golem15/sm-translate-plugin/models"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/pact"
)
const (
// FAQsControllerID is the cabana controller id of the FAQs screen.
FAQsControllerID = "golem15.faq.faqs"
// PermissionFAQs is the PHP permission golem15.faq.faqs.
PermissionFAQs = "golem15.faq.faqs"
)
// faqsAdminController lists and edits FAQs: a name, a slug, the locale, the
// active switch, a rich text description and the question repeater.
type faqsAdminController struct {
app func() *backpack.App
}
func (faqsAdminController) ID() string { return FAQsControllerID }
func (faqsAdminController) ModelName() string { return `Golem15\FAQ\Models\FAQ` }
func (faqsAdminController) ConfigDir() string { return "controllers/faqs" }
func (faqsAdminController) RequiredPermissions() []string {
return []string{PermissionFAQs}
}
func (faqsAdminController) NewRecord() any { return &models.FAQ{} }
// AdminFieldRelations binds the locale field to the translate Locale through
// locale_id (PHP belongsTo locale).
func (faqsAdminController) AdminFieldRelations() []cabana.FieldRelationContract {
return []cabana.FieldRelationContract{{
Field: "locale",
Kind: "belongsTo",
NewRelated: func() any { return &translate.Locale{} },
ForeignKey: "locale_id",
}}
}
// errLocaleRequired is the field message of a FAQ saved without a locale.
const errLocaleRequired = "golem15.faq::lang.errors.locale_required"
// FormAfterCreate refuses a FAQ without a locale. cabana writes locale_id
// from the relation field after the model rules ran, so the check runs here,
// inside the save's transaction: the refusal rolls the row back.
func (faqsAdminController) FormAfterCreate(_ context.Context, model any) error {
return requireLocale(model)
}
// FormAfterUpdate applies the same check to an update.
func (faqsAdminController) FormAfterUpdate(_ context.Context, model any) error {
return requireLocale(model)
}
func requireLocale(model any) error {
faq, ok := model.(*models.FAQ)
if !ok || faq == nil || faq.LocaleID != 0 {
return nil
}
return &cabana.ValidationError{Details: map[string]any{"locale": []string{errLocaleRequired}}}
}
var (
_ pact.FormAfterCreate = faqsAdminController{}
_ pact.FormAfterUpdate = faqsAdminController{}
_ pact.AdminController = faqsAdminController{}
_ pact.AdminPermissioned = faqsAdminController{}
_ pact.AdminRecordSource = faqsAdminController{}
_ cabana.FieldRelationProvider = faqsAdminController{}
)

View File

@@ -0,0 +1,25 @@
# ===================================
# Form Behavior Config
# ===================================
# Record name
name: golem15.faq::lang.models.faq.label
# Model Form Field configuration
form: ~/plugins/golem15/faq/models/faq/fields.yaml
# Model Class name
modelClass: Golem15\FAQ\Models\FAQ
# Default redirect location
defaultRedirect: golem15/faq/faqs
# Create page
create:
redirect: golem15/faq/faqs/update/:id
redirectClose: golem15/faq/faqs
# Update page
update:
redirect: golem15/faq/faqs
redirectClose: golem15/faq/faqs

View File

@@ -0,0 +1,39 @@
# ===================================
# List Behavior Config
# ===================================
# Model List Column configuration
list: ~/plugins/golem15/faq/models/faq/columns.yaml
# Model Class name
modelClass: Golem15\FAQ\Models\FAQ
# List Title
title: golem15.faq::lang.models.faq.label_plural
# Link URL for each record
recordUrl: golem15/faq/faqs/update/:id
# Message to display if the list is empty
noRecordsMessage: backend::lang.list.no_records
# Records to display per page
recordsPerPage: 20
# Options to provide the user when selecting how many records to display per page
perPageOptions: [20, 40, 80, 100, 120]
# Displays the list column set up button
showSetup: true
# Displays the sorting link on each column
showSorting: true
# Display checkboxes next to each record
showCheckboxes: true
# Toolbar: the PHP list_toolbar partial held create and delete selected
toolbar:
buttons: [create, delete]
search:
prompt: backend::lang.list.search_prompt