CoolCMS API

SearchBox extends BaseComponent
in package
uses FormTrait, SearchBoxRegionTrait

Search box of the Bazaar module.

Styles:

  • Mini: fulltext plus button (module headers)
  • Medium: fulltext plus category plus region (hero of /inzerce, /firmy)
  • Large: type switch plus category/region/price (main page of template2)

Style and StyleInline are backward-compatible aliases for Mini, called by template1, which is an active theme. Do not remove them without changing its templates.

Usage:

{control frontBazaarSearchBox, 'Mini'}
{control frontBazaarSearchBox:medium}

Table of Contents

Constants

REGION_FIELD_TEMPLATE  : string = __DIR__ . '/../../../Base/Front/Components/Sear...
Shared Latte part of the field; component (and theme) templates only place it.
REGION_LINK_NO_CATEGORY  : string = '-'
Placeholder category meaning "no category" in the listing link.
REGION_NOT_FOUND_MESSAGE  : string = 'front.system.components.regionSuggest.messages...
Flash shown when the typed locality cannot be resolved (full key; the layout translates it via `{_$flash->message}`).
REGION_PLACEHOLDER_KEY  : string = 'areaPlaceholder'
Suggest placeholder key (domain `front.system.components.regionSuggest`): region, district, municipality, postcode.
TYPE_PARAMETER_KEY  : mixed = 'type'
Key of the parameter that carries the advert type. Options are its children in the tree.

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
$categoryManager  : CategoryManager
$parameterManager  : ParameterManager
$priceStepsConfig  : AdvertPriceStepsConfig
$regionManager  : RegionManager
$regionSuggestFactory  : RegionSuggestFactory
$typeParameterCache  : object|null

Methods

__call()  : mixed
Dispatches `renderXyz()` to `render('Xyz', ...$args)`.
__construct()  : mixed
createComponent()  : IComponent|null
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
hasServiceByType()  : bool
Returns whether the container has a service of the given type.
initializeAllContexts()  : void
injectDeviceDetector()  : void
onCreate()  : void
onSuccess()  : void
render()  : void
renderLarge()  : void
Type switch plus category/region/price. Main page of template2.
renderMedium()  : void
Fulltext plus category plus region. Hero of the signposts (/inzerce, /firmy).
renderMini()  : void
Fulltext plus button. Module headers.
setAppContext()  : $this
setComponentParameter()  : void
Sets a component parameter.
setCurrencyContext()  : $this
setLanguageContext()  : $this
setLocalizationContext()  : $this
setSettingContext()  : $this
setThemeContext()  : $this
setup()  : void
setupAppContext()  : void
typedComponent()  : T
Returns a subcomponent with a verified type.
createComponentForm()  : Form
createComponentRegionSuggest()  : RegionSuggest
frontPresenter()  : BasePresenter|null
Returns the front presenter the component is attached to (null outside the front end or when detached).
getTranslator()  : ITranslator
setTemplateFile()  : void
setTranslator()  : self
addRegionFields()  : void
Adds the locality fields to the form.
applyStyleRules()  : void
Fulltext is required only in `mini`, where it is the only field.
buildMainRegionOptions()  : array<int, NotTranslate>
buildRegionLinkParameters()  : array<string, mixed>
Adds `regionId` to the listing link parameters (and the placeholder category when there is none).
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).
currentTypeOptionId()  : int|null
Currently selected type option from the URL (`?p[<id>]=<optionId>`).
findTypeParameter()  : object|null
languageId()  : int|null
Id of the current language for reading entity translations.
loadMainCategories()  : array<int, NotTranslate>
loadTypeOptions()  : array<int, NotTranslate>
pluckNames()  : array<int, NotTranslate>
Extracts `id => name` from entities with translations.
prepareParametersForRedirectLink()  : array<string, mixed>
Composes the link parameters for the list.
prepareRegionField()  : void
Sets variables for the shared Latte field part ({@see REGION_FIELD_TEMPLATE}).
priceOptions()  : array<int|string, NotTranslate>
Returns "Price up to" options in the site currency (currency of the localization region): steps from `advert.bazaarPriceSteps` ({@see AdvertPriceStepsConfig}), caption by localization.
resolveRegionLinkValue()  : int|string|null
Returns the `regionId` value for the listing link.
resolveSearchLanguageId()  : int
resolveSearchPreferRegionId()  : int|null
The country of the website localization takes precedence in the offer (same as in `RegionSuggest`).
safeDefault()  : int|string|null
Preselected select value, only if it is really in its options.

Constants

REGION_FIELD_TEMPLATE

Shared Latte part of the field; component (and theme) templates only place it.

private string REGION_FIELD_TEMPLATE = __DIR__ . '/../../../Base/Front/Components/SearchBox/Templates/Default/Elements/Region.latte'

Placeholder category meaning "no category" in the listing link.

private string REGION_LINK_NO_CATEGORY = '-'

IMPORTANT: the route mask is <slug>[/<categoryId>][/<regionId>]; without a category the router would read /inzeraty/<region href> as a category and the listing would silently ignore the locality. The region menu (RegionListComponent) uses the same placeholder; the listing finds no such category, so no category filter applies.

REGION_NOT_FOUND_MESSAGE

Flash shown when the typed locality cannot be resolved (full key; the layout translates it via `{_$flash->message}`).

private string REGION_NOT_FOUND_MESSAGE = 'front.system.components.regionSuggest.messages.notFound'

REGION_PLACEHOLDER_KEY

Suggest placeholder key (domain `front.system.components.regionSuggest`): region, district, municipality, postcode.

private string REGION_PLACEHOLDER_KEY = 'areaPlaceholder'

TYPE_PARAMETER_KEY

Key of the parameter that carries the advert type. Options are its children in the tree.

private mixed TYPE_PARAMETER_KEY = 'type'

Properties

$componentParameters

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

component parameters set by setComponentParameter()

$translatorDomain

protected string|null $translatorDomain = 'front.bazaar.components.searchBox'

$typeParameterCache

private object|null $typeParameterCache = null

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>>

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

onSuccess()

public onSuccess(Form $form, ArrayHash $values) : void
Parameters
$form : Form
$values : ArrayHash

render()

public render([string $style = 'Style' ][, bool $fulltext = false ]) : void
Parameters
$style : string = 'Style'

template variant (Style, Mini, Medium, Large, ...)

$fulltext : bool = false

large only: whether the card also renders the search text field (fulltext). Default false leaves the output unchanged. The field itself is placed by the specific theme in its template (Templates/<Theme>/<Variant>/Large.latte); the default Default/Large.latte only adds it before the category.

renderLarge()

Type switch plus category/region/price. Main page of template2.

public renderLarge([bool $fulltext = false ]) : void
Parameters
$fulltext : bool = false

whether to also render the search text field, see render(); default false leaves it unchanged

renderMedium()

Fulltext plus category plus region. Hero of the signposts (/inzerce, /firmy).

public renderMedium() : void

renderMini()

Fulltext plus button. Module headers.

public renderMini() : void

setComponentParameter()

Sets a component parameter.

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

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

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

addRegionFields()

Adds the locality fields to the form.

private addRegionFields(Form $form, callable $t) : void

The default comes from the URL, where regionId is translated to href in the route.

IMPORTANT: do not set it into the select without safeDefault(); a value outside the options throws a 500.

Parameters
$form : Form
$t : callable

applyStyleRules()

Fulltext is required only in `mini`, where it is the only field.

private applyStyleRules(string $style) : void

In medium there are category and region next to it; the user may want to browse a whole category and type nothing, so a requirement would block that. large has no fulltext at all.

Parameters
$style : string

buildMainRegionOptions()

private buildMainRegionOptions() : array<int, NotTranslate>
Return values
array<int, NotTranslate> —

tree roots (countries) for the <select> of a website without import

buildRegionLinkParameters()

Adds `regionId` to the listing link parameters (and the placeholder category when there is none).

private buildRegionLinkParameters(Form $form, ArrayHash $values, array<string, mixed> $params) : array<string, mixed>
Parameters
$form : Form
$values : ArrayHash
$params : array<string, mixed>
Return values
array<string, mixed>

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

currentTypeOptionId()

Currently selected type option from the URL (`?p[<id>]=<optionId>`).

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

findTypeParameter()

private findTypeParameter() : object|null
Return values
object|null

languageId()

Id of the current language for reading entity translations.

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

loadMainCategories()

private loadMainCategories() : array<int, NotTranslate>
Return values
array<int, NotTranslate>

loadTypeOptions()

private loadTypeOptions() : array<int, NotTranslate>
Return values
array<int, NotTranslate> —

option id => name

pluckNames()

Extracts `id => name` from entities with translations.

private pluckNames(iterable<string|int, object> $items) : array<int, NotTranslate>

NOTE: entities have no getName(); the name lives in the translation table and is read via getTranslation($languageId)?->getName() (see CategoryListComponent/Templates/Default/Style_1.latte). Without a language or translation the item is skipped so the select has no empty rows.

IMPORTANT: names are already localized data, not keys, so they are wrapped in NotTranslate. The form has a translator (for captions) and would otherwise look up every option as a key: the main page reported 32 missing translations.

Parameters
$items : iterable<string|int, object>
Return values
array<int, NotTranslate>

Composes the link parameters for the list.

private prepareParametersForRedirectLink(Form $form, ArrayHash $values) : array<string, mixed>

Route mask (cms_system_routes): <slug>[/<categoryId>][/<regionId>][/s-<search>][/p-<page>][/o-<order>]... What is not in the mask (p, priceTo) Nette appends as a query string.

Parameters
$form : Form
$values : ArrayHash
Return values
array<string, mixed>

prepareRegionField()

Sets variables for the shared Latte field part ({@see REGION_FIELD_TEMPLATE}).

private prepareRegionField() : void

priceOptions()

Returns "Price up to" options in the site currency (currency of the localization region): steps from `advert.bazaarPriceSteps` ({@see AdvertPriceStepsConfig}), caption by localization.

private priceOptions() : array<int|string, NotTranslate>

The option value is a bare number: the price filter is not converted.

Return values
array<int|string, NotTranslate> —

formatted amount, not a key, see self::pluckNames()

resolveRegionLinkValue()

Returns the `regionId` value for the listing link.

private resolveRegionLinkValue(Form $form, ArrayHash $values) : int|string|null

Select: the chosen id; suggest: the id from the carrier or resolved from the text (RegionManager::resolveSearchRegionId()). A typed but unresolved locality yields the listing without a locality plus a warning, because silently ignoring it would look like results for the typed place.

Parameters
$form : Form
$values : ArrayHash
Return values
int|string|null

resolveSearchPreferRegionId()

The country of the website localization takes precedence in the offer (same as in `RegionSuggest`).

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

safeDefault()

Preselected select value, only if it is really in its options.

private safeDefault(mixed $value, array<int|string, string|Stringable> $options) : int|string|null

IMPORTANT: without this guard the advert list crashes. The router translates categoryId and regionId in the URL to a name (CategoryNameTranslator), so getParameter('categoryId') returns 'automobily', not 12. Nette SelectBox::setDefaultValue() throws InvalidArgumentException for a value outside the options, giving a 500 for the whole page, also in the header (StyleInline), i.e. on /inzerce and all lists.

An unknown value is therefore ignored and the select stays on the prompt.

Parameters
$value : mixed
$options : array<int|string, string|Stringable>
Return values
int|string|null
On this page

Search results