diff --git a/adminstuds.php b/adminstuds.php index 9c7ac25..29f3d27 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 0000000..11723e9 --- /dev/null +++ b/app/classes/Framadate/Exception/MomentAlreadyExistsException.php @@ -0,0 +1,9 @@ +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; - } /** @@ -283,34 +277,29 @@ class AdminPollService { * * @param $slots array All the slots of the poll * @param $datetime int The datetime of the new slot - * @return null|\stdClass An object like this one: {insert:X, slot:Y} where Y can be null. + * @return \stdClass An object like this one: {insert:X, slot:Y} where Y can be null. */ private function findInsertPosition($slots, $datetime) { $result = new \stdClass(); $result->slot = null; - $result->insert = -1; - - $i = 0; + $result->insert = 0; foreach ($slots as $slot) { $rowDatetime = $slot->title; $moments = explode(',', $slot->moments); if ($datetime == $rowDatetime) { - $i += count($moments); - // Here we have to insert at the end of a slot + $result->insert += count($moments); $result->slot = $slot; - $result->insert = $i; break; } elseif ($datetime < $rowDatetime) { - // Here we have to insert a new slot + // We have to insert before this slot break; } else { - $i += count($moments); + $result->insert += count($moments); } } - $result->insert = $i; return $result; } diff --git a/locale/de.json b/locale/de.json index 06d1c32..0096ec0 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 ab727f8..756caa6 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 96c0d69..36ab06c 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 be0d838..57a20ad 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 272389e..8b283f6 100644 --- a/locale/it.json +++ b/locale/it.json @@ -335,6 +335,7 @@ "Comment failed": "Commento fallito", "You can't create a poll with hidden results with the following edition option:": "Non potete creare un sondaggio con i risultati nascosti con queste opzioni: : ", "Failed to delete column": "Impossibile eliminare la colonna", + "The column already exists": "IT_La colonne existe déjà", "MISSING_VALUES": "Valori mancanti", "CANT_CONNECT_TO_DATABASE": "Impossibile connettersi al database" }