Commit c0d1cdc4 authored by Tino Goratsch's avatar Tino Goratsch

converted the Gallery module to use the new validation facility

parent 63a0f7db
......@@ -233,7 +233,7 @@ class Pictures extends Core\Modules\AdminController
function () use ($formData, $settings, $id) {
$file = $this->request->getFiles()->get('file');
$this->pictureValidator->validateCreate($file);
$this->pictureValidator->validate($file);
$upload = new Core\Helpers\Upload('gallery');
$result = $upload->moveFile($file['tmp_name'], $file['name']);
......@@ -275,7 +275,7 @@ class Pictures extends Core\Modules\AdminController
function () use ($formData, $settings, $picture, $id) {
$file = $this->request->getFiles()->get('file');
$this->pictureValidator->validateEdit($file);
$this->pictureValidator->validate($file, false);
$updateValues = [
'description' => Core\Functions::strEncode($formData['description'], true),
......
......@@ -59,15 +59,15 @@ services:
gallery.validator.gallery:
class: %gallery.validator.gallery.fqdn%
arguments: ['@core.lang', '@core.validator.rules.misc', '@core.validator.rules.router.aliases', '@core.validator.rules.date', '@core.modules']
arguments: ['@core.lang', @core.validator, '@core.validator.rules.misc']
gallery.validator.picture:
class: %gallery.validator.picture.fqdn%
arguments: ['@core.lang', '@core.validator.rules.misc', '@core.validator.rules.mime']
arguments: ['@core.lang', @core.validator, '@core.validator.rules.misc']
gallery.validator.settings:
class: %gallery.validator.settings.fqdn%
arguments: ['@core.lang', '@core.validator.rules.misc', '@core.modules']
arguments: ['@core.lang', @core.validator, '@core.validator.rules.misc', '@core.modules']
gallery.installer.schema:
class: %gallery.installer.schema.fqdn%
......
......@@ -2,6 +2,7 @@
namespace ACP3\Modules\ACP3\Gallery\Validator;
use ACP3\Core;
use ACP3\Modules\ACP3\Seo\Validator\ValidationRules\UriAliasValidationRule;
/**
* Class Gallery
......@@ -10,38 +11,26 @@ use ACP3\Core;
class Gallery extends Core\Validator\AbstractValidator
{
/**
* @var Core\Validator\Rules\Date
* @var \ACP3\Core\Validator\Validator
*/
protected $dateValidator;
/**
* @var Core\Validator\Rules\Router\Aliases
*/
protected $aliasesValidator;
/**
* @var \ACP3\Core\Modules
*/
protected $modules;
protected $validator;
/**
* @param Core\Lang $lang
* @param Core\Validator\Rules\Misc $validate
* @param Core\Validator\Rules\Router\Aliases $aliasesValidator
* @param Core\Validator\Rules\Date $dateValidator
* @param Core\Modules $modules
* Gallery constructor.
*
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\Validator\Validator $validator
* @param \ACP3\Core\Validator\Rules\Misc $validate
*/
public function __construct(
Core\Lang $lang,
Core\Validator\Rules\Misc $validate,
Core\Validator\Rules\Router\Aliases $aliasesValidator,
Core\Validator\Rules\Date $dateValidator,
Core\Modules $modules
Core\Validator\Validator $validator,
Core\Validator\Rules\Misc $validate
)
{
parent::__construct($lang, $validate);
$this->aliasesValidator = $aliasesValidator;
$this->dateValidator = $dateValidator;
$this->modules = $modules;
$this->validator = $validator;
}
/**
......@@ -53,55 +42,33 @@ class Gallery extends Core\Validator\AbstractValidator
*/
public function validate(array $formData, $uriAlias = '')
{
$this->validateFormKey();
$this->errors = [];
if ($this->dateValidator->date($formData['start'], $formData['end']) === false) {
$this->errors['date'] = $this->lang->t('system', 'select_date');
}
if (strlen($formData['title']) < 3) {
$this->errors['title'] = $this->lang->t('gallery', 'type_in_gallery_title');
}
if (!empty($formData['alias']) && $this->aliasesValidator->uriAliasExists($formData['alias'], $uriAlias) === true) {
$this->errors['alias'] = $this->lang->t('seo', 'alias_unallowed_characters_or_exists');
}
$this->_checkForFailedValidation();
}
/**
* @param array $formData
*
* @throws Core\Exceptions\InvalidFormToken
* @throws Core\Exceptions\ValidationFailed
*/
public function validateSettings(array $formData)
{
$this->validateFormKey();
$this->errors = [];
if (empty($formData['dateformat']) || ($formData['dateformat'] !== 'long' && $formData['dateformat'] !== 'short')) {
$this->errors['dateformat'] = $this->lang->t('system', 'select_date_format');
}
if ($this->validate->isNumber($formData['sidebar']) === false) {
$this->errors['sidebar'] = $this->lang->t('system', 'select_sidebar_entries');
}
if (!isset($formData['overlay']) || $formData['overlay'] != 1 && $formData['overlay'] != 0) {
$this->errors['overlay'] = $this->lang->t('gallery', 'select_use_overlay');
}
if ($this->modules->isActive('comments') === true && (!isset($formData['comments']) || $formData['comments'] != 1 && $formData['comments'] != 0)) {
$this->errors['comments'] = $this->lang->t('gallery', 'select_allow_comments');
}
if ($this->validate->isNumber($formData['thumbwidth']) === false || $this->validate->isNumber($formData['width']) === false || $this->validate->isNumber($formData['maxwidth']) === false) {
$this->errors[] = $this->lang->t('gallery', 'invalid_image_width_entered');
}
if ($this->validate->isNumber($formData['thumbheight']) === false || $this->validate->isNumber($formData['height']) === false || $this->validate->isNumber($formData['maxheight']) === false) {
$this->errors[] = $this->lang->t('gallery', 'invalid_image_height_entered');
}
if ($this->validate->isNumber($formData['filesize']) === false) {
$this->errors['filesize'] = $this->lang->t('gallery', 'invalid_image_filesize_entered');
}
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Core\Validator\ValidationRules\DateValidationRule::NAME,
[
'data' => $formData,
'field' => ['start', 'end'],
'message' => $this->lang->t('system', 'select_date')
])
->addConstraint(
Core\Validator\ValidationRules\NotEmptyValidationRule::NAME,
[
'data' => $formData,
'field' => 'title',
'message' => $this->lang->t('gallery', 'type_in_gallery_title')
])
->addConstraint(
UriAliasValidationRule::NAME,
[
'data' => $formData,
'field' => 'alias',
'message' => $this->lang->t('seo', 'alias_unallowed_characters_or_exists'),
'extra' => [
'path' => $uriAlias
]
]);
$this->_checkForFailedValidation();
$this->validator->validate();
}
}
......@@ -10,66 +10,47 @@ use ACP3\Core;
class Picture extends Core\Validator\AbstractValidator
{
/**
* @var Core\Validator\Rules\Mime
* @var \ACP3\Core\Validator\Validator
*/
protected $mimeValidator;
protected $validator;
/**
* @param Core\Lang $lang
* @param Core\Validator\Rules\Misc $validate
* @param Core\Validator\Rules\Mime $mimeValidator
* @param Core\Lang $lang
* @param \ACP3\Core\Validator\Validator $validator
* @param Core\Validator\Rules\Misc $validate
*/
public function __construct(
Core\Lang $lang,
Core\Validator\Rules\Misc $validate,
Core\Validator\Rules\Mime $mimeValidator
Core\Validator\Validator $validator,
Core\Validator\Rules\Misc $validate
)
{
parent::__construct($lang, $validate);
$this->mimeValidator = $mimeValidator;
$this->validator = $validator;
}
/**
* @param array $file
* @param bool $fileRequired
*
* @throws Core\Exceptions\InvalidFormToken
* @throws Core\Exceptions\ValidationFailed
* @throws \ACP3\Core\Exceptions\ValidationFailed
*/
public function validateCreate(array $file)
public function validate(array $file, $fileRequired = true)
{
$this->validateFormKey();
$this->errors = [];
if (empty($file['tmp_name'])) {
$this->errors['file'] = $this->lang->t('gallery', 'no_picture_selected');
}
if (!empty($file['tmp_name']) &&
($this->mimeValidator->isPicture($file['tmp_name']) === false || $file['error'] !== UPLOAD_ERR_OK)
) {
$this->errors['file'] = $this->lang->t('gallery', 'invalid_image_selected');
}
$this->_checkForFailedValidation();
}
/**
* @param array $file
*
* @throws Core\Exceptions\InvalidFormToken
* @throws Core\Exceptions\ValidationFailed
*/
public function validateEdit(array $file)
{
$this->validateFormKey();
$this->errors = [];
if (!empty($file['tmp_name']) &&
($this->mimeValidator->isPicture($file['tmp_name']) === false || $file['error'] !== UPLOAD_ERR_OK)
) {
$this->errors['file'] = $this->lang->t('gallery', 'invalid_image_selected');
}
$this->_checkForFailedValidation();
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Core\Validator\ValidationRules\PictureValidationRule::NAME,
[
'data' => $file,
'field' => 'file',
'message' => $this->lang->t('gallery', 'invalid_image_selected'),
'extra' => [
'required' => $fileRequired
]
]);
$this->validator->validate();
}
}
\ No newline at end of file
......@@ -13,20 +13,27 @@ class Settings extends Core\Validator\AbstractValidator
* @var \ACP3\Core\Modules
*/
protected $modules;
/**
* @var \ACP3\Core\Validator\Validator
*/
protected $validator;
/**
* @param Core\Lang $lang
* @param Core\Validator\Rules\Misc $validate
* @param Core\Modules $modules
* @param Core\Lang $lang
* @param \ACP3\Core\Validator\Validator $validator
* @param Core\Validator\Rules\Misc $validate
* @param Core\Modules $modules
*/
public function __construct(
Core\Lang $lang,
Core\Validator\Validator $validator,
Core\Validator\Rules\Misc $validate,
Core\Modules $modules
)
{
parent::__construct($lang, $validate);
$this->validator = $validator;
$this->modules = $modules;
}
......@@ -38,28 +45,78 @@ class Settings extends Core\Validator\AbstractValidator
*/
public function validate(array $formData)
{
$this->validateFormKey();
$this->validator
->addConstraint(Core\Validator\ValidationRules\FormTokenValidationRule::NAME)
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'dateformat',
'message' => $this->lang->t('system', 'select_date_format'),
'extra' => [
'haystack' => ['long', 'short']
]
])
->addConstraint(
Core\Validator\ValidationRules\IntegerValidationRule::NAME,
[
'data' => $formData,
'field' => 'sidebar',
'message' => $this->lang->t('system', 'select_sidebar_entries')
])
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'overlay',
'message' => $this->lang->t('gallery', 'select_use_overlay'),
'extra' => [
'haystack' => [0, 1]
]
])
->addConstraint(
Core\Validator\ValidationRules\IntegerValidationRule::NAME,
[
'data' => $formData,
'field' => 'thumbwidth',
'message' => $this->lang->t('gallery', 'invalid_image_width_entered')
])
->addConstraint(
Core\Validator\ValidationRules\IntegerValidationRule::NAME,
[
'data' => $formData,
'field' => 'width',
'message' => $this->lang->t('gallery', 'invalid_image_width_entered')
])
->addConstraint(
Core\Validator\ValidationRules\IntegerValidationRule::NAME,
[
'data' => $formData,
'field' => 'thumbheight',
'message' => $this->lang->t('gallery', 'invalid_image_height_entered')
])
->addConstraint(
Core\Validator\ValidationRules\IntegerValidationRule::NAME,
[
'data' => $formData,
'field' => 'height',
'message' => $this->lang->t('gallery', 'invalid_image_height_entered')
]);
$this->errors = [];
if (empty($formData['dateformat']) || ($formData['dateformat'] !== 'long' && $formData['dateformat'] !== 'short')) {
$this->errors['dateformat'] = $this->lang->t('system', 'select_date_format');
}
if ($this->validate->isNumber($formData['sidebar']) === false) {
$this->errors['sidebar'] = $this->lang->t('system', 'select_sidebar_entries');
}
if (!isset($formData['overlay']) || $formData['overlay'] != 1 && $formData['overlay'] != 0) {
$this->errors['overlay'] = $this->lang->t('gallery', 'select_use_overlay');
}
if ($this->modules->isActive('comments') === true && (!isset($formData['comments']) || $formData['comments'] != 1 && $formData['comments'] != 0)) {
$this->errors['comments'] = $this->lang->t('gallery', 'select_allow_comments');
}
if ($this->validate->isNumber($formData['thumbwidth']) === false || $this->validate->isNumber($formData['width']) === false) {
$this->errors[] = $this->lang->t('gallery', 'invalid_image_width_entered');
}
if ($this->validate->isNumber($formData['thumbheight']) === false || $this->validate->isNumber($formData['height']) === false) {
$this->errors[] = $this->lang->t('gallery', 'invalid_image_height_entered');
if ($this->modules->isActive('comments') === true) {
$this->validator
->addConstraint(
Core\Validator\ValidationRules\InArrayValidationRule::NAME,
[
'data' => $formData,
'field' => 'comments',
'message' => $this->lang->t('gallery', 'select_allow_comments'),
'extra' => [
'haystack' => [0, 1]
]
]);
}
$this->_checkForFailedValidation();
$this->validator->validate();
}
}
\ No newline at end of file
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