Files
MoneyFlix/crm/Modules/Advertising/Controllers/LandingTemplatesController.php
T
1sonhador 88a5659fe8 fix: Correct user permissions and UserModel query
- 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
2025-10-15 14:14:00 +01:00

320 lines
11 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Modules\Advertising\Controllers;
use App\Core\BaseController;
use App\Core\DatabaseManager;
use App\Core\ErrorReporter;
use App\Core\InputSanitizer;
use App\Core\View;
use App\Modules\Advertising\Models\LandingTemplateModel;
use App\Modules\Advertising\Models\BrandModel;
/**
* Landing Templates Controller
*
* Gerencia templates de landing por marca/país
* Sistema "aldabado" - 1 template = 1 imagem = Infinitos trackers
*/
class LandingTemplatesController extends BaseController
{
private LandingTemplateModel $landingTemplateModel;
private BrandModel $brandModel;
public function __construct()
{
parent::__construct('Advertising');
$this->landingTemplateModel = new LandingTemplateModel();
$this->brandModel = new BrandModel();
}
/**
* Lista de landing templates
*/
public function index(): void
{
try {
$filters = [
'brand_id' => InputSanitizer::get('brand_id', 'GET', ''),
'country_code' => InputSanitizer::get('country_code', 'GET', ''),
'template_type' => InputSanitizer::get('template_type', 'GET', '')
];
$templates = $this->landingTemplateModel->getAll($filters, 50, 0);
$brands = $this->brandModel->getAll();
View::render('layouts/main.php', [
'title' => 'Landing Templates',
'page_name' => 'apps/advertising/landing-templates',
'page_content' => function() use ($templates, $brands, $filters) {
View::render('layouts/page-title.php', [
'pagetitle' => 'Advertising',
'title' => 'Landing Templates'
]);
View::render('@advertising/landing-templates', [
'templates' => $templates,
'brands' => $brands,
'filters' => $filters
]);
}
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_templates_index']);
$this->errorResponse('Erro ao carregar templates', 500);
}
}
/**
* Criar novo template
*/
public function create(): void
{
try {
$brands = $this->brandModel->getAll();
View::render('layouts/main.php', [
'title' => 'Criar Landing Template',
'page_name' => 'apps/advertising/landing-template-create',
'page_content' => function() use ($brands) {
View::render('layouts/page-title.php', [
'pagetitle' => 'Advertising',
'title' => 'Criar Landing Template'
]);
View::render('@advertising/landing-template-create', [
'brands' => $brands
]);
}
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_create_page']);
$this->errorResponse('Erro ao carregar página', 500);
}
}
/**
* Editar template
*/
public function edit(int $id): void
{
try {
$template = $this->landingTemplateModel->findByBrandAndCountry($id, '');
if (!$template) {
header('Location: /advertising/landing-templates?error=not_found&id=' . $id);
exit;
}
$brands = $this->brandModel->getAll();
View::render('layouts/main.php', [
'title' => 'Editar Landing Template',
'page_name' => 'apps/advertising/landing-template-edit',
'page_content' => function() use ($template, $brands) {
View::render('layouts/page-title.php', [
'pagetitle' => 'Advertising',
'title' => 'Editar Landing Template'
]);
View::render('@advertising/landing-template-edit', [
'template' => $template,
'brands' => $brands
]);
}
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_edit', 'id' => $id]);
$this->errorResponse('Erro ao carregar template', 500);
}
}
/**
* Visualizar template
*/
public function view(int $id): void
{
try {
$template = $this->landingTemplateModel->findByBrandAndCountry($id, '');
if (!$template) {
header('Location: /advertising/landing-templates?error=not_found&id=' . $id);
exit;
}
View::render('layouts/main.php', [
'title' => 'Visualizar Landing Template',
'page_name' => 'apps/advertising/landing-template-view',
'page_content' => function() use ($template) {
View::render('layouts/page-title.php', [
'pagetitle' => 'Advertising',
'title' => 'Visualizar Landing Template'
]);
View::render('@advertising/landing-template-view', [
'template' => $template
]);
}
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_view', 'id' => $id]);
$this->errorResponse('Erro ao carregar template', 500);
}
}
/**
* API: Listar templates
*/
public function getTemplates(): void
{
try {
header('Content-Type: application/json');
$filters = [
'brand_id' => InputSanitizer::get('brand_id', 'GET', ''),
'country_code' => InputSanitizer::get('country_code', 'GET', ''),
'template_type' => InputSanitizer::get('template_type', 'GET', '')
];
$limit = max(1, min(100, InputSanitizer::integer('limit', 'GET', 20)));
$offset = max(0, InputSanitizer::integer('offset', 'GET', 0));
$templates = $this->landingTemplateModel->getAll($filters, $limit, $offset);
echo json_encode([
'success' => true,
'data' => $templates,
'total' => count($templates)
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_templates_api_get']);
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro interno']);
}
}
/**
* API: Criar template
*/
public function createTemplate(): void
{
try {
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !isset($input['brand_id']) || !isset($input['country_code']) || !isset($input['image_path'])) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Dados inválidos']);
return;
}
$templateId = $this->landingTemplateModel->create($input);
echo json_encode([
'success' => true,
'message' => 'Template criado com sucesso',
'data' => ['id' => $templateId]
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_api_create']);
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro ao criar template']);
}
}
/**
* API: Atualizar template
*/
public function updateTemplate(int $id): void
{
try {
header('Content-Type: application/json');
$input = json_decode(file_get_contents('php://input'), true);
if (!$input) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Dados inválidos']);
return;
}
$success = $this->landingTemplateModel->update($id, $input);
if ($success) {
echo json_encode([
'success' => true,
'message' => 'Template atualizado com sucesso'
]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro ao atualizar template']);
}
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_api_update', 'id' => $id]);
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro interno']);
}
}
/**
* API: Eliminar template
*/
public function deleteTemplate(int $id): void
{
try {
header('Content-Type: application/json');
$success = $this->landingTemplateModel->delete($id);
if ($success) {
echo json_encode([
'success' => true,
'message' => 'Template eliminado com sucesso'
]);
} else {
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro ao eliminar template']);
}
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_api_delete', 'id' => $id]);
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro interno']);
}
}
/**
* API: Verificar se template existe
*/
public function checkTemplate(): void
{
try {
header('Content-Type: application/json');
$brandId = InputSanitizer::integer('brand_id', 'GET', 0);
$countryCode = InputSanitizer::get('country_code', 'GET', '');
if (!$brandId || !$countryCode) {
http_response_code(400);
echo json_encode(['success' => false, 'message' => 'Parâmetros inválidos']);
return;
}
$exists = $this->landingTemplateModel->exists($brandId, $countryCode);
echo json_encode([
'success' => true,
'data' => ['exists' => $exists]
]);
} catch (\Exception $e) {
ErrorReporter::report($e, ['context' => 'landing_template_api_check']);
http_response_code(500);
echo json_encode(['success' => false, 'message' => 'Erro interno']);
}
}
}