mirror of
https://github.com/Sarjuuk/aowow.git
synced 2025-11-29 15:58:16 +08:00
Template/Update (Part 9)
* convert filter handler
This commit is contained in:
64
endpoints/filter/filter.php
Normal file
64
endpoints/filter/filter.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Aowow;
|
||||
|
||||
if (!defined('AOWOW_REVISION'))
|
||||
die('illegal access');
|
||||
|
||||
|
||||
class FilterBaseResponse extends TextResponse
|
||||
{
|
||||
private array $catg = [];
|
||||
private string $page = '';
|
||||
private ?Filter $filter = null;
|
||||
|
||||
public function __construct(string $pageParam)
|
||||
{
|
||||
if (!$pageParam)
|
||||
return;
|
||||
|
||||
parent::__construct($pageParam);
|
||||
|
||||
$catg = null;
|
||||
if (strstr($pageParam, '='))
|
||||
[$this->page, $catg] = explode('=', $pageParam);
|
||||
else
|
||||
$this->page = $pageParam;
|
||||
|
||||
if ($catg !== null)
|
||||
$this->catg = explode('.', $catg);
|
||||
|
||||
$opts = ['parentCats' => $this->catg];
|
||||
|
||||
// so usually the page call is just the DBTypes file string with a plural 's' .. but then there are currencies
|
||||
$fileStr = match ($this->page)
|
||||
{
|
||||
'currencies' => 'currency',
|
||||
default => substr($this->page, 0, -1)
|
||||
};
|
||||
|
||||
// yes, the whole _POST! .. should the input fields be exposed and static so they can be evaluated via BaseResponse::initRequestData() ?
|
||||
$this->filter = Type::newFilter($fileStr, $_POST, $opts);
|
||||
}
|
||||
|
||||
protected function generate() : void
|
||||
{
|
||||
$url = '?'.$this->page;
|
||||
|
||||
$this->filter?->mergeCat($this->catg);
|
||||
|
||||
if ($this->catg)
|
||||
$url .= '='.implode('.', $this->catg);
|
||||
|
||||
if ($x = $this->filter?->buildGETParam())
|
||||
$url .= '&filter='.$x;
|
||||
|
||||
if ($this->filter?->error)
|
||||
$_SESSION['error']['fi'] = $this->filter::class;
|
||||
|
||||
// do get request
|
||||
$this->redirectTo = $url;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,71 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Aowow;
|
||||
|
||||
if (!defined('AOWOW_REVISION'))
|
||||
die('illegal access');
|
||||
|
||||
|
||||
class AjaxFilter extends AjaxHandler
|
||||
{
|
||||
public $doRedirect = true;
|
||||
|
||||
private $cat = [];
|
||||
private $page = '';
|
||||
private $filter = null;
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
if (!$params)
|
||||
return;
|
||||
|
||||
parent::__construct($params);
|
||||
|
||||
$p = explode('=', $params[0]);
|
||||
|
||||
$this->page = $p[0];
|
||||
|
||||
if (isset($p[1]))
|
||||
$this->cat[] = $p[1];
|
||||
|
||||
if (count($params) > 1)
|
||||
for ($i = 1; $i < count($params); $i++)
|
||||
$this->cat[] = $params[$i];
|
||||
|
||||
$opts = ['parentCats' => $this->cat];
|
||||
|
||||
// so usually the page call is just the DBTypes file string with a plural 's' .. but then there are currencies
|
||||
$fileStr = match ($this->page)
|
||||
{
|
||||
'currencies' => 'currency',
|
||||
default => substr($this->page, 0, -1)
|
||||
};
|
||||
|
||||
// yes, the whole _POST! .. should the input fields be exposed and static so they can be evaluated via BaseResponse::initRequestData() ?
|
||||
$this->filter = Type::newFilter($fileStr, $_POST, $opts);
|
||||
|
||||
// always this one
|
||||
$this->handler = 'handleFilter';
|
||||
}
|
||||
|
||||
protected function handleFilter() : string
|
||||
{
|
||||
$url = '?'.$this->page;
|
||||
|
||||
$this->filter?->mergeCat($this->cat);
|
||||
|
||||
if ($this->cat)
|
||||
$url .= '='.implode('.', $this->cat);
|
||||
|
||||
if ($x = $this->filter?->buildGETParam())
|
||||
$url .= '&filter='.$x;
|
||||
|
||||
if ($this->filter?->error)
|
||||
$_SESSION['error']['fi'] = get_class($this->filter);
|
||||
|
||||
// do get request
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user