122 lines
3.8 KiB
PHP
122 lines
3.8 KiB
PHP
<?php
|
|
namespace ProVM\KI\Common\Controller\Web\Admin;
|
|
|
|
use Psr\Container\ContainerInterface as Container;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use ProVM\KI\Common\Alias\View;
|
|
|
|
class Home {
|
|
public function __invoke(Request $request, Response $response, View $view, Container $container): Response {
|
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
|
$container->get('folders.data'),
|
|
'avisos.json'
|
|
]);
|
|
$avisos = json_decode(trim(file_get_contents($filename)));
|
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
|
$container->get('folders.data'),
|
|
'resumen.json'
|
|
]);
|
|
$resumen = json_decode(trim(file_get_contents($filename)));
|
|
return $view->render($response, 'admin.home', compact('avisos', 'resumen'));
|
|
}
|
|
public function add(Request $request, Response $response, Container $container): Response {
|
|
$post = $request->getParsedBody();
|
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
|
$container->get('folders.data'),
|
|
'avisos.json'
|
|
]);
|
|
$avisos = json_decode(trim(file_get_contents($filename)));
|
|
|
|
if (isset($post['estado'])) {
|
|
$avisos->activo = ($post['estado'] !== 'false');
|
|
} else {
|
|
$aviso = (object) [
|
|
'titulo' => '',
|
|
'contenido' => ''
|
|
];
|
|
if (isset($post['id'])) {
|
|
$aviso = $avisos->avisos[$post['id']];
|
|
}
|
|
|
|
foreach ($aviso as $k => $v) {
|
|
if ($v != $post[$k]) {
|
|
$aviso->$k = $post[$k];
|
|
}
|
|
}
|
|
|
|
if (isset($post['id'])) {
|
|
$avisos->avisos[$post['id']] = $aviso;
|
|
} else {
|
|
$avisos->avisos []= $aviso;
|
|
}
|
|
}
|
|
|
|
$status = (file_put_contents($filename, json_encode($avisos, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
|
|
|
|
$output = [
|
|
'informacion' => $post,
|
|
'estado' => $status,
|
|
'avisos' => $avisos
|
|
];
|
|
$response->getBody()->write(json_encode($output));
|
|
return $response
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withStatus(201);
|
|
}
|
|
public function delete(Request $request, Response $response, Container $container): Response {
|
|
$post = $request->getParsedBody();
|
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
|
$container->get('folders.data'),
|
|
'avisos.json'
|
|
]);
|
|
$avisos = json_decode(trim(file_get_contents($filename)));
|
|
|
|
if (isset($avisos->avisos[$post['id']])) {
|
|
unset($avisos->avisos[$post['id']]);
|
|
$avisos->avisos = array_values($avisos->avisos);
|
|
}
|
|
|
|
$status = (file_put_contents($filename, json_encode($avisos, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
|
|
|
|
$output = [
|
|
'informacion' => $post,
|
|
'estado' => $status,
|
|
'avisos' => $avisos
|
|
];
|
|
$response->getBody()->write(json_encode($output));
|
|
return $response
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withStatus(201);
|
|
}
|
|
public function edit(Request $request, Response $response, Container $container): Response {
|
|
$post = $request->getParsedBody();
|
|
$filename = implode(DIRECTORY_SEPARATOR, [
|
|
$container->get('folders.data'),
|
|
'resumen.json'
|
|
]);
|
|
$resumen = json_decode(trim(file_get_contents($filename)));
|
|
|
|
$ind = $resumen[$post['id']];
|
|
|
|
foreach ($ind as $k => $v) {
|
|
if ($v != $post[$k]) {
|
|
$ind->$k = $post[$k];
|
|
}
|
|
}
|
|
$resumen[$post['id']] = $ind;
|
|
|
|
$status = (file_put_contents($filename, json_encode($resumen, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
|
|
|
|
$output = [
|
|
'informacion' => $post,
|
|
'estado' => $status,
|
|
'resumen' => $resumen
|
|
];
|
|
$response->getBody()->write(json_encode($output));
|
|
return $response
|
|
->withHeader('Content-Type', 'application/json')
|
|
->withStatus(201);
|
|
}
|
|
}
|