Skip to content

Commit

Permalink
Dev: Load storage usage with Ajax instead of on page load
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Sep 21, 2017
1 parent 9fd0c42 commit bbd3f61
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 59 deletions.
21 changes: 13 additions & 8 deletions application/controllers/admin/globalsettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@ private function _displaySettings()
$data['sideMenuBehaviour'] = getGlobalSetting('sideMenuBehaviour');
$data['aListOfThemeObjects'] = AdminTheme::getAdminThemeList();

$data['storage'] = $this->getStorageData();

$this->_renderWrappedTemplate('', 'globalSettings_view', $data);
}

/**
* Get data to use in storage tab.
* @return array
* Loaded by Ajax when user clicks "Calculate storage".
* @return void
*/
protected function getStorageData()
public function getStorageData()
{
Yii::import('application.helpers.admin.ajax_helper', true);
$data = array();

$uploaddir = Yii::app()->getConfig("uploaddir");
Expand All @@ -127,12 +126,18 @@ protected function getStorageData()
$data['totalStorage'] = humanFilesize(folderSize($uploaddir), $decimals);
$data['templateSize'] = humanFilesize(folderSize($uploaddir . '/templates'), $decimals);
$data['surveySize'] = humanFilesize(folderSize($uploaddir . '/surveys'), $decimals);
$data['labelSize'] = humanFilesize(folderSize($uploaddir . '/surveys'), $decimals);
$data['labelSize'] = humanFilesize(folderSize($uploaddir . '/labels'), $decimals);

$data['surveys'] = $this->getSurveyFolderStorage($uploaddir, $decimals);
$data['surveys'] = $this->getSurveyFolderStorage($uploaddir, $decimals);
$data['templates'] = $this->getTemplateFolderStorage($uploaddir, $decimals);

return $data;
$html = Yii::app()->getController()->renderPartial(
'/admin/global_settings/_storage_ajax',
$data,
true
);

ls\ajax\AjaxHelper::outputHtml($html, 'global-settings-storage');
}

/**
Expand Down
5 changes: 1 addition & 4 deletions application/views/admin/globalSettings_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@

<div id="storage" class="tab-pane col-md-6 col-md-offset-1">
<?php
$this->renderPartial(
"./global_settings/_storage",
$storage
);
$this->renderPartial("./global_settings/_storage");
?>
</div>
</div>
Expand Down
57 changes: 11 additions & 46 deletions application/views/admin/global_settings/_storage.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,20 @@
<?php

/**
* @var string $uploaddir
* @since 2017-09-20
* @author Olle Härstedt
*/



?>

<label><?php eT('Overview'); ?></label>
<table class='table table-striped table-bordered'>
<tr>
<td><?php eT('Total storage:'); ?></td>
<td><?php echo $totalStorage; ?></td>
</tr>
<tr>
<td><?php eT('Survey storage:'); ?></td>
<td><?php echo $surveySize; ?></td>
</tr>
<tr>
<td><?php eT('Template storage:'); ?></td>
<td><?php echo $templateSize; ?></td>
</tr>
<tr>
<td><?php eT('Label set storage:'); ?></td>
<td><?php echo $labelSize; ?></td>
</tr>
</table>

<?php if ($surveys): ?>
<label><?php eT('Survey storage'); ?></label>
<table class='table table-striped table-bordered'>
<?php foreach ($surveys as $survey): ?>
<tr>
<td><?php echo $survey['name']; ?> (<?php echo $survey['sid']; ?>)</td>
<td><?php echo $survey['size']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>

<?php if ($templates): ?>
<label><?php eT('Template storage'); ?></label>
<table class='table table-striped table-bordered'>
<?php foreach ($templates as $templates): ?>
<tr>
<td><?php echo $templates['name']; ?></td>
<td><?php echo $templates['size']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<div id='global-settings-storage'>
<input type='hidden' name='global-settings-storage-url' value='<?php echo Yii::app()->createUrl('admin/globalsettings', array('sa' => 'getStorageData')); ?>' />
<button id='global-settings-calculate-storage' class='btn btn-default '>
<span class='fa fa-cogs'></span>&nbsp;
<?php eT('Calculate storage'); ?>
</button>
<br/>
<span class='text-muted'>
<?php eT('Depending on the number of uploaded files, this might take some time.'); ?>
</span>
</div>
52 changes: 52 additions & 0 deletions application/views/admin/global_settings/_storage_ajax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @since 2017-09-20
* @author Olle Härstedt
*/

?>

<label><?php eT('Overview'); ?></label>
<table class='table table-striped table-bordered'>
<tr>
<td><?php eT('Total storage:'); ?></td>
<td><?php echo $totalStorage; ?></td>
</tr>
<tr>
<td><?php eT('Survey storage:'); ?></td>
<td><?php echo $surveySize; ?></td>
</tr>
<tr>
<td><?php eT('Template storage:'); ?></td>
<td><?php echo $templateSize; ?></td>
</tr>
<tr>
<td><?php eT('Label set storage:'); ?></td>
<td><?php echo $labelSize; ?></td>
</tr>
</table>

<?php if ($surveys): ?>
<label><?php eT('Survey storage'); ?></label>
<table class='table table-striped table-bordered'>
<?php foreach ($surveys as $survey): ?>
<tr>
<td><?php echo $survey['name']; ?> (<?php echo $survey['sid']; ?>)</td>
<td><?php echo $survey['size']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>

<?php if ($templates): ?>
<label><?php eT('Template storage'); ?></label>
<table class='table table-striped table-bordered'>
<?php foreach ($templates as $templates): ?>
<tr>
<td><?php echo $templates['name']; ?></td>
<td><?php echo $templates['size']; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
15 changes: 14 additions & 1 deletion scripts/admin/globalsettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ $(document).ready(function(){
$('#btnAdd').click(addLanguages);
$("#frmglobalsettings").submit(UpdateRestrictedLanguages);

var getStorageUrl = '';
$('#global-settings-calculate-storage').on(
'click',
function(ev) {
ev.preventDefault();
var url = $('input[name="global-settings-storage-url"]').val();
LS.ajax({
url: url,
method: 'GET'
});
return false;
}
);

// Code copied from: https://stackoverflow.com/questions/18999501/bootstrap-3-keep-selected-tab-on-page-refresh
if (location.hash) {
$('a[href=\'' + location.hash + '\']').tab('show');
Expand All @@ -18,7 +32,6 @@ $(document).ready(function(){
if (activeTab) {
$('a[href="' + activeTab + '"]').tab('show');
}

$('body').on('click', 'a[data-toggle=\'tab\']', function (e) {
e.preventDefault();
var tab_name = this.getAttribute('href');
Expand Down

0 comments on commit bbd3f61

Please sign in to comment.