2020-10-20 13:55:48 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Lean;
|
|
|
|
|
2021-03-01 16:01:27 +00:00
|
|
|
use CSLSJ\Common\RequestResponse\{ PdfResponse, ImageResponse, DownloadResponse };
|
|
|
|
|
2020-12-07 02:36:42 +00:00
|
|
|
use Picea,
|
|
|
|
Picea\Ui\Method\FormContext;
|
2020-10-20 13:55:48 +00:00
|
|
|
|
2020-12-07 02:36:42 +00:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2020-10-20 13:55:48 +00:00
|
|
|
use Storage\Session;
|
|
|
|
|
|
|
|
use Laminas\Diactoros\Response\{ HtmlResponse, TextResponse, RedirectResponse, JsonResponse };
|
|
|
|
|
|
|
|
use Ulmus\EntityCollection;
|
|
|
|
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
2020-12-07 02:36:42 +00:00
|
|
|
use TheBugs\Email\MailerInterface;
|
2020-10-20 13:55:48 +00:00
|
|
|
|
|
|
|
use Notes\Route\Annotation\Object\Route as RouteParam,
|
|
|
|
Notes\Route\Annotation\Method\Route,
|
|
|
|
Notes\Security\Annotation\Security,
|
|
|
|
Notes\Tell\Annotation\Language;
|
|
|
|
|
|
|
|
use function file_get_contents;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Security("locked" => false)
|
|
|
|
* @RouteParam("methods" => [ "GET", "POST", "DELETE" ])
|
|
|
|
*/
|
|
|
|
trait ControllerTrait {
|
2020-12-07 17:28:05 +00:00
|
|
|
public ? Session $session;
|
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
public ? Picea\Picea $picea;
|
|
|
|
|
2020-12-07 02:36:42 +00:00
|
|
|
public ? MailerInterface $mailer;
|
|
|
|
|
|
|
|
public array $contextList = [];
|
2021-02-16 03:10:04 +00:00
|
|
|
|
2020-12-07 02:36:42 +00:00
|
|
|
public function exportJson(ServerRequestInterface $request, string $entityClass, bool $includeRelations = true, ? callable $callback = null) : ResponseInterface
|
|
|
|
{
|
|
|
|
foreach($entityClass::repository()->filterServerRequest( $entityClass::searchRequest()->fromRequest($request->withQueryParams($request->getQueryParams() + ['limit' => PHP_INT_MAX,])) )->loadAll() as $entity) {
|
|
|
|
$data[] = $callback ? call_user_func($callback, $entity->toArray($includeRelations)) : $entity->toArray($includeRelations);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->renderJson( array_filter($data ?? [], function($row) { return $row !== null; }));
|
2020-10-20 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function renderView(string $view, ?array $variables = null) : ResponseInterface
|
|
|
|
{
|
|
|
|
return static::renderHtml(
|
|
|
|
$this->picea->renderHtml($view, $variables ?? [], $this)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderDocumentation(string $filename) : ResponseInterface
|
|
|
|
{
|
|
|
|
return $this->renderMarkdown( file_get_contents(getenv("PROJECT_PATH") . "/meta/doc/$filename.md") );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function redirect(string $url, int $code = 302, array $headers = []) {
|
|
|
|
return new RedirectResponse($url, $code, $headers);
|
|
|
|
}
|
2020-12-07 02:36:42 +00:00
|
|
|
|
2021-03-01 16:01:27 +00:00
|
|
|
public function renderPdf($rawdata, int $status = 200, array $headers = []) : PdfResponse
|
|
|
|
{
|
|
|
|
return new PdfResponse($rawdata, $status, $headers);
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:55:48 +00:00
|
|
|
public static function renderText(string $html, int $code = 200, array $headers = []) : ResponseInterface
|
|
|
|
{
|
|
|
|
return new TextResponse($html, $code, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderHtml(string $html, int $code = 200, array $headers = []) : ResponseInterface
|
|
|
|
{
|
|
|
|
return new HtmlResponse($html, $code, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderJson(array $data, int $code = 200, array $headers = []) : ResponseInterface
|
|
|
|
{
|
|
|
|
return new JsonResponse($data, $code, $headers);
|
|
|
|
}
|
2020-12-07 02:36:42 +00:00
|
|
|
|
2021-02-16 03:10:04 +00:00
|
|
|
public static function renderDownloadable(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface
|
|
|
|
{
|
|
|
|
return new DownloadResponse($data, $filename, $code, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function renderImage(string $data, int $code = 200, array $headers = []) : ResponseInterface
|
|
|
|
{
|
|
|
|
return new ImageResponse($data, $code, $headers);
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:28:05 +00:00
|
|
|
public function fromResponse(ResponseInterface $response)
|
|
|
|
{
|
|
|
|
if ( $response->getStatusCode() === 200 ) {
|
|
|
|
if ( $response instanceof \Zend\Diactoros\Response\JsonResponse) {
|
|
|
|
return $response->getPayload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function asset(string $url, array $parameters = []) : string
|
|
|
|
{
|
|
|
|
if (! $this->picea ) {
|
|
|
|
throw new \Exception("A picea object must be provided on class initialization to use this method.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->picea->compiler->getExtensionFromToken('asset')->buildAssetUrl($url, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function url(string $url, array $parameters = []) : string
|
|
|
|
{
|
|
|
|
if (! $this->picea ) {
|
|
|
|
throw new \Exception("A picea object must be provided on class initialization to use this method.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->picea->compiler->getExtensionFromToken('url')->buildUrl($url, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function route(string $name, array $parameters = []) : string
|
|
|
|
{
|
|
|
|
if (! $this->picea ) {
|
|
|
|
throw new \Exception("A picea object must be provided on class initialization to use this method.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->picea->compiler->getExtensionFromToken('route')->buildRouteUrl($name, $parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function json($data, int $flags = 0) {
|
|
|
|
return htmlentities(json_encode($data, $flags), ENT_QUOTES, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2020-12-07 02:36:42 +00:00
|
|
|
public function pushContext(FormContext $context) : FormContext
|
|
|
|
{
|
|
|
|
$this->contextList[$context->formName ?? uniqid("context_")] = $context;
|
|
|
|
|
|
|
|
return $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function context(string $name) : FormContext
|
|
|
|
{
|
|
|
|
return $this->contextList[$name];
|
|
|
|
}
|
2020-10-20 13:55:48 +00:00
|
|
|
}
|