From 6a070ca4e061d74f19d515ec205eafed7e99465c Mon Sep 17 00:00:00 2001 From: Ryan Prather Date: Tue, 2 Jul 2024 01:38:36 -0400 Subject: [PATCH] Added shareNote method to share an individual note with an email --- src/Controller/AjaxController.php | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/Controller/AjaxController.php b/src/Controller/AjaxController.php index 5d31caa..c506516 100644 --- a/src/Controller/AjaxController.php +++ b/src/Controller/AjaxController.php @@ -4,6 +4,7 @@ namespace App\Controller; use DateTime; +use Parsedown; use App\Entity\Reference; use App\Entity\Template; use App\Entity\User; @@ -11,10 +12,14 @@ use App\Entity\Bible; use App\Entity\Speaker; use App\Entity\Series; use App\Entity\Note; +use App\Entity\NoteShares; +use App\Utils\Utils; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\Address; use Symfony\Component\Routing\Attribute\Route; class AjaxController extends AbstractController @@ -347,6 +352,61 @@ class AjaxController extends AbstractController return $res; } + #[Route('/share-note', name: 'app_share_note', methods: ['POST'])] + public function shareNote(Request $req, EntityManagerInterface $emi, MailerInterface $mailer): Response + { + $data = json_decode($req->getContent()); + $note = $emi->getRepository(Note::class)->find($data->id); + /** @var User $user */ + $user = $this->getUser(); + $shared = $emi->getRepository(User::class)->findBy(['email' => $data->email]); + $email = $data->email; + $isRegistered = false; + + if (!$note) { + $res = new Response(); + $res->setContent(json_encode([ + 'msg' => 'Note Not Found' + ])); + } + + if (is_array($shared) && count($shared) > 0) { + $ns = new NoteShares(); + $ns->setNote($note) + ->setOwner($user) + ->setShare($shared[0]); + $emi->persist($ns); + $emi->flush(); + + $email = $shared[0]->getEmail(); + $isRegistered = true; + } + + $Parsedown = new Parsedown(); + + $util = new Utils(); + $util->sendEmail( + $mailer, + new Address('ryan@rkprather.com', 'Sermon Notes'), + new Address($email), + 'Note Shared', + $this->renderView('emails/note-shared.html.twig', [ + 'note' => $note, + 'owner' => $user, + 'isRegistered' => $isRegistered, + 'formattedText' => $Parsedown->text($note->getText()), + 'domain' => $_ENV['DOMAIN'], + ]) + ); + + $res = new Response(); + $res->setContent(json_encode([ + 'msg' => 'shared' + ])); + + return $res; + } + #[Route('/get-passage/{passage}', name: 'app_get_passage')] public function getPassage($passage, EntityManagerInterface $emi): Response {