Files
sm-user-plugin/models/organisation.go
Jakub Zych f20a8d3b98 feat: organisation logo (attachOne logo) and payload helpers
- Organisation gains the public attachOne logo (OrganisationLogoField),
  editable as an image upload on the Organisations form; avatar unchanged
- OrganisationLogoURL and OrganisationLogoFields give applications the
  has_logo, logo and logo_url keys for their organisation payloads
- no migration: attachments live in system_files
2026-10-23 17:03:13 +02:00

74 lines
2.6 KiB
Go

package models
import (
"time"
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
"gorm.io/gorm"
)
// Organisation is a row of golem15_user_organisations, the PHP Organisation
// model: a named group of users (users.organisation_id) that other plugins
// hang organisation-level data on. The table is shared with those plugins
// and gets no column from the admin screen.
type Organisation struct {
ID uint `gorm:"column:id;primaryKey"`
Slug string `gorm:"column:slug"`
Name string `gorm:"column:name"`
Description *string `gorm:"column:description"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
// Members is the PHP hasMany members relation: the users whose
// organisation_id is this organisation. It is not a column. It is loaded
// only on request, written only by the admin members manager (which sets
// and clears users.organisation_id), and never serialized.
Members []User `gorm:"foreignKey:OrganisationID;references:ID" json:"-"`
}
func (Organisation) TableName() string { return "golem15_user_organisations" }
// MorphName is the PHP class string stored in system_files.attachment_type.
func (Organisation) MorphName() string { return `Golem15\User\Models\Organisation` }
// OrganisationAvatarField is the attachOne relation name of the
// organisation's picture.
const OrganisationAvatarField = "avatar"
// OrganisationLogoField is the attachOne relation name of the
// organisation's logo, the image a site shows wherever it displays the
// organisation.
const OrganisationLogoField = "logo"
// AttachRelations declares the public attachOne avatar (PHP $attachOne) and
// the public attachOne logo.
func (Organisation) AttachRelations() []attach.Relation {
return []attach.Relation{{Name: OrganisationAvatarField, Public: true}, {Name: OrganisationLogoField, Public: true}}
}
// Fillable is the PHP $fillable list.
func (Organisation) Fillable() []string {
return []string{"name", "slug", "description"}
}
// Rules is the PHP rule set without alpha_dash on slug, which the rule
// engine does not know: the admin controller checks the slug's characters.
func (Organisation) Rules() map[string]string {
return map[string]string{
"name": "required|max:255",
"slug": "required|unique:golem15_user_organisations",
"description": "nullable|max:5000",
}
}
// BeforeValidate fills an empty slug from the name, the PHP beforeValidate.
func (o *Organisation) BeforeValidate(_ *gorm.DB) error {
if o.Slug == "" && o.Name != "" {
o.Slug = Slugify(o.Name)
}
return nil
}
func init() {
Register(Organisation{})
}