From fd2fe4fb6fcf8387868ce71cca7766e220fad396 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 3 Feb 2020 08:59:22 +0000 Subject: [PATCH 1/5] Add routes and UI elements to add compatible battery to vehicle. #323 --- config/routes/battery.yaml | 5 ++ src/Controller/BatteryController.php | 26 ++++++ src/Controller/VehicleController.php | 8 ++ templates/vehicle/form.html.twig | 115 +++++++++++++++++++++------ 4 files changed, 130 insertions(+), 24 deletions(-) diff --git a/config/routes/battery.yaml b/config/routes/battery.yaml index 04a9d952..f8c31836 100644 --- a/config/routes/battery.yaml +++ b/config/routes/battery.yaml @@ -39,6 +39,11 @@ battery_delete: controller: App\Controller\BatteryController::destroy methods: [DELETE] +battery_get: + path: /ajax/battery_find + controller: App\Controller\BatteryController::getBattery + methods: [GET] + # battery manufacturers bmfg_list: diff --git a/src/Controller/BatteryController.php b/src/Controller/BatteryController.php index 059d8b57..bea9eafd 100644 --- a/src/Controller/BatteryController.php +++ b/src/Controller/BatteryController.php @@ -415,6 +415,32 @@ class BatteryController extends Controller ]); } + public function getBattery(Request $req) + { + // no access check, grant all for this ajax call + + error_log($req->query->get('id')); + // parse the id: model size + $batt_specs = explode(" ", $req->query->get('id')); + $bmodel_id = $batt_specs[0]; + $bsize_id = $batt_specs[1]; + + // find the battery using model and size + $em = $this->getDoctrine()->getManager(); + $query = $em->createQuery('select b from App\Entity\Battery b join b.model bm + join b.size bs + where bm.id = :bm_id and bs.id = :bs_id') + ->setParameter('bm_id', $bmodel_id) + ->setParameter('bs_id', $bsize_id); + + $result = $query->getResult(); + + foreach ($result as $row) + { + error_log('id ' . $row->getID()); + } + } + // check if datatable filter is present and append to query protected function setQueryFilters($datatable, &$query) { if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { diff --git a/src/Controller/VehicleController.php b/src/Controller/VehicleController.php index 2df6ec74..f7f8c625 100644 --- a/src/Controller/VehicleController.php +++ b/src/Controller/VehicleController.php @@ -4,7 +4,10 @@ namespace App\Controller; use App\Entity\Vehicle; use App\Entity\VehicleManufacturer; +use App\Entity\BatteryManufacturer; use App\Entity\Battery; +use App\Entity\BatteryModel; +use App\Entity\BatterySize; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; @@ -225,6 +228,11 @@ class VehicleController extends Controller $params['years'] = $this->generateYearOptions(); $params['obj'] = $row; + // get battery information for compatible batteries + $params['bmfgs'] = $em->getRepository(BatteryManufacturer::class)->findAll(); + $params['bmodels'] = $em->getRepository(BatteryModel::class)->findAll(); + $params['bsizes'] = $em->getRepository(BatterySize::class)->findAll(); + // response return $this->render('vehicle/form.html.twig', $params); } diff --git a/templates/vehicle/form.html.twig b/templates/vehicle/form.html.twig index 4669d1c2..fdfa1a64 100644 --- a/templates/vehicle/form.html.twig +++ b/templates/vehicle/form.html.twig @@ -96,23 +96,50 @@ {% if mode == 'update' %} -
- -
-
-

- Compatible Batteries -

-
-
-
-
-
-
-
+
+
+
+

+ Compatible Batteries +

+
+
+
+
+
+
+
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+
{% endif %} - - +
@@ -201,19 +228,22 @@ $(function() { }); }); + // initialize battery arrays var battRows = []; var batteryIds = []; + var battModelSize = [] {% for batt in obj.getBatteries %} - trow = { - id: "{{ batt.getID }}", - model: "{{ batt.getModel.getName|default('') }}", - size: "{{ batt.getSize.getName|default('') }}", - sell_price: "{{ batt.getSellingPrice }}" - }; + trow = { + id: "{{ batt.getID }}", + model: "{{ batt.getModel.getName|default('') }}", + size: "{{ batt.getSize.getName|default('') }}", + sell_price: "{{ batt.getSellingPrice }}" + }; - battRows.push(trow); - batteryIds.push({{ batt.getID }}); + battRows.push(trow); + batteryIds.push({{ batt.getID }}); + battModelSize.push({{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }}); {% endfor %} // remove battery from table @@ -237,6 +267,43 @@ $(function() { battTable.reload(); }); + // add a battery to the table + $("#btn-add-battery").click(function() { + var bmfg_id = $("#bmfg").val(); + var bmodel_id = $("#bmodel").val(); + var bsize_id = $("#bsize").val(); + + var bmodelsize = (bmodel_id + ' ' + bsize_id); + + console.log(bmodelsize); + + if (battModelSize.indexOf(bmodelsize) !== -1) { + swal({ + title: 'Whoops', + text: 'This battery is already on the list.', + type: 'warning' + }); + return true; + } + + // get the battery given the model and size + $.ajax({ + method: "GET", + url: "{{ url('battery_get') }}", + data: {id: bmodelsize} + }).done(function(response) { + + }) + + // add battery to arrays + battModelSize.push(bmodelsize); + + // refresh the data table + battTable.originalDataSet = battRows; + battTable.reload(); + + }); + // battery data table var battOptions = { data: { From 6bcdf70902b528039c0f615b9a487ad3708caa4a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 3 Feb 2020 11:03:52 +0000 Subject: [PATCH 2/5] Display the battery to be added to the form. #323 --- src/Controller/BatteryController.php | 17 +++++++++++++++-- templates/vehicle/form.html.twig | 28 ++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/Controller/BatteryController.php b/src/Controller/BatteryController.php index bea9eafd..5786945f 100644 --- a/src/Controller/BatteryController.php +++ b/src/Controller/BatteryController.php @@ -419,7 +419,6 @@ class BatteryController extends Controller { // no access check, grant all for this ajax call - error_log($req->query->get('id')); // parse the id: model size $batt_specs = explode(" ", $req->query->get('id')); $bmodel_id = $batt_specs[0]; @@ -435,10 +434,24 @@ class BatteryController extends Controller $result = $query->getResult(); + if (empty($result)) + throw $this->createNotFoundException('The item does not exist'); + + $batteries = []; + foreach ($result as $row) { - error_log('id ' . $row->getID()); + $batteries[] = [ + 'id' => $row->getID(), + 'model' => $row->getModel()->getName(), + 'size' => $row->getSize()->getName(), + 'price' => $row->getSellingPrice(), + ]; } + + return $this->json([ + 'data' => $batteries + ]); } // check if datatable filter is present and append to query diff --git a/templates/vehicle/form.html.twig b/templates/vehicle/form.html.twig index fdfa1a64..cabb658e 100644 --- a/templates/vehicle/form.html.twig +++ b/templates/vehicle/form.html.twig @@ -292,16 +292,28 @@ $(function() { url: "{{ url('battery_get') }}", data: {id: bmodelsize} }).done(function(response) { - - }) + if (response.data.length > 0) { + var batt = response.data[0]; - // add battery to arrays - battModelSize.push(bmodelsize); + batteryIds.push(batt.id); + + brow = { + id: batt.id, + model: batt.model, + size: batt.size, + sell_price: batt.price + }; + + battRows.push(brow); - // refresh the data table - battTable.originalDataSet = battRows; - battTable.reload(); - + // add battery to arrays + battModelSize.push(bmodelsize); + + // refresh the data table + battTable.originalDataSet = battRows; + battTable.reload(); + } + }) }); // battery data table From ac908e9bcd6106b8d6c69ecb363b1f013080fccc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 3 Feb 2020 11:23:57 +0000 Subject: [PATCH 3/5] Add saving of battery to vehicle. #323 --- src/Controller/VehicleController.php | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Controller/VehicleController.php b/src/Controller/VehicleController.php index f7f8c625..d09d3752 100644 --- a/src/Controller/VehicleController.php +++ b/src/Controller/VehicleController.php @@ -310,6 +310,41 @@ class VehicleController extends Controller } } } + // need to check if a battery has been added + if (count($current_batteries) < count($batteries)) + { + // get the ids of current batteries + $cbatt_ids = []; + foreach ($current_batteries as $cbatt) + { + $cbatt_ids = [ + $cbatt->getID(), + ]; + } + + // find the battery to add + foreach ($batteries as $batt) + { + $batt_id = $batt; + if (in_array($batt_id, $cbatt_ids)) + { + // do nothing since battery already there + continue; + } + else + { + // batt is the new battery + $battery = $em->getRepository(Battery::class)->find($batt_id); + + if (!empty($battery)) + { + $battery->addVehicle($row); + + $em->persist($battery); + } + } + } + } } else { From 96efa32a124167e729e06f3c021f7ecd79f5de84 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 4 Feb 2020 01:39:14 +0000 Subject: [PATCH 4/5] Fixes for battery_ajax_get. #323 --- config/routes/battery.yaml | 2 +- src/Controller/BatteryController.php | 26 ++++++++++++++------ templates/vehicle/form.html.twig | 36 ++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/config/routes/battery.yaml b/config/routes/battery.yaml index f8c31836..a32aa1b5 100644 --- a/config/routes/battery.yaml +++ b/config/routes/battery.yaml @@ -39,7 +39,7 @@ battery_delete: controller: App\Controller\BatteryController::destroy methods: [DELETE] -battery_get: +battery_ajax_get: path: /ajax/battery_find controller: App\Controller\BatteryController::getBattery methods: [GET] diff --git a/src/Controller/BatteryController.php b/src/Controller/BatteryController.php index 5786945f..e392dd6b 100644 --- a/src/Controller/BatteryController.php +++ b/src/Controller/BatteryController.php @@ -420,22 +420,33 @@ class BatteryController extends Controller // no access check, grant all for this ajax call // parse the id: model size - $batt_specs = explode(" ", $req->query->get('id')); - $bmodel_id = $batt_specs[0]; - $bsize_id = $batt_specs[1]; + $bmfg_id = $req->query->get('mfg_id'); + $bmodel_id = $req->query->get('model_id'); + $bsize_id = $req->query->get('size_id'); // find the battery using model and size $em = $this->getDoctrine()->getManager(); - $query = $em->createQuery('select b from App\Entity\Battery b join b.model bm - join b.size bs - where bm.id = :bm_id and bs.id = :bs_id') + $query = $em->createQuery('SELECT b FROM App\Entity\Battery b + JOIN b.model bm + JOIN b.size bs + JOIN b.manufacturer bmfg + WHERE bm.id = :bm_id + AND bs.id = :bs_id + AND bmfg.id = :bmfg_id') + ->setParameter('bmfg_id', $bmfg_id) ->setParameter('bm_id', $bmodel_id) ->setParameter('bs_id', $bsize_id); $result = $query->getResult(); if (empty($result)) - throw $this->createNotFoundException('The item does not exist'); + { + // TODO: will have to change from 422 to 404 + return $this->json([ + 'success' => false, + 'errors' => "Battery does not exist", + ], 422); + } $batteries = []; @@ -443,6 +454,7 @@ class BatteryController extends Controller { $batteries[] = [ 'id' => $row->getID(), + 'manufacturer' => $row->getManufacturer()->getName(), 'model' => $row->getModel()->getName(), 'size' => $row->getSize()->getName(), 'price' => $row->getSellingPrice(), diff --git a/templates/vehicle/form.html.twig b/templates/vehicle/form.html.twig index cabb658e..09bee95f 100644 --- a/templates/vehicle/form.html.twig +++ b/templates/vehicle/form.html.twig @@ -231,11 +231,12 @@ $(function() { // initialize battery arrays var battRows = []; var batteryIds = []; - var battModelSize = [] + var battMfgModelSize = [] {% for batt in obj.getBatteries %} trow = { id: "{{ batt.getID }}", + manufacturer: "{{ batt.getManufacturer.getName|default('') }} ", model: "{{ batt.getModel.getName|default('') }}", size: "{{ batt.getSize.getName|default('') }}", sell_price: "{{ batt.getSellingPrice }}" @@ -243,7 +244,7 @@ $(function() { battRows.push(trow); batteryIds.push({{ batt.getID }}); - battModelSize.push({{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }}); + battMfgModelSize.push({{ batt.getManufacturer.getID }} + ' ' + {{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }}); {% endfor %} // remove battery from table @@ -273,11 +274,9 @@ $(function() { var bmodel_id = $("#bmodel").val(); var bsize_id = $("#bsize").val(); - var bmodelsize = (bmodel_id + ' ' + bsize_id); + var batt_key = (bmfg_id + ' ' + bmodel_id + ' ' + bsize_id); - console.log(bmodelsize); - - if (battModelSize.indexOf(bmodelsize) !== -1) { + if (battMfgModelSize.indexOf(batt_key) !== -1) { swal({ title: 'Whoops', text: 'This battery is already on the list.', @@ -289,8 +288,8 @@ $(function() { // get the battery given the model and size $.ajax({ method: "GET", - url: "{{ url('battery_get') }}", - data: {id: bmodelsize} + url: "{{ url('battery_ajax_get') }}", + data: {mfg_id: bmfg_id, model_id: bmodel_id, size_id: bsize_id} }).done(function(response) { if (response.data.length > 0) { var batt = response.data[0]; @@ -299,6 +298,7 @@ $(function() { brow = { id: batt.id, + manufacturer: batt.manufacturer, model: batt.model, size: batt.size, sell_price: batt.price @@ -307,13 +307,25 @@ $(function() { battRows.push(brow); // add battery to arrays - battModelSize.push(bmodelsize); + battMfgModelSize.push(batt_key); // refresh the data table battTable.originalDataSet = battRows; battTable.reload(); } - }) + }).fail(function(response) { + // TODO: change response status to 404. For some strange reason, + // if status is set to 422, the swal does not display. + // Instead, we get the technical error box. + if (response.status == 422) { + swal({ + title: 'Whoops', + text: 'This battery does not exist.', + type: 'warning' + }); + return true; + } + }); }); // battery data table @@ -335,6 +347,10 @@ $(function() { title: 'ID', width: 50 }, + { + field: 'manufacturer', + title: 'Manufacturer', + }, { field: 'size', title: 'Size' From 56a797533d65e02f735dc1bc668c5fab8eb27b88 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 4 Feb 2020 01:54:43 +0000 Subject: [PATCH 5/5] Add processing of deleting and adding a battery in one request. #323 --- src/Controller/VehicleController.php | 56 ++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/Controller/VehicleController.php b/src/Controller/VehicleController.php index d09d3752..7c388062 100644 --- a/src/Controller/VehicleController.php +++ b/src/Controller/VehicleController.php @@ -286,6 +286,7 @@ class VehicleController extends Controller $batteries = $req->request->get('batteries'); if (!empty($batteries)) { + // TODO: need to move the checking for batteries to a function // need to check if a battery has been removed if (count($current_batteries) > count($batteries)) { @@ -345,6 +346,61 @@ class VehicleController extends Controller } } } + // check if battery was deleted then another one was added + if (count($current_batteries) == count($batteries)) + { + // find deleted battery + foreach ($current_batteries as $cbatt) + { + $cbatt_id = $cbatt->getID(); + if (in_array($cbatt_id, $batteries)) + { + // do nothing, move to next element + continue; + } + else + { + // cbatt_id has been deleted + $del_battery = $em->getRepository(Battery::class)->find($cbatt_id); + + if (!empty($del_battery)) + { + $del_battery->removeVehicle($row); + } + } + } + + // find the battery to add + $cbatt_ids = []; + foreach ($current_batteries as $cbatt) + { + $cbatt_ids = [ + $cbatt->getID(), + ]; + } + + foreach ($batteries as $batt) + { + $batt_id = $batt; + if (in_array($batt_id, $cbatt_ids)) + { + // do nothing since battery already there + continue; + } + else + { + // batt is the new battery + $new_battery = $em->getRepository(Battery::class)->find($batt_id); + + if (!empty($new_battery)) + { + $new_battery->addVehicle($row); + + $em->persist($new_battery); + } + } + } + } } else {