Skip to content

Commit 8197493

Browse files
author
Till Brehm
committed
Merge branch '5372-refactor-dashlets' into 'develop'
Refactor info tab and add it for reseller_edit Closes #6528 and #5372 See merge request ispconfig/ispconfig3!1778
2 parents 046bd3e + ec8fea5 commit 8197493

File tree

13 files changed

+221
-128
lines changed

13 files changed

+221
-128
lines changed

interface/lib/classes/auth.inc.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function is_superadmin() {
5353
}
5454
}
5555

56+
public function is_reseller() {
57+
if($this->has_clients($_SESSION['s']['user']['userid'])) {
58+
return true;
59+
} else {
60+
return false;
61+
}
62+
}
5663
public function has_clients($userid) {
5764
global $app, $conf;
5865

interface/lib/classes/functions.inc.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,33 @@ public function intval($string, $force_numeric = false) {
287287
* @return string - formated bytes
288288
*/
289289
public function formatBytes($size, $precision = 2) {
290+
// 0 is a special as it would give NAN otehrwise.
291+
if ($size == 0) {
292+
return 0;
293+
}
294+
290295
$base=log($size)/log(1024);
291296
$suffixes=array('', ' kB', ' MB', ' GB', ' TB');
292297
return round(pow(1024, $base-floor($base)), $precision).$suffixes[floor($base)];
293298
}
294299

300+
/**
301+
* Function to change bytes to kB, MB, GB or TB or the translated string 'Unlimited' for -1
302+
* @param int $size - size in bytes
303+
* @param int precicion - after-comma-numbers (default: 2)
304+
* @return string - formated bytes
305+
*/
306+
public function formatBytesOrUnlimited($size, $precision = 2) {
307+
global $app;
308+
309+
if ($size == -1) {
310+
return $app->lng('unlimited_txt');
311+
}
312+
else {
313+
return $this->formatBytes($size, $precision);
314+
}
315+
316+
}
295317

296318
/**
297319
* Normalize a path and strip duplicate slashes from it
@@ -652,6 +674,22 @@ public function func_client_cancel($client_id,$cancel) {
652674
return $result;
653675
}
654676

677+
/**
678+
* Lookup a client's group + all groups he is reselling.
679+
*
680+
* @return string Comma separated list of groupid's
681+
*/
682+
function clientid_to_groups_list($client_id) {
683+
global $app;
684+
685+
if ($client_id != null) {
686+
// Get the clients groupid, and incase it's a reseller the groupid's of it's clients.
687+
$group = $app->db->queryOneRecord("SELECT GROUP_CONCAT(groupid) AS groups FROM `sys_group` WHERE client_id IN (SELECT client_id FROM `client` WHERE client_id=? OR parent_client_id=?)", $client_id, $client_id);
688+
return $group['groups'];
689+
}
690+
return null;
691+
}
692+
655693
}
656694

657695
?>

interface/lib/classes/quota_lib.inc.php

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public function get_quota_data($clientid = null, $readable = true) {
1414
//print_r($monitor_data);
1515

1616
// select all websites or websites belonging to client
17-
$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND type = 'vhost'".(($clientid != null)?" AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)":'') . " ORDER BY domain", $clientid);
17+
$q = "SELECT * FROM web_domain WHERE type = 'vhost' AND ";
18+
$q .= $app->tform->getAuthSQL('r', '', '', $app->functions->clientid_to_groups_list($clientid));
19+
$q .= " ORDER BY domain";
20+
$sites = $app->db->queryAllRecords($q, $clientid);
1821

1922
//print_r($sites);
2023
if(is_array($sites) && !empty($sites)){
@@ -36,9 +39,10 @@ public function get_quota_data($clientid = null, $readable = true) {
3639
if (!is_numeric($sites[$i]['hard'])) $sites[$i]['hard']=$sites[$i]['hard'][1];
3740
if (!is_numeric($sites[$i]['files'])) $sites[$i]['files']=$sites[$i]['files'][1];
3841

39-
$sites[$i]['used_raw'] = $sites[$i]['used'];
40-
$sites[$i]['soft_raw'] = $sites[$i]['soft'];
41-
$sites[$i]['hard_raw'] = $sites[$i]['hard'];
42+
// Convert from kb to bytes, and use -1 for instead of 0 for Unlimited.
43+
$sites[$i]['used_raw'] = $sites[$i]['used'] * 1024;
44+
$sites[$i]['soft_raw'] = ($sites[$i]['soft'] > 0) ? $sites[$i]['soft'] * 1024 : -1;
45+
$sites[$i]['hard_raw'] = ($sites[$i]['hard'] > 0) ? $sites[$i]['hard'] * 1024 : -1;
4246
$sites[$i]['files_raw'] = $sites[$i]['files'];
4347
$sites[$i]['used_percentage'] = ($sites[$i]['soft'] > 0 && $sites[$i]['used'] > 0 ? round($sites[$i]['used'] * 100 / $sites[$i]['soft']) : 0);
4448

@@ -53,43 +57,14 @@ public function get_quota_data($clientid = null, $readable = true) {
5357
if($used_ratio >= 0.8) $sites[$i]['display_colour'] = '#fd934f';
5458
if($used_ratio >= 1) $sites[$i]['display_colour'] = '#cc0000';
5559

56-
if($sites[$i]['used'] > 1024) {
57-
$sites[$i]['used'] = round($sites[$i]['used'] / 1024, 1).' MB';
58-
} else {
59-
if ($sites[$i]['used'] != '') $sites[$i]['used'] .= ' KB';
60-
}
61-
62-
if($sites[$i]['soft'] > 1024) {
63-
$sites[$i]['soft'] = round($sites[$i]['soft'] / 1024, 1).' MB';
64-
} else {
65-
$sites[$i]['soft'] .= ' KB';
66-
}
67-
68-
if($sites[$i]['hard'] > 1024) {
69-
$sites[$i]['hard'] = round($sites[$i]['hard'] / 1024, 1).' MB';
70-
} else {
71-
$sites[$i]['hard'] .= ' KB';
72-
}
73-
74-
if($sites[$i]['soft'] == " KB") $sites[$i]['soft'] = $app->lng('unlimited_txt');
75-
if($sites[$i]['hard'] == " KB") $sites[$i]['hard'] = $app->lng('unlimited_txt');
76-
77-
if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = $app->lng('unlimited_txt');
78-
if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = $app->lng('unlimited_txt');
7960

8061
/*
8162
if(!strstr($sites[$i]['used'],'M') && !strstr($sites[$i]['used'],'K')) $sites[$i]['used'].= ' B';
8263
if(!strstr($sites[$i]['soft'],'M') && !strstr($sites[$i]['soft'],'K')) $sites[$i]['soft'].= ' B';
8364
if(!strstr($sites[$i]['hard'],'M') && !strstr($sites[$i]['hard'],'K')) $sites[$i]['hard'].= ' B';
8465
*/
8566
}
86-
else {
87-
if (empty($sites[$i]['soft'])) $sites[$i]['soft'] = -1;
88-
if (empty($sites[$i]['hard'])) $sites[$i]['hard'] = -1;
8967

90-
if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = -1;
91-
if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = -1;
92-
}
9368
}
9469
}
9570

@@ -245,7 +220,10 @@ public function get_mailquota_data($clientid = null, $readable = true, $email =
245220

246221
}
247222
// select all email accounts or email accounts belonging to client
248-
$emails = $app->db->queryAllRecords("SELECT * FROM mail_user".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : '') . " ORDER BY email", $clientid);
223+
$q = "SELECT * FROM mail_user WHERE";
224+
$q .= $app->tform->getAuthSQL('r', '', '', $app->functions->clientid_to_groups_list($clientid));
225+
$q .= " ORDER BY email";
226+
$emails = $app->db->queryAllRecords($q, $clientid);
249227

250228
//print_r($emails);
251229
if(is_array($emails) && !empty($emails)) {
@@ -280,17 +258,8 @@ public function get_mailquota_data($clientid = null, $readable = true, $email =
280258
if($used_ratio >= 0.8) $emails[$i]['display_colour'] = '#fd934f';
281259
if($used_ratio >= 1) $emails[$i]['display_colour'] = '#cc0000';
282260

283-
if($emails[$i]['quota'] == 0){
284-
$emails[$i]['quota'] = $app->lng('unlimited_txt');
285-
} else {
286-
$emails[$i]['quota'] = round($emails[$i]['quota'] / 1048576, 1).' MB';
287-
}
288-
289-
290-
if($emails[$i]['used'] < 1544000) {
291-
$emails[$i]['used'] = round($emails[$i]['used'] / 1024, 1).' KB';
292-
} else {
293-
$emails[$i]['used'] = round($emails[$i]['used'] / 1048576, 1).' MB';
261+
if($emails[$i]['quota'] == 0) {
262+
$emails[$i]['quota'] = -1;
294263
}
295264
}
296265
}
@@ -317,18 +286,21 @@ public function get_databasequota_data($clientid = null, $readable = true) {
317286
//print_r($monitor_data);
318287

319288
// select all databases belonging to client
320-
$databases = $app->db->queryAllRecords("SELECT * FROM web_database".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : '') . " ORDER BY database_name", $clientid);
289+
$q = "SELECT * FROM web_database WHERE";
290+
$q .= $app->tform->getAuthSQL('r', '', '', $app->functions->clientid_to_groups_list($clientid));
291+
$q .= " ORDER BY database_name";
292+
$databases = $app->db->queryAllRecords($q);
321293

322294
//print_r($databases);
323295
if(is_array($databases) && !empty($databases)){
324296
for($i=0;$i<sizeof($databases);$i++){
325297
$databasename = $databases[$i]['database_name'];
326298

327-
$databases[$i]['used'] = isset($monitor_data[$databasename]['size']) ? $monitor_data[$databasename]['size'] : 0;
299+
$size = isset($monitor_data[$databasename]['size']) ? $monitor_data[$databasename]['size'] : 0;
328300

329-
$databases[$i]['quota_raw'] = $databases[$i]['database_quota'];
330-
$databases[$i]['used_raw'] = $databases[$i]['used'] / 1024 / 1024; //* quota is stored as MB - calculated bytes
331-
$databases[$i]['used_percentage'] = (($databases[$i]['database_quota'] > 0) && ($databases[$i]['used'] > 0)) ? round($databases[$i]['used_raw'] * 100 / $databases[$i]['database_quota']) : 0;
301+
$databases[$i]['database_quota_raw'] = ($databases[$i]['database_quota'] == -1) ? -1 : $databases[$i]['database_quota'] * 1000 * 1000;
302+
$databases[$i]['used_raw'] = $size; // / 1024 / 1024; //* quota is stored as MB - calculated bytes
303+
$databases[$i]['used_percentage'] = (($databases[$i]['database_quota'] > 0) && ($size > 0)) ? round($databases[$i]['used_raw'] * 100 / $databases[$i]['database_quota_raw']) : 0;
332304

333305
if ($readable) {
334306
// colours
@@ -341,18 +313,8 @@ public function get_databasequota_data($clientid = null, $readable = true) {
341313
if($used_ratio >= 0.8) $databases[$i]['display_colour'] = '#fd934f';
342314
if($used_ratio >= 1) $databases[$i]['display_colour'] = '#cc0000';
343315

344-
if($databases[$i]['database_quota'] == -1) {
345-
$databases[$i]['database_quota'] = $app->lng('unlimited_txt');
346-
} else {
347-
$databases[$i]['database_quota'] = $databases[$i]['database_quota'] . ' MB';
348-
}
349316

350317

351-
if($databases[$i]['used'] < 1544000) {
352-
$databases[$i]['used'] = round($databases[$i]['used'] / 1024, 1).' KB';
353-
} else {
354-
$databases[$i]['used'] = round($databases[$i]['used'] / 1048576, 1).' MB';
355-
}
356318
}
357319
}
358320
}

interface/web/client/client_edit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function onShowEdit() {
8282
chdir('../dashboard');
8383

8484
$dashlet_list = array();
85-
$dashlets = array('databasequota.php', 'limits.php', 'mailquota.php', 'quota.php');
85+
$dashlets = array('quota.php', 'databasequota.php', 'mailquota.php', 'limits.php');
8686
$current_client_id = $this->id;
8787

8888
foreach ($dashlets as $file) {

interface/web/client/form/reseller.tform.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
$form["db_table"] = "client";
4747
$form["db_table_idx"] = "client_id";
4848
$form["db_history"] = "yes";
49-
$form["tab_default"] = "address";
49+
$form["tab_default"] = "info";
5050
$form["list_default"] = "reseller_list.php";
5151
$form["auth"] = 'yes';
5252

@@ -81,6 +81,12 @@
8181
}
8282
}
8383

84+
$form["tabs"]['info'] = array(
85+
'title' => "Info",
86+
'width' => 100,
87+
'template' => "templates/reseller_edit_info.htm",
88+
'fields' => array()
89+
);
8490
$form["tabs"]['address'] = array (
8591
'title' => "Address",
8692
'width' => 100,

interface/web/client/reseller_edit.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,45 @@ function onShowNew() {
7272
}
7373
}
7474

75+
// Hide the info tab when creating a new client.
76+
unset($app->tform->formDef["tabs"]['info']);
77+
$app->tform->formDef["tab_default"] = "address";
78+
7579
parent::onShowNew();
7680
}
7781

7882

83+
function onShowEdit() {
84+
global $app, $conf;
85+
chdir('../dashboard');
86+
87+
$dashlet_list = array();
88+
$dashlets = array('quota.php', 'databasequota.php', 'mailquota.php', 'limits.php');
89+
$current_client_id = $this->id;
90+
91+
foreach ($dashlets as $file) {
92+
if ($file != '.' && $file != '..' && !is_dir(ISPC_WEB_PATH.'/dashboard/dashlets/'.$file)) {
93+
$dashlet_name = substr($file, 0, -4);
94+
$dashlet_class = 'dashlet_'.$dashlet_name;
95+
include_once ISPC_WEB_PATH.'/dashboard/dashlets/'.$file;
96+
$dashlet_list[$dashlet_name] = new $dashlet_class;
97+
$dashlets_html .= $dashlet_list[$dashlet_name]->show($current_client_id);
98+
}
99+
}
100+
$app->tpl->setVar('dashlets', $dashlets_html);
101+
102+
chdir('../client');
103+
104+
$tmp = $app->db->queryOneRecord("SELECT company_name, contact_firstname, contact_name, email FROM client WHERE client_id = ?", $current_client_id);
105+
106+
$app->tpl->setVar('company_name', $tmp['company_name']);
107+
$app->tpl->setVar('contact_name', $tmp['contact_name']);
108+
$app->tpl->setVar('email', $tmp['email']);
109+
110+
parent::onShowEdit();
111+
}
112+
113+
79114
function onSubmit() {
80115
global $app, $conf;
81116

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<input type="hidden" name="id" value="{tmpl_var name='id'}">
2+
<div class='page-header'>
3+
<legend>Reseller info</legend>
4+
<div>
5+
<ul>
6+
<li>Company Name: {tmpl_var name='company_name'}</li>
7+
<li>Contact Name: {tmpl_var name='contact_name'}</li>
8+
<li>E-mail: <a href="mailto:{tmpl_var name='email'}">{tmpl_var name='email'}</a></li>
9+
<li>Login as:
10+
<tmpl_if name="is_admin">
11+
<a class="btn btn-default formbutton-success formbutton-narrow" data-load-content="login/login_as.php?cid={tmpl_var name='id'}"><span class="icon icon-loginas"></span></a>
12+
<tmpl_elseif name="is_reseller">
13+
<a class="btn btn-default formbutton-success formbutton-narrow" data-load-content="login/login_as.php?cid={tmpl_var name='id'}"><span class="icon icon-loginas"></span></a>
14+
</tmpl_if>
15+
</li>
16+
</ul>
17+
</div>
18+
19+
20+
<tmpl_var name="dashlets">
21+
22+
</div>
23+
24+

interface/web/dashboard/dashboard.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,18 @@
214214
}
215215
}
216216

217+
if ($app->auth->is_admin() || $app->auth->is_reseller()) {
218+
$limit_to_client_id = null;
219+
}
220+
else {
221+
$limit_to_client_id = $_SESSION['s']['user']['client_id'];
222+
}
217223

218224
/* Fill the left column */
219225
$leftcol = array();
220226
foreach($leftcol_dashlets as $name) {
221227
if(isset($dashlet_list[$name])) {
222-
$leftcol[]['content'] = $dashlet_list[$name]->show();
228+
$leftcol[]['content'] = $dashlet_list[$name]->show($limit_to_client_id);
223229
}
224230
}
225231
$app->tpl->setloop('leftcol', $leftcol);
@@ -228,7 +234,7 @@
228234
$rightcol = array();
229235
foreach($rightcol_dashlets as $name) {
230236
if(isset($dashlet_list[$name])) {
231-
$rightcol[]['content'] = $dashlet_list[$name]->show();
237+
$rightcol[]['content'] = $dashlet_list[$name]->show($limit_to_client_id);
232238
}
233239
}
234240
$app->tpl->setloop('rightcol', $rightcol);

interface/web/dashboard/dashlets/databasequota.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class dashlet_databasequota {
44

5-
function show() {
5+
function show($limit_to_client_id = null) {
66
global $app;
77

88
//* Loading Template
@@ -24,27 +24,22 @@ function show() {
2424
if(is_file($lng_file)) include $lng_file;
2525
$tpl->setVar($wb);
2626

27-
$databases = $app->quota_lib->get_databasequota_data( ($_SESSION["s"]["user"]["typ"] != 'admin') ? $_SESSION['s']['user']['client_id'] : null);
27+
$databases = $app->quota_lib->get_databasequota_data($limit_to_client_id);
2828
//print_r($databases);
2929

30-
$has_databasequota = false;
30+
$total_used = 0;
3131
if(is_array($databases) && !empty($databases)){
32+
foreach ($databases as &$db) {
33+
$db['used'] = $app->functions->formatBytes($db['used_raw'], 0);
34+
$db['database_quota'] = $app->functions->formatBytesOrUnlimited($db['database_quota_raw'], 0);
35+
36+
$total_used += $db['used_raw'];
37+
}
3238
$databases = $app->functions->htmlentities($databases);
3339
$tpl->setloop('databasequota', $databases);
34-
$has_databasequota = isset($databases[0]['used']);
40+
$tpl->setVar('total_used', $app->functions->formatBytes($total_used, 0));
41+
42+
return $tpl->grab();
3543
}
36-
$tpl->setVar('has_databasequota', $has_databasequota);
37-
38-
return $tpl->grab();
3944
}
40-
4145
}
42-
43-
44-
45-
46-
47-
48-
49-
50-
?>

0 commit comments

Comments
 (0)