From 94e87a318281484ad70ca9bdf3ab61877b4d5a8a Mon Sep 17 00:00:00 2001
From: "Olivier Perez [a570709]" <olivier.perez@worldline.com>
Date: Mon, 22 Dec 2014 14:18:33 +0100
Subject: [PATCH] WIP> admin: Add availability to add a slot to a poll

---
 adminstuds.php                                | 10 ++++++
 app/classes/Framadate/FramaDB.php             | 31 ++++++++++++++++++-
 .../Framadate/Services/AdminPollService.php   | 25 +++++++++++++++
 tpl/add_slot.tpl                              |  7 ++---
 4 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/adminstuds.php b/adminstuds.php
index 66100cd0..948499ec 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -291,6 +291,16 @@ if (isset($_POST['add_slot'])) {
     $smarty->display('add_slot.tpl');
     exit;
 }
+if (isset($_POST['confirm_add_slot'])) {
+    $newdate = filter_input(INPUT_POST, 'newdate', FILTER_DEFAULT);
+    $newmoment = filter_input(INPUT_POST, 'newmoment', FILTER_DEFAULT);
+
+    if ($adminPollService->addSlot($poll_id, $newdate, $newmoment)) {
+        $message = new Message('success', _('Column added.'));
+    } else {
+        $message = new Message('danger', _('Failed to add the column.'));
+    }
+}
 
 // Retrieve data
 $slots = $pollService->allSlotsByPollId($poll_id);
diff --git a/app/classes/Framadate/FramaDB.php b/app/classes/Framadate/FramaDB.php
index 0bbb0360..2e03d577 100644
--- a/app/classes/Framadate/FramaDB.php
+++ b/app/classes/Framadate/FramaDB.php
@@ -33,7 +33,7 @@ class FramaDB {
     function areTablesCreated() {
         $result = $this->pdo->query('SHOW TABLES');
         $schemas = $result->fetchAll(\PDO::FETCH_COLUMN);
-        return !empty(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs']));
+        return 0 != count(array_diff($schemas, ['comments', 'sondage', 'sujet_studs', 'user_studs']));
     }
 
     function prepare($sql) {
@@ -127,6 +127,35 @@ class FramaDB {
         return $prepared->execute([$index, $index + 2, $poll_id]);
     }
 
+    /**
+     * Find the slot into poll for a given datetime.
+     *
+     * @param $poll_id int The ID of the poll
+     * @param $datetime int The datetime of the slot
+     * @return mixed Object The slot found, or null
+     */
+    function findSlotByPollIdAndDatetime($poll_id, $datetime) {
+        $prepared = $this->prepare('SELECT * FROM sujet_studs WHERE id_sondage = ? AND SUBSTRING_INDEX(sujet, \'@\', 1) = ?');
+
+        $prepared->execute([$poll_id, $datetime]);
+        $slot = $prepared->fetch();
+        $prepared->closeCursor();
+
+        return $slot;
+    }
+
+    /**
+     * Insert a new slot into a given poll.
+     *
+     * @param $poll_id int The ID of the poll
+     * @param $slot mixed The value of the slot
+     * @return bool true if action succeeded
+     */
+    function insertSlot($poll_id, $slot) {
+        $prepared = $this->prepare('INSERT INTO sujet_studs (id_sondage, sujet) VALUES (?,?)');
+        return $prepared->execute([$poll_id, $slot]);
+    }
+
     /**
      * Update a slot into a poll.
      *
diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php
index 450ed788..b6bca393 100644
--- a/app/classes/Framadate/Services/AdminPollService.php
+++ b/app/classes/Framadate/Services/AdminPollService.php
@@ -106,5 +106,30 @@ class AdminPollService {
         $this->connect->commit();
     }
 
+    public function addSlot($poll_id, $newdate, $newmoment) {
+        $ex = explode('/', $newdate);
+        $datetime = mktime(0,0,0, $ex[1], $ex[0], $ex[2]);
+
+        $slot = $this->connect->findSlotByPollIdAndDatetime($poll_id, $datetime);
+
+        if ($slot != null) {
+            // Update found slot
+            $moments = explode('@', $slot->sujet)[1];
+            foreach ($moments as $moment) {
+                if ($moment == $newmoment) {
+                    return false;
+                }
+            }
+            // TODO Implements
+
+        } else {
+            // TODO Found index of insertion, in order to update user votes
+            $this->connect->insertSlot($poll_id, $datetime . '@' . $newmoment);
+        }
+
+        return true;
+
+    }
+
 }
  
\ No newline at end of file
diff --git a/tpl/add_slot.tpl b/tpl/add_slot.tpl
index b8f15fc2..98378175 100644
--- a/tpl/add_slot.tpl
+++ b/tpl/add_slot.tpl
@@ -16,16 +16,15 @@
                 </div>
             </div>
             <div class="form-group">
-                <label for="newhour" class="col-md-4">{_("Time")}</label>
+                <label for="newmoment" class="col-md-4">{_("Time")}</label>
                 <div class="col-md-8">
-                    <input type="text" id="newhour" name="newhour" class="form-control" />
+                    <input type="text" id="newmoment" name="newmoment" class="form-control" />
                 </div>
             </div>
-            <div class="pull-right">
+            <div class="form-group">
                 <button class="btn btn-default" type="submit" name="back">{_('Back to the poll')}</button>
                 <button type="submit" name="confirm_add_slot" class="btn btn-success">{_('Add a column')}</button>
             </div>
-            <div class="clearfix"></div>
         </div>
     </form>
 {/block}
\ No newline at end of file
-- 
GitLab