Skip to content

Commit 5754cc4

Browse files
committed
Admin: Migrate email tester to Symfony form and controller structure
1 parent 35a819f commit 5754cc4

File tree

6 files changed

+143
-100
lines changed

6 files changed

+143
-100
lines changed

public/main/admin/email_tester.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

public/main/template/default/admin/email_tester.html.twig

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/CoreBundle/Controller/Admin/AdminController.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
use Chamilo\CoreBundle\Entity\ResourceLink;
1414
use Chamilo\CoreBundle\Entity\ResourceNode;
1515
use Chamilo\CoreBundle\Entity\ResourceType;
16+
use Chamilo\CoreBundle\Form\TestEmailType;
1617
use Chamilo\CoreBundle\Framework\Container;
1718
use Chamilo\CoreBundle\Helpers\AccessUrlHelper;
1819
use Chamilo\CoreBundle\Helpers\CidReqHelper;
20+
use Chamilo\CoreBundle\Helpers\MailHelper;
1921
use Chamilo\CoreBundle\Helpers\QueryCacheHelper;
2022
use Chamilo\CoreBundle\Helpers\TempUploadHelper;
2123
use Chamilo\CoreBundle\Helpers\UserHelper;
@@ -27,13 +29,16 @@
2729
use Chamilo\CourseBundle\Entity\CDocument;
2830
use Doctrine\DBAL\Connection;
2931
use Doctrine\ORM\EntityManagerInterface;
32+
use MessageManager;
3033
use RuntimeException;
3134
use Symfony\Component\HttpFoundation\JsonResponse;
3235
use Symfony\Component\HttpFoundation\Request;
3336
use Symfony\Component\HttpFoundation\Response;
34-
use Symfony\Component\Routing\Annotation\Route;
37+
use Symfony\Component\Routing\Attribute\Route;
3538
use Symfony\Component\Security\Http\Attribute\IsGranted;
39+
use Symfony\Contracts\Translation\TranslatorInterface;
3640
use Throwable;
41+
use UserManager;
3742

3843
#[Route('/admin')]
3944
class AdminController extends BaseController
@@ -727,6 +732,50 @@ public function runCleanupTempUploads(
727732
return $this->redirectToRoute('admin_cleanup_temp_uploads', [], Response::HTTP_SEE_OTHER);
728733
}
729734

735+
#[IsGranted('ROLE_ADMIN')]
736+
#[Route('/email_tester', name: 'admin_email_tester')]
737+
public function testEmail(
738+
Request $request,
739+
MailHelper $mailHelper,
740+
TranslatorInterface $translator,
741+
SettingsManager $settingsManager,
742+
UserHelper $userHelper,
743+
): Response {
744+
$errorsInfo = MessageManager::failedSentMailErrors();
745+
746+
$form = $this->createForm(TestEmailType::class);
747+
$form->handleRequest($request);
748+
749+
if ($form->isSubmitted() && $form->isValid()) {
750+
$data = $form->getData();
751+
752+
$mailHelper->send(
753+
$translator->trans('User testing of e-mail configuration'),
754+
$data['destination'],
755+
$data['subject'],
756+
$data['content'],
757+
UserManager::formatUserFullName($userHelper->getCurrent()),
758+
$settingsManager->getSetting('mail.mailer_from_email')
759+
);
760+
761+
$this->addFlash(
762+
'success',
763+
$translator->trans('E-mail sent. This procedure works in all aspects similarly to the normal e-mail sending of Chamilo, but allows for more flexibility in terms of destination e-mail and message body.')
764+
);
765+
766+
return $this->redirectToRoute('admin_email_tester');
767+
}
768+
769+
return $this->render(
770+
'@ChamiloCore/Admin/email_tester.html.twig',
771+
[
772+
'mailer_dsn' => $settingsManager->getSetting('mail.mailer_dsn'),
773+
'form' => $form,
774+
'errors' => $errorsInfo,
775+
]
776+
);
777+
}
778+
730779
/**
731780
* Create a visible CDocument in a course from an existing ResourceFile.
732781
*/

src/CoreBundle/Controller/Admin/IndexBlocksController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ private function getItemsSettings(): array
613613
if (is_dir(api_get_path(SYS_TEST_PATH))) {
614614
$items[] = [
615615
'class' => 'item-email-tester',
616-
'url' => '/main/admin/email_tester.php',
616+
'url' => '/admin/email_tester',
617617
'label' => $this->translator->trans('E-mail tester'),
618618
];
619619
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/* For licensing terms, see /license.txt */
4+
5+
namespace Chamilo\CoreBundle\Form;
6+
7+
use Chamilo\CoreBundle\Settings\SettingsManager;
8+
use Symfony\Component\Form\AbstractType;
9+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
10+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
11+
use Symfony\Component\Form\Extension\Core\Type\TextType;
12+
use Symfony\Component\Form\FormBuilderInterface;
13+
use Symfony\Component\OptionsResolver\OptionsResolver;
14+
use Symfony\Contracts\Translation\TranslatorInterface;
15+
16+
class TestEmailType extends AbstractType
17+
{
18+
public function __construct(
19+
private readonly TranslatorInterface $translator,
20+
) {}
21+
22+
public function buildForm(FormBuilderInterface $builder, array $options): void
23+
{
24+
$builder
25+
->add(
26+
'destination',
27+
EmailType::class,
28+
[
29+
'label' => $this->translator->trans('Destination'),
30+
]
31+
)
32+
->add(
33+
'subject',
34+
TextType::class,
35+
[
36+
'label' => $this->translator->trans('Subject'),
37+
]
38+
)
39+
->add(
40+
'content',
41+
TextareaType::class,
42+
[
43+
'label' => $this->translator->trans('Message'),
44+
'attr' => [
45+
'rows' => 20,
46+
],
47+
]
48+
)
49+
;
50+
}
51+
52+
public function configureOptions(OptionsResolver $resolver): void
53+
{
54+
$resolver->setDefaults([
55+
// Configure your form options here
56+
]);
57+
}
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{% extends "@ChamiloCore/Layout/layout_one_col.html.twig" %}
2+
3+
{% block content %}
4+
<div class="row">
5+
<div class="col-md-6">
6+
<p>
7+
<strong>{{ 'Mailer DSN'|trans }}</strong><br>
8+
<code>{{ mailer_dsn }}</code>
9+
</p>
10+
<br>
11+
12+
{{ form_start(form) }}
13+
{{ form_row(form.destination) }}
14+
{{ form_row(form.subject) }}
15+
{{ form_row(form.content) }}
16+
17+
<button class="btn btn--primary">{{ 'Send test email'|trans }}</button>
18+
{{ form_end(form) }}
19+
</div>
20+
<div class="col-md-6">
21+
{% if errors is not empty %}
22+
<h4 class="page-header">{{ 'Errors'|get_lang }}</h4>
23+
<ul>
24+
{% for error in errors %}
25+
<li>
26+
{{ 'Email: %s. %s ago'|format(error.mail, error.time) }}
27+
<pre>{{ error.reason|replace({'\n': '<br>'}) }}</pre>
28+
</li>
29+
{% endfor %}
30+
</ul>
31+
{% endif %}
32+
</div>
33+
</div>
34+
{% endblock %}

0 commit comments

Comments
 (0)