diff --git a/bootstrap/web/setup.php b/bootstrap/web/setup.php index 1c43856..0c4b040 100644 --- a/bootstrap/web/setup.php +++ b/bootstrap/web/setup.php @@ -117,5 +117,8 @@ return [ 'username' => $c->get('email')->user->name, 'password' => $c->get('email')->user->password ]); + }, + Psr\Log\LoggerInterface::class => function(Container $c) { + return new ProVM\Common\Implementation\Logger(implode(DIRECTORY_SEPARATOR, [$c->get('folders.base'), 'admin.log'])); } ]; diff --git a/common/Controller/Web/Admin/Nosotros.php b/common/Controller/Web/Admin/Nosotros.php index 63af10e..22534b6 100644 --- a/common/Controller/Web/Admin/Nosotros.php +++ b/common/Controller/Web/Admin/Nosotros.php @@ -4,6 +4,7 @@ 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 Psr\Log\LoggerInterface as Logger; use ProVM\KI\Common\Alias\View; class Nosotros { @@ -15,19 +16,30 @@ class Nosotros { $nosotros = trim(json_decode(trim(file_get_contents($filename)))); return $view->render($response, 'admin.nosotros', compact('nosotros')); } - public function guardar(Request $request, Response $response, Container $container) { + public function guardar(Request $request, Response $response, Container $container, Logger $logger) { + $logger->info('Llamado a guardar en Nosotros.'); $post = $request->getParsedBody(); + $logger->info('Información en post:', compact('post')); $filename = implode(DIRECTORY_SEPARATOR, [ $container->get('folders.data'), 'nosotros.json' ]); $nosotros = trim(json_decode(trim(file_get_contents($filename)))); + $logger->info('Contenido de nosotros.json', compact('nosotros')); $data = trim(json_decode(json_encode($post['nosotros']))); $status = false; if ($nosotros != $data) { $status = (file_put_contents($filename, json_encode($post['nosotros'])) !== false); + if ($status) { + $logger->info('Guardados los cambios en nosotros.json'); + } else { + $logger->notice('No se pudo guardar los cambios en nosotros.json'); + } + } else { + $logger->info('No se han hecho cambios en nosotros.'); } + $code = 301; if ($status) { $code = 302; diff --git a/common/Controller/Web/Nosotros.php b/common/Controller/Web/Nosotros.php index 5ae767a..90d84c7 100644 --- a/common/Controller/Web/Nosotros.php +++ b/common/Controller/Web/Nosotros.php @@ -17,11 +17,11 @@ class Nosotros { $max_phrase = 50; if (strlen($nosotros) > $min) { $half = strlen($nosotros) / 2; - $pos = strpos($nosotros, '.', $half); - if ($pos > $half + $max_phrase) { - $pos = strrpos($nosotros, '.', -$half); + $pos = mb_strpos($nosotros, '.', $half); // Siguiente punto despues de la mitad + if ($pos > $half + $max_phrase) { // Si está muy lejos de la mitad busca otra posicion + $pos = mb_strrpos($nosotros, '.', -$half) + 1; // Punto antes de la mitad } - $nosotros = $this->str_split_unicode($nosotros, $pos, '-'); + $nosotros = $this->str_split_unicode($nosotros, $pos); if (count($nosotros) > 2) { $s1 = array_shift($nosotros); $nosotros = [$s1, implode('', $nosotros)]; diff --git a/provm/common/Implementation/Logger.php b/provm/common/Implementation/Logger.php new file mode 100644 index 0000000..4e5de41 --- /dev/null +++ b/provm/common/Implementation/Logger.php @@ -0,0 +1,67 @@ +output_file = $filename; + } + public function emergency($message, array $context = []) { + return $this->log(LogLevel::EMERGENCY, $message, $context); + } + public function alert($message, array $context = []) { + return $this->log(LogLevel::ALERT, $message, $context); + } + public function critical($message, array $context = []) { + return $this->log(LogLevel::CRITICAL, $message, $context); + } + public function error($message, array $context = []) { + return $this->log(LogLevel::ERROR, $message, $context); + } + public function warning($message, array $context = []) { + return $this->log(LogLevel::WARNING, $message, $context); + } + public function notice($message, array $context = []) { + return $this->log(LogLevel::NOTICE, $message, $context); + } + public function info($message, array $context = []) { + return $this->log(LogLevel::INFO, $message, $context); + } + public function debug($message, array $context = []) { + return $this->log(LogLevel::DEBUG, $message, $context); + } + public function log($level, $message, array $context = []) { + $refl = new \ReflectionClass(LogLevel::class); + $levels = $refl->getConstants(); + if (array_search($level, array_values($levels)) === false) { + throw new \InvalidArgumentException('Invalid log level.'); + } + + $f = Carbon::now('America/Santiago'); + $output = [ + 'time' => $f->format('Y-m-d H:i:s,v'), + 'message' => $message + ]; + if (count($context) > 0) { + $output['context'] = $context; + } + + $data = json_decode(trim(file_get_contents($this->output_file))); + if ($data === null) { + $data = []; + } + $data []= $output; + file_put_contents($this->output_file, json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE)); + } +}