Skip to content

Commit

Permalink
Refactoring the registration module
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Aug 14, 2021
1 parent a40ad33 commit f935469
Show file tree
Hide file tree
Showing 21 changed files with 601 additions and 119 deletions.
2 changes: 1 addition & 1 deletion modules/homepage/config/routes.php
Expand Up @@ -9,5 +9,5 @@
*/

return function (\League\Route\Router $router) {
$router->get('/', [\Homepage\Controllers\HomepageController::class, 'index']);
$router->get('/', [\Homepage\Controllers\HomepageController::class, 'index'])->setName('homepage.index');
};
20 changes: 20 additions & 0 deletions modules/registration/config/routes.php
@@ -0,0 +1,20 @@
<?php

/**
* This file is part of JohnCMS Content Management System.
*
* @copyright JohnCMS Community
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
* @link https://johncms.com JohnCMS Project
*/

use League\Route\RouteGroup;
use League\Route\Router;
use Registration\Controllers\RegistrationController;

return function (Router $router) {
$router->get('/registration[/]', [RegistrationController::class, 'index'])->setName('registration.index');
$router->group('/registration', function (RouteGroup $route) {
$route->post('/store[/]', [RegistrationController::class, 'store'])->setName('registration.store');
});
};
70 changes: 70 additions & 0 deletions modules/registration/src/Controllers/RegistrationController.php
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of JohnCMS Content Management System.
*
* @copyright JohnCMS Community
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
* @link https://johncms.com JohnCMS Project
*/

namespace Registration\Controllers;

use Johncms\Controller\BaseController;
use Johncms\Exceptions\ValidationException;
use Johncms\Http\RedirectResponse;
use Johncms\Users\AuthProviders\SessionAuthProvider;
use Johncms\Users\UserManager;
use Registration\Forms\RegistrationForm;
use Throwable;

class RegistrationController extends BaseController
{
protected string $module_name = 'registration';

/**
* @throws Throwable
*/
public function index(): string
{
$this->render->addData(
[
'title' => __('Registration'),
'page_title' => __('Registration'),
'keywords' => __('Registration'),
'description' => __('Registration'),
]
);
$this->nav_chain->add(__('Registration'), route('registration.index'));

$registrationForm = new RegistrationForm();

$data = [
'formFields' => $registrationForm->getFormFields(),
'validationErrors' => $registrationForm->getValidationErrors(),
'storeUrl' => route('registration.store'),
];

return $this->render->render('registration::index', ['data' => $data]);
}

public function store(UserManager $userManager): RedirectResponse
{
$registrationForm = new RegistrationForm();
try {
// Validate the form
$registrationForm->validate();
// Create user
$user = $userManager->create($registrationForm->getRequestValues());
// Authorize the user
$session_provider = di(SessionAuthProvider::class);
$session_provider->store($user);
return (new RedirectResponse(route('homepage.index')));
} catch (ValidationException $validationException) {
// Redirect to the registration form if the form is invalid
return (new RedirectResponse(route('registration.index')))
->withPost()
->withValidationErrors($validationException->getErrors());
}
}
}
142 changes: 142 additions & 0 deletions modules/registration/src/Forms/RegistrationForm.php
@@ -0,0 +1,142 @@
<?php

/**
* This file is part of JohnCMS Content Management System.
*
* @copyright JohnCMS Community
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
* @link https://johncms.com JohnCMS Project
*/

declare(strict_types=1);

namespace Registration\Forms;

use Johncms\Exceptions\ValidationException;
use Johncms\Forms\Inputs\AbstractInput;
use Johncms\Forms\Inputs\InputPassword;
use Johncms\Forms\Inputs\InputText;
use Johncms\Http\Request;
use Johncms\Http\Session;
use Johncms\Users\User;
use Johncms\Validator\Validator;
use Laminas\Validator\Hostname;

class RegistrationForm
{
/** @var AbstractInput[] */
protected array $formFields = [];

protected ?array $requestValues = null;

protected Request $request;

public function __construct()
{
$this->request = di(Request::class);
$this->prepareFormFields();
}

protected function prepareFormFields(): void
{
$this->formFields = [
'login' => (new InputText())
->setLabel(__('Login'))
->setPlaceholder(__('Enter your login'))
->setNameAndId('login')
->setHelpText(__('Min. %s, Max. %s characters. Allowed letters of the latin alphabets and numbers.', 3, 50))
->setValue($this->getValue('login'))
->setValidationRules(
[
'NotEmpty',
'StringLength' => ['min' => 3, 'max' => 50],
'ModelNotExists' => [
'model' => User::class,
'field' => 'login',
],
]
),
'email' => (new InputText())
->setLabel(__('E-mail'))
->setPlaceholder(__('Enter your e-mail'))
->setNameAndId('email')
->setHelpText(__('Specify an existing e-mail because a confirmation of registration will be sent to it.'))
->setValue($this->getValue('email'))
->setValidationRules(
[
'NotEmpty',
'ModelNotExists' => [
'model' => User::class,
'field' => 'email',
],
'EmailAddress' => [
'allow' => Hostname::ALLOW_DNS,
'useMxCheck' => true,
'useDeepMxCheck' => true,
],
]
),
'password' => (new InputPassword())
->setLabel(__('Password'))
->setPlaceholder(__('Password'))
->setNameAndId('password')
->setHelpText(__('Min. %s characters.', 6))
->setValidationRules(
[
'NotEmpty',
'StringLength' => ['min' => 6],
]
),
];
}

public function getValue(string $fieldName, mixed $default = null)
{
return $this->request->getPost($fieldName, $default);
}

public function getValidationErrors(): array
{
$session = di(Session::class);
return (array) $session->getFlash(Validator::VALIDATION_ERRORS_KEY);
}

public function getFormFields(): array
{
return $this->formFields;
}

public function validate(): void
{
$rules = $this->collectValidationRules();
$values = $this->getRequestValues();
$validator = new Validator($values, $rules);
if (! $validator->isValid()) {
throw ValidationException::withErrors($validator->getErrors());
}
}

protected function collectValidationRules(): array
{
$rules = [];
foreach ($this->formFields as $key => $formField) {
if (! empty($formField->validationRules)) {
$rules[$key] = $formField->validationRules;
}
}
return $rules;
}

public function getRequestValues(): array
{
if ($this->requestValues !== null) {
return $this->requestValues;
}

$this->requestValues = [];
foreach ($this->formFields as $key => $formField) {
$this->requestValues[$key] = $this->request->getPost($formField->name);
}
return $this->requestValues;
}
}
135 changes: 19 additions & 116 deletions modules/registration/templates/index.phtml
@@ -1,129 +1,32 @@
<?php $this->layout(
'system::layout/default',
[
'title' => __('Registration'),
'page_title' => __('Registration'),
]
) ?>
<?php

<form method="post">
/**
* @var Mobicms\Render\Template\Template $this
* @var array $data
* @var array $config
* @noinspection PhpUnhandledExceptionInspection
*/

$this->layout('system::layout/default');
?>
<form method="post" class="mb-3" action="<?= $data['storeUrl'] ?>">
<h4><?= __('Registration') ?></h4>

<?php if ($config['mod_reg'] === 1): ?>
<div class="alert alert-warning"><?= __('You can get authorized on the site after confirmation of your registration.') ?></div>
<?php endif ?>

<?php if (! empty($errors['name_lat'])): ?>
<div class="alert alert-danger"><?= implode(', ', $errors['name_lat']) ?></div>
<?php endif ?>

<div class="alert alert-info">
<?= __('Please, do not register names like 111, shhhh, uuuu, etc. They will be deleted. <br /> Also all the profiles registered via proxy servers will be deleted') ?>
</div>

<div class="form-group">
<label for="name"><?= __('Choose Nickname') ?></label>
<input type="text"
class="form-control <?= (isset($errors['name']) ? 'is-invalid' : '') ?>"
maxlength="20"
minlength="2"
name="name"
id="name"
required
value="<?= $this->e($fields['name']) ?>"
placeholder="<?= __('Username') ?>"
>
<?php if (isset($errors['name'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['name']) ?></div>
<?php endif ?>
<div class="text-muted small">
<?= __('Min. 2, Max. 20 characters.<br />Allowed letters of the russian and latin alphabets, numbers and symbols - = @ ! ? ~ _ ( ) [ ] . * (Except zero)') ?>
</div>
</div>
<div class="form-group">
<label for="password"><?= __('Assign a password') ?></label>
<input type="password"
class="form-control <?= (isset($errors['password']) ? 'is-invalid' : '') ?>"
maxlength="20"
minlength="2"
name="password"
id="password"
required
value="<?= $this->e($fields['password']) ?>"
placeholder="<?= __('Assign a password') ?>"
>
<?php if (isset($errors['password'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['password']) ?></div>
<?php endif ?>
</div>
<?php if (! empty($config['user_email_required']) || ! empty($config['user_email_confirmation'])): ?>
<div class="form-group">
<label for="email"><?= __('E-mail') ?></label>
<input type="email"
class="form-control <?= (isset($errors['email']) ? 'is-invalid' : '') ?>"
name="email"
id="email"
required
value="<?= $this->e($fields['email']) ?>"
placeholder="<?= __('E-mail') ?>"
>
<?php if (isset($errors['email'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['email']) ?></div>
<?php endif ?>
</div>
<?php endif ?>
<div class="form-group">
<label for="gender"><?= __('Select Gender') ?></label>
<select id="gender" name="sex" class="form-control <?= (isset($errors['sex']) ? 'is-invalid' : '') ?>">
<option value="?">- ? -</option>
<option value="m"<?= ($fields['sex'] === 'm' ? ' selected="selected"' : '') ?>><?= __('Man') ?></option>
<option value="zh"<?= ($fields['sex'] === 'zh' ? ' selected="selected"' : '') ?>><?= __('Woman') ?></option>
</select>
<?php if (isset($errors['sex'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['sex']) ?></div>
<?php endif ?>
</div>
<div class="form-group">
<label for="imname"><?= __('Your name') ?></label>
<input type="text"
class="form-control <?= (isset($errors['imname']) ? 'is-invalid' : '') ?>"
maxlength="20"
name="imname"
id="imname"
value="<?= $this->e($fields['imname']) ?>"
placeholder="<?= __('Your name') ?>"
>
<?php if (isset($errors['imname'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['imname']) ?></div>
<?php endif ?>
</div>
<div class="form-group">
<label for="about"><?= __('Tell us a little about yourself') ?></label>
<textarea class="form-control <?= (isset($errors['about']) ? 'is-invalid' : '') ?>" name="about" id="about" rows="3"><?= $this->e($fields['about']) ?></textarea>
<?php if (isset($errors['about'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['about']) ?></div>
<?php endif ?>
</div>

<img alt="<?= __('Verification code') ?>" src="<?= $captcha ?>">
<div class="form-group">
<label for="captcha"><?= __('Enter verification code') ?></label>
<input type="text"
class="form-control <?= (isset($errors['captcha']) ? 'is-invalid' : '') ?>"
maxlength="6"
name="captcha"
id="captcha"
autocomplete="off"
required
placeholder="<?= __('Verification code') ?>"
>
<?php if (isset($errors['captcha'])): ?>
<div class="invalid-feedback"><?= implode('<br>', $errors['captcha']) ?></div>
<?php endif ?>
<div class="text-muted small">
<?= __('If you cannot see the image code, enable graphics in your browser and refresh this page') ?>
</div>
</div>
<?= $this->fetch(
'system::forms/simple_form',
[
'fields' => $data['formFields'],
'errors' => $data['validationErrors'],
]
); ?>

<input type="submit" name="submit" class="btn btn-primary" value="<?= __('Registration') ?>">
<button type="submit" name="submit" class="btn btn-primary"><?= __('Registration') ?></button>
</form>
1 change: 1 addition & 0 deletions system/src/Application.php
Expand Up @@ -61,6 +61,7 @@ private function setupTranslator(): void

public function handleRequest(): void
{
$this->container->bind(RouterFactory::class, RouterFactory::class, true);
$router = $this->container->get(RouterFactory::class);
(new SapiEmitter())->emit($router->dispatch());
}
Expand Down

0 comments on commit f935469

Please sign in to comment.