adminstuds.php: Realize the update of poll in database

This commit is contained in:
Olivier PEREZ 2014-12-18 13:57:25 +01:00
parent 716efd6d58
commit 8e15a008fe
4 changed files with 72 additions and 3 deletions

View File

@ -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);

View File

@ -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));

View File

@ -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;
}
}

View File

@ -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);
}