From 8e15a008fea4fc2a324d7f4635325cb1c82906e8 Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Thu, 18 Dec 2014 13:57:25 +0100 Subject: [PATCH] adminstuds.php: Realize the update of poll in database --- adminstuds.php | 59 ++++++++++++++++++- app/classes/Framadate/FramaDB.php | 6 ++ .../Framadate/Services/InputService.php | 6 +- .../Framadate/Services/PollService.php | 4 ++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/adminstuds.php b/adminstuds.php index 11eb8eb..f0bc76c 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -40,8 +40,8 @@ $inputService = new InputService(); /* PAGE */ /* ---- */ -if(!empty($_GET['poll']) && strlen($_GET['poll']) === 24) { - $admin_poll_id = filter_input(INPUT_GET, 'poll', FILTER_VALIDATE_REGEXP, ['options'=>['regexp'=>'/^[a-z0-9]+$/']]); +if (!empty($_GET['poll']) && strlen($_GET['poll']) === 24) { + $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 = $pollService->findById($poll_id); } @@ -52,6 +52,61 @@ if (!$poll) { 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 $slots = $pollService->allSlotsByPollId($poll_id); $votes = $pollService->allUserVotesByPollId($poll_id); diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php index fcaa4a5..8978772 100644 --- a/app/classes/Framadate/FramaDB.php +++ b/app/classes/Framadate/FramaDB.php @@ -65,6 +65,12 @@ class FramaDB 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) { $prepared = $this->prepare('SELECT * FROM comments WHERE id_sondage = ? ORDER BY id_comment'); $prepared->execute(array($poll_id)); diff --git a/app/classes/Framadate/Services/InputService.php b/app/classes/Framadate/Services/InputService.php index d48f4f0..d985bb5 100644 --- a/app/classes/Framadate/Services/InputService.php +++ b/app/classes/Framadate/Services/InputService.php @@ -29,7 +29,7 @@ class InputService { * 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. */ - function filterArray($arr, $type, $options) { + function filterArray(array $arr, $type, $options) { $newArr = []; foreach($arr as $id=>$item) { @@ -42,4 +42,8 @@ class InputService { return $newArr; } + function filterAllowedValues($value, array $allowedValues) { + return in_array($value, $allowedValues, true) ? $value : null; + } + } \ No newline at end of file diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index 33959f0..9256b27 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -34,6 +34,10 @@ class PollService { return null; } + function updatePoll($poll) { + return $this->connect->updatePoll($poll); + } + function allCommentsByPollId($poll_id) { return $this->connect->allCommentsByPollId($poll_id); }