UrlNaming - Generate id unless if user has already choosen one

This commit is contained in:
Olivier PEREZ 2015-12-05 16:30:49 +01:00
parent fac20a5908
commit 1efd7b9ab0
2 changed files with 23 additions and 7 deletions

View File

@ -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 = ?');

View File

@ -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();