cms-niceties/src/Ajax/Ex/AjaxControllerEx.php

212 lines
5.5 KiB
PHP
Raw Normal View History

2024-02-02 14:42:48 +01:00
<?php
namespace A2nt\CMSNiceties\Ajax\Ex;
2024-02-02 17:19:43 +01:00
use SilverStripe\Control\Controller;
2024-02-02 15:32:15 +01:00
use SilverStripe\Control\Director;
2024-02-07 21:37:57 +01:00
use SilverStripe\Core\Config\Config;
2024-02-02 14:42:48 +01:00
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
2024-02-02 18:15:24 +01:00
use SilverStripe\Forms\Form;
2024-02-06 11:16:47 +01:00
use SilverStripe\ORM\FieldType\DBHTMLText;
2024-02-02 14:42:48 +01:00
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
use SilverStripe\Security\Security;
2024-02-02 17:19:43 +01:00
use SilverStripe\View\SSViewer;
2024-02-02 14:42:48 +01:00
/**
2024-02-07 21:37:57 +01:00
* Class \A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx
2024-02-02 14:42:48 +01:00
*
2024-02-07 21:37:57 +01:00
* @property \A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx $owner
2024-02-02 14:42:48 +01:00
*/
2024-02-02 17:19:43 +01:00
class AjaxControllerEx extends Extension
2024-02-02 14:42:48 +01:00
{
2024-02-07 21:37:57 +01:00
private static $no_placeholders = false;
private static $show_labels = false;
2024-02-02 14:42:48 +01:00
private static $allowed_actions = [
'LoginFormEx',
'LostPasswordForm',
'passwordsent',
];
2024-02-07 21:37:57 +01:00
private static function _processFields(Form $form)
2024-02-02 18:15:24 +01:00
{
2024-02-07 21:37:57 +01:00
$cfg = Config::inst()->get(__CLASS__);
2024-02-02 18:15:24 +01:00
$fields = $form->Fields();
2024-02-07 21:37:57 +01:00
foreach ($fields as $field) {
$name = $field->getName();
if ($name === 'Remember') {
continue;
}
$field
2024-02-02 18:15:24 +01:00
->setAttribute('required', 'required')
->addExtraClass('required');
2024-02-07 21:37:57 +01:00
/*
* A2nt\CMSNiceties\Ajax\Ex\AjaxControllerEx:
* show_labels: false
* no_placeholders: false
*/
if (!$cfg['no_placeholders']) {
$placeholder = $field->Title();
$field->setAttribute(
'placeholder',
$placeholder
);
}
if (!$cfg['show_labels']) {
$field->setTitle('');
}
2024-02-02 18:15:24 +01:00
}
}
2024-02-02 14:42:48 +01:00
public function LoginFormEx()
{
$ctrl = $this->owner;
/* @var Form $form */
$form = $ctrl->LoginForm();
2024-02-07 21:37:57 +01:00
self::_processFields($form);
2024-02-02 15:32:15 +01:00
//$form->addExtraClass('ajax-form');
if ($form->get_protector()) {
$form->enableSpamProtection();
}
2024-02-02 14:42:48 +01:00
2024-02-27 15:24:11 +01:00
$form->setLegend(
_t(
'SilverStripe\\Security\\Security.LOGINFORMLEGEND',
'Log in'
)
);
2024-02-02 14:42:48 +01:00
return $form;
}
public function LostPasswordForm()
{
if (Security::getCurrentUser()) {
return;
}
$ctrl = $this->owner;
$form = Injector::inst()->get(MemberAuthenticator::class)
->getLostPasswordHandler($ctrl->Link())
->lostPasswordForm();
2024-02-07 21:37:57 +01:00
self::_processFields($form);
2024-02-02 15:32:15 +01:00
$form->addExtraClass('ajax-form');
2024-02-27 15:24:11 +01:00
$form->Actions()->first()->setTitle(
_t(
'SilverStripe\\Security\\Security.SUBMITTITLE',
'Submit'
)
);
2024-02-02 15:32:15 +01:00
if ($form->get_protector()) {
$form->enableSpamProtection();
}
2024-02-02 14:42:48 +01:00
2024-02-27 15:24:11 +01:00
$form->setLegend(
_t(
'SilverStripe\\Security\\Security.LOSTPASSWORDFORMLEGEND',
'I\'ve lost my password'
)
);
2024-02-02 14:42:48 +01:00
return $form;
}
2024-02-02 17:19:43 +01:00
public static function isFormRequest()
{
$ctrl = Controller::curr();
/* @var $req SilverStripe\Control\HTTPRequest */
$req = $ctrl->getRequest();
return $req->getHeader('x-requested-form') || $req->requestVar('formid');
}
2024-02-02 14:42:48 +01:00
public function passwordsent()
{
$ctrl = $this->owner;
2024-02-02 17:19:43 +01:00
if (self::isFormRequest() && Director::is_ajax()) {
2024-02-02 15:32:15 +01:00
$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;
}
2024-02-02 14:42:48 +01:00
return Injector::inst()->get(MemberAuthenticator::class)
->getLostPasswordHandler($ctrl->Link())
->passwordsent();
}
2024-02-02 17:19:43 +01:00
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,
2024-02-06 11:16:47 +01:00
'MainContent' => $ctrl->customise([
'Layout' => DBHTMLText::create()->setValue($response->getBody()),
])->renderWith('Includes/MainContent')->RAW(),
2024-02-02 17:19:43 +01:00
]));
}
2024-02-02 14:42:48 +01:00
}