Diff #4 - trunk/share/HttpControllers/Webapp/Modals.php
3,298 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: Modals.php
--- Modals.php (nonexistent) +++ Modals.php (revision 4) @@ -0,0 +1,117 @@ +<?php +declare(strict_types = 1); + +namespace App\HttpControllers\Webapp; + +use Apex\Svc{View, Convert, Container}; +use Nyholm\Psr7\Response; +use Psr\Http\Message{ServerRequestInterface, ResponseInterface}; +use Psr\Http\Server{MiddlewareInterface, RequestHandlerInterface}; +use Apex\Armor\Armor; + +/** + * Http Controller - ~alias~ + / +class Modals implements MiddlewareInterface +{ + + #[Inject(View::class)] + private View $view; + + #[Inject(Container::class)] + private Container $cntr; + + #[Inject(Convert::class)] + private Convert $convert; + + #[Inject(Armor::class)] + private Armor $armor; + + /** + * Process request + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $app): ResponseInterface + { + + // Set content type to JSON + $app->setContentType('application/json'); + + // Set auth type + $auth_type = $app->request('auth_type', 'user'); + $this->armor->loadPolicy($auth_type); + + // Check auth + if ($session = $this->armor->checkAuth($auth_type)) { + $area = $auth_type == 'admin' ? 'admin' : 'members'; + $app->setArea($area, true); + $app->setSession($session); + } elseif ($auth_type == 'admin') { + throw new \Exception("You do not have permission to perform this action."); + } + + // Parse uri + $parts = explode("/", trim($app->getPath(), '/')); + if ($parts < 3) { + throw new \Exception("Invalid URI"); + } + + // Get filename + $filename = SITE_PATH . '/src/' . $this->convert->case($parts[1], 'title') . '/Opus/Modals/' . $this->convert->case($parts[2], 'title') . '.html'; + if (!file_exists($filename)) { + throw new \Exception("No file exists for this modal"); + } + + // Get class name + $class_name = "App\" . $this->convert->case($parts[1], 'title') . "\Opus\Modals\" . $this->convert->case($parts[2], 'title'); + if (class_exists($class_name)) { + $obj = $this->cntr->make($class_name); + $obj->show($this->view, $app); + } + + // Parse html + $html = file_get_contents($filename); + if (preg_match("/^(.?)
<
h1>(.*?)<\/h1>/si", $html, $match)) { + $title = $match[2]; + $html = str_replace($match[0], '', $html); + } else { + $title = 'Dialog'; + } + $html = $this->view->renderBlock($html); + + // Set response + $json = json_encode([ + 'title' => $title, + 'body' => $html + ]); + + // Create PSR7 compliant response + if ($app->config('core.enable_javascript') != 1) { + + // Set variables + $this->view->assign('title', $title); + $this->view->assign('contents', $html); + $this->view->setTemplateFile('modal_nojs'); + + // Create PSR7 response + $response = new Response( + body: $this->view->render() + ); + + // JSON response for AJAX function + } else { + + $response = (new Response(body: $json)) + ->withStatus(200) + ->withAddedHeader('Content-type', 'application/json'); + } + + // Return + return $response; + } + + +} + + + +
Full Code
<?php declare(strict_types = 1);
namespace App\HttpControllers\Webapp;
use Apex\Svc{View, Convert, Container}; use Nyholm\Psr7\Response; use Psr\Http\Message{ServerRequestInterface, ResponseInterface}; use Psr\Http\Server{MiddlewareInterface, RequestHandlerInterface}; use Apex\Armor\Armor;
/** * Http Controller - ~alias~ */ class Modals implements MiddlewareInterface {
#[Inject(View::class)]
private View $view;
#[Inject(Container::class)]
private Container $cntr;
#[Inject(Convert::class)]
private Convert $convert;
#[Inject(Armor::class)]
private Armor $armor;
/**
* Process request
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $app): ResponseInterface
{
// Set content type to JSON
$app->setContentType('application/json');
// Set auth type
$auth_type = $app->request('auth_type', 'user');
$this->armor->loadPolicy($auth_type);
// Check auth
if ($session = $this->armor->checkAuth($auth_type)) {
$area = $auth_type == 'admin' ? 'admin' : 'members';
$app->setArea($area, true);
$app->setSession($session);
} elseif ($auth_type == 'admin') {
throw new \Exception("You do not have permission to perform this action.");
}
// Parse uri
$parts = explode("/", trim($app->getPath(), '/'));
if ($parts < 3) {
throw new \Exception("Invalid URI");
}
// Get filename
$filename = SITE_PATH . '/src/' . $this->convert->case($parts[1], 'title') . '/Opus/Modals/' . $this->convert->case($parts[2], 'title') . '.html';
if (!file_exists($filename)) {
throw new \Exception("No file exists for this modal");
}
// Get class name
$class_name = "App\\" . $this->convert->case($parts[1], 'title') . "\\Opus\\Modals\\" . $this->convert->case($parts[2], 'title');
if (class_exists($class_name)) {
$obj = $this->cntr->make($class_name);
$obj->show($this->view, $app);
}
// Parse html
$html = file_get_contents($filename);
if (preg_match("/^(.*?)<h1>(.*?)<\/h1>/si", $html, $match)) {
$title = $match[2];
$html = str_replace($match[0], '', $html);
} else {
$title = 'Dialog';
}
$html = $this->view->renderBlock($html);
// Set response
$json = json_encode([
'title' => $title,
'body' => $html
]);
// Create PSR7 compliant response
if ($app->config('core.enable_javascript') != 1) {
// Set variables
$this->view->assign('title', $title);
$this->view->assign('contents', $html);
$this->view->setTemplateFile('modal_nojs');
// Create PSR7 response
$response = new Response(
body: $this->view->render()
);
// JSON response for AJAX function
} else {
$response = (new Response(body: $json))
->withStatus(200)
->withAddedHeader('Content-type', 'application/json');
}
// Return
return $response;
}
}