Added filter and pagination to Product
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user