From 40f2a1729cdefe1d7406de1a99e25678b14f6b94 Mon Sep 17 00:00:00 2001
From: Olivier PEREZ <olivier@olivierperez.fr>
Date: Tue, 3 Nov 2015 21:15:47 +0100
Subject: [PATCH] Better message when fail to create column

---
 adminstuds.php                                | 25 ++++++++++---------
 .../MomentAlreadyExistsException.php          |  9 +++++++
 .../Framadate/Services/AdminPollService.php   | 18 +++++--------
 locale/de.json                                |  3 ++-
 locale/en.json                                |  1 +
 locale/es.json                                |  1 +
 locale/fr.json                                |  1 +
 locale/it.json                                |  1 +
 8 files changed, 34 insertions(+), 25 deletions(-)
 create mode 100644 app/classes/Framadate/Exception/MomentAlreadyExistsException.php

diff --git a/adminstuds.php b/adminstuds.php
index 9c7ac25c..29f3d270 100644
--- a/adminstuds.php
+++ b/adminstuds.php
@@ -17,6 +17,7 @@
  * Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
  */
 use Framadate\Editable;
+use Framadate\Exception\MomentAlreadyExistsException;
 use Framadate\Message;
 use Framadate\Services\AdminPollService;
 use Framadate\Services\InputService;
@@ -393,21 +394,21 @@ if (isset($_GET['add_slot'])) {
     exit;
 }
 if (isset($_POST['confirm_add_slot'])) {
-    if ($poll->format === 'D') {
-        $newdate = strip_tags($_POST['newdate']);
-        $newmoment = str_replace(',', '-', strip_tags($_POST['newmoment']));
+    try {
+        if ($poll->format === 'D') {
+            $newdate = strip_tags($_POST['newdate']);
+            $newmoment = str_replace(',', '-', strip_tags($_POST['newmoment']));
 
-        $ex = explode('/', $newdate);
-        $result = $adminPollService->addDateSlot($poll_id, mktime(0, 0, 0, $ex[1], $ex[0], $ex[2]), $newmoment);
-    } else {
-        $newslot = str_replace(',', '-', strip_tags($_POST['choice']));
-        $result = $adminPollService->addClassicSlot($poll_id, $newslot);
-    }
+            $ex = explode('/', $newdate);
+            $adminPollService->addDateSlot($poll_id, mktime(0, 0, 0, $ex[1], $ex[0], $ex[2]), $newmoment);
+        } else {
+            $newslot = str_replace(',', '-', strip_tags($_POST['choice']));
+            $adminPollService->addClassicSlot($poll_id, $newslot);
+        }
 
-    if ($result) {
         $message = new Message('success', __('adminstuds', 'Choice added'));
-    } else {
-        $message = new Message('danger', __('Error', 'Failed to add the column'));
+    } catch (MomentAlreadyExistsException $e) {
+        $message = new Message('danger', __('Error', 'The column already exists'));
     }
 }
 
diff --git a/app/classes/Framadate/Exception/MomentAlreadyExistsException.php b/app/classes/Framadate/Exception/MomentAlreadyExistsException.php
new file mode 100644
index 00000000..11723e9c
--- /dev/null
+++ b/app/classes/Framadate/Exception/MomentAlreadyExistsException.php
@@ -0,0 +1,9 @@
+<?php
+namespace Framadate\Exception;
+
+class MomentAlreadyExistsException extends \Exception {
+
+    function __construct() {
+    }
+
+}
diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php
index 35e40c7e..409511d8 100644
--- a/app/classes/Framadate/Services/AdminPollService.php
+++ b/app/classes/Framadate/Services/AdminPollService.php
@@ -1,6 +1,7 @@
 <?php
 namespace Framadate\Services;
 
+use Framadate\Exception\MomentAlreadyExistsException;
 use Framadate\FramaDB;
 use Framadate\Repositories\RepositoryFactory;
 
@@ -196,7 +197,7 @@ class AdminPollService {
      * @param $poll_id int The ID of the poll
      * @param $datetime int The datetime
      * @param $new_moment string The moment's name
-     * @return bool true if added
+     * @throws MomentAlreadyExistsException When the moment to add already exists in database
      */
     public function addDateSlot($poll_id, $datetime, $new_moment) {
         $this->logService->log('ADD_SLOT', 'id:' . $poll_id . ', datetime:' . $datetime . ', moment:' . $new_moment);
@@ -207,16 +208,13 @@ class AdminPollService {
         // Begin transaction
         $this->connect->beginTransaction();
 
-        if ($result == null) {
-            // The moment already exists
-            return false;
-        } elseif ($result->slot != null) {
+        if ($result->slot != null) {
             $slot = $result->slot;
             $moments = explode(',', $slot->moments);
 
             // Check if moment already exists (maybe not necessary)
             if (in_array($new_moment, $moments)) {
-                return false;
+                throw new MomentAlreadyExistsException();
             }
 
             // Update found slot
@@ -232,8 +230,6 @@ class AdminPollService {
         // Commit transaction
         $this->connect->commit();
 
-        return true;
-
     }
 
     /**
@@ -244,7 +240,7 @@ class AdminPollService {
      *
      * @param $poll_id int The ID of the poll
      * @param $title int The title
-     * @return bool true if added
+     * @throws MomentAlreadyExistsException When the moment to add already exists in database
      */
     public function addClassicSlot($poll_id, $title) {
         $this->logService->log('ADD_SLOT', 'id:' . $poll_id . ', title:' . $title);
@@ -257,7 +253,7 @@ class AdminPollService {
         }, $slots);
         if (in_array($title, $titles)) {
             // The moment already exists
-            return false;
+            throw new MomentAlreadyExistsException();
         }
 
 
@@ -272,8 +268,6 @@ class AdminPollService {
         // Commit transaction
         $this->connect->commit();
 
-        return true;
-
     }
 
     /**
diff --git a/locale/de.json b/locale/de.json
index 06d1c324..0096ec0a 100644
--- a/locale/de.json
+++ b/locale/de.json
@@ -335,7 +335,8 @@
         "Comment failed": "Abgabe des Kommentars gescheitert",
         "You can't create a poll with hidden results with the following edition option:": "Sie können mit der folgenden Editier-Option keine Umfrage mit versteckten Ergebnissen erzeugen:",
         "Failed to delete column": "Löschen der Spalte fehlgeschlagen",
+        "The column already exists": "DE_La colonne existe déjà",
         "MISSING_VALUES": "Fehlende Werte",
         "CANT_CONNECT_TO_DATABASE": "Kann nicht mit der Datenbank verbinden"
     }
-}
\ No newline at end of file
+}
diff --git a/locale/en.json b/locale/en.json
index ab727f81..756caa65 100644
--- a/locale/en.json
+++ b/locale/en.json
@@ -335,6 +335,7 @@
     "Comment failed": "Comment failed",
     "You can't create a poll with hidden results with the following edition option:": "You can't create a poll with hidden results with the following option: ",
     "Failed to delete column": "Failed to delete column",
+    "The column already exists": "The column already exists",
     "MISSING_VALUES": "Missing values",
     "CANT_CONNECT_TO_DATABASE": "Unable to connect to database"
   }
diff --git a/locale/es.json b/locale/es.json
index 96c0d692..36ab06c6 100644
--- a/locale/es.json
+++ b/locale/es.json
@@ -335,6 +335,7 @@
         "Comment failed": "ES_Commentaire échoué",
         "You can't create a poll with hidden results with the following edition option:": "ES_Vous ne pouvez pas créer de sondage avec résulats cachés avec les options d'éditions suivantes : ",
         "Failed to delete column": "Error al eliminar la columna",
+        "The column already exists": "ES_La colonne existe déjà",
         "MISSING_VALUES": "Los valores perdidos",
         "CANT_CONNECT_TO_DATABASE": "No se puede conectar a la base de datos"
     }
diff --git a/locale/fr.json b/locale/fr.json
index be0d838a..57a20ad4 100644
--- a/locale/fr.json
+++ b/locale/fr.json
@@ -350,6 +350,7 @@
     "Comment failed": "Commentaire échoué",
     "You can't create a poll with hidden results with the following edition option:": "Vous ne pouvez pas créer de sondage avec résulats cachés avec les options d'éditions suivantes : ",
     "Failed to delete column": "Échec de la suppression de colonne",
+    "The column already exists": "La colonne existe déjà",
     "MISSING_VALUES": "Il manque des valeurs",
     "CANT_CONNECT_TO_DATABASE": "Impossible de se connecter à la base de données"
   }
diff --git a/locale/it.json b/locale/it.json
index e976a447..59595a40 100644
--- a/locale/it.json
+++ b/locale/it.json
@@ -335,6 +335,7 @@
     "Comment failed": "IT_Commentaire échoué",
     "You can't create a poll with hidden results with the following edition option:": "IT_Vous ne pouvez pas créer de sondage avec résulats cachés avec les options d'éditions suivantes : ",
     "Failed to delete column": "Impossibile eliminare colonna",
+    "The column already exists": "IT_La colonne existe déjà",
     "MISSING_VALUES": "Valori mancanti",
     "CANT_CONNECT_TO_DATABASE": "Impossibile connettersi al database"
   }
-- 
GitLab