Added more Pagination
This commit is contained in:
@@ -23,16 +23,27 @@ class OrderController extends mfBaseController {
|
||||
if($this->request->filter) {
|
||||
$filter = $this->getPreparedFilter($this->request->filter);
|
||||
}
|
||||
|
||||
// pagination defaults
|
||||
$pagination = [];
|
||||
$pagination['start'] = 0;
|
||||
$pagination['count'] = 5;
|
||||
$pagination['maxItems'] = 0;
|
||||
|
||||
if(is_numeric($this->request->s)) {
|
||||
$pagination['start'] = intval($this->request->s);
|
||||
}
|
||||
|
||||
$my_networks = [];
|
||||
|
||||
if($this->me->is("Admin")) {
|
||||
$my_networks = NetworkModel::getAll();
|
||||
$this->layout()->set("mynetworks", $my_networks);
|
||||
|
||||
/*
|
||||
if(!count($filter)) {
|
||||
$this->layout()->set("orders", OrderModel::getAll());
|
||||
return true;
|
||||
}
|
||||
}*/
|
||||
|
||||
if($filter['network_id']) {
|
||||
//$this->layout()->set("orders", OrderModel::search(['network_id' => $filter['network_id']]));
|
||||
@@ -63,35 +74,48 @@ class OrderController extends mfBaseController {
|
||||
|
||||
// get Orders in my networks
|
||||
|
||||
$my_network_ids = [];
|
||||
$orders = [];
|
||||
//var_dump($this->me->my_networks);exit;
|
||||
foreach($my_networks as $network) {
|
||||
$order_search = [
|
||||
'network_id' => $network->id
|
||||
];
|
||||
$my_network_ids[] = $network->id;
|
||||
|
||||
if(is_array($filter) && count($filter)) {
|
||||
foreach($filter as $name => $value) {
|
||||
$order_search[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
foreach(OrderModel::search($order_search) as $order) {
|
||||
if(!array_key_exists($order->id, $orders)) {
|
||||
$orders[$order->id] = $order;
|
||||
}
|
||||
}
|
||||
$order_search = [
|
||||
'network_id' => $my_network_ids
|
||||
];
|
||||
|
||||
if(is_array($filter) && count($filter)) {
|
||||
foreach($filter as $name => $value) {
|
||||
$order_search[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$pagination['maxItems'] = OrderModel::count($order_search);
|
||||
foreach(OrderModel::search($order_search, $pagination) as $order) {
|
||||
if(!array_key_exists($order->id, $orders)) {
|
||||
$orders[$order->id] = $order;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$lonelyOrders = [];
|
||||
if(!count($filter)) {
|
||||
foreach(OrderModel::search(['create_by' => $this->me->id]) as $order) {
|
||||
if($this->me->isAdmin()) {
|
||||
$lonelies = OrderModel::search(['product_id' => null]);
|
||||
} else {
|
||||
$lonelies = OrderModel::search(['product_id' => null, 'create_by' => $this->me->id]);
|
||||
}
|
||||
foreach($lonelies as $order) {
|
||||
if(!array_key_exists($order->id, $orders)) {
|
||||
$orders[$order->id] = $order;
|
||||
$lonelyOrders[$order->id] = $order;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->layout()->set("orders", $orders);
|
||||
$this->layout()->set("lonelyOrders", $lonelyOrders);
|
||||
$this->layout()->set("pagination", $pagination);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -131,8 +131,37 @@ class OrderModel {
|
||||
return $orders;
|
||||
}
|
||||
|
||||
public static function count($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
/*$sql = "SELECT COUNT(*) as cnt FROM `Order`
|
||||
LEFT JOIN OrderProduct ON (OrderProduct.order_id = `Order`.id)
|
||||
LEFT JOIN Product ON (Product.id = OrderProduct.product_id)
|
||||
LEFT JOIN Termination ON (Termination.id = OrderProduct.termination_id)
|
||||
LEFT JOIN Building ON (Building.id = Termination.building_id)
|
||||
WHERE $where
|
||||
GROUP BY `Order`.id";*/
|
||||
|
||||
$sql = "SELECT COUNT(*) as cnt FROM
|
||||
(SELECT `Order`.id FROM `Order`
|
||||
LEFT JOIN OrderProduct ON (OrderProduct.order_id = `Order`.id)
|
||||
LEFT JOIN Product ON (Product.id = OrderProduct.product_id)
|
||||
LEFT JOIN Termination ON (Termination.id = OrderProduct.termination_id)
|
||||
LEFT JOIN Building ON (Building.id = Termination.building_id)
|
||||
WHERE $where
|
||||
GROUP BY `Order`.id)
|
||||
as o";
|
||||
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
return $data->cnt;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function search($filter) {
|
||||
public static function search($filter, $limit=false) {
|
||||
$items = [];
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
@@ -149,8 +178,17 @@ class OrderModel {
|
||||
LEFT JOIN Termination ON (Termination.id = OrderProduct.termination_id)
|
||||
LEFT JOIN Building ON (Building.id = Termination.building_id)
|
||||
WHERE $where
|
||||
GROUP BY `Order`.id";
|
||||
|
||||
GROUP BY `Order`.id
|
||||
ORDER BY `Order`.order_date ASC";
|
||||
|
||||
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)) {
|
||||
@@ -187,6 +225,15 @@ class OrderModel {
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("product_id", $filter)) {
|
||||
$product_id = $filter['product_id'];
|
||||
if(is_numeric($product_id)) {
|
||||
$where .= " AND OrderProduct.product_id=$product_id";
|
||||
} elseif($product_id === null) {
|
||||
$where .= " AND OrderProduct.product_id IS NULL";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("productgroup_id", $filter)) {
|
||||
$productgroup_id = $filter['productgroup_id'];
|
||||
if(is_numeric($productgroup_id)) {
|
||||
@@ -205,6 +252,8 @@ class OrderModel {
|
||||
$network_id = $filter['network_id'];
|
||||
if(is_numeric($network_id)) {
|
||||
$where .= " AND Building.network_id=$network_id";
|
||||
} elseif(is_array($network_id)) {
|
||||
$where .= " AND Building.network_id IN (". implode(",", $network_id).")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user