Files
sm-faq-plugin/models/faq.go
Jakub Zych 1c5491232a 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
2026-10-12 17:24:52 +02:00

58 lines
2.0 KiB
Go

package models
import (
"time"
translate "git.golem15.com/golem15/sm-translate-plugin/models"
"git.golem15.com/golem15/summercms/modules/lagoon"
)
// Content is the stored question and answer list: the PHP $jsonable content,
// one {question, answer} object per row.
type Content = []map[string]any
// FAQ is one FAQ in one locale, stored on golem15_faq_faqs: the PHP
// Golem15\FAQ\Models\FAQ. A site keeps one row per locale under the same
// slug, told apart by locale_id (the translate Locale).
type FAQ struct {
ID uint `gorm:"column:id;primaryKey"`
Name string `gorm:"column:name"`
Slug string `gorm:"column:slug"`
LocaleID uint `gorm:"column:locale_id"`
Locale *translate.Locale `gorm:"foreignKey:LocaleID" json:"-"`
IsActive bool `gorm:"column:is_active"`
Description *string `gorm:"column:description"`
Content lagoon.Jsonable[Content] `gorm:"column:content"`
CreatedAt *time.Time `gorm:"column:created_at"`
UpdatedAt *time.Time `gorm:"column:updated_at"`
}
func (FAQ) TableName() string { return "golem15_faq_faqs" }
// MorphName is the PHP class string of the model.
func (FAQ) MorphName() string { return `Golem15\FAQ\Models\FAQ` }
// Fillable is the list an admin save may set. The locale is set through the
// locale relation field, which writes locale_id.
func (FAQ) Fillable() []string {
return []string{"name", "slug", "locale_id", "is_active", "description", "content"}
}
// Rules require a name and a slug. The locale is required too, but the admin
// sets locale_id from the locale relation field after validation, so the
// controller checks it once the row is written (a missing locale rolls the
// save back).
func (FAQ) Rules() map[string]string {
return map[string]string{
"name": "required",
"slug": "required",
}
}
// Items returns the stored rows.
func (f FAQ) Items() Content { return f.Content.Get() }
func init() {
Register(FAQ{})
}