diff --git a/config/routes/battery.yaml b/config/routes/battery.yaml index 04a9d952..a32aa1b5 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_ajax_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..e392dd6b 100644 --- a/src/Controller/BatteryController.php +++ b/src/Controller/BatteryController.php @@ -415,6 +415,57 @@ class BatteryController extends Controller ]); } + public function getBattery(Request $req) + { + // no access check, grant all for this ajax call + + // parse the id: model size + $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 + 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)) + { + // TODO: will have to change from 422 to 404 + return $this->json([ + 'success' => false, + 'errors' => "Battery does not exist", + ], 422); + } + + $batteries = []; + + foreach ($result as $row) + { + $batteries[] = [ + 'id' => $row->getID(), + 'manufacturer' => $row->getManufacturer()->getName(), + '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 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..7c388062 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); } @@ -278,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)) { @@ -302,6 +311,96 @@ 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); + } + } + } + } + // 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 { diff --git a/templates/vehicle/form.html.twig b/templates/vehicle/form.html.twig index 4669d1c2..09bee95f 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,23 @@ $(function() { }); }); + // initialize battery arrays var battRows = []; var batteryIds = []; + var battMfgModelSize = [] {% 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 }}", + manufacturer: "{{ batt.getManufacturer.getName|default('') }} ", + 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 }}); + battMfgModelSize.push({{ batt.getManufacturer.getID }} + ' ' + {{ batt.getModel.getID }} + ' ' + {{ batt.getSize.getID }}); {% endfor %} // remove battery from table @@ -237,6 +268,66 @@ $(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 batt_key = (bmfg_id + ' ' + bmodel_id + ' ' + bsize_id); + + if (battMfgModelSize.indexOf(batt_key) !== -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_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]; + + batteryIds.push(batt.id); + + brow = { + id: batt.id, + manufacturer: batt.manufacturer, + model: batt.model, + size: batt.size, + sell_price: batt.price + }; + + battRows.push(brow); + + // add battery to arrays + 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 var battOptions = { data: { @@ -256,6 +347,10 @@ $(function() { title: 'ID', width: 50 }, + { + field: 'manufacturer', + title: 'Manufacturer', + }, { field: 'size', title: 'Size'