Added shareNote method to share an individual note with an email

This commit is contained in:
Ryan Prather 2024-07-02 01:38:36 -04:00
parent fa8ef2ab78
commit 6a070ca4e0

View File

@ -4,6 +4,7 @@ namespace App\Controller;
use DateTime; use DateTime;
use Parsedown;
use App\Entity\Reference; use App\Entity\Reference;
use App\Entity\Template; use App\Entity\Template;
use App\Entity\User; use App\Entity\User;
@ -11,10 +12,14 @@ use App\Entity\Bible;
use App\Entity\Speaker; use App\Entity\Speaker;
use App\Entity\Series; use App\Entity\Series;
use App\Entity\Note; use App\Entity\Note;
use App\Entity\NoteShares;
use App\Utils\Utils;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
class AjaxController extends AbstractController class AjaxController extends AbstractController
@ -347,6 +352,61 @@ class AjaxController extends AbstractController
return $res; 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')] #[Route('/get-passage/{passage}', name: 'app_get_passage')]
public function getPassage($passage, EntityManagerInterface $emi): Response public function getPassage($passage, EntityManagerInterface $emi): Response
{ {