Source Controller

This commit is contained in:
Ryan Prather 2024-05-13 21:08:06 -04:00
parent 81fecfbc59
commit 21f014d08d
6 changed files with 677 additions and 0 deletions

0
src/Controller/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,358 @@
<?php
namespace App\Controller;
use DateTime;
use App\Entity\Reference;
use App\Entity\Template;
use App\Entity\User;
use App\Entity\Bible;
use App\Entity\Speaker;
use App\Entity\Series;
use App\Entity\Note;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class AjaxController extends AbstractController
{
/**
* A method to retrieve a template
*
* @param Request $req description
* @param EntityManagerInterface $emi description
*
* @return Response
*/
#[Route('/retrieve-template', name: 'app_retrieve_template')]
public function retrieveTemplate(Request $req, EntityManagerInterface $emi): Response
{
$ret = new Response();
$template_id = json_decode($req->getContent())->template;
$template = $emi->getRepository(Template::class)->find($template_id);
$ret->setContent($template->getContent());
return $ret;
}
#[Route('/save-template', name: 'app_save_template')]
public function saveTemplate(Request $req, EntityManagerInterface $emi): Response
{
$ret = new Response();
$req = json_decode($req->getContent());
$template_value = $req->template_value;
$template_id = $req->template_id;
$template_name = $req->template_name;
$user = $this->getUser();
if($template_id) {
$template = $emi->getRepository(Template::class)->find($template_id);
$template->setName($template_name);
$template->setContent($template_value);
$template->setUser($user);
$emi->persist($template);
$emi->flush();
$ret->setContent('Updated');
} else {
$template = new Template();
$template->setName($template_name);
$template->setContent($template_value);
$template->setUser($user);
$emi->persist($template);
$emi->flush();
$ret->setContent('Added');
}
return $ret;
}
/**
* Retrieve a speaker from the database based on the given request and entity manager.
*
* @param Request $req The request object
* @param EntityManagerInterface $emi The entity manager
* @return Response
*/
#[Route('/retrieve-speaker', name: 'app_retrieve_speaker')]
public function retrieveSpeaker(Request $req, EntityManagerInterface $emi): Response
{
$ret = new Response();
$speaker_id = json_decode($req->getContent())->speaker;
$speaker = $emi->getRepository(Speaker::class)->find($speaker_id);
$ret->setContent($speaker->getProfile());
return $ret;
}
#[Route('/save-speaker', name: 'app_save_speaker')]
public function saveSpeaker(Request $req, EntityManagerInterface $emi): Response
{
$ret = new Response();
$data = json_decode($req->getContent());
$user = $this->getUser();
$speaker = new Speaker();
$speaker->setName($data->speakerName);
$speaker->setUser($user);
$emi->persist($speaker);
$emi->flush();
$ret->setContent(json_encode([
'id' => $speaker->getId(),
'msg' => $speaker->getName().' added'
]));
return $ret;
}
/**
* Method to retrieve a series
*
* @param Request $req description
* @param EntityManagerInterface $emi description
*
* @return Response
*/
#[Route('/retrieve-series', name: 'app_retrieve_series')]
public function retrieveSeries(Request $req, EntityManagerInterface $emi): Response
{
$ret = new Response();
$series_id = json_decode($req->getContent())->series;
$series = $emi->getRepository(Series::class)->find($series_id);
$ret->setContent($series->getName());
return $ret;
}
#[Route('/save-series', name: 'app_save_series')]
public function saveSeries(Request $req, EntityManagerInterface $emi): Response
{
$ret = new Response();
$data = json_decode($req->getContent());
$user = $this->getUser();
$series = new Series();
$series->setName($data->seriesName);
$series->setUser($user);
$emi->persist($series);
$emi->flush();
$ret->setContent(json_encode([
'id' => $series->getId(),
'msg' => $series->getName().' added'
]));
return $ret;
}
#[Route('/retrieve-reference', name: 'app_retrive_reference')]
public function retrieveReference(Request $req, EntityManagerInterface $emi): Response
{
$res = new Response();
$data = json_decode($req->getContent());
$search = $data->reference;
$ref = new Reference();
$ref->setType($data->type);
if(is_numeric($data->book)) {
$ref->setNdx($data->book);
}
ReferenceController::$emi = $emi;
$ret = match(strtolower($data->type)) {
'bible' => ReferenceController::retrieveBible("{$data->book} {$search}"),
'hc' => ReferenceController::retrieveHC($ref),
'bc' => ReferenceController::retrieveBC($ref),
'cd' => ReferenceController::retrieveCD($ref),
'wcf' => ReferenceController::retrieveWCF($ref),
'wsc' => ReferenceController::retrieveWSC($ref),
'wlc' => ReferenceController::retrieveWLC($ref),
'creed' => ReferenceController::retrieveCreed($data->book)
};
if (!is_a($ret, Reference::class)) {
$ret = new Reference();
}
$res->setContent(json_encode([
'text' => $ret->getContent(),
'title' => "{$ret->getLabel()}",
]));
return $res;
}
#[Route('/get-reference', name: 'app_get_reference')]
public function getReference(Request $req, EntityManagerInterface $emi): Response
{
$res = new Response();
$data = json_decode($req->getContent());
$ret = match ($data->type) {
'creed' => '/Creeds/',
'bc' => '/Belgic/',
'hc' => '/Heidelberg/',
'cd' => '/Dort/',
'wcf' => '/Westminster/Confessions/',
'wsc' => '/Westminster/Shorter Catechism/',
'wlc' => '/Westminster/Larger Catechism/'
};
$fc = file_get_contents(dirname(dirname(__DIR__))."/references{$ret}{$data->file}");
$res->setContent(json_encode(['text' => $fc]));
return $res;
}
#[Route('/save-reference', name: 'app_save_reference')]
public function saveReference(Request $req, EntityManagerInterface $emi): Response
{
$res = new Response();
$data = json_decode($req->getContent());
$path = match($data->type) {
'creed' => 'Creeds',
'bc' => 'Belgic',
'hc' => 'Heidelberg',
'cd' => 'Dort',
'wcf' => 'Westminster/Confessions',
'wsc' => 'Westminster/Shorter Catechism',
'wlc' => 'Westminster/Larger Catechism'
};
$ret = file_put_contents(dirname(dirname(__DIR__))."/references/{$path}/{$data->file}", $data->text);
if($ret !== false) {
$res->setContent(json_encode(['msg' => 'File Saved']));
} else {
$res->setContent(json_encode(['msg' => 'Failed to save file']));
}
return $res;
}
#[Route('/search-note', name: 'app_open_note')]
public function openNote(Request $req, EntityManagerInterface $emi): Response
{
$res = new Response();
$data = json_decode($req->getContent());
$note = $emi->getRepository(Note::class)->findNote($data->search);
$res->setContent(json_encode($note));
return $res;
}
#[Route('/get-note', name: 'app_get_note')]
public function getNote(Request $req, EntityManagerInterface $emi): Response
{
$res = new Response();
$data = json_decode($req->getContent());
$note = $emi->getRepository(Note::class)->find($data->id);
$res->setContent(json_encode($note));
return $res;
}
#[Route('/save-note', name: 'app_save_note', methods: ['POST'])]
public function saveNote(Request $req, EntityManagerInterface $emi): Response
{
$data = json_decode($req->getContent());
$note = $emi->getRepository(Note::class)->find($data->id);
if (!$note) {
$note = new Note();
$note->setId($data->id);
}
$refs = json_decode(json_encode($data->refs), true);
$series = $emi->getRepository(Series::class)->find($data->series);
$speaker = $emi->getRepository(Speaker::class)->find($data->speaker);
$user = $emi->getRepository(User::class)->findBy(['email' => $data->user]);
if (is_array($user)) {
$user = $user[0];
}
$note->setUser($user)
->setTitle($data->title)
->setDate(new DateTime($data->date))
->setSeries($series)
->setSpeaker($speaker)
->setText($data->note)
->setPassage($data->passage)
->setRefs($refs);
$emi->persist($note);
$emi->flush();
$res = new Response();
$res->setContent(json_encode([
'msg' => 'saved',
'id' => $note->getId()
]));
return $res;
}
#[Route('/discard-note', name: 'app_discard_note', methods: ['POST'])]
public function discardNote(Request $req, EntityManagerInterface $emi): Response
{
$data = json_decode($req->getContent());
$note = $emi->getRepository(Note::class)->find($data->id);
$emi->remove($note);
$emi->flush();
$res = new Response();
$res->setContent(json_encode([
'msg' => 'deleted'
]));
return $res;
}
#[Route('/get-passage/{passage}', name: 'app_get_passage')]
public function getPassage($passage, EntityManagerInterface $emi): Response
{
$passage = str_replace('+', ' ', $passage);
$book = Bible::findBook($passage);
$chapter = Bible::findChapter($passage);
$bible = new Bible();
$bible->setBook($book);
$bible->setChapter($chapter);
$verse = Bible::findVerse($passage);
$verseArr = [];
if (is_bool($verse)) {
$verseArr[0] = 1;
$verseArr[1] = 176;
} elseif (is_int($verse)) {
$verseArr[0] = $verse;
$verseArr[1] = $verse;
}
$ret = $emi->getRepository(Bible::class)->findRange($bible, $verseArr);
if (is_array($ret)) {
$text = null;
foreach($ret as $b) {
$text .= "{$b->getVerse()}. {$b->getContent()}".PHP_EOL;
}
$bible->setContent($text);
} elseif (is_a($ret, Bible::class)) {
$bible->setContent($ret->getContent());
}
$res = new Response();
$res->setContent($bible->getContent());
return $res;
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Controller;
use App\Entity\Note;
use App\Entity\Reference;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Uid\Uuid;
class DefaultController extends AbstractController
{
#[Route('/', name: 'app_index')]
public function index(): Response
{
return $this->render('default/index.html.twig');
}
#[Route('/home', name: 'app_home')]
public function home(Request $req, EntityManagerInterface $emi, #[CurrentUser()] ?User $user): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$last4Notes = $emi->getRepository(Note::class)->getLast4Notes($user);
$uuid = Uuid::v4();
return $this->render('default/home.html.twig', [
'last4Notes' => $last4Notes,
'id' => $uuid,
'isAdmin' => $this->isGranted('ROLE_ADMIN'),
]);
}
#[Route('/reference-editor', name: 'app_reference_editor')]
public function referenceEditor(EntityManagerInterface $emi): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN');
$creeds = $emi->getRepository(Reference::class)->findByType('creed');
$belgic = $emi->getRepository(Reference::class)->findByType('belgic');
$heidelberg = $emi->getRepository(Reference::class)->findByType('heidelberg');
$dort = $emi->getRepository(Reference::class)->findByType('dort');
$wcf = $emi->getRepository(Reference::class)->findByType('wcf');
$wsc = $emi->getRepository(Reference::class)->findByType('wsc');
$wlc = $emi->getRepository(Reference::class)->findByType('wlc');
return $this->render('editors/reference-editor.html.twig', [
'creeds' => $creeds,
'belgic' => $belgic,
'heidelberg' => $heidelberg,
'dort' => $dort,
'wcf' => $wcf,
'wsc' => $wsc,
'wlc' => $wlc
]);
}
#[Route('/template-editor', name: 'app_template_editor')]
public function templateEditor(): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
return $this->render('editors/template-editor.html.twig');
}
}

View File

@ -0,0 +1,175 @@
<?php
namespace App\Controller;
use App\Entity\Bible;
use App\Entity\Reference;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ReferenceController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
public static EntityManagerInterface $emi;
/**
* Method to retrieve a Bible reference
*
* @param string $ref
*
* @return Reference
*/
public static function retrieveBible($ref): Reference
{
$book = Bible::findBook($ref);
$chapter = Bible::findChapter($ref);
$passage = Bible::findVerse($ref);
$bible = new Bible();
$bible->setBook($book);
$bible->setChapter($chapter);
$label = null;
if (is_array($passage)) {
$passage_start = $passage[0];
$passage_end = $passage[1];
$bible = self::$emi->getRepository(Bible::class)->findRange($bible, [$passage_start, $passage_end]);
$passage = "{$passage_start}-{$passage_end}";
$label = "{$bible[0]->getLabel()} {$bible[0]->getChapter()}:{$passage}";
} elseif (is_int($passage)) {
$bible = self::$emi->getRepository(Bible::class)->findBy(['book' => $book, 'chapter' => $chapter, 'verse' => $passage]);
$label = "{$bible[0]->getLabel()} {$bible[0]->getChapter()}:{$passage}";
} elseif ($passage === false) {
$bible = self::$emi->getRepository(Bible::class)->findBy(['book' => $book, 'chapter' => $chapter]);
$label = "{$bible[0]->getLabel()} {$bible[0]->getChapter()}";
$passage = null;
}
if(is_array($bible)) {
$text = [];
foreach($bible as $b) {
$text[] = "{$b->getVerse()}. {$b->getContent()}";
}
} else {
$text[] = "{$bible->getVerse()}. {$bible->getContent()}";
}
$reference = "{$book} {$chapter}".($passage === null ? '' : ":{$passage}");
$ref = new Reference();
$ref->setType('bible');
$ref->setName($reference);
$ref->setLabel($label);
$ref->setContent("# {$reference}\n\n".implode("\n", $text));
return $ref;
}
/**
* Method to retrieve a creed
*
* @param string
*
* @return Reference
*/
public static function retrieveCreed($ref): ?Reference
{
$ret = null;
$r = self::$emi->getRepository(Reference::class)->findBy(['label' => $ref]);
if ($r && count($r) > 0) {
$ret = $r[0];
}
return $ret;
}
/**
* Method to retrieve a Heidelberg Catechism reference
*
* @param Reference $ref
*
* @return Reference
*/
public static function retrieveHC($ref): Reference
{
$r = self::$emi->getRepository(Reference::class)->findBy(['type' => 'hc', 'ndx' => $ref->getNdx()]);
if (!$r) {
return new Reference();
}
return $r[0];
}
/**
* Method to retrieve a Belgian Catechism reference
*
* @param Reference $ref
*
* @return Reference
*/
public static function retrieveBC($ref): Reference
{
$r = self::$emi->getRepository(Reference::class)->findBy(['type' => 'belgic', 'ndx' => $ref->getNdx()]);
if(!$r) {
return new Reference();
}
return $r[0];
}
/**
* Method to retrieve the Canon of Dort reference
*
* @param Reference $ref
*
* @return Reference
*/
public static function retrieveCD($ref): Reference
{
$r = self::$emi->getRepository(Reference::class)->findBy(['type' => 'cd', 'ndx' => $ref->getNdx()]);
if (!$r) {
return new Reference();
}
return $r[0];
}
/**
* Method to retrieve a WSC reference
*
* @param Reference $ref
*
* @return Reference
*/
public static function retrieveWSC($ref): Reference
{
$r = self::$emi->getRepository(Reference::class)->findBy(['type' => $ref->getType(), 'ndx' => $ref->getNdx()]);
return $r[0];
}
/**
* Method to retrieve a WLC reference
*
* @param Reference $ref
*
* @return Reference
*/
public static function retrieveWLC($ref): Reference
{
$r = self::$emi->getRepository(Reference::class)->findBy(['type' => $ref->getType(), 'ndx' => $ref->getNdx()]);
return $r[0];
}
/**
* Method to retrieve a WCF reference
*
* @param Reference $ref
*
* @return Reference
*/
public static function retrieveWCF($ref): Reference
{
$r = self::$emi->getRepository(Reference::class)->findBy(['type' => $ref->getType(), 'ndx' => $ref->getNdx()]);
return $r[0];
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
)
->setRoles(['ROLE_USER']);
$entityManager->persist($user);
$entityManager->flush();
return $this->redirectToRoute('app_home');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form,
]);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}