Skip to content

Instantly share code, notes, and snippets.

@msoroka
Last active November 2, 2022 13:57
Show Gist options
  • Save msoroka/824a002c73dd976851279004a350c2ac to your computer and use it in GitHub Desktop.
Save msoroka/824a002c73dd976851279004a350c2ac to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use App\Form\Type\CompetitionApplicationType;
use Carbon\Carbon;
use Exception;
use Pimcore\Controller\FrontendController;
use Pimcore\Mail;
use Pimcore\Model\DataObject\CompetitionApplication;
use Pimcore\Model\DataObject\Service;
use Pimcore\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends FrontendController
{
public function defaultAction(Request $request, Translator $translator): Response
{
$competitionApplication = new CompetitionApplication();
$form = $this->createForm(CompetitionApplicationType::class, $competitionApplication);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$competitionApplication = $form->getData();
try {
$this->processCompetitionApplication($competitionApplication);
$this->addFlash('success', $translator->trans('form.competition-application.success'));
} catch (Exception $e) {
$this->addFlash('error', $translator->trans('form.competition-application.error'));
}
}
return $this->render('default/default.html.twig', [
'form' => $form->createView()
]);
}
/**
* @throws Exception
*/
private function processCompetitionApplication(CompetitionApplication $competitionApplication): void
{
$now = Carbon::now();
$competitionApplication->setDate($now);
$competitionApplication->setKey($now->timestamp);
$competitionApplication->setPublished(true);
$competitionApplication->setParent(Service::createFolderByPath($now->format('/Y/m/d')));
$competitionApplication->save();
$mail = new Mail();
$mail->to('competition@example.com');
$mail->subject('New competition application');
$mail->html("<p>Firstname: {{ firstname }}</p><p>Email: {{ email }}</p><p>Message: {{ message }}</p>");
$mail->setParams([
'firstname' => $competitionApplication->getFirstname(),
'email' => $competitionApplication->getEmail(),
'message' => $competitionApplication->getMessage(),
]);
$mail->send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment