- Fix storage permissions to use 'www' user instead of 'www-data' - Fix UserModel getAllForAdmin query to avoid placeholders in LIMIT/OFFSET - Import missing modules: Advertising, Dashboard, Analytics - Update deploy script to copy files to public directory
355 lines
14 KiB
PHP
355 lines
14 KiB
PHP
<?php
|
|
/**
|
|
* Modular Dashboard View
|
|
*
|
|
* Renders user's customized dashboard with widgets from different modules.
|
|
* Widgets are displayed in user-defined order and can be customized.
|
|
*/
|
|
|
|
$widgets = $widgets ?? [];
|
|
$widget_manager = $widget_manager ?? null;
|
|
?>
|
|
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<!-- Dashboard Controls -->
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<div>
|
|
<h4 class="mb-1">Dashboard Personalizado</h4>
|
|
<p class="text-muted mb-0">Widgets dos seus módulos ativos</p>
|
|
</div>
|
|
<div>
|
|
<button type="button" class="btn btn-outline-primary btn-sm" onclick="openDashboardSettings()">
|
|
<i class="ri-settings-3-line"></i> Personalizar
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (empty($widgets)): ?>
|
|
<!-- No Widgets State -->
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<div class="card">
|
|
<div class="card-body text-center py-5">
|
|
<div class="avatar-lg mx-auto mb-4">
|
|
<div class="avatar-title bg-primary-subtle text-primary rounded-circle">
|
|
<i class="ri-dashboard-3-line fs-1"></i>
|
|
</div>
|
|
</div>
|
|
<h5 class="mb-3">Dashboard Vazio</h5>
|
|
<p class="text-muted mb-4">Ainda não tem widgets configurados no seu dashboard.</p>
|
|
<button type="button" class="btn btn-primary" onclick="openDashboardSettings()">
|
|
<i class="ri-add-line"></i> Adicionar Widgets
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<!-- Widgets Grid -->
|
|
<div class="row" id="dashboard-widgets">
|
|
<?php foreach ($widgets as $widget): ?>
|
|
<?php
|
|
$size = $widget['effective_size'] ?? 'medium';
|
|
$colClass = match($size) {
|
|
'small' => 'col-xl-3 col-lg-4 col-md-6',
|
|
'medium' => 'col-xl-4 col-lg-6 col-md-6',
|
|
'large' => 'col-xl-6 col-lg-8 col-md-12',
|
|
'full' => 'col-12',
|
|
default => 'col-xl-4 col-lg-6 col-md-6'
|
|
};
|
|
?>
|
|
<div class="<?php echo $colClass; ?> mb-4" data-widget-id="<?php echo htmlspecialchars($widget['widget_id']); ?>">
|
|
<div class="card widget-card h-100">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<h6 class="card-title mb-0">
|
|
<i class="ri-widget-line me-1"></i>
|
|
<?php echo htmlspecialchars($widget['widget_name']); ?>
|
|
</h6>
|
|
<div class="dropdown">
|
|
<button class="btn btn-ghost-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
|
|
<i class="ri-more-2-line"></i>
|
|
</button>
|
|
<ul class="dropdown-menu dropdown-menu-end">
|
|
<li><a class="dropdown-item" href="#" onclick="configureWidget('<?php echo $widget['widget_id']; ?>')">
|
|
<i class="ri-settings-3-line"></i> Configurar
|
|
</a></li>
|
|
<li><a class="dropdown-item" href="#" onclick="hideWidget('<?php echo $widget['widget_id']; ?>')">
|
|
<i class="ri-eye-off-line"></i> Ocultar
|
|
</a></li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if ($widget_manager): ?>
|
|
<?php echo $widget_manager->renderWidget($widget); ?>
|
|
<?php else: ?>
|
|
<div class="alert alert-warning">
|
|
<i class="ri-alert-line"></i> Widget manager não disponível
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Dashboard Settings Modal -->
|
|
<div class="modal fade" id="dashboardSettingsModal" tabindex="-1">
|
|
<div class="modal-dialog modal-lg">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Personalizar Dashboard</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<h6>Widgets Disponíveis</h6>
|
|
<div id="available-widgets" class="border rounded p-3" style="min-height: 300px;">
|
|
<!-- Populated via AJAX -->
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<h6>Widgets Ativos</h6>
|
|
<div id="active-widgets" class="border rounded p-3" style="min-height: 300px;">
|
|
<!-- Populated via AJAX -->
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
|
|
<button type="button" class="btn btn-primary" onclick="saveDashboardSettings()">Guardar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// Dashboard customization functions
|
|
function openDashboardSettings() {
|
|
const modal = new bootstrap.Modal(document.getElementById('dashboardSettingsModal'));
|
|
loadAvailableWidgets();
|
|
loadActiveWidgets();
|
|
modal.show();
|
|
}
|
|
|
|
function loadAvailableWidgets() {
|
|
fetch('/api/dashboard/available-widgets')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const container = document.getElementById('available-widgets');
|
|
container.innerHTML = data.widgets.map(widget => `
|
|
<div class="d-flex justify-content-between align-items-center p-2 border-bottom">
|
|
<div>
|
|
<strong>${widget.widget_name}</strong>
|
|
<br><small class="text-muted">${widget.widget_description || ''}</small>
|
|
</div>
|
|
<button class="btn btn-sm btn-outline-primary" onclick="addWidget('${widget.widget_id}')">
|
|
<i class="ri-add-line"></i>
|
|
</button>
|
|
</div>
|
|
`).join('');
|
|
})
|
|
.catch(error => console.error('Error loading widgets:', error));
|
|
}
|
|
|
|
function loadActiveWidgets() {
|
|
// Load user's active widgets for reordering
|
|
fetch('/api/dashboard/user-widgets')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
const container = document.getElementById('active-widgets');
|
|
container.innerHTML = data.widgets.map((widget, index) => `
|
|
<div class="d-flex justify-content-between align-items-center p-2 border-bottom" data-widget-id="${widget.widget_id}">
|
|
<div>
|
|
<strong>${widget.widget_name}</strong>
|
|
<br><small class="text-muted">Posição: ${widget.position_order}</small>
|
|
</div>
|
|
<div>
|
|
<button class="btn btn-sm btn-outline-secondary me-1" onclick="moveWidget('${widget.widget_id}', 'up')">
|
|
<i class="ri-arrow-up-line"></i>
|
|
</button>
|
|
<button class="btn btn-sm btn-outline-secondary me-1" onclick="moveWidget('${widget.widget_id}', 'down')">
|
|
<i class="ri-arrow-down-line"></i>
|
|
</button>
|
|
<button class="btn btn-sm btn-outline-danger" onclick="removeWidget('${widget.widget_id}')">
|
|
<i class="ri-delete-bin-line"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
`).join('');
|
|
})
|
|
.catch(error => console.error('Error loading active widgets:', error));
|
|
}
|
|
|
|
function addWidget(widgetId) {
|
|
fetch('/api/dashboard/add-widget', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({widget_id: widgetId})
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
loadActiveWidgets();
|
|
showToast('Widget adicionado com sucesso!', 'success');
|
|
}
|
|
})
|
|
.catch(error => console.error('Error adding widget:', error));
|
|
}
|
|
|
|
function removeWidget(widgetId) {
|
|
fetch('/api/dashboard/remove-widget', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({widget_id: widgetId})
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
loadActiveWidgets();
|
|
showToast('Widget removido com sucesso!', 'success');
|
|
}
|
|
})
|
|
.catch(error => console.error('Error removing widget:', error));
|
|
}
|
|
|
|
function saveDashboardSettings() {
|
|
location.reload(); // Reload to show changes
|
|
}
|
|
|
|
function hideWidget(widgetId) {
|
|
if (confirm('Tem certeza que deseja ocultar este widget?')) {
|
|
removeWidget(widgetId);
|
|
}
|
|
}
|
|
|
|
function configureWidget(widgetId) {
|
|
// Implement widget-specific configuration
|
|
const widgetConfigs = {
|
|
'error_summary': {
|
|
title: 'Configurar Widget de Erros',
|
|
fields: [
|
|
{name: 'refresh_interval', label: 'Intervalo de Atualização (segundos)', type: 'number', default: 30},
|
|
{name: 'show_critical_only', label: 'Mostrar apenas erros críticos', type: 'checkbox', default: false},
|
|
{name: 'max_errors', label: 'Máximo de erros a mostrar', type: 'number', default: 10}
|
|
]
|
|
},
|
|
'advertising_stats': {
|
|
title: 'Configurar Widget de Publicidade',
|
|
fields: [
|
|
{name: 'campaign_filter', label: 'Filtrar por campanha', type: 'select', options: ['Todas', 'Ativas', 'Pausadas']},
|
|
{name: 'date_range', label: 'Período', type: 'select', options: ['Hoje', 'Últimos 7 dias', 'Últimos 30 dias']},
|
|
{name: 'show_creatives', label: 'Mostrar criativos', type: 'checkbox', default: true}
|
|
]
|
|
},
|
|
'crm_overview': {
|
|
title: 'Configurar Widget CRM',
|
|
fields: [
|
|
{name: 'show_deals', label: 'Mostrar deals', type: 'checkbox', default: true},
|
|
{name: 'show_leads', label: 'Mostrar leads', type: 'checkbox', default: true},
|
|
{name: 'show_contacts', label: 'Mostrar contactos', type: 'checkbox', default: true},
|
|
{name: 'refresh_interval', label: 'Intervalo de Atualização (segundos)', type: 'number', default: 60}
|
|
]
|
|
}
|
|
};
|
|
|
|
const config = widgetConfigs[widgetId];
|
|
if (!config) {
|
|
alert('Configuração não disponível para este widget');
|
|
return;
|
|
}
|
|
|
|
// Criar modal de configuração
|
|
const modal = document.createElement('div');
|
|
modal.className = 'modal fade';
|
|
modal.innerHTML = `
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">${config.title}</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<form id="widget-config-form">
|
|
${config.fields.map(field => `
|
|
<div class="mb-3">
|
|
<label class="form-label">${field.label}</label>
|
|
${field.type === 'checkbox' ?
|
|
`<input type="checkbox" class="form-check-input" name="${field.name}" ${field.default ? 'checked' : ''}>` :
|
|
field.type === 'select' ?
|
|
`<select class="form-select" name="${field.name}">
|
|
${field.options.map(opt => `<option value="${opt}">${opt}</option>`).join('')}
|
|
</select>` :
|
|
`<input type="${field.type}" class="form-control" name="${field.name}" value="${field.default || ''}">`
|
|
}
|
|
</div>
|
|
`).join('')}
|
|
</form>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
|
|
<button type="button" class="btn btn-primary" onclick="saveWidgetConfig('${widgetId}')">Guardar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
document.body.appendChild(modal);
|
|
const modalInstance = new bootstrap.Modal(modal);
|
|
modalInstance.show();
|
|
|
|
// Remover modal do DOM quando fechado
|
|
modal.addEventListener('hidden.bs.modal', () => {
|
|
document.body.removeChild(modal);
|
|
});
|
|
}
|
|
|
|
function saveWidgetConfig(widgetId) {
|
|
const form = document.getElementById('widget-config-form');
|
|
const formData = new FormData(form);
|
|
const config = {};
|
|
|
|
for (let [key, value] of formData.entries()) {
|
|
config[key] = value;
|
|
}
|
|
|
|
// Guardar configuração
|
|
fetch('/api/dashboard/widget-config', {
|
|
method: 'POST',
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify({
|
|
widget_id: widgetId,
|
|
config: config
|
|
})
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
showToast('Configuração guardada com sucesso!', 'success');
|
|
bootstrap.Modal.getInstance(document.querySelector('.modal')).hide();
|
|
} else {
|
|
showToast('Erro ao guardar configuração', 'danger');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error saving widget config:', error);
|
|
showToast('Erro ao guardar configuração', 'danger');
|
|
});
|
|
}
|
|
|
|
function showToast(message, type = 'info') {
|
|
// Simple toast notification - could be enhanced
|
|
const toast = document.createElement('div');
|
|
toast.className = `alert alert-${type} position-fixed top-0 end-0 m-3`;
|
|
toast.style.zIndex = '9999';
|
|
toast.innerHTML = message;
|
|
document.body.appendChild(toast);
|
|
setTimeout(() => toast.remove(), 3000);
|
|
}
|
|
</script>
|