- 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
429 lines
21 KiB
PHP
429 lines
21 KiB
PHP
|
|
|
|
<div class="row" x-data="googleSheetsConfigure()">
|
|
<div class="col-12">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<div class="row align-items-center">
|
|
<div class="col">
|
|
<h4 class="card-title mb-0">
|
|
<i class="ri-google-line text-success me-2"></i>
|
|
Configuração Google Sheets
|
|
</h4>
|
|
<p class="text-muted mb-0">Configure as credenciais e URLs do Google Sheets</p>
|
|
</div>
|
|
<div class="col-auto">
|
|
<a href="/advertising/google-sheets-sync" class="btn btn-secondary">
|
|
<i class="ri-arrow-left-line me-1"></i> Voltar
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="card-body">
|
|
<!-- Loading State -->
|
|
<div x-show="loading" class="text-center py-5">
|
|
<div class="spinner-border text-primary" role="status">
|
|
<span class="visually-hidden">A carregar...</span>
|
|
</div>
|
|
<p class="mt-3">A carregar configurações...</p>
|
|
</div>
|
|
|
|
<!-- Error State -->
|
|
<div x-show="error && !loading" class="alert alert-danger">
|
|
<i class="ri-error-warning-line me-2"></i>
|
|
<span x-text="error"></span>
|
|
</div>
|
|
|
|
<!-- Configuration Form -->
|
|
<div x-show="!loading && !error">
|
|
<form @submit.prevent="saveConfiguration()">
|
|
<!-- Google Sheets API Configuration -->
|
|
<div class="row mb-4">
|
|
<div class="col-12">
|
|
<h5 class="text-muted mb-3">
|
|
<i class="ri-settings-3-line me-1"></i>
|
|
Configuração da API
|
|
</h5>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="mb-3">
|
|
<label for="apiKey" class="form-label">API Key do Google</label>
|
|
<input type="password" class="form-control" id="apiKey" x-model="config.api_key" required>
|
|
<small class="text-muted">Chave da API do Google Sheets</small>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="mb-3">
|
|
<label for="clientId" class="form-label">Client ID</label>
|
|
<input type="text" class="form-control" id="clientId" x-model="config.client_id" required>
|
|
<small class="text-muted">ID do cliente OAuth 2.0</small>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="mb-3">
|
|
<label for="clientSecret" class="form-label">Client Secret</label>
|
|
<input type="password" class="form-control" id="clientSecret" x-model="config.client_secret" required>
|
|
<small class="text-muted">Secret do cliente OAuth 2.0</small>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="mb-3">
|
|
<label for="redirectUri" class="form-label">Redirect URI</label>
|
|
<input type="url" class="form-control" id="redirectUri" x-model="config.redirect_uri" required>
|
|
<small class="text-muted">URI de redirecionamento OAuth</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Google Sheets URLs -->
|
|
<div class="row mb-4">
|
|
<div class="col-12">
|
|
<h5 class="text-muted mb-3">
|
|
<i class="ri-link me-1"></i>
|
|
URLs dos Google Sheets
|
|
</h5>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="mb-3">
|
|
<label for="campaignsSheetUrl" class="form-label">URL do Sheet de Campanhas</label>
|
|
<input type="url" class="form-control" id="campaignsSheetUrl" x-model="config.campaigns_sheet_url" required>
|
|
<small class="text-muted">URL completa do Google Sheet com campanhas</small>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="mb-3">
|
|
<label for="creativesSheetUrl" class="form-label">URL do Sheet de Criativos</label>
|
|
<input type="url" class="form-control" id="creativesSheetUrl" x-model="config.creatives_sheet_url" required>
|
|
<small class="text-muted">URL completa do Google Sheet com criativos</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Sync Settings -->
|
|
<div class="row mb-4">
|
|
<div class="col-12">
|
|
<h5 class="text-muted mb-3">
|
|
<i class="ri-sync-line me-1"></i>
|
|
Configurações de Sincronização
|
|
</h5>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<div class="mb-3">
|
|
<label for="syncInterval" class="form-label">Intervalo de Sincronização</label>
|
|
<select class="form-select" id="syncInterval" x-model="config.sync_interval">
|
|
<option value="hourly">A cada hora</option>
|
|
<option value="daily">Diariamente</option>
|
|
<option value="weekly">Semanalmente</option>
|
|
<option value="manual">Manual</option>
|
|
</select>
|
|
<small class="text-muted">Frequência de sincronização automática</small>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<div class="mb-3">
|
|
<label for="autoSync" class="form-label">Sincronização Automática</label>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" id="autoSync" x-model="config.auto_sync">
|
|
<label class="form-check-label" for="autoSync">Ativar sincronização automática</label>
|
|
</div>
|
|
<small class="text-muted">Sincroniza automaticamente quando há alterações</small>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-4">
|
|
<div class="mb-3">
|
|
<label for="notifyOnSync" class="form-label">Notificações</label>
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" id="notifyOnSync" x-model="config.notify_on_sync">
|
|
<label class="form-check-label" for="notifyOnSync">Notificar após sincronização</label>
|
|
</div>
|
|
<small class="text-muted">Envia notificação quando a sincronização termina</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Test Connection -->
|
|
<div class="row mb-4">
|
|
<div class="col-12">
|
|
<div class="card border">
|
|
<div class="card-body">
|
|
<h6 class="card-title mb-3">
|
|
<i class="ri-test-tube-line me-1"></i>
|
|
Testar Conexão
|
|
</h6>
|
|
<p class="text-muted">Teste a conexão com o Google Sheets antes de guardar</p>
|
|
|
|
<div class="d-flex gap-2">
|
|
<button type="button" class="btn btn-outline-primary" @click="testConnection()" :disabled="testing">
|
|
<i class="ri-test-tube-line me-1" x-show="!testing"></i>
|
|
<span class="spinner-border spinner-border-sm me-1" x-show="testing"></span>
|
|
<span x-text="testing ? 'A testar...' : 'Testar Conexão'"></span>
|
|
</button>
|
|
|
|
<button type="button" class="btn btn-outline-info" @click="previewSheets()" :disabled="previewing">
|
|
<i class="ri-eye-line me-1" x-show="!previewing"></i>
|
|
<span class="spinner-border spinner-border-sm me-1" x-show="previewing"></span>
|
|
<span x-text="previewing ? 'A carregar...' : 'Pré-visualizar'"></span>
|
|
</button>
|
|
</div>
|
|
|
|
<div x-show="testResult" class="mt-3">
|
|
<div class="alert" :class="testResult.success ? 'alert-success' : 'alert-danger'">
|
|
<i :class="testResult.success ? 'ri-check-line' : 'ri-error-warning-line'" class="me-2"></i>
|
|
<span x-text="testResult.message"></span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Action Buttons -->
|
|
<div class="row">
|
|
<div class="col-12">
|
|
<div class="d-flex justify-content-end gap-2">
|
|
<a href="/advertising/google-sheets-sync" class="btn btn-light">Cancelar</a>
|
|
<button type="submit" class="btn btn-primary" :disabled="saving">
|
|
<i class="ri-save-line me-1" x-show="!saving"></i>
|
|
<span class="spinner-border spinner-border-sm me-1" x-show="saving"></span>
|
|
<span x-text="saving ? 'A guardar...' : 'Guardar Configuração'"></span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Preview Modal -->
|
|
<div class="modal fade" id="previewModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-xl">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">
|
|
<i class="ri-eye-line me-2"></i>
|
|
Pré-visualização dos Dados
|
|
</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div x-show="previewData">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th>Campanha</th>
|
|
<th>Status</th>
|
|
<th>Data de Início</th>
|
|
<th>Data de Fim</th>
|
|
<th>Orçamento</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<template x-for="campaign in previewData.campaigns" :key="campaign.id">
|
|
<tr>
|
|
<td x-text="campaign.name"></td>
|
|
<td>
|
|
<span class="badge" :class="getStatusBadgeClass(campaign.status)">
|
|
<span x-text="campaign.status"></span>
|
|
</span>
|
|
</td>
|
|
<td x-text="formatDate(campaign.start_date)"></td>
|
|
<td x-text="formatDate(campaign.end_date)"></td>
|
|
<td x-text="formatCurrency(campaign.budget)"></td>
|
|
</tr>
|
|
</template>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Fechar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function googleSheetsConfigure() {
|
|
return {
|
|
loading: true,
|
|
error: null,
|
|
saving: false,
|
|
testing: false,
|
|
previewing: false,
|
|
testResult: null,
|
|
previewData: null,
|
|
config: {
|
|
api_key: '',
|
|
client_id: '',
|
|
client_secret: '',
|
|
redirect_uri: '',
|
|
campaigns_sheet_url: '',
|
|
creatives_sheet_url: '',
|
|
sync_interval: 'daily',
|
|
auto_sync: true,
|
|
notify_on_sync: true
|
|
},
|
|
|
|
init() {
|
|
this.loadConfiguration();
|
|
},
|
|
|
|
async loadConfiguration() {
|
|
try {
|
|
this.loading = true;
|
|
const response = await fetch('/api/advertising/google-sheets/config');
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
this.config = { ...this.config, ...data.data };
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro ao carregar configuração:', error);
|
|
this.error = 'Erro ao carregar configuração';
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
async saveConfiguration() {
|
|
try {
|
|
this.saving = true;
|
|
|
|
const response = await fetch('/api/advertising/google-sheets/config', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(this.config)
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
this.showSuccess('Configuração guardada com sucesso!');
|
|
} else {
|
|
this.showError(data.message || 'Erro ao guardar configuração');
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro ao guardar configuração:', error);
|
|
this.showError('Erro de conexão');
|
|
} finally {
|
|
this.saving = false;
|
|
}
|
|
},
|
|
|
|
async testConnection() {
|
|
try {
|
|
this.testing = true;
|
|
this.testResult = null;
|
|
|
|
const response = await fetch('/api/advertising/google-sheets/test-connection', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(this.config)
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
this.testResult = {
|
|
success: data.success,
|
|
message: data.message || (data.success ? 'Conexão testada com sucesso!' : 'Erro na conexão')
|
|
};
|
|
} catch (error) {
|
|
console.error('Erro ao testar conexão:', error);
|
|
this.testResult = {
|
|
success: false,
|
|
message: 'Erro de conexão'
|
|
};
|
|
} finally {
|
|
this.testing = false;
|
|
}
|
|
},
|
|
|
|
async previewSheets() {
|
|
try {
|
|
this.previewing = true;
|
|
|
|
const response = await fetch('/api/advertising/google-sheets/preview', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(this.config)
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
this.previewData = data.data;
|
|
const modal = new bootstrap.Modal(document.getElementById('previewModal'));
|
|
modal.show();
|
|
} else {
|
|
this.showError(data.message || 'Erro ao carregar pré-visualização');
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro ao carregar pré-visualização:', error);
|
|
this.showError('Erro de conexão');
|
|
} finally {
|
|
this.previewing = false;
|
|
}
|
|
},
|
|
|
|
getStatusBadgeClass(status) {
|
|
if (status === 'active') return 'bg-success';
|
|
if (status === 'paused') return 'bg-warning';
|
|
if (status === 'draft') return 'bg-secondary';
|
|
return 'bg-info';
|
|
},
|
|
|
|
formatDate(dateString) {
|
|
if (!dateString) return '--';
|
|
return new Date(dateString).toLocaleDateString('pt-PT');
|
|
},
|
|
|
|
formatCurrency(amount) {
|
|
if (!amount || amount <= 0) return '0,00 €';
|
|
return new Intl.NumberFormat('pt-PT', {
|
|
style: 'currency',
|
|
currency: 'EUR'
|
|
}).format(amount);
|
|
},
|
|
|
|
showSuccess(message) {
|
|
const alert = document.createElement('div');
|
|
alert.className = 'alert alert-success alert-dismissible fade show';
|
|
alert.innerHTML = `
|
|
<i class="ri-check-line me-2"></i> ${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
`;
|
|
document.querySelector('.col-12').insertBefore(alert, document.querySelector('.col-12').firstChild);
|
|
},
|
|
|
|
showError(message) {
|
|
const alert = document.createElement('div');
|
|
alert.className = 'alert alert-danger alert-dismissible fade show';
|
|
alert.innerHTML = `
|
|
<i class="ri-error-warning-line me-2"></i> ${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
`;
|
|
document.querySelector('.col-12').insertBefore(alert, document.querySelector('.col-12').firstChild);
|
|
}
|
|
};
|
|
}
|
|
</script>
|