Compare commits

...

4 Commits

Author SHA1 Message Date
Tony Air 53a1b8ff35 IMPR: AJAX processing 2024-02-02 23:24:58 +02:00
Tony Air cd78ad1f9e IMPR: AJAX processing 2024-02-02 19:15:24 +02:00
Tony Air a81011feb2 IMPR: AJAX processing 2024-02-02 18:19:43 +02:00
Tony Air 70f42541f2 IMPR: Minor form updates 2024-02-02 16:32:15 +02:00
4 changed files with 208 additions and 61 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'
@ -45,6 +45,8 @@ SilverStripe\Core\Injector\Injector:
class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RecordEditor
SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor:
class: A2nt\CMSNiceties\Forms\GridField\GridFieldConfig_RelationEditor
SilverStripe\Forms\FormRequestHandler:
class: A2nt\CMSNiceties\Ajax\AjaxFormRequestHandler
SilverStripe\UserForms\Form\UserForm:
extensions:

View File

@ -0,0 +1,44 @@
<?php
namespace A2nt\CMSNiceties\Ajax;
use A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx;
use SilverStripe\Forms\FormRequestHandler;
use SilverStripe\ORM\ValidationResult;
class AjaxFormRequestHandler extends FormRequestHandler
{
private static $allowed_actions = [
'httpSubmission',
];
/**
* Handle a form submission. GET and POST requests behave identically.
* Populates the form with {@link loadDataFrom()}, calls {@link validate()},
* and only triggers the requested form action/method
* if the form is valid.
*
* @param HTTPRequest $request
* @return HTTPResponse
* @throws HTTPResponse_Exception
*/
public function httpSubmission($request)
{
$resp = parent::httpSubmission($request);
if (!AjaxControllerEx::isFormRequest()) {
return $resp;
}
$validation = $this->form->validationResult();
if (!$validation->isValid()) {
$messages = $validation->getMessages();
return json_encode([
'status' => ValidationResult::TYPE_ERROR,
'msgs' => $messages,
]);
}
return $resp;
}
}

161
src/Ajax/Ex/AjaxControllerEx.php Executable file
View File

@ -0,0 +1,161 @@
<?php
namespace A2nt\CMSNiceties\Ajax\Ex;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\Form;
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 AjaxControllerEx extends Extension
{
private static $allowed_actions = [
'LoginFormEx',
'LostPasswordForm',
'passwordsent',
];
private static function _makeAllFieldsRequired(Form $form)
{
$fields = $form->Fields();
foreach ($fields as $f) {
$f
->setAttribute('required', 'required')
->addExtraClass('required');
}
}
public function LoginFormEx()
{
$ctrl = $this->owner;
/* @var Form $form */
$form = $ctrl->LoginForm();
self::_makeAllFieldsRequired($form);
//$form->addExtraClass('ajax-form');
$form->setLegend('Sign in to your service account');
if ($form->get_protector()) {
$form->enableSpamProtection();
}
return $form;
}
public function LostPasswordForm()
{
if (Security::getCurrentUser()) {
return;
}
$ctrl = $this->owner;
$form = Injector::inst()->get(MemberAuthenticator::class)
->getLostPasswordHandler($ctrl->Link())
->lostPasswordForm();
self::_makeAllFieldsRequired($form);
$form->addExtraClass('ajax-form');
$form->setLegend('Restore your password');
if ($form->get_protector()) {
$form->enableSpamProtection();
}
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 (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."
);
$json = json_encode([
'status' => 'success',
'message' => '<div class="alert alert-success">'.$message.'</div>',
]);
return $json;
}
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(),
]));
}
}

View File

@ -1,60 +0,0 @@
<?php
namespace A2nt\CMSNiceties\Ajax\Ex;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
use SilverStripe\Security\Security;
/**
* Class \App\Service\Ex\ServiceAreaController
*
* @property \A2nt\CMSNiceties\Ajax\Ex\AjaxLoginFormControllerEx $owner
*/
class AjaxLoginFormControllerEx extends Extension
{
private static $allowed_actions = [
'LoginFormEx',
'LostPasswordForm',
'passwordsent',
];
public function LoginFormEx()
{
$ctrl = $this->owner;
/* @var Form $form */
$form = $ctrl->LoginForm();
$form->setLegend('Sign in to your service account');
//$form->enableSpamProtection();
return $form;
}
public function LostPasswordForm()
{
if (Security::getCurrentUser()) {
return;
}
$ctrl = $this->owner;
$form = Injector::inst()->get(MemberAuthenticator::class)
->getLostPasswordHandler($ctrl->Link())
->lostPasswordForm();
$form->setLegend('Restore your password');
//$form->enableSpamProtection();
return $form;
}
public function passwordsent()
{
$ctrl = $this->owner;
return Injector::inst()->get(MemberAuthenticator::class)
->getLostPasswordHandler($ctrl->Link())
->passwordsent();
}
}