Compare commits

...

2 Commits

Author SHA1 Message Date
c3ce84143f Logger para ver Nosotros 2020-06-10 22:50:04 -04:00
6e3418402c FIX: testo multilinea en faq 2020-06-10 21:01:09 -04:00
6 changed files with 90 additions and 8 deletions

View File

@ -117,5 +117,8 @@ return [
'username' => $c->get('email')->user->name, 'username' => $c->get('email')->user->name,
'password' => $c->get('email')->user->password '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']));
} }
]; ];

View File

@ -4,6 +4,7 @@ namespace ProVM\KI\Common\Controller\Web\Admin;
use Psr\Container\ContainerInterface as Container; use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Log\LoggerInterface as Logger;
use ProVM\KI\Common\Alias\View; use ProVM\KI\Common\Alias\View;
class Nosotros { class Nosotros {
@ -15,19 +16,30 @@ class Nosotros {
$nosotros = trim(json_decode(trim(file_get_contents($filename)))); $nosotros = trim(json_decode(trim(file_get_contents($filename))));
return $view->render($response, 'admin.nosotros', compact('nosotros')); 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(); $post = $request->getParsedBody();
$logger->info('Información en post:', compact('post'));
$filename = implode(DIRECTORY_SEPARATOR, [ $filename = implode(DIRECTORY_SEPARATOR, [
$container->get('folders.data'), $container->get('folders.data'),
'nosotros.json' 'nosotros.json'
]); ]);
$nosotros = trim(json_decode(trim(file_get_contents($filename)))); $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']))); $data = trim(json_decode(json_encode($post['nosotros'])));
$status = false; $status = false;
if ($nosotros != $data) { if ($nosotros != $data) {
$status = (file_put_contents($filename, json_encode($post['nosotros'])) !== false); $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; $code = 301;
if ($status) { if ($status) {
$code = 302; $code = 302;

View File

@ -17,11 +17,11 @@ class Nosotros {
$max_phrase = 50; $max_phrase = 50;
if (strlen($nosotros) > $min) { if (strlen($nosotros) > $min) {
$half = strlen($nosotros) / 2; $half = strlen($nosotros) / 2;
$pos = strpos($nosotros, '.', $half); $pos = mb_strpos($nosotros, '.', $half); // Siguiente punto despues de la mitad
if ($pos > $half + $max_phrase) { if ($pos > $half + $max_phrase) { // Si está muy lejos de la mitad busca otra posicion
$pos = strrpos($nosotros, '.', -$half); $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) { if (count($nosotros) > 2) {
$s1 = array_shift($nosotros); $s1 = array_shift($nosotros);
$nosotros = [$s1, implode('', $nosotros)]; $nosotros = [$s1, implode('', $nosotros)];

View File

@ -0,0 +1,67 @@
<?php
namespace ProVM\Common\Implementation;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Carbon\Carbon;
class Logger implements LoggerInterface {
protected $output_file;
public function __construct(string $filename) {
if (!file_exists($filename)) {
$f = fopen($filename, 'c');
fclose($f);
}
if (!file_exists($filename)) {
throw new \Exception('Can not create log file.');
}
$this->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));
}
}

View File

@ -47,7 +47,7 @@
@foreach ($faqs as $faq) @foreach ($faqs as $faq)
{ {
titulo: '{{$faq->titulo}}', titulo: '{{$faq->titulo}}',
contenido: '{{$faq->contenido}}' contenido: "{{implode("\\n", explode(PHP_EOL, $faq->contenido))}}"
}, },
@endforeach @endforeach
], ],
@ -82,7 +82,7 @@
titulo: $("input[name='titulo']").val(), titulo: $("input[name='titulo']").val(),
contenido: $("textarea[name='contenido']").val() contenido: $("textarea[name='contenido']").val()
} }
if (edit) { if (faq.edit) {
input['id'] = $("input[name='id']").val() input['id'] = $("input[name='id']").val()
} }
var url = '{{$urls->admin}}/faqs/add' var url = '{{$urls->admin}}/faqs/add'

View File

@ -14,7 +14,7 @@
</div> </div>
<div class="content"> <div class="content">
<p> <p>
{{$faq->contenido}} {!!nl2br($faq->contenido)!!}
</p> </p>
</div> </div>
@endforeach @endforeach