IMPR: AJAX processing

This commit is contained in:
Tony Air 2024-02-02 18:19:43 +02:00
parent 70f42541f2
commit a81011feb2
2 changed files with 68 additions and 6 deletions

View File

@ -15,7 +15,7 @@ SilverStripe\SiteConfig\SiteConfig:
PageController:
extensions:
- A2nt\CMSNiceties\Extensions\PageControllerEx
- A2nt\CMSNiceties\Ajax\Ex\AjaxLoginFormControllerEx
- A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx
SilverStripe\CMS\Model\SiteTree:
default_container_class: 'container'

View File

@ -2,18 +2,21 @@
namespace A2nt\CMSNiceties\Ajax\Ex;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
use SilverStripe\Security\Security;
use SilverStripe\View\SSViewer;
/**
* Class \App\Service\Ex\ServiceAreaController
*
* @property \A2nt\CMSNiceties\Ajax\Ex\AjaxLoginFormControllerEx $owner
*/
class AjaxLoginFormControllerEx extends Extension
class AjaxControllerEx extends Extension
{
private static $allowed_actions = [
'LoginFormEx',
@ -59,11 +62,20 @@ class AjaxLoginFormControllerEx extends Extension
return $form;
}
public static function isFormRequest()
{
$ctrl = Controller::curr();
/* @var $req SilverStripe\Control\HTTPRequest */
$req = $ctrl->getRequest();
return $req->getHeader('x-requested-form') || $req->requestVar('formid');
}
public function passwordsent()
{
$ctrl = $this->owner;
if (Director::is_ajax()) {
if (self::isFormRequest() && Director::is_ajax()) {
$message = _t(
'SilverStripe\\Security\\Security.PASSWORDRESETSENTTEXT',
"Thank you. A reset link has been sent, provided an account exists for this email address."
@ -75,13 +87,63 @@ class AjaxLoginFormControllerEx extends Extension
]);
return $json;
/*$response = $ctrl->getResponse();
$response->setBody($json);
die($response->output());*/
}
return Injector::inst()->get(MemberAuthenticator::class)
->getLostPasswordHandler($ctrl->Link())
->passwordsent();
}
public static function processAJAX($tpls)
{
foreach ($tpls as $tpl) {
if (is_array($tpl)) {
continue;
}
$a_tpl = explode('\\', $tpl);
$last_name = array_pop($a_tpl);
$a_tpl[] = 'Layout';
$a_tpl[] = $last_name;
$a_tpl = implode('\\', $a_tpl);
if (SSViewer::hasTemplate($a_tpl)) {
$tpl = $a_tpl;
break;
}
}
//
$tpl = is_array($tpl) ? 'Page' : $tpl;
$tpl = ($tpl !== 'Page') ? $tpl : 'Layout/Page';
return SSViewer::create($tpl);
}
public function prepareAjaxResponse($response)
{
$ctrl = $this->owner;
$record = $ctrl->dataRecord;
$req = $ctrl->getRequest();
$url = $req->getURL();
$url = $url === 'home' ? '/' : $url;
$resources = array_merge(
$ctrl->config()->get('graphql_resources'),
$ctrl->config()->get('ajax_resources')
);
$response->setBody(json_encode([
'ID' => $record->ID,
'Title' => $record->Title,
'Link' => $ctrl->Link(),
'CSSClass' => $ctrl->CSSClass(),
'Resources' => $resources,
'RequestLink' => $url,
'MainContent' => $response->getBody(),
]));
}
}