Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
263 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
modules/johncms/personal/src/Controllers/SettingsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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 Johncms\Personal\Controllers; | ||
|
||
use Johncms\Controller\BaseController; | ||
use Johncms\Exceptions\ValidationException; | ||
use Johncms\Http\Response\RedirectResponse; | ||
use Johncms\Http\Session; | ||
use Johncms\Personal\Forms\SettingsForm; | ||
use Johncms\Users\Exceptions\RuntimeException; | ||
use Johncms\Users\User; | ||
use Johncms\Users\UserManager; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Throwable; | ||
|
||
class SettingsController extends BaseController | ||
{ | ||
protected string $moduleName = 'johncms/personal'; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->metaTagManager->setAll(__('User Profile')); | ||
$this->navChain->add(__('Personal account'), route('personal.index')); | ||
$this->navChain->add(__('Settings'), route('personal.settings')); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function index(SettingsForm $settingsForm, Session $session): string|ResponseInterface | ||
{ | ||
return $this->render->render('personal::settings', [ | ||
'data' => [ | ||
'formFields' => $settingsForm->getFormFields(), | ||
'validationErrors' => $settingsForm->getValidationErrors(), | ||
'success' => $session->getFlash('success'), | ||
'errors' => $session->getFlash('errors'), | ||
'storeUrl' => route('personal.settings.store'), | ||
'backButton' => route('personal.index'), | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function store(SettingsForm $settingsForm, User $user, UserManager $userManager, Session $session): ResponseInterface|RedirectResponse | ||
{ | ||
try { | ||
// Validate the form | ||
$settingsForm->validate(); | ||
try { | ||
$userManager->update($user->id, ['settings' => $settingsForm->getRequestValues()]); | ||
$session->flash('success', __('Settings saved successfully')); | ||
return new RedirectResponse(route('personal.settings')); | ||
} catch (RuntimeException $exception) { | ||
$session->flash('errors', $exception->getMessage()); | ||
return (new RedirectResponse(route('personal.settings')))->withPost(); | ||
} | ||
} catch (ValidationException $validationException) { | ||
// Redirect if the form is invalid | ||
return (new RedirectResponse(route('personal.settings'))) | ||
->withPost() | ||
->withValidationErrors($validationException->getErrors()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Johncms\Personal\Forms; | ||
|
||
use Johncms\Forms\AbstractForm; | ||
use Johncms\Forms\Inputs\Checkbox; | ||
use Johncms\Forms\Inputs\InputText; | ||
use Johncms\Forms\Inputs\Select; | ||
use Johncms\i18n\Languages; | ||
use Johncms\Users\User; | ||
use Johncms\View\Themes; | ||
|
||
class SettingsForm extends AbstractForm | ||
{ | ||
public function __construct( | ||
protected ?User $userData = null | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function prepareFormFields(): array | ||
{ | ||
$fields = []; | ||
$fields['lang'] = (new Select()) | ||
->setOptions($this->getLanguages()) | ||
->setLabel(__('Language')) | ||
->setNameAndId('lang') | ||
->setValue($this->getValue('lang')); | ||
|
||
$fields['timezone'] = (new Select()) | ||
->setOptions($this->getTimezones()) | ||
->setLabel(__('Timezone')) | ||
->setNameAndId('timezone') | ||
->setValue($this->getValue('timezone')); | ||
|
||
$fields['directUrl'] = (new Checkbox()) | ||
->setLabel(__('Direct links')) | ||
->setNameAndId('directUrl') | ||
->setHelpText(__('When this option is enabled, links to other sites will open without an intermediate page.')) | ||
->setValue(true) | ||
->setChecked((bool) $this->getValue('directUrl')); | ||
|
||
$fields['perPage'] = (new InputText()) | ||
->setLabel(__('Elements per page')) | ||
->setPlaceholder(__('Elements per page')) | ||
->setNameAndId('perPage') | ||
->setValue($this->getValue('perPage')); | ||
|
||
$fields['theme'] = (new Select()) | ||
->setOptions($this->getThemesList()) | ||
->setLabel(__('Site theme')) | ||
->setNameAndId('theme') | ||
->setValue($this->getValue('theme')); | ||
|
||
return $fields; | ||
} | ||
|
||
public function getValue(string $fieldName, mixed $default = null) | ||
{ | ||
if ($this->userData) { | ||
// Base fields | ||
return parent::getValue($fieldName, $this->userData->settings?->$fieldName); | ||
} | ||
return parent::getValue($fieldName, $default); | ||
} | ||
|
||
private function getThemesList(): array | ||
{ | ||
$themes = di(Themes::class)->getThemes(); | ||
$themesList = []; | ||
foreach ($themes as $theme) { | ||
if ($theme === 'example') { | ||
continue; | ||
} | ||
$themesList[] = [ | ||
'name' => $theme, | ||
'value' => $theme, | ||
]; | ||
} | ||
return $themesList; | ||
} | ||
|
||
// TODO: Change the list of timezones | ||
private function getTimezones(): array | ||
{ | ||
return [ | ||
[ | ||
'value' => 'Europe/Moscow', | ||
'name' => d__('system', 'Europe/Moscow'), | ||
], | ||
[ | ||
'value' => 'Europe/Berlin', | ||
'name' => d__('system', 'Europe/Berlin'), | ||
], | ||
[ | ||
'value' => 'America/New_York', | ||
'name' => d__('system', 'America/New_York'), | ||
], | ||
]; | ||
} | ||
|
||
private function getLanguages(): array | ||
{ | ||
$lngList = Languages::getLngList(); | ||
$options = []; | ||
foreach ($lngList as $key => $item) { | ||
$options[] = [ | ||
'name' => $item['name'], | ||
'value' => $key, | ||
]; | ||
} | ||
|
||
return $options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,120 +1,26 @@ | ||
<?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 | ||
*/ | ||
|
||
/** | ||
* @var $title | ||
* @var $page_title | ||
* @var $data | ||
*/ | ||
|
||
$this->layout( | ||
'system::layout/default', | ||
[ | ||
'title' => $title, | ||
'page_title' => $title, | ||
] | ||
); | ||
|
||
$userConfig = $user->config; | ||
|
||
$this->layout('system::layout/default'); | ||
?> | ||
|
||
<div class="buttons mb-2"> | ||
<?php foreach ($data['buttons'] as $button): ?> | ||
<a href="<?= $button['url'] ?>" class="btn btn-outline-primary mb-1 <?= $button['active'] ? 'active' : '' ?>"><?= $button['name'] ?></a> | ||
<?php endforeach; ?> | ||
</div> | ||
|
||
<?php if (! empty($data['success_message'])): ?> | ||
<form method="post" class="mb-3" action="<?= $data['storeUrl'] ?>"> | ||
<?php if (! empty($data['success'])): ?> | ||
<?= $this->fetch('system::app/alert', ['alert_type' => 'alert-success', 'alert' => $data['success']]) ?> | ||
<?php endif ?> | ||
<?php if (! empty($data['errors'])): ?> | ||
<?= $this->fetch('system::app/alert', ['alert_type' => 'alert-danger', 'alert' => $data['errors']]) ?> | ||
<?php endif ?> | ||
<?= $this->fetch( | ||
'system::app/alert', | ||
'system::forms/simple_form', | ||
[ | ||
'alert_type' => 'alert-success', | ||
'alert' => $data['success_message'], | ||
'fields' => $data['formFields'], | ||
'errors' => $data['validationErrors'], | ||
] | ||
) ?> | ||
<?php endif; ?> | ||
<form action="<?= $data['form_action'] ?>" method="post"> | ||
<h3><?= __('Time settings') ?></h3> | ||
<div class="form-group"> | ||
<label for="timeshift"><?= __('Shift of time') ?> (+-12)</label> | ||
<input type="text" | ||
class="form-control" | ||
name="timeshift" | ||
id="timeshift" | ||
size="2" | ||
maxlength="3" | ||
value="<?= $userConfig->timeshift ?>" | ||
placeholder="<?= __('Shift of time') ?>" | ||
> | ||
<div class="small text-muted"> | ||
<?= __('System time') ?>: <?= $data['system_time'] ?> | ||
</div> | ||
</div> | ||
|
||
<h3><?= __('System Functions') ?></h3> | ||
<div class="custom-control custom-checkbox"> | ||
<input type="checkbox" class="form-check-input" name="directUrl" value="1" id="directUrl" <?= $userConfig->directUrl ? 'checked' : '' ?>> | ||
<label class="form-check-label" for="directUrl"><?= __('Direct URL') ?></label> | ||
</div> | ||
<div class="custom-control custom-checkbox mb-3"> | ||
<input type="checkbox" class="form-check-input" name="youtube" value="1" id="youtube" <?= $userConfig->youtube ? 'checked' : '' ?>> | ||
<label class="form-check-label" for="youtube"><?= __('Youtube Player') ?></label> | ||
</div> | ||
|
||
<h3><?= __('Text entering') ?></h3> | ||
<div class="form-group"> | ||
<label for="fieldHeight"><?= __('Height of field') ?> (1-9)</label> | ||
<input type="text" | ||
class="form-control" | ||
name="fieldHeight" | ||
id="fieldHeight" | ||
maxlength="1" | ||
value="<?= $userConfig->fieldHeight ?>" | ||
placeholder="<?= __('Height of field') ?>" | ||
> | ||
</div> | ||
|
||
<h3><?= __('Appearance') ?></h3> | ||
<div class="form-group"> | ||
<label for="kmess"><?= __('Size of Lists') ?> (5-99)</label> | ||
<input type="text" | ||
class="form-control" | ||
name="kmess" | ||
id="kmess" | ||
maxlength="2" | ||
value="<?= $userConfig->kmess ?>" | ||
placeholder="<?= __('Size of Lists') ?>" | ||
> | ||
</div> | ||
|
||
<?php if (! empty($data['lng_list'])): ?> | ||
<h3><?= __('Select Language') ?></h3> | ||
<div class="form-group"> | ||
<label for="iso"><?= __('Select Language') ?></label> | ||
<select class="form-control" id="iso" name="iso"> | ||
<?php foreach ($data['lng_list'] as $key => $lang): ?> | ||
<option value="<?= $key ?>" | ||
<?= ($key === $data['user_lng'] || ($key === $config['lng'] && empty($data['user_lng']))) ? 'selected' : '' ?> | ||
><?= $lang['name'] ?><?= ($key === $config['lng'] ? ' (' . __('Site Default') . ')' : '') ?></option> | ||
<?php endforeach; ?> | ||
</select> | ||
</div> | ||
<?php endif; ?> | ||
<div class="d-flex flex-wrap justify-content-between"> | ||
<div class="mb-1 me-1"> | ||
<input type="submit" name="submit" value="<?= __('Save') ?>" class="btn btn-primary"/> | ||
<a href="?act=office" class="btn btn-secondary"><?= __('Cancel') ?></a> | ||
</div> | ||
<div> | ||
<a href="?act=settings&reset" class="btn btn-danger"><?= __('Reset Settings') ?></a> | ||
</div> | ||
</div> | ||
); ?> | ||
<button type="submit" name="submit" class="btn btn-primary"><?= __('Save') ?></button> | ||
<a href="<?= $data['backButton'] ?>" class="btn btn-outline-secondary"><?= __('Back') ?></a> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.