From 9aa3eca812abf414c73e3ec93527945eac965d82 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Tue, 18 Jul 2023 17:16:34 +0200 Subject: [PATCH] Add verification on phone number on contact import --- controllers/internals/Contact.php | 12 ++++- controllers/publics/Contact.php | 50 ++++++++----------- ...20230606180455_add_index_on_sended_uid.php | 2 +- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/controllers/internals/Contact.php b/controllers/internals/Contact.php index 2d8afee..f3debe4 100644 --- a/controllers/internals/Contact.php +++ b/controllers/internals/Contact.php @@ -141,8 +141,11 @@ namespace controllers\internals; $nb_insert = 0; $head = null; + $line_nb = 0; while ($line = fgetcsv($file_handler)) { + $line_nb ++; + if (null === $head) { $head = $line; @@ -185,9 +188,16 @@ namespace controllers\internals; } $data = json_encode($data); + $contact_name = $line[array_keys($line)[0]]; + $phone_number = \controllers\internals\Tool::parse_phone($line[array_keys($line)[1]]); + if (!$phone_number) + { + throw new \Exception('Erreur à la ligne ' . $line_nb . ' colonne 1, numéro de téléphone invalide.'); + } + try { - $success = $this->create($id_user, $line[array_keys($line)[1]], $line[array_keys($line)[0]], $data); + $success = $this->create($id_user, $line[array_keys($line)[1]], $contact_name, $data); if ($success) { ++$nb_insert; diff --git a/controllers/publics/Contact.php b/controllers/publics/Contact.php index 69b0cc8..6c2a789 100644 --- a/controllers/publics/Contact.php +++ b/controllers/publics/Contact.php @@ -11,6 +11,8 @@ namespace controllers\publics; +use Exception; + /** * Page des contacts. */ @@ -345,40 +347,28 @@ namespace controllers\publics; return $this->redirect(\descartes\Router::url('Contact', 'list')); } - //Try to import file - $invalid_type = false; - - switch ($read_file['mime_type']) + try { - case 'text/csv': - $result = $this->internal_contact->import_csv($id_user, $read_file['content']); - - break; - - case 'application/json': - $result = $this->internal_contact->import_json($id_user, $read_file['content']); - - break; - - default: - if ('csv' === $read_file['extension']) - { + $result = false; + switch (true) + { + case ($read_file['mime_type'] === 'text/csv' || 'csv' === $read_file['extension']) : $result = $this->internal_contact->import_csv($id_user, $read_file['content']); - } - elseif ('json' === $read_file['extension']) - { - $result = $this->internal_contact->import_json($id_user, $read_file['content']); - } - else - { - $invalid_type = true; - $result = false; - } - } - if ($invalid_type) + break; + + case ($read_file['mime_type'] === 'text/json' || 'json' === $read_file['extension']) : + $result = $this->internal_contact->import_json($id_user, $read_file['content']); + + break; + + default: + throw new Exception('Le type de fichier n\'est pas valide.'); + } + } + catch (\Exception $e) { - \FlashMessage\FlashMessage::push('danger', 'Le type de fichier n\'est pas valide.'); + \FlashMessage\FlashMessage::push('danger', 'Erreur lors de l\'import: ' . $e->getMessage()); return $this->redirect(\descartes\Router::url('Contact', 'list')); } diff --git a/db/migrations/20230606180455_add_index_on_sended_uid.php b/db/migrations/20230606180455_add_index_on_sended_uid.php index 06ea6e9..b0be881 100644 --- a/db/migrations/20230606180455_add_index_on_sended_uid.php +++ b/db/migrations/20230606180455_add_index_on_sended_uid.php @@ -18,7 +18,7 @@ class AddIndexOnSendedUid extends AbstractMigration $table = $this->table('call'); $table->changeColumn('uid', 'string', ['limit' => 100]); - $table->addIndex('uid'); + $table->addIndex('uid'); $table->update(); } }