From 884cd7b7f10d8eed2d5885233ce1c45920b61022 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Sun, 4 Feb 2018 23:10:32 +0800 Subject: [PATCH] Add image file to battery, update cleanup task for orphaned images --- config/routes/battery.yaml | 5 ++ public/assets/images/battery.gif | Bin 0 -> 4993 bytes src/Command/UploadCleanupCommand.php | 23 +++++-- src/Controller/BatteryController.php | 21 +++++- src/Controller/RiderController.php | 2 +- src/Entity/Battery.php | 16 +++++ templates/battery/form.html.twig | 99 +++++++++++++++++++++------ templates/battery/list.html.twig | 11 +++ templates/rider/form.html.twig | 4 +- 9 files changed, 152 insertions(+), 29 deletions(-) create mode 100644 public/assets/images/battery.gif 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 0000000000000000000000000000000000000000..c8bc9b96cdb9e67df5086fdd4de2e7d405de919b GIT binary patch literal 4993 zcmeHL=Tj5Fw@pImiinB<1w%(5LTD0t@4bdzLJhqKq!W;)fPny^7<%ZTNVA2aQUz2* zDT*jvilTJG)AxS!X5Rns?uXrb@7_6Q&Y9W$usV9$atK#3pcwF=3c$m|BP1jwDk=(x z!zCpprKF^8-@dJ=sHmi*q@tpts;a7{rlzj0uBoZ1rKP2AFNU|?uy zXk=t$Y;0^|Vq$7)YHn_BVPRouX=!C;Wo>P3V`GCvB5iGL?daiH(g-NJvOdPR3%fDJdywX=&-{=@}UrnVFec zSy|cH**Q5mxw*MG94;>}FF!vYkH;4l78Vs15eS6h;^LB$l7|l;5{bl$ii*n0%BrfW z>gwv6ni>*`R99Em(9qD>*x1z6)ZE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ploadCleanupCommand.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 @@

{% if mode == 'update' %} Edit Battery - {{ obj.getProductCode() }} + {{ obj.getProductCode }} {% else %} New Battery {% endif %} @@ -32,7 +32,7 @@ -
+
@@ -45,14 +45,14 @@ - +
- +
@@ -64,7 +64,7 @@ @@ -76,7 +76,7 @@ @@ -88,12 +88,43 @@
+
+
+ +
+
+

+ Drop files here or click to upload. +

+ + Upload only valid PNG, GIF, or JPEG images + +
+
+ {% if mode == 'update' and obj.getImageFile %} + Leave blank for unchanged + {% endif %} + +
+ {% if mode == 'update' %} +
+ +
+
+ {% endif %} +
@@ -107,7 +138,7 @@ - + In months
@@ -115,7 +146,7 @@ - + In months @@ -133,7 +164,7 @@ - + In millimeters (mm) @@ -141,7 +172,7 @@ - + In millimeters (mm) @@ -149,7 +180,7 @@ - + In millimeters (mm) @@ -159,7 +190,7 @@ - + In millimeters (mm) @@ -167,7 +198,7 @@ - + In minutes @@ -193,7 +224,7 @@ @@ -212,6 +243,7 @@
+ Cancel
@@ -227,6 +259,31 @@ {% block scripts %}