CoolCMS API

RatingThread extends BaseComponent
in package
uses ModalAwareTrait, PrivilegeGuardTrait

Manages the ratings of one thread ({@see \App\UI\Api\Base\Models\Entities\RatingThread}) in the admin.

Keyed by threadId, so it is host-agnostic: it renders in any admin edit form that owns a rating thread (article, company, ...) via {control ratingThread $threadId}.

The list is flat (ratings have no tree). Adding and editing run in the shared admin ModalWindow: the addRating! and editRating! signals render _modal.latte to a string, fill the modal with it and open it from PHP (showModal()). The form is submitted via ajax and the modal stays open after saving; showModal() only refills its content (shows success) and the ratingList snippet is redrawn. The thread travels in the form as a hidden threadId, not as a persistent parameter, because link('this') of the form does not carry it. Deleting is soft: the deleted flag is toggled by the toggleDeleted! signal.

Table of Contents

Properties

$container  : Container
$routerDispatcher  : RouterDispatcher
$translator  : ITranslator
$appContext  : AppContext
$componentParameters  : array<string, mixed>
$currencyContext  : CurrencyContext|null
$deviceDetector  : DeviceDetector|null
$languageContext  : LanguageContext|null
$localizationContext  : LocalizationContext|null
$settingContext  : SettingContext
$themeContext  : ThemeContext|null
$translatorDomain  : string|null
$adding  : bool
A new rating is being added (modal with an empty form).
$editId  : int|null
Edited rating (set by `handleEditRating()` or by the submit to re-render the modal).
$justSaved  : bool
The rating was just saved, so the modal shows a confirmation instead of the form (stays open).
$ratingManager  : RatingManager
$threadContextProvider  : callable(): mixed|null
IMPORTANT: the hidden `threadId` field is a client-editable echo; the authority is the server-side host context (`fn() => $this->editedArticle?->getRatingThreadId()`). It is read at signal/submit time, when the host's `action*()` has already loaded the edited entity.
$threadId  : int
Thread whose ratings the component manages. Not a persistent parameter, because Nette does not reliably carry it into signals and `link('this')`; it is passed explicitly as a parameter of every signal (`{link addRating! threadId => …}`) and into the form submit via the hidden `threadId` field.
$toggleFailed  : bool
The `deleted` toggle (`toggleDeleted!`) failed, so the list renders an error message.

Methods

__call()  : mixed
Dispatches `renderXyz()` to `render('Xyz', ...$args)`.
__construct()  : mixed
createComponent()  : IComponent|null
createComponentRatingForm()  : Form
getAppContext()  : AppContext
getComponentParameter()  : mixed
Returns a component parameter, or null when it is not set.
getContainer()  : Container
getCurrencyContext()  : CurrencyContext|null
getLanguageContext()  : LanguageContext|null
getLocalizationContext()  : LocalizationContext|null
getRouterDispatcher()  : RouterDispatcher
getSettingContext()  : SettingContext
getSettings()  : array<int, array<string, mixed>>
getThemeContext()  : ThemeContext|null
handleAddRating()  : void
Opens the shared modal for adding a rating.
handleEditRating()  : void
Opens the shared modal for editing a rating.
handleToggleDeleted()  : void
Soft-deletes or restores a rating (toggles the `deleted` flag).
hasServiceByType()  : bool
Returns whether the container has a service of the given type.
initializeAllContexts()  : void
injectDeviceDetector()  : void
onCreate()  : void
onRatingFormError()  : void
Handles failed validation (missing stars, expired CSRF token...): redraws the modal content so the errors show. Restores the edit/add state from the submitted hidden fields.
onRatingFormSuccess()  : void
render()  : void
setAppContext()  : $this
setComponentParameter()  : void
Sets a component parameter.
setCurrencyContext()  : $this
setLanguageContext()  : $this
setLocalizationContext()  : $this
setSettingContext()  : $this
setThemeContext()  : $this
setThreadContext()  : static
Sets the server-side thread context supplied by the host.
setup()  : void
setupAppContext()  : void
typedComponent()  : T
Returns a subcomponent with a verified type.
frontPresenter()  : BasePresenter|null
Returns the front presenter the component is attached to (null outside the front end or when detached).
getModalWindow()  : ModalWindow
getTranslator()  : ITranslator
isTargetAllowed()  : bool
Returns whether the logged-in user may open the given admin target. For visibility (tiles, autocomplete, buttons); it does not throw.
requirePrivilege()  : void
Throws 403 when the logged-in user lacks the given admin privilege.
resolvePageTarget()  : string|null
Returns the RBAC key of the target page: `<presenter>:<action>`, e.g. `:Admin:System:User:show`.
setTemplateFile()  : void
setTranslator()  : self
buildNewRating()  : Rating
Builds a new rating attached to the thread (the remaining fields are filled by the success handler).
buildSummary()  : array{count: int, average: float, distribution: array}
Builds the thread summary for the detail view (average, count, distribution by stars).
contextThreadId()  : int|null
Thread id from the host context; null when the host edits no thread (fail-closed).
createMultiplierComponent()  : Multiplier<string|int, IComponent>
Creates a multiplier for the component.
createNewComponentInstance()  : IComponent|null
Creates a new component instance without adding it to the presenter; used by the multiplier.
createStandardComponent()  : IComponent|null
Creates a standard component (not a multiplier).
findRatingInThread()  : Rating|null
Finds a rating always within the thread. No unscoped variant exists on purpose, so future code cannot use one by mistake.
intInRange()  : int
listToText()  : string
Converts a list of strings to a textarea value (one item per line).
loadRatings()  : array<int, Rating>
Loads the thread's ratings (flat list, newest first).
prepareValueItems()  : array<int, string>
Returns the star select items (5 to 1).
renderModalContent()  : string
Renders the modal content (header and form or confirmation) to a string for `ModalWindow`.
requireContextThread()  : int
Signal gate: a `threadId` from the URL may be used only when it equals the host context; otherwise 403.
showModal()  : void
Fills and opens the shared admin {@see \App\UI\Admin\System\Components\ModalWindowComponent\ModalWindow} with the current state (form or confirmation). Idempotent: called again after the submit, it redraws only the modal content (the modal stays open, see `ModalWindow::open()`).
str()  : string
strOrNull()  : string|null
textToList()  : array<int, string>|null
Converts a textarea (one item per line) to a list of strings; empty input gives null.

Properties

$componentParameters

protected array<string, mixed> $componentParameters = []

component parameters set by setComponentParameter()

$translatorDomain

protected string|null $translatorDomain = 'admin.base.components.ratingThread'

$adding

A new rating is being added (modal with an empty form).

private bool $adding = false

$editId

Edited rating (set by `handleEditRating()` or by the submit to re-render the modal).

private int|null $editId = null

$justSaved

The rating was just saved, so the modal shows a confirmation instead of the form (stays open).

private bool $justSaved = false

$threadContextProvider

IMPORTANT: the hidden `threadId` field is a client-editable echo; the authority is the server-side host context (`fn() => $this->editedArticle?->getRatingThreadId()`). It is read at signal/submit time, when the host's `action*()` has already loaded the edited entity.

private callable(): mixed|null $threadContextProvider = null

Fail-closed: without a context the component saves nothing and opens no modal, so every new host must call setThreadContext().

$threadId

Thread whose ratings the component manages. Not a persistent parameter, because Nette does not reliably carry it into signals and `link('this')`; it is passed explicitly as a parameter of every signal (`{link addRating! threadId => …}`) and into the form submit via the hidden `threadId` field.

private int $threadId = 0

$toggleFailed

The `deleted` toggle (`toggleDeleted!`) failed, so the list renders an error message.

private bool $toggleFailed = false

Per request (the component is rebuilt on every request), so the message does not survive another redraw. Flash cannot be used: the signal runs via ajax and the layout's flashMessagesSnippet is not redrawn in a component response.

Methods

__call()

Dispatches `renderXyz()` to `render('Xyz', ...$args)`.

public __call(string $name, array<string|int, mixed> $args) : mixed

Called only when the component has no renderXyz() method of its own.

Parameters
$name : string
$args : array<string|int, mixed>

createComponent()

public createComponent(string $name) : IComponent|null
Parameters
$name : string
Return values
IComponent|null

getComponentParameter()

Returns a component parameter, or null when it is not set.

public getComponentParameter(string $name) : mixed
Parameters
$name : string

getContainer()

public getContainer() : Container
Return values
Container

getSettings()

public getSettings() : array<int, array<string, mixed>>
Return values
array<int, array<string, mixed>>

handleAddRating()

Opens the shared modal for adding a rating.

public handleAddRating(int $threadId) : void
Parameters
$threadId : int

handleEditRating()

Opens the shared modal for editing a rating.

public handleEditRating(int $threadId, int $id) : void
Parameters
$threadId : int
$id : int

handleToggleDeleted()

Soft-deletes or restores a rating (toggles the `deleted` flag).

public handleToggleDeleted(int $threadId, int $id) : void
Parameters
$threadId : int
$id : int
Attributes
#[Requires]
$methods: 'POST'

hasServiceByType()

Returns whether the container has a service of the given type.

public hasServiceByType(string $className) : bool

The name is composed dynamically (module plus component), so class_exists() runs first, and a true result guarantees a valid type.

Parameters
$className : string
Tags
phpstan-assert-if-true

class-string $className

Return values
bool

onRatingFormError()

Handles failed validation (missing stars, expired CSRF token...): redraws the modal content so the errors show. Restores the edit/add state from the submitted hidden fields.

public onRatingFormError(Form $form) : void
Parameters
$form : Form

onRatingFormSuccess()

public onRatingFormSuccess(Form $form, ArrayHash<string|int, mixed> $values) : void
Parameters
$form : Form
$values : ArrayHash<string|int, mixed>

render()

public render([int|null $threadId = null ]) : void
Parameters
$threadId : int|null = null

setComponentParameter()

Sets a component parameter.

public setComponentParameter(string $name, mixed $value) : void
Parameters
$name : string
$value : mixed

setThreadContext()

Sets the server-side thread context supplied by the host.

public setThreadContext(callable(): mixed $provider) : static
Parameters
$provider : callable(): mixed
Return values
static

typedComponent()

Returns a subcomponent with a verified type.

public typedComponent(string $name, T> $type) : T

Use it instead of an inline @var above $this['x'] (which does not narrow the type) and instead of the generic IComponent that $this['x'] returns. See Form::typedComponent().

Parameters
$name : string
$type : T>
Tags
template
Return values
T

frontPresenter()

Returns the front presenter the component is attached to (null outside the front end or when detached).

protected frontPresenter() : BasePresenter|null
Return values
BasePresenter|null

getTranslator()

protected getTranslator() : ITranslator
Return values
ITranslator

isTargetAllowed()

Returns whether the logged-in user may open the given admin target. For visibility (tiles, autocomplete, buttons); it does not throw.

protected isTargetAllowed(string|null $key) : bool

IMPORTANT: fail-closed like requirePrivilege(): an empty key and a presenter without isAllowed() both mean no. A component that skips this question lists the target to everyone.

Parameters
$key : string|null
Return values
bool

requirePrivilege()

Throws 403 when the logged-in user lacks the given admin privilege.

protected requirePrivilege(string $key) : void

Call it as the first statement of a mutating signal, before loading data, so the check cannot be bypassed by something being written first.

Parameters
$key : string

resolvePageTarget()

Returns the RBAC key of the target page: `<presenter>:<action>`, e.g. `:Admin:System:User:show`.

protected static resolvePageTarget(object|null $page) : string|null

Same interpretation as Menu::filterByPrivileges(), BasePresenter::authorizeListing() and PrivilegeCheckTrait::checkCurrentPrivilege(). The key is derived from the page target only, never from a file or class name.

Returns null when the page is missing or has no presenter, so there is nothing to allow.

Parameters
$page : object|null

AdminPage entity (hydrated, hence just object)

Return values
string|null

setTemplateFile()

protected setTemplateFile(string $dir, string $filename) : void
Parameters
$dir : string
$filename : string

setTranslator()

protected setTranslator(ITranslator $translator) : self
Parameters
$translator : ITranslator
Return values
self

buildNewRating()

Builds a new rating attached to the thread (the remaining fields are filled by the success handler).

private buildNewRating(int $threadId) : Rating
Parameters
$threadId : int
Return values
Rating

buildSummary()

Builds the thread summary for the detail view (average, count, distribution by stars).

private buildSummary(array<int, Rating> $ratings) : array{count: int, average: float, distribution: array}

Counts only non-deleted ratings.

Parameters
$ratings : array<int, Rating>
Return values
array{count: int, average: float, distribution: array}

contextThreadId()

Thread id from the host context; null when the host edits no thread (fail-closed).

private contextThreadId() : int|null
Return values
int|null

createMultiplierComponent()

Creates a multiplier for the component.

private createMultiplierComponent(string $name) : Multiplier<string|int, IComponent>
Parameters
$name : string
Return values
Multiplier<string|int, IComponent>

createNewComponentInstance()

Creates a new component instance without adding it to the presenter; used by the multiplier.

private createNewComponentInstance(string $baseName[, int|string|null $uniqueKey = null ]) : IComponent|null
Parameters
$baseName : string
$uniqueKey : int|string|null = null
Return values
IComponent|null

createStandardComponent()

Creates a standard component (not a multiplier).

private createStandardComponent(string $name) : IComponent|null
Parameters
$name : string
Return values
IComponent|null

findRatingInThread()

Finds a rating always within the thread. No unscoped variant exists on purpose, so future code cannot use one by mistake.

private findRatingInThread(int $id, int $threadId) : Rating|null
Parameters
$id : int
$threadId : int
Return values
Rating|null

intInRange()

private intInRange(mixed $value, int $min, int $max, int $default) : int
Parameters
$value : mixed
$min : int
$max : int
$default : int
Return values
int

listToText()

Converts a list of strings to a textarea value (one item per line).

private listToText(array<int, string> $items) : string
Parameters
$items : array<int, string>
Return values
string

loadRatings()

Loads the thread's ratings (flat list, newest first).

private loadRatings() : array<int, Rating>
Return values
array<int, Rating>

prepareValueItems()

Returns the star select items (5 to 1).

private prepareValueItems() : array<int, string>
Return values
array<int, string>

renderModalContent()

Renders the modal content (header and form or confirmation) to a string for `ModalWindow`.

private renderModalContent() : string
Return values
string

requireContextThread()

Signal gate: a `threadId` from the URL may be used only when it equals the host context; otherwise 403.

private requireContextThread(int $threadId) : int
Parameters
$threadId : int
Return values
int

showModal()

Fills and opens the shared admin {@see \App\UI\Admin\System\Components\ModalWindowComponent\ModalWindow} with the current state (form or confirmation). Idempotent: called again after the submit, it redraws only the modal content (the modal stays open, see `ModalWindow::open()`).

private showModal() : void

str()

private str(mixed $value) : string
Parameters
$value : mixed
Return values
string

strOrNull()

private strOrNull(mixed $value) : string|null
Parameters
$value : mixed
Return values
string|null

textToList()

Converts a textarea (one item per line) to a list of strings; empty input gives null.

private textToList(mixed $value) : array<int, string>|null
Parameters
$value : mixed
Return values
array<int, string>|null
On this page

Search results