From 1efd7b9ab0ab1ce376cf7e66fc3879f1ae5662fe Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Sat, 5 Dec 2015 16:30:49 +0100 Subject: [PATCH] UrlNaming - Generate id unless if user has already choosen one --- .../Framadate/Repositories/PollRepository.php | 8 +++++++ .../Framadate/Services/PollService.php | 22 +++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/classes/Framadate/Repositories/PollRepository.php b/app/classes/Framadate/Repositories/PollRepository.php index fd62e55..e2315a3 100644 --- a/app/classes/Framadate/Repositories/PollRepository.php +++ b/app/classes/Framadate/Repositories/PollRepository.php @@ -47,6 +47,14 @@ class PollRepository extends AbstractRepository { return $prepared->rowCount() > 0; } + public function existsByAdminId($admin_poll_id) { + $prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('poll') . '` WHERE admin_id = ?'); + + $prepared->execute(array($admin_poll_id)); + + return $prepared->rowCount() > 0; + } + function update($poll) { $prepared = $this->prepare('UPDATE `' . Utils::table('poll') . '` SET title=?, admin_name=?, admin_mail=?, description=?, end_date=?, active=?, editable=?, hidden=?, password_hash=?, results_publicly_visible=? WHERE id = ?'); diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index 3897791..2ccb9f8 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -20,9 +20,8 @@ namespace Framadate\Services; use Framadate\Form; use Framadate\FramaDB; -use Framadate\Utils; -use Framadate\Security\Token; use Framadate\Repositories\RepositoryFactory; +use Framadate\Security\Token; class PollService { @@ -112,12 +111,21 @@ class PollService { * @return string */ function createPoll(Form $form) { - // Generate poll IDs, loop while poll ID already exists - do { - $poll_id = $this->random(16); - } while ($this->pollRepository->existsById($poll_id)); - $admin_poll_id = $poll_id . $this->random(8); + + if (empty($form->id)) { // User want us to generate an id for him + do { + $poll_id = $this->random(16); + } while ($this->pollRepository->existsById($poll_id)); + $admin_poll_id = $poll_id . $this->random(8); + + } else { // User have choosen the poll id + $poll_id = $form->id; + do { + $admin_poll_id = $this->random(24); + } while ($this->pollRepository->existsByAdminId($admin_poll_id)); + + } // Insert poll + slots $this->pollRepository->beginTransaction();