From b8d2f9de308ac4e116737517a60d6a8358895e79 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 28 Jan 2022 16:54:19 +0200 Subject: [PATCH] action renderer --- .../Espo/Core/Utils/Client/ActionRenderer.php | 61 +++++++++++++++++++ .../Espo/EntryPoints/ChangePassword.php | 21 +++---- 2 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 application/Espo/Core/Utils/Client/ActionRenderer.php diff --git a/application/Espo/Core/Utils/Client/ActionRenderer.php b/application/Espo/Core/Utils/Client/ActionRenderer.php new file mode 100644 index 0000000000..6bb0499042 --- /dev/null +++ b/application/Espo/Core/Utils/Client/ActionRenderer.php @@ -0,0 +1,61 @@ +clientManager = $clientManager; + } + + public function render(string $controller, string $action, ?array $data = null): string + { + $encodedData = Json::encode($data); + + $script = " + require('{$controller}', Controller => { + let controller = new Controller(app.baseController.params, app.getControllerInjection()); + controller.masterView = app.masterView; + controller.doAction('{$action}', {$encodedData}); + }); + "; + + return $this->clientManager->render($script); + } +} diff --git a/application/Espo/EntryPoints/ChangePassword.php b/application/Espo/EntryPoints/ChangePassword.php index 8f858d95e1..34f6094f50 100644 --- a/application/Espo/EntryPoints/ChangePassword.php +++ b/application/Espo/EntryPoints/ChangePassword.php @@ -33,10 +33,11 @@ use Espo\Core\Exceptions\BadRequest; use Espo\Entities\PasswordChangeRequest; +use Espo\Core\Utils\Client\ActionRenderer; + use Espo\Core\EntryPoint\EntryPoint; use Espo\Core\EntryPoint\Traits\NoAuth; use Espo\Core\Utils\Config; -use Espo\Core\Utils\ClientManager; use Espo\Core\Api\Request; use Espo\Core\Api\Response; @@ -49,15 +50,15 @@ class ChangePassword implements EntryPoint private Config $config; - private ClientManager $clientManager; - private EntityManager $entityManager; - public function __construct(Config $config, ClientManager $clientManager, EntityManager $entityManager) + private ActionRenderer $actionRenderer; + + public function __construct(Config $config, EntityManager $entityManager, ActionRenderer $actionRenderer) { $this->config = $config; - $this->clientManager = $clientManager; $this->entityManager = $entityManager; + $this->actionRenderer = $actionRenderer; } public function run(Request $request, Response $response): void @@ -91,14 +92,8 @@ class ChangePassword implements EntryPoint 'notFound' => !$passwordChangeRequest, ]; - $runScript = " - app.getController('PasswordChangeRequest', controller => { - controller.doAction('passwordChange', " . json_encode($options) . "); - }); - "; + $html = $this->actionRenderer->render('controllers/password-change-request', 'passwordChange', $options); - $response->writeBody( - $this->clientManager->render($runScript) - ); + $response->writeBody($html); } }