CoolCMS API

CustomAsset extends BaseEntity
in package
implements IdentifiableInterface uses IdentifiableTrait, PageCollectionTrait

AbstractYes

Custom code: a library of code snippets inserted into website pages (`cms_system_custom_assets`).

Lets an administrator without server file access add a style or a measurement script. It did not fit into cms_system_settings (value is varchar(255)), hence an own table.

The code is a library: a page runs exactly the snippets assigned to it (association PageCollectionTrait::$pages), nothing more, nothing less. There is no site-wide mode: a snippet assigned to no page runs nowhere.

Columns decide what happens to the code:

  • type CustomAssetType: what the code is wrapped in (<style>, <script>, nothing)
  • placement CustomAssetPlacement: where in the page
  • cookieCategory: whether to wait for cookie consent; category key from config/Shared/cookieConsent.neon (CookieConsentConfig)
  • addToNewPages: a newly created page gets this snippet assigned automatically (hook PageManager)

IMPORTANT: the content is intentionally not sanitized. That is the whole point of the screen, and the author holds the privilege :Admin:System:CustomAsset:edit (assigning to pages additionally :Admin:System:CustomAsset:assign), granted separately (inserting JS is effectively full control of the page). The front end only wraps the code in a tag and prints it with |noescape; the only safeguard is removing the closing tag that would let the code break out of <style>/<script> (CustomAssets::wrap()).

NOTE: no @template on purpose: the concrete page class is determined by #[CollectionOf] in each branch separately. A generic parameter would force @extends/@return annotations on all setters (PHPStan missingType.generics) and bring only a type nobody reads.

Attributes
#[MappedSuperclass]
#[TableAlias]
$alias: 'customAsset'

Table of Contents

Interfaces

IdentifiableInterface
Entity with a simple integer primary key. Most entities have one (often via {@see \App\Core\Traits\Shared\Models\Entities\IdentifiableTrait}); texts and links with a composite key do not, which is why `getId()` is not on {@see \App\Core\Base\BaseEntity}. Code that needs the id of a generic entity asks for this interface (parameter type, template bound, `instanceof`).

Properties

$active  : bool
$addToNewPages  : bool
A newly created page gets this snippet assigned automatically.
$code  : string
$cookieCategory  : string
$id  : int|null
$insertTime  : DateTime
$name  : string
$pages  : Collection<int, BaseEntity>
$placement  : string
$sort  : int
$type  : string
$updateDatetime  : DateTime|null

Methods

__construct()  : mixed
addPage()  : $this
callInitMethods()  : void
Calls `init{TraitName}()` for every used trait that defines such a method.
getActive()  : bool
getAddToNewPages()  : bool
getAllTraits()  : array<string, string>
Returns all traits the class uses, including those used by parent classes and nested traits.
getCode()  : string
getCookieCategory()  : string
getId()  : int|null
getInsertTime()  : DateTime
getName()  : string
getPage()  : TPage|null
getPages()  : Collection<int, TPage>
getPlacement()  : string
getPlacementEnum()  : CustomAssetPlacement|null
getSort()  : int
getType()  : string
getTypeEnum()  : CustomAssetType|null
getUpdateDatetime()  : DateTime|null
isActive()  : bool
isAddToNewPages()  : bool
isPersisted()  : bool
needsCookieConsent()  : bool
An empty category means the code runs without waiting for cookie consent.
pageCollectionInit()  : void
removePage()  : $this
setActive()  : $this
setAddToNewPages()  : $this
setCode()  : $this
setCookieCategory()  : $this
setId()  : $this
setInsertTime()  : $this
setName()  : $this
setPages()  : $this
setPlacement()  : $this
setSort()  : $this
setType()  : $this
setUpdateDatetime()  : $this
getTraitsRecursive()  : array<string, string>
Walks the trait list recursively and collects nested traits too.

Properties

$active

protected bool $active = true
Attributes
#[Column]
$name: 'active'
$type: 'boolean'

$addToNewPages

A newly created page gets this snippet assigned automatically.

protected bool $addToNewPages = false
Attributes
#[Column]
$name: 'add_to_new_pages'
$type: 'boolean'

$code

protected string $code = ''
Attributes
#[Column]
$name: 'code'
$type: 'text'

$cookieCategory

protected string $cookieCategory = ''
Attributes
#[Column]
$name: 'cookie_category'
$type: 'string'
$length: 20

$id

protected int|null $id = null
Attributes
#[Column]
$name: 'custom_asset_id'
$type: 'integer'
$options: ['unsigned' => true]
#[GeneratedValue]
#[Id]

$insertTime

protected DateTime $insertTime = new DateTime()
Attributes
#[Column]
$name: 'insert_time'
$type: 'datetime'
#[ExternalReadonly]

$name

protected string $name = ''
Attributes
#[Column]
$name: 'name'
$type: 'string'
$length: 255

$placement

protected string $placement = CustomAssetPlacement::BodyEnd->value
Attributes
#[Column]
$name: 'placement'
$type: 'string'
$length: 15

$sort

protected int $sort = 0
Attributes
#[Column]
$name: 'sort'
$type: 'integer'
$options: ['unsigned' => true]

$type

protected string $type = CustomAssetType::Js->value
Attributes
#[Column]
$name: 'type'
$type: 'string'
$length: 10

$updateDatetime

protected DateTime|null $updateDatetime = null
Attributes
#[Column]
$name: 'update_datetime'
$type: 'datetime'
$nullable: true
#[ExternalReadonly]

Methods

__construct()

public __construct([int|null $id = null ][, string $name = '' ][, string $type = CustomAssetType::Js->value ][, string $placement = CustomAssetPlacement::BodyEnd->value ][, string $cookieCategory = '' ][, string $code = '' ][, bool $active = true ][, int $sort = 0 ][, bool $addToNewPages = false ][, DateTime $insertTime = new DateTime() ][, DateTime|null $updateDatetime = null ][, Collection $pages = new ArrayCollection() ]) : mixed
Parameters
$id : int|null = null
$name : string = ''
$type : string = CustomAssetType::Js->value
$placement : string = CustomAssetPlacement::BodyEnd->value
$cookieCategory : string = ''
$code : string = ''
$active : bool = true
$sort : int = 0
$addToNewPages : bool = false
$insertTime : DateTime = new DateTime()
$updateDatetime : DateTime|null = null
$pages : Collection = new ArrayCollection()

addPage()

public addPage(TPage $page) : $this
Parameters
$page : TPage
Return values
$this

callInitMethods()

Calls `init{TraitName}()` for every used trait that defines such a method.

public callInitMethods() : void

getActive()

public getActive() : bool
Return values
bool

getAddToNewPages()

public getAddToNewPages() : bool
Return values
bool

getAllTraits()

Returns all traits the class uses, including those used by parent classes and nested traits.

public getAllTraits() : array<string, string>
Return values
array<string, string>

getCode()

public getCode() : string
Return values
string

getCookieCategory()

public getCookieCategory() : string
Return values
string

getInsertTime()

public getInsertTime() : DateTime
Return values
DateTime

getName()

public getName() : string
Return values
string

getPage()

public getPage(int $pageId) : TPage|null
Parameters
$pageId : int
Return values
TPage|null

getPages()

public getPages() : Collection<int, TPage>
Return values
Collection<int, TPage>

getPlacement()

public getPlacement() : string
Return values
string

getType()

public getType() : string
Return values
string

getUpdateDatetime()

public getUpdateDatetime() : DateTime|null
Return values
DateTime|null

isActive()

public isActive() : bool
Return values
bool

isAddToNewPages()

public isAddToNewPages() : bool
Return values
bool

needsCookieConsent()

An empty category means the code runs without waiting for cookie consent.

public needsCookieConsent() : bool

NOTE: only "something is filled in" is checked, not whether the category exists in the cookie bar; the entity does not know the configuration. Validity is guarded by the form select, which offers exactly the bar categories (CookieConsentConfig::selectableKeys()).

Return values
bool

removePage()

public removePage(TPage $page) : $this
Parameters
$page : TPage
Return values
$this

setActive()

public setActive(bool $active) : $this
Parameters
$active : bool
Return values
$this

setAddToNewPages()

public setAddToNewPages(bool $addToNewPages) : $this
Parameters
$addToNewPages : bool
Return values
$this

setCode()

public setCode(string $code) : $this
Parameters
$code : string
Return values
$this

setCookieCategory()

public setCookieCategory(string $cookieCategory) : $this
Parameters
$cookieCategory : string
Return values
$this

setId()

public setId(int|null $id) : $this
Parameters
$id : int|null
Return values
$this

setInsertTime()

public setInsertTime(DateTime $insertTime) : $this
Parameters
$insertTime : DateTime
Return values
$this

setName()

public setName(string $name) : $this
Parameters
$name : string
Return values
$this

setPages()

public setPages(Collection<int, TPage>|array<int, TPage> $pages) : $this
Parameters
$pages : Collection<int, TPage>|array<int, TPage>
Return values
$this

setPlacement()

public setPlacement(string $placement) : $this
Parameters
$placement : string
Return values
$this

setSort()

public setSort(int $sort) : $this
Parameters
$sort : int
Return values
$this

setType()

public setType(string $type) : $this
Parameters
$type : string
Return values
$this

setUpdateDatetime()

public setUpdateDatetime(DateTime|null $updateDatetime) : $this
Parameters
$updateDatetime : DateTime|null
Return values
$this

getTraitsRecursive()

Walks the trait list recursively and collects nested traits too.

private static getTraitsRecursive(array<string, string> $traits) : array<string, string>
Parameters
$traits : array<string, string>
Return values
array<string, string>
On this page

Search results