138 lines
3.8 KiB
PHP
138 lines
3.8 KiB
PHP
<?php
|
|
|
|
class IvtProductModel {
|
|
|
|
public static function create(Array $data) {
|
|
throw new Exception("Please use IvtProductController to create IvtProducts");
|
|
}
|
|
|
|
public static function getAll() {
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
|
|
|
$res = $db->select("products", "*", "1=1 ORDER BY id");
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new IvtProduct($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
public static function getFirst($filter) {
|
|
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
|
|
$sql = "SELECT products.* FROM products
|
|
LEFT JOIN customer_product ON (products.id = customer_product.pid)
|
|
WHERE $where
|
|
GROUP BY products.id
|
|
ORDER by products.name LIMIT 1";
|
|
|
|
|
|
//$res = $db->select("products", "*", "$where ORDER BY id LIMIT 1");
|
|
$res = $db->query($sql);
|
|
if($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new IvtProduct($data);
|
|
if($item->id) {
|
|
return $item;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function count($filter) {
|
|
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
//$sql = "SELECT COUNT(*) cnt FROM products WHERE $where ORDER by id";
|
|
$sql = "SELECT COUNT(*) as cnt FROM (
|
|
SELECT products.* FROM products
|
|
LEFT JOIN customer_product ON (products.id = customer_product.pid)
|
|
WHERE $where
|
|
GROUP BY products.id
|
|
ORDER by products.name
|
|
) as p
|
|
";
|
|
|
|
$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(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
//$sql = "SELECT * FROM products WHERE $where ORDER by id";
|
|
$sql = "SELECT products.* FROM products
|
|
LEFT JOIN customer_product ON (products.id = customer_product.pid)
|
|
WHERE $where
|
|
GROUP BY products.id
|
|
ORDER by products.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);
|
|
if($db->num_rows($res)) {
|
|
while($data = $db->fetch_object($res)) {
|
|
$items[] = new IvtProduct($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter) {
|
|
$where = "1=1 ";
|
|
|
|
if(array_key_exists("customer_product_id", $filter)) {
|
|
$customer_product_id = $filter["customer_product_id"];
|
|
if($customer_product_id === true) {
|
|
$where .= " AND customer_product.id IS NOT NULL";
|
|
}
|
|
if($customer_product_id === false) {
|
|
$where .= " AND customer_product.id IS NULL";
|
|
}
|
|
}
|
|
|
|
/*
|
|
if(array_key_exists("street", $filter)) {
|
|
$street = FronkDB::singleton()->escape($filter["street"]);
|
|
if($street) {
|
|
$where .= " AND street like '%$street%'";
|
|
}
|
|
}
|
|
|
|
|
|
if(array_key_exists("street", $filter)) {
|
|
$street = FronkDB::singleton()->escape($filter["street"]);
|
|
if($street) {
|
|
$where .= " AND street like '%$street%'";
|
|
}
|
|
}
|
|
*/
|
|
|
|
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
}
|