adminstuds.php: Realize the update of poll in database
This commit is contained in:
parent
716efd6d58
commit
8e15a008fe
|
@ -40,8 +40,8 @@ $inputService = new InputService();
|
||||||
/* PAGE */
|
/* PAGE */
|
||||||
/* ---- */
|
/* ---- */
|
||||||
|
|
||||||
if(!empty($_GET['poll']) && strlen($_GET['poll']) === 24) {
|
if (!empty($_GET['poll']) && strlen($_GET['poll']) === 24) {
|
||||||
$admin_poll_id = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_REGEXP, ['options'=>['regexp'=>'/^[a-z0-9]+$/']]);
|
$admin_poll_id = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^[a-z0-9]+$/']]);
|
||||||
$poll_id = substr($admin_poll_id, 0, 16);
|
$poll_id = substr($admin_poll_id, 0, 16);
|
||||||
$poll = $pollService->findById($poll_id);
|
$poll = $pollService->findById($poll_id);
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,61 @@ if (!$poll) {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------
|
||||||
|
// Update poll info
|
||||||
|
// -------------------------------
|
||||||
|
if (isset($_POST['update_poll_info'])) {
|
||||||
|
$updated = false;
|
||||||
|
$field = $inputService->filterAllowedValues($_POST['update_poll_info'], ['title', 'admin_mail', 'comment', 'rules']);
|
||||||
|
|
||||||
|
// Update the right poll field
|
||||||
|
if ($field == 'title') {
|
||||||
|
$title = $filter_input(INPUT_POST, 'title', FILTER_DEFAULT);
|
||||||
|
if ($title) {
|
||||||
|
$poll->title = $title;
|
||||||
|
$updated = true;
|
||||||
|
}
|
||||||
|
} elseif ($field == 'admin_mail') {
|
||||||
|
$admin_mail = filter_input(INPUT_POST, 'admin_mail', FILTER_VALIDATE_EMAIL);
|
||||||
|
if ($admin_mail) {
|
||||||
|
$poll->admin_mail = $admin_mail;
|
||||||
|
$updated = true;
|
||||||
|
}
|
||||||
|
} elseif ($field == 'comment') {
|
||||||
|
$comment = filter_input(INPUT_POST, 'comment', FILTER_DEFAULT);
|
||||||
|
if ($comment) {
|
||||||
|
$poll->comment = $comment;
|
||||||
|
$updated = true;
|
||||||
|
}
|
||||||
|
} elseif ($field == 'rules') {
|
||||||
|
$rules = filter_input(INPUT_POST, 'rules', FILTER_DEFAULT);
|
||||||
|
switch ($rules) {
|
||||||
|
case 0:
|
||||||
|
$poll->active = false;
|
||||||
|
$poll->editable = false;
|
||||||
|
$updated = true;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
$poll->active = true;
|
||||||
|
$poll->editable = false;
|
||||||
|
$updated = true;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
$poll->active = true;
|
||||||
|
$poll->editable = true;
|
||||||
|
$updated = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update poll in database
|
||||||
|
if ($updated && $pollService->updatePoll($poll)) {
|
||||||
|
$message = new Message('success', _('Poll saved.'));
|
||||||
|
} else {
|
||||||
|
$message = new Message('danger', _('Failed to save poll.'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve data
|
// Retrieve data
|
||||||
$slots = $pollService->allSlotsByPollId($poll_id);
|
$slots = $pollService->allSlotsByPollId($poll_id);
|
||||||
$votes = $pollService->allUserVotesByPollId($poll_id);
|
$votes = $pollService->allUserVotesByPollId($poll_id);
|
||||||
|
|
|
@ -65,6 +65,12 @@ class FramaDB
|
||||||
return $poll;
|
return $poll;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updatePoll($poll) {
|
||||||
|
$prepared = $this->prepare('UPDATE sondage SET title=?, admin_mail=?, comment=?, active=?, editable=? WHERE sondage.poll_id = ?');
|
||||||
|
|
||||||
|
return $prepared->execute([$poll->title, $poll->admin_mail, $poll->comment, $poll->active, $poll->editable, $poll->poll_id]);
|
||||||
|
}
|
||||||
|
|
||||||
function allCommentsByPollId($poll_id) {
|
function allCommentsByPollId($poll_id) {
|
||||||
$prepared = $this->prepare('SELECT * FROM comments WHERE id_sondage = ? ORDER BY id_comment');
|
$prepared = $this->prepare('SELECT * FROM comments WHERE id_sondage = ? ORDER BY id_comment');
|
||||||
$prepared->execute(array($poll_id));
|
$prepared->execute(array($poll_id));
|
||||||
|
|
|
@ -29,7 +29,7 @@ class InputService {
|
||||||
* This method filter an array calling "filter_var" on each items.
|
* This method filter an array calling "filter_var" on each items.
|
||||||
* Only items validated are added at their own indexes, the others are not returned.
|
* Only items validated are added at their own indexes, the others are not returned.
|
||||||
*/
|
*/
|
||||||
function filterArray($arr, $type, $options) {
|
function filterArray(array $arr, $type, $options) {
|
||||||
$newArr = [];
|
$newArr = [];
|
||||||
|
|
||||||
foreach($arr as $id=>$item) {
|
foreach($arr as $id=>$item) {
|
||||||
|
@ -42,4 +42,8 @@ class InputService {
|
||||||
return $newArr;
|
return $newArr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function filterAllowedValues($value, array $allowedValues) {
|
||||||
|
return in_array($value, $allowedValues, true) ? $value : null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -34,6 +34,10 @@ class PollService {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updatePoll($poll) {
|
||||||
|
return $this->connect->updatePoll($poll);
|
||||||
|
}
|
||||||
|
|
||||||
function allCommentsByPollId($poll_id) {
|
function allCommentsByPollId($poll_id) {
|
||||||
return $this->connect->allCommentsByPollId($poll_id);
|
return $this->connect->allCommentsByPollId($poll_id);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user