- Added a context list (from picea-ui form) to the ControllerTrait

- Added some default i18n text
- Moved some reusable widget into lean view
This commit is contained in:
mcndave committed 2020-12-07 02:36:42 +00:00
1 parent abafee0b30
commit 4bd68a74f8
6 files changed
+75 -8

No files matched your search

+32 -7
View File
@@ -2,8 +2,10 @@
namespace Lean;
use Picea;
use Picea,
Picea\Ui\Method\FormContext;
use Psr\Http\Message\ServerRequestInterface;
use Storage\Session;
use Laminas\Diactoros\Response\{ HtmlResponse, TextResponse, RedirectResponse, JsonResponse };
@@ -12,7 +14,7 @@ use Ulmus\EntityCollection;
use Psr\Http\Message\ResponseInterface;
# use TheBugs\Email\MailerInterface;
use TheBugs\Email\MailerInterface;
use Notes\Route\Annotation\Object\Route as RouteParam,
Notes\Route\Annotation\Method\Route,
@@ -31,12 +33,23 @@ trait ControllerTrait {
public ? Picea\Picea $picea;
public MailerInterface $mailer;
public function __construct(? Picea\Picea $picea, Session $session/*, MailerInterface $mailer*/) {
public ? MailerInterface $mailer;
public array $contextList = [];
public function __construct(? Picea\Picea $picea, Session $session, ? MailerInterface $mailer = null) {
$this->picea = $picea;
$this->session = $session;
// $this->mailer = $mailer;
$this->mailer = $mailer;
}
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; }));
}
public function renderView(string $view, ?array $variables = null) : ResponseInterface
@@ -54,7 +67,7 @@ trait ControllerTrait {
protected function redirect(string $url, int $code = 302, array $headers = []) {
return new RedirectResponse($url, $code, $headers);
}
public static function renderText(string $html, int $code = 200, array $headers = []) : ResponseInterface
{
return new TextResponse($html, $code, $headers);
@@ -69,4 +82,16 @@ trait ControllerTrait {
{
return new JsonResponse($data, $code, $headers);
}
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];
}
}