Skip to content

Commit

Permalink
impruved admin section. Added photo field
Browse files Browse the repository at this point in the history
  • Loading branch information
msalakhov committed Mar 28, 2022
1 parent f63f7d4 commit 5f048c2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
3 changes: 2 additions & 1 deletion app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
photoDir: "%kernel.project_dir%/public/uploads/photos"
photoDirAbs: "%kernel.project_dir%/public/uploads/photos"
photoDir: "/public/uploads/photos"
default_admin_email: '[email protected]'

services:
Expand Down
29 changes: 21 additions & 8 deletions app/src/Controller/Admin/ClientCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

use App\Entity\Client;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Symfony\Component\ErrorHandler\Debug;

class ClientCrudController extends AbstractCrudController
{
Expand All @@ -12,12 +21,16 @@ public static function getEntityFqcn(): string
return Client::class;
}

// public function configureFields(string $pageName): iterable
// {
// return [
// IdField::new('id'),
// TextField::new('title'),
// TextEditorField::new('description'),
// ];
// }
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id')->onlyOnIndex(),
TextField::new('name'),
EmailField::new('email'),
TextField::new('city'),
ImageField::new('photo')->setBasePath('/uploads/photos')->setUploadDir($this->getParameter('photoDir')),
DateField::new('renewalTerm'),
AssociationField::new('user'),
];
}
}
14 changes: 7 additions & 7 deletions app/src/Controller/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public function create(UserInterface $user, Request $request): Response

try {
$photo->move(
$this->getParameter('photoDir'),
$this->getParameter('photoDirAbs'),
$fileName
);
} catch (FileException $e) {
//unable to upload photo
}

$imageOptimazer = new ImageOptimizer();
$imageOptimazer->resize($this->getParameter('photoDir') . '/' . $fileName);
$imageOptimazer->resize($this->getParameter('photoDirAbs') . '/' . $fileName);

$client->setPhoto($fileName);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public function edit(Request $request, $id, UserInterface $user): Response
}

if ($client->getPhoto()) {
$photo = new File($this->getParameter('photoDir') . '/' . $client->getPhoto());
$photo = new File($this->getParameter('photoDirAbs') . '/' . $client->getPhoto());
$fileName = $photo->getFilename();

$client->setPhoto($photo);
Expand All @@ -138,15 +138,15 @@ public function edit(Request $request, $id, UserInterface $user): Response

try {
$photo->move(
$this->getParameter('photoDir'),
$this->getParameter('photoDirAbs'),
$fileName
);
} catch (FileException $e) {
//unable to upload photo
}

$imageOptimazer = new ImageOptimizer();
$imageOptimazer->resize($this->getParameter('photoDir') . '/' . $fileName);
$imageOptimazer->resize($this->getParameter('photoDirAbs') . '/' . $fileName);


}
Expand Down Expand Up @@ -491,7 +491,7 @@ public function uploadIns(Request $request, $id, $insId, ClientRepository $clien

try {
$file->move(
$this->getParameter('photoDir'),
$this->getParameter('photoDirAbs'),
$encodedFileName
);
} catch (FileException $e) {
Expand Down Expand Up @@ -580,7 +580,7 @@ public function upload(Request $request, $id, ClientRepository $clientRepository

try {
$file->move(
$this->getParameter('photoDir'),
$this->getParameter('photoDirAbs'),
$encodedFileName
);
} catch (FileException $e) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,9 @@ public function setCreatedAtValue()
{
$this->createdAt = new \DateTimeImmutable();
}

public function __toString(): string
{
return "#{$this->id} ({$this->email})";
}
}

0 comments on commit 5f048c2

Please sign in to comment.