PollFormTrait
Add/edit form of a poll.
Multilingual question (question + active translation) in language tabs, the active switch,
a multiplier of answers (multilingual text + vote count, owned collection Poll.answers with
cascade) and a multiplier of poll places (PollPlaceRel rows chosen through Picker).
Presenter dependencies:
$this->pollManagerPollManager$this->pollPlaceManagerPollPlaceManager (picker)$this->adminLanguageManagerLanguageManager$this->editedPoll?Poll (null when adding)$this->translatorDomainstring (from BasePresenter)
Table of Contents
Constants
- CONFIRMED_FLAG : string = '_confirmed'
- POST flag of a confirmed action, set by the confirmation modal (`datagrid.js`, {@see \App\UI\Admin\System\Components\DataGridComponent\DataGrid::CONFIRM_FIELD}).
- REMOVED_ANSWERS_FIELD : string = 'removedAnswerIds'
- Hidden field with the ids of answers with votes whose removal was confirmed (comma separated).
Methods
- createComponentEditForm() : Form
- addAnswerMultiplier() : void
- Multiplier of answers. Row = hidden `answerId` (saved, so updated), `count` and the multilingual `text_<langId>`. Create/remove buttons have the `ajax` class, so naja redraws the `answers` snippet (removing an answer with votes goes without AJAX through a confirmation, see the template).
- addPollBehaviorFields() : void
- Poll behavior — the active switch.
- addPollPlaceMultiplier() : void
- Multiplier of poll places (`PollPlaceRel` rows through Picker; row = hidden `pollPlaceId` + label).
- addPollTranslationFields() : void
- Multilingual question — active translation switch + question text.
- buildPollAnswers() : ArrayCollection<int, PollAnswer>
- buildPollEntity() : Poll
- buildPollPlaces() : ArrayCollection<int, PollPlaceRel>
- confirmedRemovals() : array<int, int>
- findPollTranslation() : PollText|null
- getPollFormLanguages() : array<int, Language>
- nStr() : string
- rememberConfirmedAnswerRemoval() : void
- Remembers the removal of an answer with votes confirmed in the modal (the confirmation flag is in the POST of the remove button). The removal itself only drops the row from the form; the answer and its votes are deleted by the next save, which accepts it only for an id remembered here.
- savePollForm() : bool
- setPollFormDefaults() : void
- validatePollAnswers() : void
- Server-side checks of the answer rows (admin walkthrough A12, A13): - an existing answer id must belong to this poll; a forged or foreign `answerId` used to delete the real answer with its votes (orphan removal) and create a new one with the same text; - an answer with votes may disappear only with a confirmed removal ({@see rememberConfirmedAnswerRemoval()}); the confirmation used to be client-side only, a POST without the row deleted the answer and its votes.
Constants
CONFIRMED_FLAG
POST flag of a confirmed action, set by the confirmation modal (`datagrid.js`, {@see \App\UI\Admin\System\Components\DataGridComponent\DataGrid::CONFIRM_FIELD}).
private
string
CONFIRMED_FLAG
= '_confirmed'
REMOVED_ANSWERS_FIELD
Hidden field with the ids of answers with votes whose removal was confirmed (comma separated).
private
string
REMOVED_ANSWERS_FIELD
= 'removedAnswerIds'
Methods
createComponentEditForm()
protected
createComponentEditForm() : Form
Return values
FormaddAnswerMultiplier()
Multiplier of answers. Row = hidden `answerId` (saved, so updated), `count` and the multilingual `text_<langId>`. Create/remove buttons have the `ajax` class, so naja redraws the `answers` snippet (removing an answer with votes goes without AJAX through a confirmation, see the template).
private
addAnswerMultiplier(Form $form) : void
The answer text is required in the first language (the website default) and in every language with the active translation on (like the question). Clearing a text therefore does not delete the answer — the save is rejected; an answer is deleted only by the "Remove" button.
Parameters
- $form : Form
addPollBehaviorFields()
Poll behavior — the active switch.
private
addPollBehaviorFields(Form $form) : void
Parameters
- $form : Form
addPollPlaceMultiplier()
Multiplier of poll places (`PollPlaceRel` rows through Picker; row = hidden `pollPlaceId` + label).
private
addPollPlaceMultiplier(Form $form) : void
Parameters
- $form : Form
addPollTranslationFields()
Multilingual question — active translation switch + question text.
private
addPollTranslationFields(Form $form) : void
Parameters
- $form : Form
buildPollAnswers()
private
buildPollAnswers(ArrayHash<string|int, mixed> $values, Poll $poll, array<int, bool> $activeByLang) : ArrayCollection<int, PollAnswer>
Parameters
- $values : ArrayHash<string|int, mixed>
- $poll : Poll
- $activeByLang : array<int, bool>
Return values
ArrayCollection<int, PollAnswer>buildPollEntity()
private
buildPollEntity(ArrayHash<string|int, mixed> $values) : Poll
Parameters
- $values : ArrayHash<string|int, mixed>
Return values
PollbuildPollPlaces()
private
buildPollPlaces(ArrayHash<string|int, mixed> $values) : ArrayCollection<int, PollPlaceRel>
Parameters
- $values : ArrayHash<string|int, mixed>
Return values
ArrayCollection<int, PollPlaceRel>confirmedRemovals()
private
confirmedRemovals(mixed $value) : array<int, int>
Parameters
- $value : mixed
Return values
array<int, int> —confirmed answer ids from the hidden field
findPollTranslation()
private
findPollTranslation(Poll $poll, int $langId) : PollText|null
Parameters
- $poll : Poll
- $langId : int
Return values
PollText|nullgetPollFormLanguages()
private
getPollFormLanguages() : array<int, Language>
Return values
array<int, Language>nStr()
private
nStr(mixed $value) : string
Parameters
- $value : mixed
Return values
stringrememberConfirmedAnswerRemoval()
Remembers the removal of an answer with votes confirmed in the modal (the confirmation flag is in the POST of the remove button). The removal itself only drops the row from the form; the answer and its votes are deleted by the next save, which accepts it only for an id remembered here.
private
rememberConfirmedAnswerRemoval(Form $form) : void
The multiplier never creates the container of the removed row (it drops it from the HTTP data), so the removed answer id is read from the POST row that carries the remove button.
Parameters
- $form : Form
savePollForm()
private
savePollForm(ArrayHash<string|int, mixed> $values) : bool
Parameters
- $values : ArrayHash<string|int, mixed>
Return values
boolsetPollFormDefaults()
private
setPollFormDefaults(Form $form) : void
Parameters
- $form : Form
validatePollAnswers()
Server-side checks of the answer rows (admin walkthrough A12, A13): - an existing answer id must belong to this poll; a forged or foreign `answerId` used to delete the real answer with its votes (orphan removal) and create a new one with the same text; - an answer with votes may disappear only with a confirmed removal ({@see rememberConfirmedAnswerRemoval()}); the confirmation used to be client-side only, a POST without the row deleted the answer and its votes.
private
validatePollAnswers(Form $form) : void
Parameters
- $form : Form