Commit 63a0f7db authored by Tino Goratsch's avatar Tino Goratsch

convered the Files module to use the new validation facility

parent 1aac1214
<?php
namespace ACP3\Core\Validator\ValidationRules;
/**
* Class FileUploadValidationRule
* @package ACP3\Core\Validator\ValidationRules
*/
class FileUploadValidationRule extends AbstractValidationRule
{
const NAME = 'file_upload';
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
$required = isset($extra['required']) ? (bool)$extra['required'] : true;
return ($this->isFileUpload($data) || ($required === false && empty($data)));
}
/**
* @param $data
*
* @return bool
*/
protected function isFileUpload($data)
{
return (is_array($data) && !empty($data['tmp_name']) && !empty($data['size']) && $data['error'] === UPLOAD_ERR_OK);
}
}
\ No newline at end of file
......@@ -9,6 +9,21 @@ class PictureValidationRule extends AbstractValidationRule
{
const NAME = 'picture';
/**
* @var \ACP3\Core\Validator\ValidationRules\FileUploadValidationRule
*/
protected $fileUploadValidationRule;
/**
* PictureValidationRule constructor.
*
* @param \ACP3\Core\Validator\ValidationRules\FileUploadValidationRule $fileUploadValidationRule
*/
public function __construct(FileUploadValidationRule $fileUploadValidationRule)
{
$this->fileUploadValidationRule = $fileUploadValidationRule;
}
/**
* @inheritdoc
*/
......@@ -21,7 +36,7 @@ class PictureValidationRule extends AbstractValidationRule
'required' => true
], $extra);
if ($this->isFileUpload($data)) {
if ($this->fileUploadValidationRule->isValid($data)) {
return $this->isPicture(
$data['tmp_name'],
$params['width'],
......@@ -35,16 +50,6 @@ class PictureValidationRule extends AbstractValidationRule
return false;
}
/**
* @param $data
*
* @return bool
*/
protected function isFileUpload($data)
{
return (is_array($data) && !empty($data['tmp_name']) && !empty($data['size']) && $data['error'] === UPLOAD_ERR_OK);
}
/**
* @param string $file
* @param int $width
......
......@@ -12,6 +12,11 @@ services:
tags:
- { name: core.validator.validation_rule }
core.validator.validation_rules.file_upload_validation_rule:
class: ACP3\Core\Validator\ValidationRules\FileUploadValidationRule
tags:
- { name: core.validator.validation_rule }
core.validator.validation_rules.form_token_validation_rule:
class: ACP3\Core\Validator\ValidationRules\FormTokenValidationRule
arguments: [@core.request, @core.session]
......@@ -40,6 +45,7 @@ services:
core.validator.validation_rules.picture_validation_rule:
class: ACP3\Core\Validator\ValidationRules\PictureValidationRule
arguments: [@core.validator.validation_rules.file_upload_validation_rule]
tags:
- { name: core.validator.validation_rule }
......
......@@ -129,12 +129,12 @@ class Index extends Core\Modules\AdminController
if (!empty($item) && $this->categoryRepository->resultExists($item) === true) {
$category = $this->categoryRepository->getCategoryDeleteInfosById($item);
$serviceId = strtolower($category['module'] . '.model');
if ($this->container->has($serviceId)) {
if ($this->get($serviceId)->countAll('', $item) > 0) {
$isInUse = true;
continue;
}
$serviceId = strtolower($category['module'] . '.' . $category['module'] . 'repository');
if ($this->container->has($serviceId) &&
$this->get($serviceId)->countAll('', $item) > 0
) {
$isInUse = true;
continue;
}
// Kategoriebild ebenfalls löschen
......@@ -153,7 +153,7 @@ class Index extends Core\Modules\AdminController
$text = $this->lang->t('system', $bool !== false ? 'delete_success' : 'delete_error');
}
$this->redirectMessages()->setMessage($bool, $text);
return $this->redirectMessages()->setMessage($bool, $text);
}
);
}
......
......@@ -4,6 +4,8 @@ parameters:
categories.helpers.fqdn: 'ACP3\Modules\ACP3\Categories\Helpers'
categories.model.categoryrepository.fqdn: 'ACP3\Modules\ACP3\Categories\Model\CategoryRepository'
categories.validator.fqdn: 'ACP3\Modules\ACP3\Categories\Validator'
categories.validator.validation_rules.category_exists_validation_rule.fqdn: 'ACP3\Modules\ACP3\Categories\Validator\ValidationRules\CategoryExistsValidationRule'
categories.validator.validation_rules.duplicate_category_validation_rule.fqdn: 'ACP3\Modules\ACP3\Categories\Validator\ValidationRules\DuplicateCategoryValidationRule'
categories.installer.schema.fqdn: 'ACP3\Modules\ACP3\Categories\Installer\Schema'
categories.installer.migration.fqdn: 'ACP3\Modules\ACP3\Categories\Installer\Migration'
......@@ -32,6 +34,18 @@ services:
class: %categories.validator.fqdn%
arguments: ['@core.lang', @core.validator, '@core.validator.rules.misc', '@categories.model']
categories.validator.validation_rules.category_exists_validation_rule:
class: %categories.validator.validation_rules.category_exists_validation_rule.fqdn%
arguments: [@categories.helpers]
tags:
- { name: core.validator.validation_rule }
categories.validator.validation_rules.duplicate_category_validation_rule:
class: %categories.validator.validation_rules.duplicate_category_validation_rule.fqdn%
arguments: [@categories.helpers]
tags:
- { name: core.validator.validation_rule }
categories.installer.schema:
class: %categories.installer.schema.fqdn%
......
<?php
namespace ACP3\Modules\ACP3\Categories\Validator\ValidationRules;
use ACP3\Core\Validator\ValidationRules\AbstractValidationRule;
use ACP3\Modules\ACP3\Categories\Helpers;
/**
* Class CategoryExistsValidationRule
* @package ACP3\Modules\ACP3\Categories\Validator\ValidationRules
*/
class CategoryExistsValidationRule extends AbstractValidationRule
{
const NAME = 'categories_category_exists';
/**
* @var \ACP3\Modules\ACP3\Categories\Helpers
*/
protected $categoriesHelper;
/**
* CategoryExistsValidationRule constructor.
*
* @param \ACP3\Modules\ACP3\Categories\Helpers $categoriesHelper
*/
public function __construct(Helpers $categoriesHelper)
{
$this->categoriesHelper = $categoriesHelper;
}
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
if (is_array($data) && is_array($field)) {
$categoryId = reset($field);
$createCategory = next($field);
return !empty($data[$createCategory]) || $this->categoriesHelper->categoryExists($data[$categoryId]);
}
return false;
}
}
\ No newline at end of file
<?php
namespace ACP3\Modules\ACP3\Categories\Validator\ValidationRules;
use ACP3\Core\Validator\ValidationRules\AbstractValidationRule;
use ACP3\Modules\ACP3\Categories\Helpers;
......
......@@ -6,6 +6,7 @@ use ACP3\Core;
use ACP3\Modules\ACP3\Categories;
use ACP3\Modules\ACP3\Comments;
use ACP3\Modules\ACP3\Files;
use ACP3\Modules\ACP3\Files\Helpers;
/**
* Class Index
......@@ -295,7 +296,7 @@ class Index extends Core\Modules\AdminController
$file = $this->request->getFiles()->get('file_internal');
}
$this->filesValidator->validateCreate($formData, $file);
$this->filesValidator->validate($formData, $file);
if (is_array($file) === true) {
$upload = new Core\Helpers\Upload('files');
......@@ -356,7 +357,7 @@ class Index extends Core\Modules\AdminController
$file = $this->request->getFiles()->get('file_internal');
}
$this->filesValidator->validateEdit($formData, $file, $id);
$this->filesValidator->validate($formData, $file, sprintf(Helpers::URL_KEY_PATTERN, $id));
$updateValues = [
'start' => $this->date->toSQL($formData['start']),
......
......@@ -8,6 +8,7 @@ parameters:
files.cache.fqdn: 'ACP3\Modules\ACP3\Files\Cache'
files.model.filesrepository.fqdn: 'ACP3\Modules\ACP3\Files\Model\FilesRepository'
files.validator.fqdn: 'ACP3\Modules\ACP3\Files\Validator'
files.validator.validation_rules.is_external_file_validation_rule.fqdn: 'ACP3\Modules\ACP3\Files\Validator\ValidationRules\IsExternalFileValidationRule'
files.installer.schema.fqdn: 'ACP3\Modules\ACP3\Files\Installer\Schema'
files.installer.migration.fqdn: 'ACP3\Modules\ACP3\Files\Installer\Migration'
......@@ -40,7 +41,12 @@ services:
files.validator:
class: %files.validator.fqdn%
arguments: ['@core.lang', '@core.validator.rules.misc', '@core.validator.rules.router.aliases', '@core.validator.rules.date', '@core.modules', '@categories.helpers']
arguments: ['@core.lang', @core.validator, '@core.validator.rules.misc', '@core.modules']
files.validator.validation_rules.is_external_file_validation_rule:
class: %files.validator.validation_rules.is_external_file_validation_rule.fqdn%
tags:
- { name: core.validator.validation_rule }
files.installer.schema:
class: %files.installer.schema.fqdn%
......
This diff is collapsed.
<?php
namespace ACP3\Modules\ACP3\Files\Validator\ValidationRules;
use ACP3\Core\Validator\ValidationRules\AbstractValidationRule;
/**
* Class IsExternalFileValidationRule
* @package ACP3\Modules\ACP3\Files\Validator\ValidationRules
*/
class IsExternalFileValidationRule extends AbstractValidationRule
{
const NAME = 'files_is_external_file';
/**
* @inheritdoc
*/
public function isValid($data, $field = '', array $extra = [])
{
if (is_array($data) && is_array($field)) {
$external = reset($field);
$filesize = next($field);
$unit = next($field);
$file = isset($extra['file']) ? $extra['file'] : null;
return !(isset($data[$external]) && (empty($file) || empty($data[$filesize]) || empty($data[$unit])));
}
return false;
}
}
\ 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