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' %} -
- -