Added filter and pagination to Product

This commit is contained in:
Frank Schubert
2022-11-24 20:36:09 +01:00
parent fc71a80dc4
commit 54ae6f62b0
119 changed files with 377 additions and 52238 deletions
+43 -2
View File
@@ -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;
+74 -7
View File
@@ -92,21 +92,55 @@ class ProductModel {
return null;
}
public static function search($filter) {
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM (
SELECT `Product`.id FROM `Product`
LEFT JOIN ProductAttribute ON (ProductAttribute.product_id = Product.id)
LEFT JOIN Producttech ON (Product.producttech_id = Producttech.id)
LEFT JOIN ProducttechAttribute ON (ProducttechAttribute.producttech_id = Producttech.id)
WHERE $where
GROUP BY Product.id
) as p
";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit=false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT Product.* FROM `Product`
LEFT JOIN Productgroup ON (Productgroup.id = Product.id)
LEFT JOIN ProductAttribute ON (ProductAttribute.product_id = Product.id)
LEFT JOIN Producttech ON (Product.producttech_id = Producttech.id)
LEFT JOIN ProducttechAttribute ON (ProducttechAttribute.producttech_id = Producttech.id)
WHERE $where
GROUP BY Product.id";
GROUP BY Product.id
ORDER BY Productgroup.name,Producttech.name,Product.name
";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($count)) {
$sql .= " LIMIT ".$limit['count'];
}
}
$res = $db->query($sql);
//$res = $db->select("Product", "*", "$where ORDER BY name, producttech_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new Product($data);
@@ -124,7 +158,7 @@ class ProductModel {
if(array_key_exists("id", $filter)) {
$id = $db->escape($filter['id']);
if($id) {
$where .= " AND Product.`id` like '%$id%'";
$where .= " AND Product.`id` = '$id'";
}
}
@@ -132,13 +166,27 @@ class ProductModel {
$productgroup_id = $filter['productgroup_id'];
if(is_numeric($productgroup_id)) {
$where .= " AND productgroup_id=$productgroup_id";
} elseif(is_array($productgroup_id) && count($productgroup_id)) {
$where .= " AND Product.productgroup_id IN (". implode(",", $productgroup_id).")";
}
}
if(array_key_exists("producttech_id", $filter)) {
$producttech_id = $filter['producttech_id'];
if(is_numeric($producttech_id)) {
$where .= " AND producttech_id=$producttech_id";
$where .= " AND Product.producttech_id=$producttech_id";
} elseif(is_array($producttech_id) && count($producttech_id)) {
$where .= " AND Product.producttech_id IN (". implode(",", $producttech_id).")";
}
}
if(array_key_exists("sla_id", $filter)) {
$sla_id = $filter['sla_id'];
if(is_numeric($sla_id)) {
$where .= " AND sla_id=$sla_id";
} elseif(is_array($sla_id) && count($sla_id)) {
$where .= " AND Product.sla_id IN (". implode(",", $sla_id).")";
}
}
@@ -149,13 +197,32 @@ class ProductModel {
}
}
if(array_key_exists("nameLike", $filter)) {
$name = $db->escape($filter['nameLike']);
if(array_key_exists("name%", $filter)) {
$name = $db->escape($filter['name%']);
if($name) {
$where .= " AND Product.`name` like '%$name%'";
}
}
if(array_key_exists("external", $filter)) {
$external = $filter['external'];
if(is_numeric($external)) {
if($external) {
$where .= " AND Product.external=1";
} else {
$where .= " AND Product.external=0";
}
}
}
if(array_key_exists("customer_type", $filter)) {
$customer_type = $db->escape($filter['customer_type']);
if($customer_type) {
$where .= " AND Producttech.`customer_type` = '$customer_type'";
}
}
if(array_key_exists("attributename", $filter)) {
$attributename = $db->escape($filter['attributename']);
if($attributename) {
+60
View File
@@ -1,5 +1,65 @@
<?php
class Productgroup extends mfBaseModel {
private $products;
public function getProperty($name) {
if($this->$name == null) {
if(!$this->id) {
return null;
}
if($name == "products") {
$this->products = mfValuecache::singleton()->get("Productgroup-id-".$this->id."-Products");
if($this->products === null) {
$this->products = ProductModel::search(['productgroup_id' => $this->id]);
if($this->products) {
mfValuecache::singleton()->set("Productgroup-id-".$this->id."-Products", $this->products);
}
}
return $this->products;
}
if($name == "creator") {
$this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($this->creator === null) {
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
}
return $this->creator;
}
if($name == "editor") {
$this->editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
if($this->editor === null) {
$this->editor = new User($this->edit_by);
if($this->editor->id) {
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
}
}
return $this->editor;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
}
@@ -15,7 +15,33 @@ class ProductgroupController extends mfBaseController {
}
protected function indexAction() {
$this->layout()->setTemplate("Productgroup/Index");
$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'] = ProductgroupModel::count($filter);
$this->layout()->set("pagination", $pagination);
$productgroups = ProductgroupModel::search($filter, $pagination);
$this->layout()->set("productgroups", $productgroups);
}
protected function addAction() {
+18 -17
View File
@@ -34,21 +34,6 @@ class ProductgroupModel {
return $model;
}
public static function getOne($id) {
if(!is_numeric($id) || !$id) {
throw new Exception("Invalid number", 400);
}
$item = [];
$db = FronkDB::singleton();
$res = $db->select("Productgroup", "*", "id=$id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Productgroup($data);
}
return $item;
}
public static function getAll() {
$items = [];
@@ -64,7 +49,7 @@ class ProductgroupModel {
}
public static function getFirst() {
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
@@ -81,7 +66,23 @@ class ProductgroupModel {
return null;
}
public static function search($filter) {
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM `Productgroup`
WHERE $where
";
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton();
+1 -1
View File
@@ -65,7 +65,7 @@ class ProducttechModel {
}
public static function getFirst() {
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);