Commit 758b8a7d authored by Tino Goratsch's avatar Tino Goratsch

merged the create and edit templates of the polls module

parent 242faf4b
......@@ -57,7 +57,7 @@ class Index extends Core\Modules\AdminController
}
if ($this->request->getPost()->has('add_answer')) {
$answers = $this->addNewAnswer();
$answers = $this->addNewAnswer($this->request->getPost()->get('answers', []));
} else {
$answers = [
['number' => 0, 'value' => ''],
......@@ -73,7 +73,10 @@ class Index extends Core\Modules\AdminController
$this->view->assign('form', array_merge($defaults, $this->request->getPost()->getAll()));
$this->view->assign('answers', $answers);
$this->view->assign('multiple', $this->get('core.helpers.forms')->selectEntry('multiple', '1', '0', 'checked'));
$options = [];
$options[] = $this->fetchMultipleChoiceOption(0);
$this->view->assign('options', $options);
$this->formTokenHelper->generateFormToken();
}
......@@ -117,9 +120,8 @@ class Index extends Core\Modules\AdminController
$this->_editPost($this->request->getPost()->getAll(), $id);
}
// Neue Antworten hinzufügen
if ($this->request->getPost()->has('add_answer')) {
$answers = $this->addNewAnswer();
$answers = $this->addNewAnswer($this->request->getPost()->get('answers', []));
} else {
$answers = $this->pollsModel->getAnswersByPollId($id);
$c_answers = count($answers);
......@@ -131,13 +133,15 @@ class Index extends Core\Modules\AdminController
}
$this->view->assign('answers', $answers);
$options = [];
$options[0]['name'] = 'reset';
$options[0]['checked'] = $this->get('core.helpers.forms')->selectEntry('reset', '1', '0', 'checked');
$options[0]['lang'] = $this->lang->t('polls', 'reset_votes');
$options[1]['name'] = 'multiple';
$options[1]['checked'] = $this->get('core.helpers.forms')->selectEntry('multiple', '1', $poll['multiple'], 'checked');
$options[1]['lang'] = $this->lang->t('polls', 'multiple_choice');
$options = [
$this->fetchMultipleChoiceOption($poll['multiple']),
[
'name' => 'reset',
'checked' => $this->get('core.helpers.forms')->selectEntry('reset', '1', '0', 'checked'),
'lang' => $this->lang->t('polls', 'reset_votes')
]
];
$this->view->assign('options', $options);
$this->view->assign('form', array_merge($poll, $this->request->getPost()->getAll()));
......@@ -232,26 +236,28 @@ class Index extends Core\Modules\AdminController
}
/**
* @param array $currentAnswers
*
* @return array
*/
private function addNewAnswer()
private function addNewAnswer(array $currentAnswers)
{
$answers = [];
// Bisherige Antworten
$i = 0;
$answersPost = $this->request->getPost()->get('answers', []);
foreach ($answersPost as $row) {
foreach ($currentAnswers as $row) {
$answers[$i]['number'] = $i;
$answers[$i]['id'] = isset($row['id']) ? $row['id'] : 0;
if (isset($row['id'])) {
$answers[$i]['id'] = $row['id'];
}
$answers[$i]['value'] = $row['value'];
++$i;
}
// Neue Antwort nur hinzufügen, wenn die vorangegangene nicht leer ist
if (!empty($answersPost[$i - 1]['value'])) {
if (!empty($currentAnswers[$i - 1]['value'])) {
$answers[$i]['number'] = $i;
$answers[$i]['id'] = 0;
$answers[$i]['value'] = '';
}
......@@ -277,13 +283,13 @@ class Index extends Core\Modules\AdminController
Polls\Model::TABLE_NAME_ANSWERS
);
}
} elseif (isset($row['delete']) && $this->get('core.validator.rules.misc')->isNumber($row['id'])) { // Antwort mitsamt Stimmen löschen
$this->pollsModel->delete($row['id'], '', Polls\Model::TABLE_NAME_ANSWERS);
$this->pollsModel->delete($row['id'], 'answer_id', Polls\Model::TABLE_NAME_VOTES);
} elseif (!empty($row['value']) && $this->get('core.validator.rules.misc')->isNumber($row['id'])) { // Antwort aktualisieren
} elseif (isset($row['delete'])) { // Antwort mitsamt Stimmen löschen
$this->pollsModel->delete((int) $row['id'], '', Polls\Model::TABLE_NAME_ANSWERS);
$this->pollsModel->delete((int) $row['id'], 'answer_id', Polls\Model::TABLE_NAME_VOTES);
} elseif (!empty($row['value'])) { // Antwort aktualisieren
$bool = $this->pollsModel->update(
['text' => Core\Functions::strEncode($row['value'])],
$row['id'],
(int) $row['id'],
Polls\Model::TABLE_NAME_ANSWERS
);
}
......@@ -291,4 +297,18 @@ class Index extends Core\Modules\AdminController
return $bool;
}
/**
* @param string $currentValue
*
* @return array
*/
private function fetchMultipleChoiceOption($currentValue)
{
return [
'name' => 'multiple',
'checked' => $this->get('core.helpers.forms')->selectEntry('multiple', '1', $currentValue, 'checked'),
'lang' => $this->lang->t('polls', 'multiple_choice')
];
}
}
......@@ -27,20 +27,32 @@
<label for="answer-{$row.number}" class="col-sm-2 control-label">{lang t="polls|answer"} {$row.number+1}</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="answers[][value]" id="answer-{$row.number}" value="{$row.value}" maxlength="120">
{if isset($row.id)}
<div class="input-group">
<input class="form-control" type="text" name="answers[{$row.number}][value]" id="answer-{$row.number}" value="{$row.value}" maxlength="120">
<div class="input-group-addon">
<input type="checkbox" name="answers[{$row.number}][delete]" value="1">
</div>
</div>
<input type="hidden" name="answers[{$row.number}][id]" value="{$row.id}" style="display:none">
{else}
<input class="form-control" type="text" name="answers[{$row.number}][value]" id="answer-{$row.number}" value="{$row.value}" maxlength="120">
{/if}
</div>
</div>
{/foreach}
<div class="form-group">
<label for="multiple" class="col-sm-2 control-label">{lang t="system|options"}</label>
<label for="{$options.0.name}" class="col-sm-2 control-label">{lang t="system|options"}</label>
<div class="col-sm-10">
<div class="checkbox">
<label for="multiple">
<input type="checkbox" name="multiple" id="multiple" value="1"{$multiple}>
{lang t="polls|multiple_choice"}
</label>
</div>
{foreach $options as $row}
<div class="checkbox">
<label for="{$row.name}">
<input type="checkbox" name="{$row.name}" id="{$row.name}" value="1"{$row.checked}>
{$row.lang}
</label>
</div>
{/foreach}
</div>
</div>
</div>
......
{extends file="asset:layout.tpl"}
{block CONTENT}
{if isset($error_msg)}
{$error_msg}
{/if}
<form action="{$REQUEST_URI}" method="post" accept-charset="UTF-8" class="form-horizontal">
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active"><a href="#tab-1" data-toggle="tab">{lang t="system|publication_period"}</a></li>
<li><a href="#tab-2" data-toggle="tab">{lang t="polls|poll"}</a></li>
</ul>
<div class="tab-content">
<div id="tab-1" class="tab-pane fade in active">
{datepicker name=['start', 'end'] value=[$form.start, $form.end]}
</div>
<div id="tab-2" class="tab-pane fade">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">{lang t="polls|question"}</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="title" id="title" value="{$form.title}" maxlength="120">
</div>
</div>
{foreach $answers as $row}
<div class="form-group">
<label for="answer-{$row.number}" class="col-sm-2 control-label">{lang t="polls|answer"} {$row.number+1}</label>
<div class="col-sm-10">
<div class="input-group">
<input class="form-control" type="text" name="answers[{$row.number}][value]" id="answer-{$row.number}" value="{$row.value}" maxlength="120">
<div class="input-group-addon">
<input type="checkbox" name="answers[{$row.number}][delete]" value="1">
</div>
</div>
<input type="hidden" name="answers[{$row.number}][id]" value="{$row.id}" style="display:none">
</div>
</div>
{/foreach}
<div class="form-group">
<label for="{$options.0.name}" class="col-sm-2 control-label">{lang t="system|options"}</label>
<div class="col-sm-10">
{foreach $options as $row}
<div class="checkbox">
<label for="{$row.name}">
<input type="checkbox" name="{$row.name}" id="{$row.name}" value="1"{$row.checked}>
{$row.lang}
</label>
</div>
{/foreach}
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="add_answer" class="btn btn-primary">{lang t="polls|add_answer"}</button>
<button type="submit" name="submit" class="btn btn-primary">{lang t="system|submit"}</button>
<a href="{uri args="acp/polls"}" class="btn btn-default">{lang t="system|cancel"}</a>
{$form_token}
</div>
</div>
</form>
{/block}
\ No newline at end of file
{extends file="asset:Polls/Admin/index.create.tpl"}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment