diff --git a/lib/Froxlor/Api/Commands/Customers.php b/lib/Froxlor/Api/Commands/Customers.php index 64ddd491..6352c6a4 100644 --- a/lib/Froxlor/Api/Commands/Customers.php +++ b/lib/Froxlor/Api/Commands/Customers.php @@ -33,7 +33,9 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource * optional specify offset for resultset * @param array $sql_orderby * optional array with index = fieldname and value = ASC|DESC to order the resultset by one or more fields - * + * @param bool $show_usages + * optional, default false + * * @access admin * @throws \Exception * @return string json-encoded array count|list @@ -41,6 +43,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource public function listing() { if ($this->isAdmin()) { + $show_usages = $this->getBoolParam('show_usages', true, false); $this->logger()->logAction(\Froxlor\FroxlorLogger::ADM_ACTION, LOG_NOTICE, "[API] list customers"); $query_fields = array(); $result_stmt = Database::prepare(" @@ -57,7 +60,47 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource $params = array_merge($params, $query_fields); Database::pexecute($result_stmt, $params, true, true); $result = array(); + + $domains_stmt = null; + $usages_stmt = null; + if ($show_usages) { + $domains_stmt = Database::prepare(" + SELECT COUNT(`id`) AS `domains` + FROM `" . TABLE_PANEL_DOMAINS . "` + WHERE `customerid` = :cid + AND `parentdomainid` = '0' + AND `id`<> :stdd + "); + $usages_stmt = Database::prepare(" + SELECT * FROM `" . TABLE_PANEL_DISKSPACE . "` + WHERE `customerid` = :cid + ORDER BY `stamp` DESC LIMIT 1 + "); + } + while ($row = $result_stmt->fetch(\PDO::FETCH_ASSOC)) { + if ($show_usages) { + // get number of domains + Database::pexecute($domains_stmt, array( + 'cid' => $row['customerid'], + 'stdd' => $row['standardsubdomain'] + )); + $domains = $domains_stmt->fetch(\PDO::FETCH_ASSOC); + $row['domains'] = intval($domains['domains']); + // get disk-space usages for web, mysql and mail + $usages = Database::pexecute_first($usages_stmt, array( + 'cid' => $row['customerid'] + )); + if ($usages) { + $row['webspace_used'] = $usages['webspace']; + $row['mailspace_used'] = $usages['mail']; + $row['dbspace_used'] = $usages['mysql']; + } else { + $row['webspace_used'] = 0; + $row['mailspace_used'] = 0; + $row['dbspace_used'] = 0; + } + } $result[] = $row; } return $this->response(200, "successful", array( @@ -103,6 +146,8 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource * optional, the customer-id * @param string $loginname * optional, the loginname + * @param bool $show_usages + * optional, default false * * @access admin, customer * @throws \Exception @@ -113,6 +158,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource $id = $this->getParam('id', true, 0); $ln_optional = ($id <= 0 ? false : true); $loginname = $this->getParam('loginname', $ln_optional, ''); + $show_usages = $this->getBoolParam('show_usages', true, false); if ($this->isAdmin()) { $result_stmt = Database::prepare(" @@ -142,6 +188,40 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource if (! $this->isAdmin() && $result['custom_notes_show'] != 1) { $result['custom_notes'] = ""; } + if ($show_usages) { + // get number of domains + $domains_stmt = Database::prepare(" + SELECT COUNT(`id`) AS `domains` + FROM `" . TABLE_PANEL_DOMAINS . "` + WHERE `customerid` = :cid + AND `parentdomainid` = '0' + AND `id`<> :stdd + "); + Database::pexecute($domains_stmt, array( + 'cid' => $result['customerid'], + 'stdd' => $result['standardsubdomain'] + )); + $domains = $domains_stmt->fetch(\PDO::FETCH_ASSOC); + $result['domains'] = intval($domains['domains']); + // get disk-space usages for web, mysql and mail + $usages_stmt = Database::prepare(" + SELECT * FROM `" . TABLE_PANEL_DISKSPACE . "` + WHERE `customerid` = :cid + ORDER BY `stamp` DESC LIMIT 1 + "); + $usages = Database::pexecute_first($usages_stmt, array( + 'cid' => $result['customerid'] + )); + if ($usages) { + $result['webspace_used'] = $usages['webspace']; + $result['mailspace_used'] = $usages['mail']; + $result['dbspace_used'] = $usages['mysql']; + } else { + $result['webspace_used'] = 0; + $result['mailspace_used'] = 0; + $result['dbspace_used'] = 0; + } + } $this->logger()->logAction($this->isAdmin() ? \Froxlor\FroxlorLogger::ADM_ACTION : \Froxlor\FroxlorLogger::USR_ACTION, LOG_NOTICE, "[API] get customer '" . $result['loginname'] . "'"); return $this->response(200, "successful", $result); } diff --git a/tests/Customers/CustomersTest.php b/tests/Customers/CustomersTest.php index 23ff8459..03076927 100644 --- a/tests/Customers/CustomersTest.php +++ b/tests/Customers/CustomersTest.php @@ -115,6 +115,14 @@ class CustomersTest extends TestCase $json_result = Customers::getLocal($admin_userdata)->listing(); $result = json_decode($json_result, true)['data']; $this->assertEquals(1, $result['count']); + $this->assertFalse(isset($result['list'][0]['webspace_used'])); + + $json_result = Customers::getLocal($admin_userdata, [ + 'show_usages' => true + ])->listing(); + $result = json_decode($json_result, true)['data']; + $this->assertEquals(1, $result['count']); + $this->assertTrue(isset($result['list'][0]['webspace_used'])); $json_result = Customers::getLocal($admin_userdata)->listingCount(); $result = json_decode($json_result, true)['data'];