- add a content textarea (size huge) after description, with en and pl phrases - add content to Category.Fillable - export classes.CheckHTML and run it from FormBeforeCreate and FormBeforeUpdate: unsafe category content answers 422 on content, blank content is stored as NULL
91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.golem15.com/golem15/sm-journal-plugin/classes"
|
|
"git.golem15.com/golem15/sm-journal-plugin/models"
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
|
"git.golem15.com/golem15/summercms/modules/pact"
|
|
)
|
|
|
|
const (
|
|
// CategoriesControllerID is the cabana controller id of the Categories screen.
|
|
CategoriesControllerID = "golem15.journal.categories"
|
|
// PermissionAccessCategories is Categories::$requiredPermissions.
|
|
PermissionAccessCategories = "golem15.journal.access_categories"
|
|
)
|
|
|
|
type categoriesAdminController struct {
|
|
app func() *backpack.App
|
|
}
|
|
|
|
func (categoriesAdminController) ID() string { return CategoriesControllerID }
|
|
|
|
func (categoriesAdminController) ModelName() string { return `Golem15\Journal\Models\Category` }
|
|
|
|
func (categoriesAdminController) ConfigDir() string { return "controllers/categories" }
|
|
|
|
func (categoriesAdminController) RequiredPermissions() []string {
|
|
return []string{PermissionAccessCategories}
|
|
}
|
|
|
|
func (categoriesAdminController) NewRecord() any { return &models.Category{} }
|
|
|
|
func (categoriesAdminController) AdminFieldRelations() []cabana.FieldRelationContract {
|
|
return []cabana.FieldRelationContract{{
|
|
Field: "parent",
|
|
Kind: "belongsTo",
|
|
NewRelated: func() any { return &models.Category{} },
|
|
ForeignKey: "parent_id",
|
|
LabelColumn: "name",
|
|
}}
|
|
}
|
|
|
|
func (categoriesAdminController) FormBeforeCreate(ctx context.Context, model any) error {
|
|
cat, ok := model.(*models.Category)
|
|
if !ok || cat == nil {
|
|
return errors.New("categories admin: form model is not a category")
|
|
}
|
|
return prepareCategoryWrite(cat)
|
|
}
|
|
|
|
func (categoriesAdminController) FormBeforeUpdate(ctx context.Context, model any) error {
|
|
cat, ok := model.(*models.Category)
|
|
if !ok || cat == nil {
|
|
return errors.New("categories admin: form model is not a category")
|
|
}
|
|
return prepareCategoryWrite(cat)
|
|
}
|
|
|
|
// prepareCategoryWrite gates the category HTML body (D-21): blank content is
|
|
// stored as NULL, and content that fails classes.CheckHTML is a 422 on
|
|
// content. Any other content is stored exactly as submitted.
|
|
func prepareCategoryWrite(cat *models.Category) error {
|
|
if cat.Content == nil {
|
|
return nil
|
|
}
|
|
if strings.TrimSpace(*cat.Content) == "" {
|
|
cat.Content = nil
|
|
return nil
|
|
}
|
|
if err := classes.CheckHTML(*cat.Content); err != nil {
|
|
return &cabana.ValidationError{Details: map[string]any{
|
|
"content": []string{err.Error()},
|
|
}}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
_ pact.AdminController = categoriesAdminController{}
|
|
_ pact.AdminPermissioned = categoriesAdminController{}
|
|
_ pact.AdminRecordSource = categoriesAdminController{}
|
|
_ pact.FormBeforeCreate = categoriesAdminController{}
|
|
_ pact.FormBeforeUpdate = categoriesAdminController{}
|
|
_ cabana.FieldRelationProvider = categoriesAdminController{}
|
|
)
|