diff --git a/config/routes/battery.yaml b/config/routes/battery.yaml index 7fa4c9e2..04a9d952 100644 --- a/config/routes/battery.yaml +++ b/config/routes/battery.yaml @@ -9,6 +9,11 @@ battery_rows: controller: App\Controller\BatteryController::rows methods: [POST] +battery_upload_image: + path: /batteries/upload + controller: App\Controller\BatteryController::uploadImage + methods: [POST] + battery_create: path: /batteries/create controller: App\Controller\BatteryController::addForm diff --git a/public/assets/images/battery.gif b/public/assets/images/battery.gif new file mode 100644 index 00000000..c8bc9b96 Binary files /dev/null and b/public/assets/images/battery.gif differ diff --git a/src/Command/UploadCleanupCommand.php b/src/Command/UploadCleanupCommand.php index 36de2bd4..94d22c6e 100644 --- a/src/Command/UploadCleanupCommand.php +++ b/src/Command/UploadCleanupCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Doctrine\Common\Persistence\ObjectManager; use App\Entity\Rider; +use App\Entity\Battery; use App\Service\FileUploader; use DirectoryIterator; @@ -41,12 +42,26 @@ class UploadCleanupCommand extends Command // get all image filenames $em = $this->object_manager; - $rows = $em->getRepository(Rider::class)->findAll(); + + $riders = $em->getRepository(Rider::class)->findAll(); $whitelist = ['.gitkeep']; - if (!empty($rows)) { - foreach ($rows as $row) { - $image = $row->getImageFile(); + if (!empty($riders)) { + foreach ($riders as $obj) { + $image = $obj->getImageFile(); + + if (!empty($image)) { + $whitelist[] = $image; + } + } + } + + $batteries = $em->getRepository(Battery::class)->findAll(); + $whitelist = ['.gitkeep']; + + if (!empty($batteries)) { + foreach ($batteries as $obj) { + $image = $obj->getImageFile(); if (!empty($image)) { $whitelist[] = $image; diff --git a/src/Controller/BatteryController.php b/src/Controller/BatteryController.php index 238738f7..990f34fa 100644 --- a/src/Controller/BatteryController.php +++ b/src/Controller/BatteryController.php @@ -9,6 +9,7 @@ use App\Entity\BatteryModel; use App\Entity\BatterySize; use App\Entity\Vehicle; use App\Entity\VehicleManufacturer; +use App\Service\FileUploader; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; @@ -123,6 +124,7 @@ class BatteryController extends BaseController $row['width'] = $orow[0]->getWidth(); $row['height'] = $orow[0]->getHeight(); $row['total_height'] = $orow[0]->getTotalHeight(); + $row['image_file'] = $orow[0]->getImageFile(); // add row metadata $row['meta'] = [ @@ -183,7 +185,8 @@ class BatteryController extends BaseController ->setWidth($req->request->get('width')) ->setHeight($req->request->get('height')) ->setTotalHeight($req->request->get('total_height')) - ->setSellingPrice($req->request->get('sell_price')); + ->setSellingPrice($req->request->get('sell_price')) + ->setImageFile($req->request->get('image_file')); // initialize error list $error_array = []; @@ -304,6 +307,7 @@ class BatteryController extends BaseController ->setHeight($req->request->get('height')) ->setTotalHeight($req->request->get('total_height')) ->setSellingPrice($req->request->get('sell_price')) + ->setImageFile($req->request->get('image_file')) ->clearVehicles(); // initialize error list @@ -398,6 +402,21 @@ class BatteryController extends BaseController $response->send(); } + public function uploadImage(Request $req, FileUploader $uploader) + { + // retrieve temporary info for file + $file = $req->files->get('image_file'); + + // upload the file + $filename = $uploader->upload($file); + + // return response + return $this->json([ + 'success' => true, + 'filename' => $filename + ]); + } + // 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/RiderController.php b/src/Controller/RiderController.php index 0f6dae90..58925058 100644 --- a/src/Controller/RiderController.php +++ b/src/Controller/RiderController.php @@ -197,7 +197,7 @@ class RiderController extends BaseController } } - public function updateForm(FileUploader $uploader, $id) + public function updateForm($id) { $this->denyAccessUnlessGranted('rider.update', null, 'No access.'); diff --git a/src/Entity/Battery.php b/src/Entity/Battery.php index 07d9d33c..e7616b89 100644 --- a/src/Entity/Battery.php +++ b/src/Entity/Battery.php @@ -123,6 +123,11 @@ class Battery */ protected $sell_price; + /** + * @ORM\Column(type="string", nullable=true) + */ + protected $image_file; + public function __construct() { $this->vehicles = new ArrayCollection(); @@ -299,4 +304,15 @@ class Battery { return $this->cust_vehicles; } + + public function setImageFile($image_file) + { + $this->image_file = $image_file; + return $this; + } + + public function getImageFile() + { + return $this->image_file; + } } diff --git a/templates/battery/form.html.twig b/templates/battery/form.html.twig index f7dfe2ca..73b6844a 100644 --- a/templates/battery/form.html.twig +++ b/templates/battery/form.html.twig @@ -24,7 +24,7 @@