From 2495a8002fa194e45d6fd686cbd5fdba36fc13da Mon Sep 17 00:00:00 2001
From: Olivier PEREZ <olivier@olivierperez.fr>
Date: Wed, 7 Jan 2015 22:47:34 +0100
Subject: [PATCH] Admin: The polls page now use Smarty template

---
 admin/polls.php                               | 100 ++++--------------
 adminstuds.php                                |   1 +
 app/classes/Framadate/Services/LogService.php |   2 +-
 tpl/admin/polls.tpl                           |  63 +++++++++++
 4 files changed, 88 insertions(+), 78 deletions(-)
 create mode 100644 tpl/admin/polls.tpl

diff --git a/admin/polls.php b/admin/polls.php
index f1cc487a..ac5780ad 100644
--- a/admin/polls.php
+++ b/admin/polls.php
@@ -26,93 +26,39 @@ use Framadate\Utils;
 include_once __DIR__ . '/../app/inc/init.php';
 include_once __DIR__ . '/../bandeaux.php';
 
+/* Variables */
+/* --------- */
+
+$polls = null;
+$poll_to_delete = null;
+
 /* Services */
 /*----------*/
+
 $logService = new LogService();
 $pollService = new PollService($connect, $logService);
 $adminPollService = new AdminPollService($connect, $pollService, $logService);
 $superAdminService = new SuperAdminService($connect);
 
-// Ce fichier index.php se trouve dans le sous-repertoire ADMIN de Studs. Il sert à afficher l'intranet de studs
-// pour modifier les sondages directement sans avoir reçu les mails. C'est l'interface d'aministration
-// de l'application.
-
-// Affichage des balises standards
-Utils::print_header(_('Polls administrator'));
-bandeau_titre(_('Polls administrator'));
-
-$polls = $superAdminService->findAllPolls();
-
-echo '<form action="' . Utils::get_server_name() . 'admin/index.php" method="POST">' . "\n";
-
-// Test et affichage du bouton de confirmation en cas de suppression de sondage
-foreach ($polls as $poll) {
-    if (!empty($_POST['supprimersondage' . $poll->id])) {
-        echo '
-        <div class="alert alert-warning text-center">
-            <h3>' . _("Confirm removal of the poll ") . '"' . $poll->id . '</h3>
-            <p><button class="btn btn-default" type="submit" value="1" name="annullesuppression">' . _('Keep this poll!') . '</button>
-            <button type="submit" name="confirmesuppression' . $poll->id . '" value="1" class="btn btn-danger">' . _('Remove this poll!') . '</button></p>
-        </div>';
-    }
+/* PAGE */
+/* ---- */
 
-    // Traitement de la confirmation de suppression
-    if (!empty($_POST['confirmesuppression' . $poll->id])) {
-        // On inclut la routine de suppression
-        $date = date('H:i:s d/m/Y');
-
-        $adminPollService->deleteEntirePoll($poll->id);
-    }
+if (!empty($_POST['delete_poll'])) {
+    $delete_id = filter_input(INPUT_POST, 'delete_poll', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^[a-z0-9]+$/']]);
+    $poll_to_delete = $pollService->findById($delete_id);
 }
 
-$btn_logs = (is_readable('../' . LOG_FILE)) ? '<a role="button" class="btn btn-default btn-xs pull-right" href="' . Utils::get_server_name() . LOG_FILE . '">' . _("Logs") . '</a>' : '';
-
-echo '<p>' . count($polls) . ' ' . _("polls in the database at this time") . $btn_logs . '</p>' . "\n";
-
-// tableau qui affiche tous les sondages de la base
-echo '<table class="table table-bordered">
-    <tr align="center">
-        <th scope="col">' . _('Poll ID') . '</th>
-        <th scope="col">' . _('Format') . '</th>
-        <th scope="col">' . _('Title') . '</th>
-        <th scope="col">' . _('Author') . '</th>
-        <th scope="col">' . _('Email') . '</th>
-        <th scope="col">' . _('Expiration\'s date') . '</th>
-        <th scope="col">' . _('Users') . '</th>
-        <th scope="col" colspan="3">' . _('Actions') . '</th>
-    </tr>' . "\n";
-
-$i = 0;
-foreach ($polls as $poll) {
-    $nb_users = $pollService->countVotesByPollId($poll->id);
-
-    if ($poll->format === 'D') {
-        $format_html = '<span class="glyphicon glyphicon-calendar" aria-hidden="true"></span><span class="sr-only">'. _('Date').'</span>';
-    } else {
-        $format_html = '<span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span><span class="sr-only">'. _('Classic').'</span>';
-    }
-    echo '
-    <tr align="center">
-        <td>' . $poll->id . '</td>
-        <td>' . $format_html . '</td>
-        <td>' . htmlentities($poll->title) . '</td>
-        <td>' . htmlentities($poll->admin_name) . '</td>
-        <td>' . htmlentities($poll->admin_mail) . '</td>';
-
-    if (strtotime($poll->end_date) > time()) {
-        echo '<td>' . date('d/m/y', strtotime($poll->end_date)) . '</td>';
-    } else {
-        echo '<td><span class="text-danger">' . date('d/m/y', strtotime($poll->end_date)) . '</span></td>';
-    }
-    echo '
-        <td>' . $nb_users . '</td>
-        <td><a href="' . Utils::getUrlSondage($poll->id) . '" class="btn btn-link" title="' . _('See the poll') . '"><span class="glyphicon glyphicon-eye-open"></span><span class="sr-only">' . _('See the poll') . '</span></a></td>
-        <td><a href="' . Utils::getUrlSondage($poll->admin_id, true) . '" class="btn btn-link" title="' . _('Change the poll') . '"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">' . _("Change the poll") . '</span></a></td>
-        <td><button type="submit" name="supprimersondage' . $poll->id . '" value="' . _('Remove the poll') . '" class="btn btn-link" title="' . _("Remove the poll") . '"><span class="glyphicon glyphicon-trash text-danger"></span><span class="sr-only">' . _('Remove the poll') . '</span></td>
-    </tr>' . "\n";
-    ++$i;
+// Traitement de la confirmation de suppression
+if (!empty($_POST['delete_confirm'])) {
+    $poll_id = filter_input(INPUT_POST, 'delete_confirm', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^[a-z0-9]+$/']]);
+    $adminPollService->deleteEntirePoll($poll_id);
 }
 
-echo '</table></form>' . "\n";
+$polls = $superAdminService->findAllPolls();
+
+// Assign data to template
+$smarty->assign('polls', $polls);
+$smarty->assign('poll_to_delete', $poll_to_delete);
+$smarty->assign('log_file', is_readable('../' . LOG_FILE) ? LOG_FILE : null);
 
-bandeau_pied(true);
+$smarty->display('admin/polls.tpl');
diff --git a/adminstuds.php b/adminstuds.php
index 0ab35633..8de758c3 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -27,6 +27,7 @@ include_once __DIR__ . '/app/inc/init.php';
 
 /* Variables */
 /* --------- */
+
 $admin_poll_id = null;
 $poll_id = null;
 $poll = null;
diff --git a/app/classes/Framadate/Services/LogService.php b/app/classes/Framadate/Services/LogService.php
index 8ccb48f9..4f684631 100644
--- a/app/classes/Framadate/Services/LogService.php
+++ b/app/classes/Framadate/Services/LogService.php
@@ -18,7 +18,7 @@ class LogService {
      * @param $message string some message
      */
     function log($tag, $message) {
-        error_log(date('Ymd His') . ' [' . $tag . '] ' . $message . "\n", 3, LOG_FILE);
+        error_log(date('Ymd His') . ' [' . $tag . '] ' . $message . "\n", 3, ROOT_DIR . LOG_FILE);
     }
 
 }
diff --git a/tpl/admin/polls.tpl b/tpl/admin/polls.tpl
new file mode 100644
index 00000000..c594caaa
--- /dev/null
+++ b/tpl/admin/polls.tpl
@@ -0,0 +1,63 @@
+{extends 'admin/admin_page.tpl'}
+
+{block 'admin_main'}
+    <form action="" method="POST">
+        {if $poll_to_delete}
+            <div class="alert alert-warning text-center">
+                <h3>{_("Confirm removal of the poll ")}"{$poll_to_delete->id}"</h3>
+
+                <p>
+                    <button class="btn btn-default" type="submit" value="1"
+                            name="annullesuppression">{_('Keep this poll!')}</button>
+                    <button type="submit" name="delete_confirm" value="{$poll_to_delete->id}"
+                            class="btn btn-danger">{_('Remove this poll!')}</button>
+                </p>
+            </div>
+        {/if}
+
+        <p>
+            {$polls|count} {_('polls in the database at this time')}
+            {if $log_file}
+                <a role="button" class="btn btn-default btn-xs pull-right" href="{$log_file|resource}">{_('Logs')}</a>
+            {/if}
+        </p>
+
+        <table class="table table-bordered">
+            <tr align="center">
+                <th scope="col">{_('Poll ID')}</th>
+                <th scope="col">{_('Format')}</th>
+                <th scope="col">{_('Title')}</th>
+                <th scope="col">{_('Author')}</th>
+                <th scope="col">{_('Email')}</th>
+                <th scope="col">{_('Expiration\'s date')}</th>
+                <th scope="col">{_('Users')}</th>
+                <th scope="col" colspan="3">{_('Actions')}</th>
+            </tr>
+            {foreach $polls as $poll}
+                <tr align="center">
+                    <td>{$poll->id}</td>
+                    <td>
+                        {if $poll->format === 'D'}
+                        <span class="glyphicon glyphicon-calendar" aria-hidden="true"></span><span class="sr-only">{ _('Date')}</span>
+                        {else}
+                        <span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span><span class="sr-only">{_('Classic')}</span>
+                        {/if}
+                    </td>
+                    <td>{htmlentities($poll->title)}</td>
+                    <td>{htmlentities($poll->admin_name)}</td>
+                    <td>{htmlentities($poll->admin_mail)}</td>
+
+                    {if strtotime($poll->end_date) > time()}
+                    <td>{date('d/m/y', strtotime($poll->end_date))}</td>
+                    {else}
+                    <td><span class="text-danger">{strtotime($poll->end_date)|date_format:'d/m/Y'}</span></td>
+                    {/if}
+                    <td>TODO</td>
+                    <td><a href="{$poll->id|poll_url}" class="btn btn-link" title="{_('See the poll')}"><span class="glyphicon glyphicon-eye-open"></span><span class="sr-only">{_('See the poll')}</span></a></td>
+                    <td><a href="{$poll->admin_id|poll_url:true}" class="btn btn-link" title="{_('Change the poll')}"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">{_('Change the poll')}</span></a></td>
+                    <td><button type="submit" name="delete_poll" value="{$poll->id}" class="btn btn-link" title="{_('Remove the poll')}"><span class="glyphicon glyphicon-trash text-danger"></span><span class="sr-only">{_('Remove the poll')}</span></td>
+                </tr>
+            {/foreach}
+        </table>
+    </form>
+{/block}
\ No newline at end of file
-- 
GitLab