Files
KI/common/Controller/Web/Admin/Faq.php

91 lines
2.6 KiB
PHP
Raw Normal View History

2020-05-26 23:04:49 -04:00
<?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;
2020-06-09 18:06:36 -04:00
use ProVM\KI\Common\Alias\View;
2020-05-26 23:04:49 -04:00
class Faq {
public function __invoke(Request $request, Response $response, View $view, Container $container) {
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'faqs.json'
]);
$faqs = json_decode(trim(file_get_contents($filename)));
return $view->render($response, 'admin.faq', compact('faqs'));
}
public function add(Request $request, Response $response, Container $container) {
$post = $request->getParsedBody();
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'faqs.json'
]);
$faqs = json_decode(trim(file_get_contents($filename)));
$faq = (object) [
'titulo' => '',
'contenido' => ''
];
if (isset($post['id']) and isset($faqs[$post['id']])) {
$faq = $faqs[$post['id']];
}
$changed = false;
foreach ($faq as $k => $v) {
if ($v != $post[$k]) {
$faq->$k = $post[$k];
$changed = true;
}
}
if (isset($post['id'])) {
$faqs[$post['id']] = $faq;
} else {
$faqs []= $faq;
}
$status = false;
if ($changed) {
$faqs = array_values($faqs);
$status = (file_put_contents($filename, json_encode($faqs, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
}
$output = [
'informacion' => $post,
'estado' => $status,
'faqs' => $faqs
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
public function delete(Request $request, Response $response, Container $container) {
$post = $request->getParsedBody();
$filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'),
'faqs.json'
]);
$faqs = json_decode(trim(file_get_contents($filename)));
$status = false;
if (isset($post['id']) and isset($faqs[$post['id']])) {
unset($faqs[$post['id']]);
$faqs = array_values($faqs);
$status = (file_put_contents($filename, json_encode($faqs, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES)) !== false);
}
$output = [
'informacion' => $post,
'estado' => $status,
'faqs' => $faqs
];
$response->getBody()->write(json_encode($output));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
}
}