Files
dockerfluxodecaixapro/app/Helpers/QuillHelper.php.bak
T
2026-09-01 10:46:29 -03:00

221 lines
6.8 KiB
Plaintext
Executable File

<?php
namespace App\Helpers;
/**
* Helper para gerenciar o Editor Quill no sistema SysCMS
*/
class QuillHelper
{
/**
* Renderiza um campo de texto com o editor Quill
*
* @param string $name Nome do campo
* @param string $id ID do elemento
* @param string $value Valor inicial
* @param array $options Opções adicionais
* @return string HTML do campo com o editor
*/
public static function renderEditor(string $name, string $id, string $value = '', array $options = []): string
{
// Opções padrão
$defaultOptions = [
'class' => 'quill-editor',
'style' => 'height: 300px;',
'placeholder' => 'Digite seu conteúdo aqui...'
];
// Mesclar opções
$options = array_merge($defaultOptions, $options);
// Construir atributos
$attributes = '';
foreach ($options as $key => $value) {
if ($key !== 'value') {
$attributes .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
}
}
// Retornar HTML do campo
return sprintf(
'<textarea name="%s" id="%s"%s>%s</textarea>',
htmlspecialchars($name),
htmlspecialchars($id),
$attributes,
htmlspecialchars($value)
);
}
/**
* Renderiza um campo de texto simples (sem Quill)
*
* @param string $name Nome do campo
* @param string $id ID do elemento
* @param string $value Valor inicial
* @param array $options Opções adicionais
* @return string HTML do campo
*/
public static function renderTextarea(string $name, string $id, string $value = '', array $options = []): string
{
// Opções padrão
$defaultOptions = [
'class' => 'form-control',
'rows' => 5
];
// Mesclar opções
$options = array_merge($defaultOptions, $options);
// Construir atributos
$attributes = '';
foreach ($options as $key => $value) {
if ($key !== 'value') {
$attributes .= ' ' . $key . '="' . htmlspecialchars($value) . '"';
}
}
// Retornar HTML do campo
return sprintf(
'<textarea name="%s" id="%s"%s>%s</textarea>',
htmlspecialchars($name),
htmlspecialchars($id),
$attributes,
htmlspecialchars($value)
);
}
/**
* Renderiza um campo de texto com editor (Quill ou textarea simples)
* baseado na configuração do sistema
*
* @param string $name Nome do campo
* @param string $id ID do elemento
* @param string $value Valor inicial
* @param array $options Opções adicionais
* @return string HTML do campo
*/
public static function renderField(string $name, string $id, string $value = '', array $options = []): string
{
// Verificar se o Quill está habilitado
$useQuill = self::isQuillEnabled();
if ($useQuill) {
return self::renderEditor($name, $id, $value, $options);
} else {
return self::renderTextarea($name, $id, $value, $options);
}
}
/**
* Verifica se o editor Quill está habilitado no sistema
*
* @return bool True se o Quill estiver habilitado
*/
public static function isQuillEnabled(): bool
{
// Verificar variável de ambiente
$useQuill = getenv('APP_EDITOR_QUILL') ?: 'true';
return filter_var($useQuill, FILTER_VALIDATE_BOOLEAN);
}
/**
* Obtém a configuração do Quill em formato JSON
*
* @return string Configuração em formato JSON
*/
public static function getQuillConfig(): string
{
$config = [
'theme' => 'snow',
'placeholder' => 'Digite seu conteúdo aqui...',
'modules' => [
'toolbar' => [
['bold', 'italic', 'underline', 'strike'],
['blockquote', 'code-block'],
[{ 'header': [1, 2] }],
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
[{ 'script': 'sub'}, { 'script': 'super' }],
[{ 'indent': '-1'}, { 'indent': '+1' }],
[{ 'direction': 'rtl' }],
[{ 'size': ['small', false, 'large', 'huge'] }],
[{ 'header': [1, 2, 3, 4, 5, 6, false] }],
[{ 'color': [] }, { 'background': [] }],
[{ 'font': [] }],
[{ 'align': [] }],
['link', 'image'],
['clean']
]
]
];
return json_encode($config);
}
/**
* Renderiza o script de inicialização do Quill
*
* @return string Script de inicialização
*/
public static function renderInitScript(): string
{
if (!self::isQuillEnabled()) {
return '';
}
$config = self::getQuillConfig();
$configJson = json_encode($config);
return <<<HTML
<script>
// Configuração personalizada do Quill
const quillSysConfig = {$configJson};
// Sobrescrever configuração padrão se necessário
if (typeof window.quillDefaultConfig !== 'undefined') {
window.quillDefaultConfig = Object.assign(window.quillDefaultConfig, quillSysConfig);
}
</script>
HTML;
}
/**
* Limpa o conteúdo HTML gerado pelo Quill para segurança
*
* @param string $html Conteúdo HTML
* @return string HTML limpo
*/
public static function sanitizeHtml(string $html): string
{
// Lista de tags permitidas
$allowedTags = '<p><br><strong><em><u><s><h1><h2><h3><h4><h5><h6><ul><ol><li><blockquote><pre><code><a><img>';
// Remover atributos perigosos
$html = strip_tags($html, $allowedTags);
// Remover atributos de estilo e eventos
$html = preg_replace('/\s*(on\w+|style|id|class)=["\'][^"\']*["\']/', '', $html);
return $html;
}
/**
* Converte o conteúdo do Quill para texto puro
*
* @param string $html Conteúdo HTML
* @return string Texto puro
*/
public static function htmlToText(string $html): string
{
// Remover tags HTML
$text = strip_tags($html);
// Converter entidades HTML
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
// Normalizar espaços em branco
$text = preg_replace('/\s+/', ' ', $text);
return trim($text);
}
}