Added filter and pagination to Product
This commit is contained in:
@@ -18,7 +18,48 @@ class ProductController extends mfBaseController {
|
||||
if(!$this->me->is(["Admin"])) {
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
$this->layout()->set("products", ProductModel::getAll());
|
||||
|
||||
$filter = [];
|
||||
if(is_array($this->request->filter)) {
|
||||
$filter = $this->request->filter;
|
||||
}
|
||||
|
||||
$this->layout->set("filter", $filter);
|
||||
|
||||
if($filter) {
|
||||
$filter = $this->getPreparedFilter($filter);
|
||||
}
|
||||
|
||||
// pagination defaults
|
||||
$pagination = [];
|
||||
$pagination['start'] = 0;
|
||||
$pagination['count'] = 20;
|
||||
$pagination['maxItems'] = 0;
|
||||
|
||||
if(is_numeric($this->request->s)) {
|
||||
$pagination['start'] = intval($this->request->s);
|
||||
}
|
||||
|
||||
$pagination['maxItems'] = ProductModel::count($filter);
|
||||
$this->layout()->set("pagination", $pagination);
|
||||
|
||||
$products = ProductModel::search($filter, $pagination);
|
||||
$this->layout()->set("products", $products);
|
||||
}
|
||||
|
||||
private function getPreparedFilter($filter) {
|
||||
$new_filter = [];
|
||||
|
||||
if(array_key_exists("name", $filter)) {
|
||||
$new_filter['name%'] = $filter['name'];
|
||||
unset($filter['name']);
|
||||
}
|
||||
|
||||
foreach($filter as $name => $value) {
|
||||
$new_filter[$name] = $value;
|
||||
}
|
||||
|
||||
return $new_filter;
|
||||
}
|
||||
|
||||
protected function addAction() {
|
||||
@@ -276,7 +317,7 @@ class ProductController extends mfBaseController {
|
||||
}
|
||||
|
||||
|
||||
$products = array_merge($products, ProductModel::search(["nameLike" => $search]));
|
||||
$products = array_merge($products, ProductModel::search(["name%" => $search]));
|
||||
|
||||
if(!is_array($products) && !count($products)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user