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 @@ -64,6 +64,7 @@ class AppControllerSubmit
'attribute' => (object)
[
'value' => null,
'required' => $this->_validator->getPageTitleRequired(),
'minlength' => $this->_validator->getPageTitleLengthMin(),
'maxlength' => $this->_validator->getPageTitleLengthMax(),
'placeholder' => sprintf(
@ -79,6 +80,7 @@ class AppControllerSubmit @@ -79,6 +80,7 @@ class AppControllerSubmit
'attribute' => (object)
[
'value' => null,
'required' => $this->_validator->getPageDescriptionRequired(),
'minlength' => $this->_validator->getPageDescriptionLengthMin(),
'maxlength' => $this->_validator->getPageDescriptionLengthMax(),
'placeholder' => sprintf(
@ -94,6 +96,7 @@ class AppControllerSubmit @@ -94,6 +96,7 @@ class AppControllerSubmit
'attribute' => (object)
[
'value' => null,
'required' => $this->_validator->getPageKeywordsRequired(),
'minlength' => $this->_validator->getPageKeywordsLengthMin(),
'maxlength' => $this->_validator->getPageKeywordsLengthMax(),
'placeholder' => sprintf(
@ -111,6 +114,7 @@ class AppControllerSubmit @@ -111,6 +114,7 @@ class AppControllerSubmit
'attribute' => (object)
[
'value' => null,
'required' => $this->_validator->getPageTorrentRequired(),
'placeholder' => sprintf(
_('Torrent file (use Ctrl to select multiple files)')
),
@ -123,9 +127,52 @@ class AppControllerSubmit @@ -123,9 +127,52 @@ class AppControllerSubmit
],
];
// Submit request
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

256
src/app/model/validator.php

@ -14,6 +14,11 @@ class AppModelValidator @@ -14,6 +14,11 @@ class AppModelValidator
// Page
/// Page title
public function getPageTitleRequired() : bool
{
return $this->_config->page->title->required;
}
public function getPageTitleLengthMin() : int
{
return $this->_config->page->title->length->min;
@ -29,7 +34,65 @@ class AppModelValidator @@ -29,7 +34,65 @@ class AppModelValidator
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
public function getPageDescriptionRequired() : bool
{
return $this->_config->page->description->required;
}
public function getPageDescriptionLengthMin() : int
{
return $this->_config->page->description->length->min;
@ -45,7 +108,65 @@ class AppModelValidator @@ -45,7 +108,65 @@ class AppModelValidator
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
public function getPageKeywordsRequired() : bool
{
return $this->_config->page->keywords->required;
}
public function getPageKeywordsLengthMin() : int
{
return $this->_config->page->keywords->length->min;
@ -71,6 +192,141 @@ class AppModelValidator @@ -71,6 +192,141 @@ class AppModelValidator
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
public function host(mixed $value, array &$error = []) : bool
{

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

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

12
src/config/validator.json

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

Loading…
Cancel
Save