Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jhvanderschee committed Sep 1, 2020
2 parents 012e152 + 63d889b commit 55eaaad
Show file tree
Hide file tree
Showing 18 changed files with 913 additions and 179 deletions.
17 changes: 17 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Tqdev/PhpCrudUi/Column/SpecificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ public function getColumns(string $table, string $action): array
return array_keys($properties);
}

public function getInfo()
{
$info = array();
if (isset($this->definition['info'])) {
$info = $this->definition['info'];
if (!isset($info['title'])) {
$info['title'] = 'PHP-CRUD-UI';
}
if (!isset($info['x-subtitle'])) {
$info['x-subtitle'] = 'by TQdev.com';
}
}
return $info;
}

public function getMenu()
{
$items = array();
Expand Down
8 changes: 8 additions & 0 deletions src/Tqdev/PhpCrudUi/Controller/MultiResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Tqdev\PhpCrudApi\Record\ErrorCode;
use Tqdev\PhpCrudApi\ResponseFactory;
use Tqdev\PhpCrudUi\Document\CsvDocument;
use Tqdev\PhpCrudUi\Document\JsonDocument;
use Tqdev\PhpCrudUi\Document\RedirectDocument;
use Tqdev\PhpCrudUi\Document\TemplateDocument;

class MultiResponder implements Responder
Expand Down Expand Up @@ -41,10 +43,16 @@ public function success($result): ResponseInterface
{
if ($result instanceof CsvDocument) {
return ResponseFactory::fromCsv(ResponseFactory::OK, (string) $result);
} elseif ($result instanceof JsonDocument) {
return ResponseFactory::fromObject(ResponseFactory::OK, json_decode((string) $result));
} elseif ($result instanceof TemplateDocument) {
$result->addVariables($this->variables);
$result->setTemplatePath($this->templatePath);
return ResponseFactory::fromHtml(ResponseFactory::OK, (string) $result);
} elseif ($result instanceof RedirectDocument) {
$result->addVariables($this->variables);
$response = ResponseFactory::fromStatus(ResponseFactory::FOUND);
return $response->withHeader('Location', (string) $result);
} else {
throw new \Exception('Document type not supported: ' . get_class($result));
}
Expand Down
34 changes: 34 additions & 0 deletions src/Tqdev/PhpCrudUi/Controller/RecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RecordController
public function __construct(Router $router, Responder $responder, RecordService $service)
{
$router->register('GET', '/', array($this, 'home'));
$router->register('GET', '/menu', array($this, 'menu'));
$router->register('GET', '/*/create', array($this, 'createForm'));
$router->register('POST', '/*/create', array($this, 'create'));
$router->register('GET', '/*/read/*', array($this, 'read'));
Expand All @@ -26,6 +27,8 @@ public function __construct(Router $router, Responder $responder, RecordService
$router->register('GET', '/*/delete/*', array($this, 'deleteForm'));
$router->register('POST', '/*/delete/*', array($this, 'delete'));
$router->register('GET', '/*/list', array($this, '_list'));
$router->register('GET', '/*/values/*', array($this, 'values'));
$router->register('POST', '/*/list', array($this, 'search'));
$router->register('GET', '/*/export', array($this, 'export'));
$this->service = $service;
$this->responder = $responder;
Expand All @@ -37,6 +40,12 @@ public function home(ServerRequestInterface $request): ResponseInterface
return $this->responder->success($result);
}

public function menu(ServerRequestInterface $request): ResponseInterface
{
$result = $this->service->menu();
return $this->responder->success($result);
}

public function createForm(ServerRequestInterface $request): ResponseInterface
{
$table = RequestUtils::getPathSegment($request, 1);
Expand Down Expand Up @@ -140,6 +149,31 @@ public function _list(ServerRequestInterface $request): ResponseInterface
return $this->responder->success($result);
}

public function values(ServerRequestInterface $request): ResponseInterface
{
$table = RequestUtils::getPathSegment($request, 1);
$column = RequestUtils::getPathSegment($request, 3);
$params = RequestUtils::getParams($request);
if (!$this->service->hasTable($table, 'list')) {
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
}
$result = $this->service->values($table, 'list', $column, $params);
return $this->responder->success($result);
}

public function search(ServerRequestInterface $request): ResponseInterface
{
$table = RequestUtils::getPathSegment($request, 1);
$action = RequestUtils::getPathSegment($request, 2);
$params = RequestUtils::getParams($request);
$body = $request->getParsedBody();
if (!$this->service->hasTable($table, $action)) {
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
}
$result = $this->service->search($table, $body, $params);
return $this->responder->success($result);
}

public function export(ServerRequestInterface $request): ResponseInterface
{
$table = RequestUtils::getPathSegment($request, 1);
Expand Down
18 changes: 18 additions & 0 deletions src/Tqdev/PhpCrudUi/Document/JsonDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tqdev\PhpCrudUi\Document;

class JsonDocument
{
private $variables;

public function __construct(array $variables)
{
$this->variables = $variables;
}

public function __toString(): string
{
return json_encode($this->variables['records']);
}
}
25 changes: 25 additions & 0 deletions src/Tqdev/PhpCrudUi/Document/RedirectDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tqdev\PhpCrudUi\Document;

class RedirectDocument
{
private $path;
private $variables;

public function __construct(string $path, array $variables)
{
$this->path = $path;
$this->variables = $variables;
}

public function addVariables(array $variables) /*: void*/
{
$this->variables = array_merge($variables, $this->variables);
}

public function __toString(): string
{
return $this->variables['base'] . $this->path;
}
}
Loading

0 comments on commit 55eaaad

Please sign in to comment.