From 49ea49ec4a0fb35de7e7039f0bae7d31c6e58524 Mon Sep 17 00:00:00 2001 From: Ibnu Maksum Date: Mon, 20 May 2024 09:33:37 +0700 Subject: [PATCH] Export CSV by Filter data --- system/controllers/customers.php | 51 ++++++++++++++++++++++++++++---- system/lan/english.json | 3 +- ui/ui/customers.tpl | 6 +++- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/system/controllers/customers.php b/system/controllers/customers.php index d3f6cd59..082f29d7 100644 --- a/system/controllers/customers.php +++ b/system/controllers/customers.php @@ -186,7 +186,7 @@ switch ($action) { } $usings = explode(',', $config['payment_usings']); $usings = array_filter(array_unique($usings)); - if(count($usings)==0){ + if (count($usings) == 0) { $usings[] = Lang::T('Cash'); } $ui->assign('usings', $usings); @@ -621,18 +621,59 @@ switch ($action) { if ($search != '') { $query = ORM::for_table('tbl_customers') - ->whereRaw("username LIKE '%$search%' OR fullname LIKE '%$search%' OR address LIKE '%$search%' ". - "OR phonenumber LIKE '%$search%' OR email LIKE '%$search%' AND status='$filter'"); + ->whereRaw("username LIKE '%$search%' OR fullname LIKE '%$search%' OR address LIKE '%$search%' " . + "OR phonenumber LIKE '%$search%' OR email LIKE '%$search%' AND status='$filter'"); } else { $query = ORM::for_table('tbl_customers'); $query->where("status", $filter); } - if($orderby=='asc'){ + if ($orderby == 'asc') { $query->order_by_asc($order); - }else{ + } else { $query->order_by_desc($order); } $d = $query->findMany(); + if (_post('export', '') == 'csv') { + $h = false; + set_time_limit(-1); + header('Pragma: public'); + header('Expires: 0'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header("Content-type: text/csv"); + header('Content-Disposition: attachment;filename="phpnuxbill_customers_' . $filter . '_' . date('Y-m-d_H_i') . '.csv"'); + header('Content-Transfer-Encoding: binary'); + + $headers = [ + 'id', + 'username', + 'fullname', + 'address', + 'phonenumber', + 'email', + 'balance', + 'service_type', + ]; + $fp = fopen('php://output', 'wb'); + if (!$h) { + fputcsv($fp, $headers, ";"); + $h = true; + } + foreach ($d as $c) { + $row = [ + $c['id'], + $c['username'], + $c['fullname'], + str_replace("\n", " ", $c['address']), + $c['phonenumber'], + $c['email'], + $c['balance'], + $c['service_type'], + ]; + fputcsv($fp, $row, ";"); + } + fclose($fp); + die(); + } $ui->assign('xheader', ''); $ui->assign('d', $d); $ui->assign('statuses', ORM::for_table('tbl_customers')->getEnum("status")); diff --git a/system/lan/english.json b/system/lan/english.json index fefadfc5..d11024ab 100644 --- a/system/lan/english.json +++ b/system/lan/english.json @@ -591,5 +591,6 @@ "Descending": "Descending", "Created_Date": "Created Date", "Inactive": "Inactive", - "Suspended": "Suspended" + "Suspended": "Suspended", + "Query": "Query" } \ No newline at end of file diff --git a/ui/ui/customers.tpl b/ui/ui/customers.tpl index ac45892a..8a4591ec 100644 --- a/ui/ui/customers.tpl +++ b/ui/ui/customers.tpl @@ -66,7 +66,11 @@
- + +