Browse Source

add required fields configuration, draft post request processing #14

main
ghost 1 year ago
parent
commit
d8f6b6d27e
  1. 47
      src/app/controller/submit.php
  2. 256
      src/app/model/validator.php
  3. 5
      src/app/view/theme/default/submit.phtml
  4. 12
      src/config/validator.json

47
src/app/controller/submit.php

@ -64,6 +64,7 @@ class AppControllerSubmit
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
'required' => $this->_validator->getPageTitleRequired(),
'minlength' => $this->_validator->getPageTitleLengthMin(), 'minlength' => $this->_validator->getPageTitleLengthMin(),
'maxlength' => $this->_validator->getPageTitleLengthMax(), 'maxlength' => $this->_validator->getPageTitleLengthMax(),
'placeholder' => sprintf( 'placeholder' => sprintf(
@ -79,6 +80,7 @@ class AppControllerSubmit
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
'required' => $this->_validator->getPageDescriptionRequired(),
'minlength' => $this->_validator->getPageDescriptionLengthMin(), 'minlength' => $this->_validator->getPageDescriptionLengthMin(),
'maxlength' => $this->_validator->getPageDescriptionLengthMax(), 'maxlength' => $this->_validator->getPageDescriptionLengthMax(),
'placeholder' => sprintf( 'placeholder' => sprintf(
@ -94,6 +96,7 @@ class AppControllerSubmit
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
'required' => $this->_validator->getPageKeywordsRequired(),
'minlength' => $this->_validator->getPageKeywordsLengthMin(), 'minlength' => $this->_validator->getPageKeywordsLengthMin(),
'maxlength' => $this->_validator->getPageKeywordsLengthMax(), 'maxlength' => $this->_validator->getPageKeywordsLengthMax(),
'placeholder' => sprintf( 'placeholder' => sprintf(
@ -111,6 +114,7 @@ class AppControllerSubmit
'attribute' => (object) 'attribute' => (object)
[ [
'value' => null, 'value' => null,
'required' => $this->_validator->getPageTorrentRequired(),
'placeholder' => sprintf( 'placeholder' => sprintf(
_('Torrent file (use Ctrl to select multiple files)') _('Torrent file (use Ctrl to select multiple files)')
), ),
@ -123,9 +127,52 @@ class AppControllerSubmit
], ],
]; ];
// Submit request
if (isset($_POST)) if (isset($_POST))
{ {
if (isset($_POST['title']))
{
$error = [];
if ($this->_validator->pageTitle($_POST['title'], $error))
{
$form->title->error = $error;
}
// @TODO check for page duplicates
$form->title->attribute->value = htmlentities($_POST['title']);
}
if (isset($_POST['description']))
{
$error = [];
if ($this->_validator->pageDescription($_POST['description'], $error))
{
$form->description->error = $error;
}
$form->description->attribute->value = htmlentities($_POST['description']);
}
if (isset($_POST['keywords']))
{
$error = [];
if ($this->_validator->pageKeywords($_POST['keywords'], $error))
{
$form->keywords->error = $error;
}
$form->keywords->attribute->value = htmlentities($_POST['keywords']);
}
// Request valid
if (empty($error))
{
// @TODO redirect
}
} }
// Render // Render

256
src/app/model/validator.php

@ -14,6 +14,11 @@ class AppModelValidator
// Page // Page
/// Page title /// Page title
public function getPageTitleRequired() : bool
{
return $this->_config->page->title->required;
}
public function getPageTitleLengthMin() : int public function getPageTitleLengthMin() : int
{ {
return $this->_config->page->title->length->min; return $this->_config->page->title->length->min;
@ -29,7 +34,65 @@ class AppModelValidator
return $this->_config->page->title->regex; return $this->_config->page->title->regex;
} }
public function pageTitle(mixed $value, array &$error = []) : bool
{
if (!is_string($value))
{
array_push(
$error,
_('Invalid page title data type')
);
return false;
}
if (empty($value) && $this->getPageTitleRequired())
{
array_push(
$error,
_('Page title required')
);
return false;
}
if (!preg_match($this->getPageTitleRegex(), $value))
{
array_push(
$error,
sprintf(
_('Page title format does not match condition "%s"'),
$this->getPageTitleRegex()
)
);
return false;
}
if (mb_strlen($value) < $this->getPageTitleLengthMin() ||
mb_strlen($value) > $this->getPageTitleLengthMax())
{
array_push(
$error,
sprintf(
_('Page title out of %s-%s chars range'),
$this->getPageTitleLengthMin(),
$this->getPageTitleLengthMax()
)
);
return false;
}
return true;
}
/// Page description /// Page description
public function getPageDescriptionRequired() : bool
{
return $this->_config->page->description->required;
}
public function getPageDescriptionLengthMin() : int public function getPageDescriptionLengthMin() : int
{ {
return $this->_config->page->description->length->min; return $this->_config->page->description->length->min;
@ -45,7 +108,65 @@ class AppModelValidator
return $this->_config->page->description->regex; return $this->_config->page->description->regex;
} }
public function pageDescription(mixed $value, array &$error = []) : bool
{
if (!is_string($value))
{
array_push(
$error,
_('Invalid page description data type')
);
return false;
}
if (empty($value) && $this->getPageDescriptionRequired())
{
array_push(
$error,
_('Page description required')
);
return false;
}
if (!preg_match($this->getPageDescriptionRegex(), $value))
{
array_push(
$error,
sprintf(
_('Page description format does not match condition "%s"'),
$this->getPageDescriptionRegex()
)
);
return false;
}
if (mb_strlen($value) < $this->getPageDescriptionLengthMin() ||
mb_strlen($value) > $this->getPageDescriptionLengthMax())
{
array_push(
$error,
sprintf(
_('Page description out of %s-%s chars range'),
$this->getPageDescriptionLengthMin(),
$this->getPageDescriptionLengthMax()
)
);
return false;
}
return true;
}
/// Page keywords /// Page keywords
public function getPageKeywordsRequired() : bool
{
return $this->_config->page->keywords->required;
}
public function getPageKeywordsLengthMin() : int public function getPageKeywordsLengthMin() : int
{ {
return $this->_config->page->keywords->length->min; return $this->_config->page->keywords->length->min;
@ -71,6 +192,141 @@ class AppModelValidator
return $this->_config->page->keywords->regex; return $this->_config->page->keywords->regex;
} }
public function pageKeywords(mixed $value, array &$error = []) : bool
{
if (!is_array($value))
{
array_push(
$error,
_('Invalid page keywords data type')
);
return false;
}
if (empty($value) && $this->getPageKeywordsRequired())
{
array_push(
$error,
_('Page keywords required')
);
return false;
}
$total = 0;
foreach ($value as $keywords)
{
if (!is_string($kt))
{
array_push(
$error,
_('Invalid magnet keyword value data type')
);
return false;
}
if (!preg_match(MAGNET_KT_REGEX, $kt))
{
array_push(
$error,
sprintf(
_('Magnet keyword format does not match condition "%s"'),
MAGNET_KT_REGEX
)
);
return false;
}
if (mb_strlen($kt) < MAGNET_KT_MIN_LENGTH ||
mb_strlen($kt) > MAGNET_KT_MAX_LENGTH)
{
array_push(
$error,
sprintf(
_('Magnet keyword out of %s-%s chars range'),
MAGNET_KT_MIN_LENGTH,
MAGNET_KT_MAX_LENGTH
)
);
return false;
}
$total++;
}
if ($total < MAGNET_KT_MIN_QUANTITY ||
$total > MAGNET_KT_MAX_QUANTITY)
{
array_push(
$error,
sprintf(
_('Magnet keywords quantity out of %s-%s range'),
MAGNET_KT_MIN_QUANTITY,
MAGNET_KT_MAX_QUANTITY
)
);
return false;
}
return true;
}
public function pageKeyword(mixed $value, array &$error = []) : bool
{
if (!is_string($value))
{
array_push(
$error,
_('Invalid page keyword data type')
);
return false;
}
if (!preg_match($this->getPageKeywordsRegex(), $value))
{
array_push(
$error,
sprintf(
_('Page keyword "%s" format does not match condition "%s"'),
$value,
$this->getPageKeywordsRegex()
)
);
return false;
}
if (mb_strlen($value) < $this->getPageDescriptionLengthMin() ||
mb_strlen($value) > $this->getPageDescriptionLengthMax())
{
array_push(
$error,
sprintf(
_('Page description out of %s-%s chars range'),
$this->getPageDescriptionLengthMin(),
$this->getPageDescriptionLengthMax()
)
);
return false;
}
return true;
}
/// Page torrent
public function getPageTorrentRequired() : bool
{
return $this->_config->page->torrent->required;
}
// Common // Common
public function host(mixed $value, array &$error = []) : bool public function host(mixed $value, array &$error = []) : bool
{ {

5
src/app/view/theme/default/submit.phtml

@ -32,6 +32,7 @@
type="text" type="text"
name="title" name="title"
id="title" id="title"
<?php echo $form->title->attribute->required ? 'required="required"' : false ?>
value="<?php echo $form->title->attribute->value ?>" value="<?php echo $form->title->attribute->value ?>"
placeholder="<?php echo $form->title->attribute->placeholder ?>" placeholder="<?php echo $form->title->attribute->placeholder ?>"
minlength="<?php echo $form->title->attribute->minlength ?>" minlength="<?php echo $form->title->attribute->minlength ?>"
@ -55,6 +56,7 @@
<textarea class="width-100 margin-t-8 <?php echo ($form->description->error ? 'background-color-red' : false) ?>" <textarea class="width-100 margin-t-8 <?php echo ($form->description->error ? 'background-color-red' : false) ?>"
name="description" name="description"
id="description" id="description"
<?php echo $form->description->attribute->required ? 'required="required"' : false ?>
placeholder="<?php echo $form->description->attribute->placeholder ?>" placeholder="<?php echo $form->description->attribute->placeholder ?>"
minlength="<?php echo $form->description->attribute->minlength ?>" minlength="<?php echo $form->description->attribute->minlength ?>"
maxlength="<?php echo $form->description->attribute->maxlength ?>"><?php echo $form->description->attribute->value ?></textarea> maxlength="<?php echo $form->description->attribute->maxlength ?>"><?php echo $form->description->attribute->value ?></textarea>
@ -76,7 +78,7 @@
<?php } ?> <?php } ?>
<textarea class="width-100 margin-t-8 <?php echo ($form->keywords->error ? 'background-color-red' : false) ?>" <textarea class="width-100 margin-t-8 <?php echo ($form->keywords->error ? 'background-color-red' : false) ?>"
name="keywords" name="keywords"
id="keywords" <?php echo $form->keywords->attribute->required ? 'required="required"' : false ?>
placeholder="<?php echo $form->keywords->attribute->placeholder ?>" placeholder="<?php echo $form->keywords->attribute->placeholder ?>"
minlength="<?php echo $form->keywords->attribute->minlength ?>" minlength="<?php echo $form->keywords->attribute->minlength ?>"
maxlength="<?php echo $form->keywords->attribute->maxlength ?>"><?php echo $form->keywords->attribute->value ?></textarea> maxlength="<?php echo $form->keywords->attribute->maxlength ?>"><?php echo $form->keywords->attribute->value ?></textarea>
@ -101,6 +103,7 @@
multiple="multiple" multiple="multiple"
name="torrent" name="torrent"
id="torrent" id="torrent"
<?php echo $form->torrent->attribute->required ? 'required="required"' : false ?>
value="<?php echo $form->torrent->attribute->value ?>" /> value="<?php echo $form->torrent->attribute->value ?>" />
</div> </div>
<!-- <!--

12
src/config/validator.json

@ -3,6 +3,7 @@
{ {
"title": "title":
{ {
"required": true,
"length": "length":
{ {
"min": 10, "min": 10,
@ -12,6 +13,7 @@
}, },
"description": "description":
{ {
"required": false,
"length": "length":
{ {
"min": 0, "min": 0,
@ -21,6 +23,7 @@
}, },
"keywords": "keywords":
{ {
"required": false,
"quantity": "quantity":
{ {
"min": 0, "min": 0,
@ -32,6 +35,15 @@
"max": 140 "max": 140
}, },
"regex": "/[\\w]+/ui" "regex": "/[\\w]+/ui"
},
"torrent":
{
"required": true,
"quantity":
{
"min": 0,
"max": 20
}
} }
} }
} }

Loading…
Cancel
Save