From f24f3e2bb9eb6cf386f81771fe36f39a07b1634f Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 23 Apr 2021 06:07:21 +0000 Subject: [PATCH] Add notification number to hub. #552 --- src/Controller/HubController.php | 34 +++++++++++++++++++++++-------- src/Entity/Hub.php | 17 ++++++++++++++++ src/Service/RisingTideGateway.php | 24 ++++++++++++++++++++++ templates/hub/form.html.twig | 12 +++++++++++ 4 files changed, 78 insertions(+), 9 deletions(-) diff --git a/src/Controller/HubController.php b/src/Controller/HubController.php index edc6e599..d2e52d6a 100644 --- a/src/Controller/HubController.php +++ b/src/Controller/HubController.php @@ -19,6 +19,7 @@ use Catalyst\MenuBundle\Annotation\Menu; use App\Service\MapTools; use App\Service\RiderTracker; +use App\Service\RisingTideGateway; class HubController extends Controller { @@ -143,6 +144,8 @@ class HubController extends Controller $time_open = DateTime::createFromFormat($format, $req->request->get('time_open')); $time_close = DateTime::createFromFormat($format, $req->request->get('time_close')); + error_log($req->request->get('notif_number')); + // set and save values $obj->setName($req->request->get('name')) ->setBranch($req->request->get('branch')) @@ -154,7 +157,8 @@ class HubController extends Controller ->setBranchCode($req->request->get('branch_code', '')) ->setStatusOpen($req->request->get('status_open', false)) ->setRiderSlots($req->request->get('rider_slots', 0)) - ->setHubViewFlag($req->request->get('flag_hub_view', false)); + ->setHubViewFlag($req->request->get('flag_hub_view', false)) + ->setNotifNumber('63' . $req->request->get('notif_number')); } protected function setQueryFilters($datatable, QueryBuilder $query) @@ -166,7 +170,7 @@ class HubController extends Controller } } - public function addSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator) + public function addSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, RisingTideGateway $rt) { $this->denyAccessUnlessGranted('hub.add', null, 'No access.'); @@ -176,14 +180,20 @@ class HubController extends Controller $em = $this->getDoctrine()->getManager(); $obj = new Hub(); + // initialize error list + $error_array = []; + + // validate the notification number + $mobile = $req->request->get('notif_number'); + $is_valid = $rt->validatePhoneNumber($mobile); + if (!$is_valid) + $error_array['notif_number'] = 'Invalid notification number'; + $this->setObject($obj, $req); // validate $errors = $validator->validate($obj); - // initialize error list - $error_array = []; - // add errors to list foreach ($errors as $error) { $error_array[$error->getPropertyPath()] = $error->getMessage(); @@ -231,7 +241,7 @@ class HubController extends Controller return $this->render('hub/form.html.twig', $params); } - public function updateSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, $id) + public function updateSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, $id, RisingTideGateway $rt) { $this->denyAccessUnlessGranted('hub.update', null, 'No access.'); @@ -243,14 +253,20 @@ class HubController extends Controller if (empty($obj)) throw $this->createNotFoundException('The item does not exist'); + // initialize error list + $error_array = []; + + // validate the notification number + $mobile = $req->request->get('notif_number'); + $is_valid = $rt->validatePhoneNumber($mobile); + if (!$is_valid) + $error_array['notif_number'] = 'Invalid notification number'; + $this->setObject($obj, $req); // validate $errors = $validator->validate($obj); - // initialize error list - $error_array = []; - // add errors to list foreach ($errors as $error) { $error_array[$error->getPropertyPath()] = $error->getMessage(); diff --git a/src/Entity/Hub.php b/src/Entity/Hub.php index 0b1769b2..ec4aff75 100644 --- a/src/Entity/Hub.php +++ b/src/Entity/Hub.php @@ -68,6 +68,12 @@ class Hub */ protected $flag_hub_view; + // notification number to send SMS to if hub doesn't have item + /** + * @ORM\Column(type="string", length=30) + */ + protected $notif_number; + public function __construct() { $this->time_open = new DateTime(); @@ -76,6 +82,7 @@ class Hub $this->outlets = new ArrayCollection(); $this->status_open = true; $this->flag_hub_view = false; + $this->notif_number = ''; } public function getRiders() @@ -185,5 +192,15 @@ class Hub return $this->flag_hub_view; } + public function setNotifNumber($notif_number) + { + $this->notif_number = $notif_number; + return $this; + } + + public function getNotifNumber() + { + return $this->notif_number; + } } diff --git a/src/Service/RisingTideGateway.php b/src/Service/RisingTideGateway.php index 38276619..acd7d4d5 100644 --- a/src/Service/RisingTideGateway.php +++ b/src/Service/RisingTideGateway.php @@ -83,4 +83,28 @@ class RisingTideGateway error_log($result); } + + public function validatePhoneNumber($mobile) + { + // check valid number + $num = trim($mobile); + + // should be 10 digits + if (strlen($num) != 10) + return false; + + // should start with '9' + if ($num[0] != '9') + return false; + + // should be numeric + if (!is_numeric($num)) + return false; + + // should not be 9900000000 + if ($num == '9900000000') + return false; + + return true; + } } diff --git a/templates/hub/form.html.twig b/templates/hub/form.html.twig index d7101260..ea300ab6 100644 --- a/templates/hub/form.html.twig +++ b/templates/hub/form.html.twig @@ -90,6 +90,18 @@
+
+ +
+ {% trans %}country_code_prefix{% endtrans %} + + +
+
+ +