first commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries;
|
||||
|
||||
use CodeIgniter\Email\Email;
|
||||
use Config\Services;
|
||||
|
||||
class EmailLibrary
|
||||
{
|
||||
/**
|
||||
* Отправить письмо для подтверждения email
|
||||
*/
|
||||
public function sendVerificationEmail(string $email, string $name, string $token): bool
|
||||
{
|
||||
$emailConfig = config('Email');
|
||||
|
||||
// Генерируем URL для подтверждения
|
||||
$verificationUrl = base_url('/auth/verify/' . $token);
|
||||
|
||||
// Рендерим HTML письма через Twig
|
||||
$twig = Services::twig();
|
||||
$htmlBody = $twig->render('emails/verification', [
|
||||
'name' => $name,
|
||||
'verification_url' => $verificationUrl,
|
||||
'app_name' => $emailConfig->fromName ?? 'Бизнес.Точка',
|
||||
]);
|
||||
|
||||
$emailer = Services::email($emailConfig);
|
||||
$emailer->setTo($email);
|
||||
$emailer->setFrom($emailConfig->fromEmail, $emailConfig->fromName);
|
||||
$emailer->setSubject('Подтверждение регистрации');
|
||||
$emailer->setMessage($htmlBody);
|
||||
|
||||
try {
|
||||
return $emailer->send();
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Ошибка отправки email: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправить уведомление об успешной регистрации (после подтверждения)
|
||||
*/
|
||||
public function sendWelcomeEmail(string $email, string $name): bool
|
||||
{
|
||||
$emailConfig = config('Email');
|
||||
|
||||
$twig = Services::twig();
|
||||
$htmlBody = $twig->render('emails/welcome', [
|
||||
'name' => $name,
|
||||
'app_name' => $emailConfig->fromName ?? 'Бизнес.Точка',
|
||||
]);
|
||||
|
||||
$emailer = Services::email($emailConfig);
|
||||
$emailer->setTo($email);
|
||||
$emailer->setFrom($emailConfig->fromEmail, $emailConfig->fromName);
|
||||
$emailer->setSubject('Добро пожаловать!');
|
||||
$emailer->setMessage($htmlBody);
|
||||
|
||||
try {
|
||||
return $emailer->send();
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Ошибка отправки email: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
use Twig\TwigGlobal;
|
||||
use App\Models\OrganizationModel;
|
||||
use Config\Services;
|
||||
|
||||
class TwigGlobalsExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_session', [$this, 'getSession'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_current_org', [$this, 'getCurrentOrg'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_alerts', [$this, 'getAlerts'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('render_pager', [$this, 'renderPager'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('is_active_route', [$this, 'isActiveRoute'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('get_current_route', [$this, 'getCurrentRoute'], ['is_safe' => ['html']]),
|
||||
];
|
||||
}
|
||||
|
||||
public function getSession()
|
||||
{
|
||||
return session();
|
||||
}
|
||||
|
||||
public function getCurrentOrg()
|
||||
{
|
||||
$session = session();
|
||||
$activeOrgId = $session->get('active_org_id');
|
||||
|
||||
if ($activeOrgId) {
|
||||
$orgModel = new OrganizationModel();
|
||||
return $orgModel->find($activeOrgId);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getAlerts(): array
|
||||
{
|
||||
$session = session();
|
||||
|
||||
$alerts = [];
|
||||
$types = ['success', 'error', 'warning', 'info'];
|
||||
foreach ($types as $type) {
|
||||
if ($msg = $session->getFlashdata($type)) {
|
||||
$alerts[] = ['type' => $type, 'message' => $msg];
|
||||
}
|
||||
}
|
||||
|
||||
if ($validationErrors = $session->getFlashdata('errors')) {
|
||||
foreach ($validationErrors as $error) {
|
||||
$alerts[] = ['type' => 'error', 'message' => $error];
|
||||
}
|
||||
}
|
||||
|
||||
return $alerts;
|
||||
}
|
||||
|
||||
public function renderPager($pager)
|
||||
{
|
||||
if (!$pager) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $pager->links();
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверяет, является ли текущий маршрут активным
|
||||
*/
|
||||
public function isActiveRoute($routes, $exact = false): bool
|
||||
{
|
||||
$currentRoute = $this->getCurrentRoute();
|
||||
|
||||
if (is_string($routes)) {
|
||||
$routes = [$routes];
|
||||
}
|
||||
|
||||
foreach ($routes as $route) {
|
||||
if ($exact) {
|
||||
// Точное совпадение
|
||||
if ($currentRoute === $route) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// Частичное совпадение (начинается с)
|
||||
// Исключаем пустую строку, так как она совпадает с любым маршрутом
|
||||
if ($route === '') {
|
||||
// Для пустого маршрута проверяем только корень
|
||||
if ($currentRoute === '') {
|
||||
return true;
|
||||
}
|
||||
} elseif (strpos($currentRoute, $route) === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Получает текущий маршрут без базового URL
|
||||
*/
|
||||
public function getCurrentRoute(): string
|
||||
{
|
||||
$uri = service('uri');
|
||||
$route = $uri->getRoutePath();
|
||||
|
||||
// Убираем начальный слеш если есть
|
||||
return ltrim($route, '/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Libraries\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class TwigJsonDecodeExtension extends AbstractExtension
|
||||
{
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('json_decode', [$this, 'jsonDecode']),
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonDecode(string $json, bool $assoc = true, int $depth = 512, int $flags = 0)
|
||||
{
|
||||
return json_decode($json, $assoc, $depth, $flags);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user