Commit 9bdce774 authored by Tino Goratsch's avatar Tino Goratsch

minor refactoring for the users module to reduce code duplication

parent 7a4805ab
......@@ -3,7 +3,6 @@
namespace ACP3\Modules\ACP3\Users\Controller;
use ACP3\Core;
use ACP3\Core\Helpers\Country;
use ACP3\Modules\ACP3\Users;
/**
......
......@@ -110,6 +110,17 @@ class Forms
return $countriesSelect;
}
/**
* @param int $entries
* @param string $language
* @param string $timeZone
* @param int $displayAddress
* @param int $displayBirthday
* @param int $displayCountry
* @param int $displayMail
*
* @return array
*/
public function fetchUserSettingsFormFields(
$entries,
$language,
......@@ -148,12 +159,12 @@ class Forms
*/
protected function displayBirthday($value)
{
$lang_birthdayDisplay = [
$langBirthdayDisplay = [
$this->lang->t('users', 'birthday_hide'),
$this->lang->t('users', 'birthday_display_completely'),
$this->lang->t('users', 'birthday_hide_year')
];
return $this->formsHelpers->checkboxGenerator('birthday_display', [0, 1, 2], $lang_birthdayDisplay, $value);
return $this->formsHelpers->checkboxGenerator('birthday_display', [0, 1, 2], $langBirthdayDisplay, $value);
}
/**
......@@ -179,7 +190,7 @@ class Forms
/**
* @param string $birthday
* @param string $country
* @param int $gender
* @param int $gender
*
* @return array
*/
......@@ -199,12 +210,12 @@ class Forms
*/
protected function fetchGenderField($value)
{
$lang_gender = [
$langGender = [
$this->lang->t('users', 'gender_not_specified'),
$this->lang->t('users', 'gender_female'),
$this->lang->t('users', 'gender_male')
];
return $this->formsHelpers->selectGenerator('gender', [1, 2, 3], $lang_gender, $value);
return $this->formsHelpers->selectGenerator('gender', [1, 2, 3], $langGender, $value);
}
}
\ No newline at end of file
......@@ -45,7 +45,7 @@
<label for="date-birthday-input" class="col-sm-2 control-label">{lang t="users|birthday"}</label>
<div class="col-sm-10">
{datepicker name="birthday" value=$birthday inputFieldOnly=true}
{datepicker name="birthday" value=$birthday inputFieldOnly=true withTime=false}
</div>
</div>
<div class="form-group">
......@@ -240,16 +240,16 @@
</div>
</div>
<div id="tab-6" class="tab-pane fade">
<div class="form-group">
<label for="pwd" class="col-sm-2 control-label">{lang t="users|pwd"}</label>
<div class="col-sm-10"><input class="form-control" type="password" name="pwd" id="pwd"></div>
</div>
<div class="form-group">
<label for="pwd-repeat" class="col-sm-2 control-label">{lang t="users|pwd_repeat"}</label>
<div class="col-sm-10"><input class="form-control" type="password" name="pwd_repeat" id="pwd-repeat"></div>
</div>
{block PASSWORD_FIELDS}
<div class="form-group">
<label for="pwd" class="col-sm-2 control-label">{lang t="users|pwd"}</label>
<div class="col-sm-10"><input class="form-control" type="password" name="pwd" id="pwd"></div>
</div>
<div class="form-group">
<label for="pwd-repeat" class="col-sm-2 control-label">{lang t="users|pwd_repeat"}</label>
<div class="col-sm-10"><input class="form-control" type="password" name="pwd_repeat" id="pwd-repeat"></div>
</div>
{/block}
</div>
</div>
</div>
......
......@@ -9,6 +9,31 @@ use ACP3\Core;
*/
abstract class AbstractUserValidator extends Core\Validator\AbstractValidator
{
/**
* @var \ACP3\Core\Validator\Rules\Date
*/
protected $dateValidator;
/**
* AbstractUserValidator constructor.
*
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\Validator\Validator $validator
* @param \ACP3\Core\Validator\Rules\Misc $validate
* @param \ACP3\Core\Validator\Rules\Date $dateValidator
*/
public function __construct(
Core\Lang $lang,
Core\Validator\Validator $validator,
Core\Validator\Rules\Misc $validate,
Core\Validator\Rules\Date $dateValidator
)
{
parent::__construct($lang, $validator, $validate);
$this->dateValidator = $dateValidator;
}
/**
* @param array $formData
* @param string $passwordField
......@@ -52,7 +77,7 @@ abstract class AbstractUserValidator extends Core\Validator\AbstractValidator
*/
protected function _gender($var)
{
return $var == 1 || $var == 2 || $var == 3;
return in_array($var, [1, 2, 3]);
}
/**
......@@ -66,4 +91,40 @@ abstract class AbstractUserValidator extends Core\Validator\AbstractValidator
{
return (bool)preg_match('/^(\d{6,9})$/', $var);
}
/**
* @param array $formData
* @param int $languageOverride
* @param int $entriesOverride
*/
protected function validateUserSettings(array $formData, $languageOverride = 1, $entriesOverride = 1)
{
if ($languageOverride == 1 && $this->lang->languagePackExists($formData['language']) === false) {
$this->errors['language'] = $this->lang->t('users', 'select_language');
}
if ($entriesOverride == 1 && $this->validate->isNumber($formData['entries']) === false) {
$this->errors['entries'] = $this->lang->t('system', 'select_records_per_page');
}
if (empty($formData['date_format_long'])) {
$this->errors['date-format-long'] = $this->lang->t('system', 'type_in_long_date_format');
}
if (empty($formData['date_format_short'])) {
$this->errors['date-format-short'] = $this->lang->t('system', 'type_in_short_date_format');
}
if ($this->dateValidator->timeZone($formData['date_time_zone']) === false) {
$this->errors['time-zone'] = $this->lang->t('system', 'select_time_zone');
}
if (in_array($formData['mail_display'], [0, 1]) === false) {
$this->errors['mail-display'] = $this->lang->t('users', 'select_mail_display');
}
if (in_array($formData['address_display'], [0, 1]) === false) {
$this->errors['address-display'] = $this->lang->t('users', 'select_address_display');
}
if (in_array($formData['country_display'], [0, 1]) === false) {
$this->errors['country-display'] = $this->lang->t('users', 'select_country_display');
}
if (in_array($formData['birthday_display'], [0, 1, 2]) === false) {
$this->errors['birthday-display'] = $this->lang->t('users', 'select_birthday_display');
}
}
}
\ No newline at end of file
......@@ -11,10 +11,6 @@ use ACP3\Modules\ACP3\Users\Model\UserRepository;
*/
class Account extends AbstractUserValidator
{
/**
* @var \ACP3\Core\Validator\Rules\Date
*/
protected $dateValidator;
/**
* @var \ACP3\Core\User
*/
......@@ -43,9 +39,8 @@ class Account extends AbstractUserValidator
UserRepository $userRepository
)
{
parent::__construct($lang, $validator, $validate);
parent::__construct($lang, $validator, $validate, $dateValidator);
$this->dateValidator = $dateValidator;
$this->user = $user;
$this->userModel = $userRepository;
}
......@@ -99,33 +94,7 @@ class Account extends AbstractUserValidator
$this->validateFormKey();
$this->errors = [];
if ($settings['language_override'] == 1 && $this->lang->languagePackExists($formData['language']) === false) {
$this->errors['language'] = $this->lang->t('users', 'select_language');
}
if ($settings['entries_override'] == 1 && $this->validate->isNumber($formData['entries']) === false) {
$this->errors['entries'] = $this->lang->t('system', 'select_records_per_page');
}
if (empty($formData['date_format_long'])) {
$this->errors['date-format-long'] = $this->lang->t('system', 'type_in_long_date_format');
}
if (empty($formData['date_format_short'])) {
$this->errors['date-format-short'] = $this->lang->t('system', 'type_in_short_date_format');
}
if ($this->dateValidator->timeZone($formData['date_time_zone']) === false) {
$this->errors['time-zone'] = $this->lang->t('system', 'select_time_zone');
}
if (in_array($formData['mail_display'], [0, 1]) === false) {
$this->errors['mail-display'] = $this->lang->t('users', 'select_mail_display');
}
if (in_array($formData['address_display'], [0, 1]) === false) {
$this->errors['address-display'] = $this->lang->t('users', 'select_address_display');
}
if (in_array($formData['country_display'], [0, 1]) === false) {
$this->errors['country-display'] = $this->lang->t('users', 'select_country_display');
}
if (in_array($formData['birthday_display'], [0, 1, 2]) === false) {
$this->errors['birthday-display'] = $this->lang->t('users', 'select_birthday_display');
}
parent::validateUserSettings($formData, $settings['language_override'], $settings['entries_override']);
$this->_checkForFailedValidation();
}
......
......@@ -14,10 +14,6 @@ class Admin extends AbstractUserValidator
* @var \ACP3\Core\Validator\Rules\ACL
*/
protected $aclValidator;
/**
* @var \ACP3\Core\Validator\Rules\Date
*/
protected $dateValidator;
/**
* @var \ACP3\Core\User
*/
......@@ -46,10 +42,9 @@ class Admin extends AbstractUserValidator
UserRepository $userRepository
)
{
parent::__construct($lang, $validator, $validate);
parent::__construct($lang, $validator, $validate, $dateValidator);
$this->aclValidator = $aclValidator;
$this->dateValidator = $dateValidator;
$this->user = $user;
$this->userModel = $userRepository;
}
......@@ -62,23 +57,38 @@ class Admin extends AbstractUserValidator
*/
public function validateSettings(array $formData)
{
$this->validateFormKey();
$this->errors = [];
if (!empty($formData['mail']) && $this->validate->email($formData['mail']) === false) {
$this->errors['mail'] = $this->lang->t('system', 'wrong_email_format');
}
if (!isset($formData['language_override']) || $formData['language_override'] != 1 && $formData['language_override'] != 0) {
$this->errors['language-override'] = $this->lang->t('users', 'select_languages_override');
}
if (!isset($formData['entries_override']) || $formData['entries_override'] != 1 && $formData['entries_override'] != 0) {
$this->errors['entries-override'] = $this->lang->t('users', 'select_entries_override');
}
if (!isset($formData['enable_registration']) || $formData['enable_registration'] != 1 && $formData['enable_registration'] != 0) {
$this->errors['enable-registration'] = $this->lang->t('users', 'select_enable_registration');
}
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Core\Validator\ValidationRules\EmailValidationRule::NAME,
[
'data' => $formData,
'field' => 'mail',
'message' => $this->lang->t('system', 'wrong_email_format')
])
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'language_override',
'message' => $this->lang->t('users', 'select_languages_override')
])
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'entries_override',
'message' => $this->lang->t('users', 'select_entries_override')
])
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'enable_registration',
'message' => $this->lang->t('users', 'select_enable_registration')
]);
$this->_checkForFailedValidation();
$this->validator->validate();
}
/**
......@@ -117,36 +127,9 @@ class Admin extends AbstractUserValidator
if (!isset($formData['super_user']) || ($formData['super_user'] != 1 && $formData['super_user'] != 0)) {
$this->errors['super-user'] = $this->lang->t('users', 'select_super_user');
}
if ($this->lang->languagePackExists($formData['language']) === false) {
$this->errors['language'] = $this->lang->t('users', 'select_language');
}
if ($this->validate->isNumber($formData['entries']) === false) {
$this->errors['entries'] = $this->lang->t('system', 'select_records_per_page');
}
if (empty($formData['date_format_long'])) {
$this->errors['date-format-long'] = $this->lang->t('system', 'type_in_long_date_format');
}
if (empty($formData['date_format_short'])) {
$this->errors['date-format-short'] = $this->lang->t('system', 'type_in_short_date_format');
}
if ($this->dateValidator->timeZone($formData['date_time_zone']) === false) {
$this->errors['time-zone'] = $this->lang->t('system', 'select_time_zone');
}
if (!empty($formData['icq']) && $this->_icq($formData['icq']) === false) {
$this->errors['icq'] = $this->lang->t('users', 'invalid_icq_number');
}
if (in_array($formData['mail_display'], [0, 1]) === false) {
$this->errors['mail-display'] = $this->lang->t('users', 'select_mail_display');
}
if (in_array($formData['address_display'], [0, 1]) === false) {
$this->errors['address-display'] = $this->lang->t('users', 'select_address_display');
}
if (in_array($formData['country_display'], [0, 1]) === false) {
$this->errors['country-display'] = $this->lang->t('users', 'select_country_display');
}
if (in_array($formData['birthday_display'], [0, 1, 2]) === false) {
$this->errors['birthday-display'] = $this->lang->t('users', 'select_birthday_display');
}
$this->validateUserSettings($formData, 1, 1);
if (isset($formData['new_pwd'])) {
$this->validateNewPassword($formData, 'new_pwd', 'new_pwd_repeat');
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment