Add poll 3

Fix manager, model, load.
Update Edit page.
This commit is contained in:
Visman 2020-11-19 21:49:08 +07:00
parent 49204fa185
commit 79aa61e738
5 changed files with 87 additions and 3 deletions

View file

@ -7,6 +7,7 @@ namespace ForkBB\Models\Pages;
use ForkBB\Core\Validator;
use ForkBB\Models\Page;
use ForkBB\Models\Post\Model as Post;
use ForkBB\Models\Topic\Model as Topic;
use function \ForkBB\__;
class Edit extends Page
@ -64,6 +65,23 @@ class Edit extends Page
'stick_fp' => $topic->stick_fp,
'edit_post' => $post->edit_post,
];
if (
$editSubject
&& '1' == $this->c->config->b_poll_enabled
) {
$poll = $topic->poll;
$args['_vars'] += [
'poll_enable' => $topic->poll_type > 0,
'poll' => [
'hide_result' => $topic->poll_term > 0,
'question' => $poll->question,
'type' => $poll->type,
'answer' => $poll->answer,
],
];
}
}
$this->nameTpl = 'post';
@ -140,6 +158,10 @@ class Edit extends Page
) {
$topic->stick_fp = $v->stick_fp ? 1 : 0;
}
// опрос
if ('1' == $this->c->config->b_poll_enabled) {
$this->changePoll($topic, $v);
}
}
// обновление сообщения
@ -170,4 +192,53 @@ class Edit extends Page
return $this->c->Redirect->page('ViewPost', ['id' => $post->id])->message('Edit redirect');
}
/**
* Изменяет(удаляет/добавляет) данные опроса
*/
protected function changePoll(Topic $topic, Validator $v)
{
if ($topic->poll_type > 0 ) {
$poll = $topic->poll;
// редактирование
if ($v->poll_enable) {
# $topic->poll_type = 0;
# $topic->poll_time = 0;
$topic->poll_term = $v->poll['hide_result']
? ($topic->poll_term ?: $this->c->config->i_poll_term)
: 0;
# $topic->poll_votes = 0;
$poll->__question = $v->poll['question'];
$poll->__answer = $v->poll['answer'];
$poll->__type = $v->poll['type'];
$this->c->polls->update($poll);
// удаление
} else {
$topic->poll_type = 0;
$topic->poll_time = 0;
$topic->poll_term = 0;
$topic->poll_votes = 0;
$this->c->polls->delete($poll);
}
// добавление
} elseif ($v->poll_enable) {
$topic->poll_type = 1;
$topic->poll_time = \time();
$topic->poll_term = $v->poll['hide_result'] ? $this->c->config->i_poll_term : 0;
$topic->poll_votes = 0;
$poll = $this->c->polls->create([
'tid' => $topic->id,
'question' => $v->poll['question'],
'answer' => $v->poll['answer'],
'type' => $v->poll['type'],
]);
$this->c->polls->insert($poll);
}
}
}

View file

@ -191,6 +191,10 @@ trait PostFormTrait
$editSubject
&& '1' == $this->c->config->b_poll_enabled
) {
$term = $editPost && $model->parent->poll_term
? $model->parent->poll_term
: $this->c->config->i_poll_term;
$fieldset = [];
$fieldset['poll_enable'] = [
@ -201,7 +205,7 @@ trait PostFormTrait
];
$fieldset['poll[hide_result]'] = [
'type' => 'checkbox',
'label' => __('Hide poll results up to %s voters', $this->c->config->i_poll_term), // ???? при редактировании взять данные из топика?
'label' => __('Hide poll results up to %s voters', $term),
'value' => '1',
'checked' => (bool) ($vars['poll']['hide_result'] ?? false),
];

View file

@ -31,7 +31,7 @@ class Load extends Action
$i = 0;
$data = [
'id' => $id,
'tid' => $id,
'question' => [],
'answer' => [],
'vote' => [],

View file

@ -53,7 +53,7 @@ class Manager extends ManagerModel
$poll = $this->Save->update($poll);
if (true === $poll->itWasModified) {
$this->reset($poll->id);
$this->reset($poll->tid);
}
return $poll;

View file

@ -7,6 +7,7 @@ namespace ForkBB\Models\Topic;
use ForkBB\Core\Container;
use ForkBB\Models\DataModel;
use ForkBB\Models\Forum\Model as Forum;
use ForkBB\Models\Poll\Model as Poll;
use PDO;
use RuntimeException;
@ -470,4 +471,12 @@ class Model extends DataModel
$this->c->DB->exec($query, $vars);
}
}
/**
* Возвращает опрос при его наличии
*/
protected function getpoll(): ?Poll
{
return $this->poll_type > 0 ? $this->c->polls->load($this->id) : null;
}
}