Kdyby Replicator – smazání položky
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- Dynab
- Člen | 5
Špatné renderování smazaných položek. První se smaže, další
zůstanou ale jen s prázdnými textovými poli, nepochopil jsem proč. Viz
zdroják celé komponenty.
V šabloně je formulář vypsán přes {control…
public function __construct(User $user, \Kdyby\Doctrine\EntityManager $entityManager, $surveyId, $tagRepo) {
parent::__construct($user, $entityManager);
$this->survey = $this->em->find(Survey::class, $surveyId);
$this->questions = $this->survey->getQuestions();
$this->tagRepo = $tagRepo;
$this->tags = $this->tagRepo->findAllArray();
$this->setDefaults();
}
public function render() {
$template = $this->template;
$exists = dirName(__FILE__)
.DIRECTORY_SEPARATOR.'templates'
.DIRECTORY_SEPARATOR.'surveyPersonalist.latte';
$template->setFile($exists);
$template->survey = $this->survey;
$template->questions = $this->questions;
$template->tags = json_encode($this->tags);
$template->render();
}
private function setDefaults() {
$this['surveyForm']['positionName']->setDefaultValue($this->survey->positionName);
$this['surveyForm']['positionDescription']->setDefaultValue($this->survey->positionDescription);
foreach ($this->questions as $k => $q) {
$this['surveyForm']['questions'][$k]['id']->setDefaultValue($q->getId());
$this['surveyForm']['questions'][$k]['text']->setDefaultValue($q->getText());
$this['surveyForm']['questions'][$k]['tag']->setDefaultValue($q->getTag()->getName());
$this['surveyForm']['questions'][$k]['points']->setDefaultValue($q->getPoints());
}
}
public function createComponentSurveyForm() {
$form = new Form;
$form->addText('positionName', 'Name:')
->addRule(Form::FILLED, 'The name was not filled in')
->setAttribute('placeholder', 'The name of the position')
->addCondition(Form::FILLED);
$form->addText('positionDescription', 'Description:')
->addRule(Form::FILLED, 'The description was not filled in')
->addCondition(Form::FILLED);
$questions = array();
foreach ($this->questions as $q) {
$questions[$q->getId()] = $q->getText();
}
$removeQuestionEvent = callback($this, 'credentialsFormRemoveQuestionClicked');
$questionsContainer = $form->addDynamic('questions', function(Container $container) use ($removeQuestionEvent, $questions) {
$container->addHidden('id');
$container->addText('text', 'Question', $questions)
->setRequired('Fill the empty questions.');
$container->addText('points', 'Points', $questions)
->setRequired('Fill the points of a question.');
$container->addText('tag', 'Tag', $questions)
->setRequired('Fill a tag.');
$container->addSubmit('removeQuestion', 'Remove question')
->setValidationScope(FALSE)
->onClick[] = $removeQuestionEvent;
});
$questionsContainer->addSubmit('addQuestion', 'Add question')
->setValidationScope(FALSE)
->onClick[] = callback($this, 'credentialsFormAddQuestionClicked');
$form->addSubmit('submit', 'Submit');
$form->onSuccess[] = $this->processSurveyForm;
return $form;
}
/**
* Add new question
*
* @param \Nette\Forms\Controls\SubmitButton $button
*/
public function credentialsFormAddQuestionClicked(SubmitButton $button) {
$button->parent->createOne();
}
/**
* Removing questions
*
* @param \Nette\Forms\Controls\SubmitButton $button
*/
public function credentialsFormRemoveQuestionClicked(SubmitButton $button) {
$questions = $button->parent->parent;
$questions->remove($button->parent, TRUE);
}
public function processSurveyForm(Form $form) {
//removeQuestion a addQuestion spousteji processSurveyForm
if($form->submitted === $form['submit']) {
$values = $form->getValues();
if ($this->survey->getType() === 'template') {
$survey = new Survey();
$this->em->persist($survey);
}
else {
$survey = $this->survey;
}
$survey->setType('questionnaire');
$survey->setPositionName($values['positionName']);
$survey->setPositionDescription($values['positionDescription']);
$this->em->persist($survey);
$idsQuestions = array_map(function($array) {
return $array->id;
}, (array) $values->questions);
foreach ($survey->getQuestions() as $q) {
$survey->removeQuestion($q);
}
foreach ($values->questions as $questionInput) {
if (is_numeric($questionInput->id)) {
$question = $this->em->find(Question::class, $questionInput->id);
$question->setText($questionInput->text);
$question->setPoints($questionInput->points);
$tag = $this->tagRepo->findOneBy(array('name' => $questionInput->tag));
if (!$tag) {
$tag = new Tag();
$tag->setName($questionInput->tag);
$this->em->persist($tag);
}
$question->setTag($tag);
} else {
$question = new Question();
$question->setPoints($questionInput->points);
$question->setSurvey($survey);
$question->setText($questionInput->text);
$tag = $this->tagRepo->findOneBy(array('name' => $questionInput->tag));
if (!$tag) {
$tag = new Tag();
$tag->setName($questionInput->tag);
$this->em->persist($tag);
}
$question->setTag($tag);
$this->em->persist($question);
}
}
unset($values->questions);
$this->em->flush();
if ($this->survey->getType() === 'template') {
$this->presenter->redirect('questionnaire', $survey->getId());
} else {
$this->redirect('this');
}
}
}