From 924bd1ed1ad98017da45f306a583b1521eb1fc4f Mon Sep 17 00:00:00 2001
From: Olivier PEREZ <olivier@olivierperez.fr>
Date: Thu, 8 Jan 2015 00:27:40 +0100
Subject: [PATCH] Issue #26 Poll admin can change expiration date

---
 adminstuds.php                                | 10 +++++-
 app/classes/Framadate/FramaDB.php             |  4 +--
 .../Framadate/Services/AdminPollService.php   |  7 +++-
 css/style.css                                 |  7 ++--
 js/core.js                                    | 14 ++++++++
 tpl/part/poll_info.tpl                        | 32 +++++++++++++------
 6 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/adminstuds.php b/adminstuds.php
index b168345f..6d71f706 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -63,7 +63,7 @@ if (!$poll) {
 
 if (isset($_POST['update_poll_info'])) {
     $updated = false;
-    $field = $inputService->filterAllowedValues($_POST['update_poll_info'], ['title', 'admin_mail', 'comment', 'rules']);
+    $field = $inputService->filterAllowedValues($_POST['update_poll_info'], ['title', 'admin_mail', 'comment', 'rules', 'expiration_date']);
 
     // Update the right poll field
     if ($field == 'title') {
@@ -103,6 +103,13 @@ if (isset($_POST['update_poll_info'])) {
                 $updated = true;
                 break;
         }
+    } elseif ($field == 'expiration_date') {
+        $expiration_date = filter_input(INPUT_POST, 'expiration_date', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]+[-/][0-9]+[-/][0-9]+#']]);
+        $expiration_date = strtotime($expiration_date);
+        if ($expiration_date) {
+            $poll->end_date = $expiration_date;
+            $updated = true;
+        }
     }
 
     // Update poll in database
@@ -110,6 +117,7 @@ if (isset($_POST['update_poll_info'])) {
         $message = new Message('success', _('Poll saved.'));
     } else {
         $message = new Message('danger', _('Failed to save poll.'));
+        $poll = $pollService->findById($poll_id);
     }
 }
 
diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php
index c41a4398..b7a9f0e5 100644
--- a/app/classes/Framadate/FramaDB.php
+++ b/app/classes/Framadate/FramaDB.php
@@ -88,9 +88,9 @@ class FramaDB {
     }
 
     function updatePoll($poll) {
-        $prepared = $this->prepare('UPDATE ' . Utils::table('poll') . ' SET title=?, admin_mail=?, comment=?, active=?, editable=? WHERE id = ?');
+        $prepared = $this->prepare('UPDATE ' . Utils::table('poll') . ' SET title=?, admin_mail=?, description=?, end_date=FROM_UNIXTIME(?), active=?, editable=? WHERE id = ?');
 
-        return $prepared->execute([$poll->title, $poll->admin_mail, $poll->comment, $poll->active, $poll->editable, $poll->id]);
+        return $prepared->execute([$poll->title, $poll->admin_mail, $poll->description, $poll->end_date, $poll->active, $poll->editable, $poll->id]);
     }
 
     function allCommentsByPollId($poll_id) {
diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php
index 39263620..a346de2a 100644
--- a/app/classes/Framadate/Services/AdminPollService.php
+++ b/app/classes/Framadate/Services/AdminPollService.php
@@ -22,7 +22,12 @@ class AdminPollService {
     }
 
     function updatePoll($poll) {
-        return $this->connect->updatePoll($poll);
+        global $config;
+        if ($poll->end_date <= strtotime($poll->creation_date) + (86400 * $config['default_poll_duration'])) {
+            return $this->connect->updatePoll($poll);
+        } else {
+            return false;
+        }
     }
 
     /**
diff --git a/css/style.css b/css/style.css
index f3bb3073..3188c8dc 100644
--- a/css/style.css
+++ b/css/style.css
@@ -120,7 +120,8 @@ caption {
 #title-form h3 .btn-edit,
 #email-form .btn-edit,
 #description-form .btn-edit,
-#poll-rules-form .btn-edit {
+#poll-rules-form .btn-edit,
+#expiration-form .btn-edit {
     position:absolute;
     left:-2000px;
 }
@@ -132,7 +133,9 @@ caption {
 #description-form .btn-edit:focus,
 #description-form:hover .btn-edit,
 #poll-rules-form .btn-edit:focus,
-#poll-rules-form:hover .btn-edit {
+#poll-rules-form:hover .btn-edit,
+#expiration-form .btn-edit:focus,
+#expiration-form:hover .btn-edit {
     position:relative !important;
     left:0;
     padding: 0px 10px;
diff --git a/js/core.js b/js/core.js
index dd4a4aa0..9e1c4b41 100644
--- a/js/core.js
+++ b/js/core.js
@@ -364,6 +364,20 @@ $(document).ready(function() {
         return false;
     });
 
+    $('#expiration-form .btn-edit').on('click', function() {
+        $('#expiration-form p').hide();
+        $('.js-expiration').removeClass("hidden");
+        $('.js-expiration input').focus();
+        return false;
+    });
+
+    $('#expiration-form .btn-cancel').on('click', function() {
+        $('#expiration-form p').show();
+        $('#expiration-form .js-expiration').addClass("hidden");
+        $('#expiration-form .btn-edit').focus();
+        return false;
+    });
+
     // Horizontal scroll buttons
     if($('.results').width() > $('.container').width()) {
         $('.scroll-buttons').removeClass('hidden');
diff --git a/tpl/part/poll_info.tpl b/tpl/part/poll_info.tpl
index f74625b4..68abcfc6 100644
--- a/tpl/part/poll_info.tpl
+++ b/tpl/part/poll_info.tpl
@@ -12,8 +12,8 @@
                         <div class="input-group">
                             <input type="text" class="form-control" id="newtitle" name="title" size="40" value="{$poll->title}" />
                             <span class="input-group-btn">
-                                <button type="submit" class="btn btn-success" name="update_poll_info" value="title" title="'{_('Save the new title')}"><span class="glyphicon glyphicon-ok"></span><span class="sr-only">{_('Save')}</span></button>
-                                <button class="btn btn-link btn-cancel" title="#_('Cancel the title edit')}"><span class="glyphicon glyphicon-remove"></span><span class="sr-only">{_('Cancel')}</span></button>
+                                <button type="submit" class="btn btn-success" name="update_poll_info" value="title" title="{_('Save the new title')}"><span class="glyphicon glyphicon-ok"></span><span class="sr-only">{_('Save')}</span></button>
+                                <button class="btn btn-link btn-cancel" title="{_('Cancel the title edit')}"><span class="glyphicon glyphicon-remove"></span><span class="sr-only">{_('Cancel')}</span></button>
                             </span>
                         </div>
                     </div>
@@ -31,7 +31,7 @@
                             <li><button class="btn btn-link" type="submit" name="remove_all_votes">{_('Remove all the votes') }</button></li>
                             <li><button class="btn btn-link" type="submit" name="remove_all_comments">{_('Remove all the comments')}</button></li>
                             <li class="divider" role="presentation"></li>
-                            <li><button class="btn btn-link" type="submit" name="delete_poll">{_("Remove the poll")}</button></li>
+                            <li><button class="btn btn-link" type="submit" name="delete_poll">{_('Remove the poll')}</button></li>
                         </ul>
                     {/if}
                 </div>
@@ -62,7 +62,7 @@
                     <h4 class="control-label">{_("Description")}{if $admin}<button class="btn btn-link btn-sm btn-edit" title="{_('Edit the description')}"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">{_('Edit')}</span></button>{/if}</h4>
                     <p class="form-control-static well">{$poll->comment}</p>
                     <div class="hidden js-desc text-right">
-                        <label class="sr-only" for="newdescription">'._("Description") .'</label>
+                        <label class="sr-only" for="newdescription">{_("Description")}</label>
                         <textarea class="form-control" id="newdescription" name="comment" rows="2" cols="40">{$poll->comment}</textarea>
                         <button type="submit" id="btn-new-desc" name="update_poll_info" value="comment" class="btn btn-sm btn-success" title="{_("Save the description")}"><span class="glyphicon glyphicon-ok"></span><span class="sr-only">{_('Save')}</span></button>
                         <button class="btn btn-default btn-sm btn-cancel" title="{_('Cancel the description edit')}"><span class="glyphicon glyphicon-remove"></span><span class="sr-only">{_('Cancel')}</span></button>
@@ -72,24 +72,36 @@
         </div>
 
         <div class="row">
-            <div class="form-group form-group {if $admin}col-md-5{else}col-md-6{/if}">
+            <div class="form-group form-group {if $admin}col-md-4{else}col-md-6{/if}">
                 <label for="public-link"><a class="public-link" href="{$poll_id|poll_url}">{_("Public link of the poll")} <span class="btn-link glyphicon glyphicon-link"></span></a></label>
                 <input class="form-control" id="public-link" type="text" readonly="readonly" value="{$poll_id|poll_url}" />
             </div>
             {if $admin}
-                <div class="form-group col-md-5">
+                <div class="form-group col-md-4">
                     <label for="admin-link"><a class="admin-link" href="{$admin_poll_id|poll_url:true}">{_("Admin link of the poll")} <span class="btn-link glyphicon glyphicon-link"></span></a></label>
                     <input class="form-control" id="admin-link" type="text" readonly="readonly" value="{$admin_poll_id|poll_url:true}" />
                 </div>
-                <div class="form-group col-md-2">
+                <div id="expiration-form" class="form-group col-md-4">
                     <h4 class="control-label">{_("Expiration's date")}</h4>
-                    <p>{$poll->end_date|date_format:$date_format['txt_date']}</p>
+                    <p>{$poll->end_date|date_format:$date_format['txt_date']}{if $admin} <button class="btn btn-link btn-sm btn-edit" title="{_('Edit the email adress')}"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">{_('Edit')}</span></button>{/if}</p>
+                    {if $admin}
+                        <div class="hidden js-expiration">
+                            <label class="sr-only" for="newtitle">{_("Title")}</label>
+                            <div class="input-group">
+                                <input type="text" class="form-control" id="newtitle" name="expiration_date" size="40" value="{$poll->end_date|date_format:$date_format['txt_date']}" />
+                                <span class="input-group-btn">
+                                    <button type="submit" class="btn btn-success" name="update_poll_info" value="expiration_date" title="{_('Save the new expiration date')}"><span class="glyphicon glyphicon-ok"></span><span class="sr-only">{_('Save')}</span></button>
+                                    <button class="btn btn-link btn-cancel" title="{_('Cancel the expiration date edit')}"><span class="glyphicon glyphicon-remove"></span><span class="sr-only">{_('Cancel')}</span></button>
+                                </span>
+                            </div>
+                        </div>
+                    {/if}
                 </div>
             {/if}
         </div>
         {if $admin}
             <div class="row">
-                <div class="col-md-5 col-md-offset-7" >
+                <div class="col-md-4 col-md-offset-8" >
                     <div id="poll-rules-form">
                         {if $poll->active}
                             {if $poll->editable}
@@ -106,7 +118,7 @@
                             {$rule_icon = '<span class="glyphicon glyphicon-lock"></span>'}
                             {$rule_txt = _('Votes and comments are locked')}
                         {/if}
-                        <p class="pull-right">{$rule_icon} {$rule_txt}<button class="btn btn-link btn-sm btn-edit" title="{_('Edit the poll rules')}"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">{_('Edit')}</span></button></p>
+                        <p class="">{$rule_icon} {$rule_txt}<button class="btn btn-link btn-sm btn-edit" title="{_('Edit the poll rules')}"><span class="glyphicon glyphicon-pencil"></span><span class="sr-only">{_('Edit')}</span></button></p>
                         <div class="hidden js-poll-rules">
                             <label class="sr-only" for="rules">{_("Poll rules")}</label>
                             <div class="input-group">
-- 
GitLab