IMPR: AJAX processing

This commit is contained in:
Tony Air 2024-02-02 18:21:05 +02:00
parent f806dfdabf
commit 2d7359b650

View File

@ -3,6 +3,8 @@
// vendor/silverstripe/errorpage/src/ErrorPageController.php // vendor/silverstripe/errorpage/src/ErrorPageController.php
// extends global PageController class // extends global PageController class
//namespace App\Pages; //namespace App\Pages;
use A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx;
use SilverStripe\CMS\Controllers\ContentController; use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
@ -16,6 +18,8 @@ use SilverStripe\View\SSViewer;
* @method \Page data() * @method \Page data()
* @mixin \Page * @mixin \Page
* @mixin \A2nt\CMSNiceties\Extensions\PageControllerEx * @mixin \A2nt\CMSNiceties\Extensions\PageControllerEx
* @mixin \A2nt\CMSNiceties\Ajax\Ex\AjaxLoginFormControllerEx
* @mixin \App\Service\Ex\ServiceSignUpControllerEx
*/ */
class PageController extends ContentController class PageController extends ContentController
{ {
@ -56,38 +60,12 @@ class PageController extends ContentController
// inject AJAX processing // inject AJAX processing
if (Director::is_ajax()) { if (Director::is_ajax()) {
return self::processAJAX($tpls); return AjaxControllerEx::processAJAX($tpls);
} }
return SSViewer::create($tpls); return SSViewer::create($tpls);
} }
protected 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);
}
protected function prepareResponse($response) protected function prepareResponse($response)
{ {
parent::prepareResponse($response); parent::prepareResponse($response);
@ -98,28 +76,4 @@ class PageController extends ContentController
$this->prepareAjaxResponse($response); $this->prepareAjaxResponse($response);
} }
} }
protected function prepareAjaxResponse($response)
{
$record = $this->dataRecord;
$req = $this->getRequest();
$url = $req->getURL();
$url = $url === 'home' ? '/' : $url;
$resources = array_merge(
$this->config()->get('graphql_resources'),
$this->config()->get('ajax_resources')
);
$response->setBody(json_encode([
'ID' => $record->ID,
'Title' => $record->Title,
'Link' => $this->Link(),
'CSSClass' => $this->CSSClass(),
'Resources' => $resources,
'RequestLink' => $url,
'MainContent' => $response->getBody(),
]));
}
} }