start work on dashboard and complete first draft of profile
This commit is contained in:
parent
96abea4198
commit
133d99d297
@ -8,11 +8,20 @@ use App\Libs\Breadcrumb;
|
||||
use App\Libs\NavList;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\CurrentUser;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
use Vich\UploaderBundle\Entity\File;
|
||||
|
||||
class DefaultController extends AbstractController
|
||||
{
|
||||
@ -56,20 +65,105 @@ class DefaultController extends AbstractController
|
||||
],
|
||||
'notifications' => $this->msgs,
|
||||
'notificationCount' => $this->notificationCount,
|
||||
'milesTravelledYTD' => 0,
|
||||
'milesTravelled30Days' => 0,
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[Route('/profile', name: 'app_profile')]
|
||||
public function profile(#[CurrentUser()] User $user): Response
|
||||
{
|
||||
public function profile(
|
||||
Request $request,
|
||||
#[CurrentUser()] User $user,
|
||||
SluggerInterface $slugger
|
||||
): Response {
|
||||
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
|
||||
$this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user);
|
||||
$this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user);
|
||||
|
||||
$this->navLinks['profile'] = NavList::PRESENT_LINK;
|
||||
|
||||
$form = $this->createFormBuilder($user)
|
||||
->add('name', TextType::class, [
|
||||
'label' => 'Name',
|
||||
'label_attr' => ['class' => 'form-label'],
|
||||
'attr' => ['class' => 'form-control'],
|
||||
])
|
||||
->add('email', EmailType::class, [
|
||||
'label' => 'Email',
|
||||
'label_attr' => ['class' => 'form-label'],
|
||||
'attr' => ['class' => 'form-control'],
|
||||
])
|
||||
->add('password', RepeatedType::class, [
|
||||
'type' => PasswordType::class,
|
||||
'invalid_message' => 'The password fields must match.',
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
'first_options' => ['label' => 'Password', 'label_attr' => ['class' => 'form-label']],
|
||||
'second_options' => ['label' => 'Repeat Password', 'label_attr' => ['class' => 'form-label']],
|
||||
])
|
||||
->add('imageName', FileType::class, [
|
||||
'label' => 'Profile Picture',
|
||||
'required' => false,
|
||||
'mapped' => false
|
||||
])
|
||||
->add('submit', SubmitType::class, [
|
||||
'label' => 'Save Profile',
|
||||
'attr' => ['class' => 'btn btn-lg bg-gradient-dark btn-lg w-100 mt-4 mb-0']
|
||||
])
|
||||
->getForm()
|
||||
;
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$firstPassword = $form->get('password')['first']->getData();
|
||||
$secondPassword = $form->get('password')['second']->getData();
|
||||
|
||||
if ($firstPassword !== $secondPassword) {
|
||||
$this->addFlash('danger', 'The password fields must match.');
|
||||
return $this->redirectToRoute('app_profile');
|
||||
}
|
||||
|
||||
$plainPassword = $form->get('password')['first']->getData();
|
||||
|
||||
if ($plainPassword) {
|
||||
$user->setPassword(
|
||||
$this->userPasswordHasher->hashPassword(
|
||||
$user,
|
||||
$plainPassword
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($form['imageName']->getData()) {
|
||||
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
|
||||
$file = $form['imageName']->getData();
|
||||
$destination = $this->getParameter('kernel.project_dir').'/public/uploads/user_images/';
|
||||
|
||||
if (!file_exists($destination)) {
|
||||
mkdir($destination, 0775);
|
||||
}
|
||||
|
||||
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
|
||||
$newFilename = $slugger->slug($originalFilename).'-'.uniqid().'.'.$file->guessExtension();
|
||||
$file->move(
|
||||
$destination,
|
||||
$newFilename
|
||||
);
|
||||
|
||||
$user->setImageName($newFilename);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('app_dashboard');
|
||||
} elseif ($form->isSubmitted() && !$form->isValid()) {
|
||||
$this->addFlash('danger', 'The form contains errors.');
|
||||
}
|
||||
|
||||
return $this->render(
|
||||
'internal/profile.html.twig',
|
||||
array_merge(
|
||||
@ -80,8 +174,16 @@ class DefaultController extends AbstractController
|
||||
],
|
||||
'notifications' => $this->msgs,
|
||||
'notificationCount' => $this->notificationCount,
|
||||
'currentUser' => $user,
|
||||
'form' => $form->createView(),
|
||||
]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[Route('/uploads/user_images/{imageName}', name: 'app_user_image')]
|
||||
public function displayUserImage(string $imageName): Response
|
||||
{
|
||||
return new BinaryFileResponse($this->getParameter('kernel.project_dir')."/public/uploads/user_images/{$imageName}");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user