Skip to content

Commit

Permalink
Add a settings page
Browse files Browse the repository at this point in the history
Fix a bunch of paths for docker
  • Loading branch information
austinwbest committed Jan 31, 2024
1 parent 296e668 commit 33edd85
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 28 deletions.
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/branches.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require 'shared.php';

if ($_POST['m'] == 'init') {
?><h3>Branches <?= '<br><code>./' . $repository ?></code></h3><hr><?php
?><h3>Branches <br><code><?= $repository ?></code></h3><hr><?php

$remoteLabel = false;
?>
Expand Down
3 changes: 2 additions & 1 deletion root/app/www/public/ajax/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
require 'shared.php';

if ($_POST['m'] == 'init') {
?><h3>Code <?= '<br><code>./' . $repository ?></code></h3><hr><?php
?><h3>Code <br><code><?= $repository ?></code></h3><hr><?php

$labels = $dataFiles = $dataLines = $colors = '';
$usedColors = [];
foreach ($fileTypes as $fileType => $fileTypeData) {
Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/commits.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require 'shared.php';

if ($_POST['m'] == 'init') {
?><h3>Commits <?= '<br><code>./' . $repository ?></code></h3><hr><?php
?><h3>Commits <br><code><?= $repository ?></code></h3><hr><?php

$regex = '/{(?<hash>(.*))}~{(?<date>(.*))}~{(?<relative>(.*))}~{(?<branch>(.*))}~{(?<note>(.*))}~{(?<authorName>(.*))}~{(?<authorEmail>(.*))}/';

Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

if ($_POST['m'] == 'cloneRepository') {
echo '<center><h4>Clone result</h4></center>';
if (is_dir(RELATIVE_PATH . REPOSITORY_PATH . $_POST['folder'])) {
if (is_dir(REPOSITORY_PATH . $_POST['folder'])) {
$error = 'The path <code>' . REPOSITORY_PATH . $_POST['folder'] . '</code> is already used';
}

Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/contributors.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require 'shared.php';

if ($_POST['m'] == 'init') {
?><h3>Contributors <?= '<br><code>./' . $repository ?></code></h3><hr><?php
?><h3>Contributors <br><code><?= $repository ?></code></h3><hr><?php

?><div class="row"><?php
foreach ($contributors['shell'] as $contributor) {
Expand Down
2 changes: 1 addition & 1 deletion root/app/www/public/ajax/overview.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require 'shared.php';

if ($_POST['m'] == 'init') {
?><h3>Overview <?= '<br><code>./' . $repository ?></code></h3><hr><?php
?><h3>Overview <br><code><?= $repository ?></code></h3><hr><?php

$regex = '/{(?<hash>(.*))}~{(?<date>(.*))}~{(?<relative>(.*))}~{(?<branch>(.*))}~{(?<note>(.*))}~{(?<authorName>(.*))}~{(?<authorEmail>(.*))}/';

Expand Down
58 changes: 58 additions & 0 deletions root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
----------------------------------
------ Created: 013024 ------
------ Austin Best ------
----------------------------------
*/

require 'shared.php';

if ($_POST['m'] == 'init') {
?><h3>Settings</h3><hr><?php

?>
<table class="table table-bordered">
<thead>
<tr><td colspan="3"><h5>Global</h5></td></tr>
<tr>
<th>Setting</th>
<th>Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>Repository location</td>
<td><input type="text" class="form-control setting" id="global-repositoryPath" value="<?= REPOSITORY_PATH ?>"></td>
<td>This is the location of all git repositories to load and use</td>
</tr>
</tbody>
<tfoot>
<tr><td colspan="3" align="center"><button class="btn btn-outline-success" onclick="saveSettings()">Save Settings</button></td></tr>
</tfoot>
</table>
<?php
}

if ($_POST['m'] == 'saveSettings') {
foreach ($_POST as $key => $val) {
if ($key == 'm') {
continue;
}

$field = $key;
if (str_contains($field, '-')) {
list($cat, $field) = explode('-', $key);
}

if ($cat) {
$settings[$cat][$field] = $val;
} else {
$settings[$field] = $val;
}
}

file_put_contents(SETTINGS_FILE, json_encode($settings));
}
13 changes: 11 additions & 2 deletions root/app/www/public/ajax/shared.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
----------------------------------
*/

require '../loader.php';
if (file_exists('loader.php')) {
define('ABSOLUTE_PATH', './');
}
if (file_exists('../loader.php')) {
define('ABSOLUTE_PATH', '../');
}
if (file_exists('../../loader.php')) {
define('ABSOLUTE_PATH', '../../');
}
require ABSOLUTE_PATH . 'loader.php';

$repository = $_POST['repository'];
$git = new Git($repository);
Expand All @@ -31,7 +40,7 @@
sort($fileParts, SORT_NUMERIC);
$linesOfCode += intval($fileParts[1]);

if (str_contains($fileParts[0], '.') && $fileParts[0][0] != '.' && !is_dir(RELATIVE_PATH . $repository . '.' . $fileParts[0][0])) {
if (str_contains($fileParts[0], '.') && $fileParts[0][0] != '.' && !is_dir(ABSOLUTE_PATH . $repository . '.' . $fileParts[0][0])) {
$filePathParts = explode('.', $fileParts[0]);
$extension = trim(end($filePathParts));

Expand Down
6 changes: 3 additions & 3 deletions root/app/www/public/classes/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

//-- BRING IN THE TRAITS
$traits = RELATIVE_PATH . 'classes/traits/Git/';
$traits = ABSOLUTE_PATH . 'classes/traits/Git/';
$traitsDir = opendir($traits);
while ($traitFile = readdir($traitsDir)) {
if (str_contains($traitFile, '.php')) {
Expand All @@ -30,7 +30,7 @@ class Git
public function __construct($repository)
{
$this->repository = $repository;
$this->cd = 'cd ' . RELATIVE_PATH . $this->repository .' && ';
$this->cd = 'cd ' . ABSOLUTE_PATH . $this->repository .' && ';
}

public function __toString()
Expand All @@ -56,7 +56,7 @@ public function pull()

public function clone($repository, $folder)
{
$cmd = 'git clone "' . $repository . '" ' . RELATIVE_PATH . REPOSITORY_PATH . $folder . ' 2>&1';
$cmd = 'git clone "' . $repository . '" ' . REPOSITORY_PATH . $folder . ' 2>&1';
$shell = shell_exec($cmd);

return ['cmd' => $cmd, 'shell' => $shell];
Expand Down
16 changes: 15 additions & 1 deletion root/app/www/public/functions/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
----------------------------------
*/

function startup()
{
if (!file_exists(SETTINGS_FILE)) {
file_put_contents(SETTINGS_FILE, '{}');
}

createDirectoryTree(REPOSITORY_PATH);
}

function createDirectoryTree($tree)
{
system('mkdir -p ' . $tree);
}

function getRepositoryList()
{
$repositories = [];
Expand Down Expand Up @@ -71,4 +85,4 @@ function byteConversion($bytes, $measurement = false, $dec = 2)
case 'TiB':
return round($bytes / pow(1024, 4), $dec);
}
}
}
3 changes: 2 additions & 1 deletion root/app/www/public/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
----------------------------------
*/

define('REPOSITORY_PATH', 'repositories/');
define('APP_DATA_PATH', '/config/');
define('SETTINGS_FILE', APP_DATA_PATH . 'settings.json');
4 changes: 2 additions & 2 deletions root/app/www/public/includes/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<script src="assets/vendors/progressbar.js/progressbar.min.js"></script>
<script src="assets/js/off-canvas.js"></script>
<script src="assets/js/hoverable-collapse.js"></script>
<script src="<?= RELATIVE_PATH ?>js/common.js?t=<?= filemtime(RELATIVE_PATH . 'js/common.js') ?>" type="text/javascript"></script>
<script src="<?= RELATIVE_PATH ?>js/git.js?t=<?= filemtime(RELATIVE_PATH . 'js/git.js') ?>" type="text/javascript"></script>
<script src="<?= ABSOLUTE_PATH ?>js/common.js?t=<?= filemtime(ABSOLUTE_PATH . 'js/common.js') ?>" type="text/javascript"></script>
<script src="<?= ABSOLUTE_PATH ?>js/git.js?t=<?= filemtime(ABSOLUTE_PATH . 'js/git.js') ?>" type="text/javascript"></script>
</body>
</html>
6 changes: 6 additions & 0 deletions root/app/www/public/includes/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
<span class="menu-title">Code</span>
</a>
</li>
<li class="nav-item menu-items">
<a class="nav-link" href="#" onclick="loadPage('settings')">
<span class="menu-icon"><i class="fas fa-cogs"></i></span>
<span class="menu-title">Settings</span>
</a>
</li>
<li class="fixed-bottom ms-2">
<!-- https://themewagon.com/themes/corona-free-responsive-bootstrap-4-admin-dashboard-template/ -->
<span class="text-muted d-block text-center text-sm-right d-sm-inline-block">Theme by <a href="https://www.bootstrapdash.com/bootstrap-admin-template/" target="_blank">bootstrapdash</a></span>
Expand Down
8 changes: 6 additions & 2 deletions root/app/www/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
<?php if (!$repositories) { ?>
<div class="col-xl-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<div class="card-body" id="page-content">
<div class="row">
No repositories found.
</div>
<h6 class="text-muted font-weight-normal">Please clone/add them to <code><?= REPOSITORY_PATH ?></code></h6>
<?php if ($settings['global']['repositoryPath']) { ?>
<h6 class="text-muted font-weight-normal">Please clone/add them to <code><?= REPOSITORY_PATH ?></code></h6>
<?php } else { ?>
<h6 class="text-muted font-weight-normal">Please clone/add them to <code><?= REPOSITORY_PATH ?></code> or open the Settings and specify a path (such as <code>/config/repositories</code> for docker)</h6>
<?php } ?>
</div>
</div>
</div>
Expand Down
34 changes: 33 additions & 1 deletion root/app/www/public/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $(document).ready(function () {
function loadPage(page)
{
const repository = $('#active-repository').val();
if (repository == '0') {
if (repository == '0' && page != 'settings') {
toast('Active Repository', 'Please select a repository before trying to view information', 'error');
return;
}
Expand All @@ -22,6 +22,33 @@ function loadPage(page)
});
}
// ---------------------------------------------------------------------------------------------
function saveSettings()
{
let params = '';
$.each($('.setting'), function () {
const field = $(this).attr('id');

let val = '';
if ($(this).is(':checkbox') || $(this).is(':radio')) {
val = $(this).prop('checked') ? 1 : 0;
} else {
val = $(this).val();
}

params += '&' + field + '=' + val;
});

$.ajax({
type: 'POST',
url: 'ajax/settings.php',
data: '&m=saveSettings' + params,
success: function (resultData) {
toast('Settings', 'Settings have been updated', 'info');
reload();
}
});
}
// ---------------------------------------------------------------------------------------------
function cloneRepository()
{
const url = $('#clone-url').val();
Expand Down Expand Up @@ -248,3 +275,8 @@ function dialogClose(elm)

}
// -------------------------------------------------------------------------------------------
function reload()
{
window.location.href='/';
}
// -------------------------------------------------------------------------------------------
33 changes: 23 additions & 10 deletions root/app/www/public/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,32 @@
// This will NOT report uninitialized variables
error_reporting(E_ERROR | E_PARSE);

//-- DETERMINE LOCATION
if (file_exists('loader.php')) {
define('RELATIVE_PATH', './');
} elseif (file_exists('../loader.php')) {
define('RELATIVE_PATH', '../');
} elseif (file_exists('../../loader.php')) {
define('RELATIVE_PATH', '../../');
if (!defined('ABSOLUTE_PATH')) {
if (file_exists('loader.php')) {
define('ABSOLUTE_PATH', './');
}
if (file_exists('../loader.php')) {
define('ABSOLUTE_PATH', '../');
}
if (file_exists('../../loader.php')) {
define('ABSOLUTE_PATH', '../../');
}
}

//-- INCLUDE DEFINES
require RELATIVE_PATH . 'includes/constants.php';
require ABSOLUTE_PATH . 'includes/constants.php';

//-- GRAB SETTINGS FILE DATA
$settings = json_decode(file_get_contents(SETTINGS_FILE), true);
if ($settings['global']['repositoryPath']) {
if (substr($settings['global']['repositoryPath'], -1) != '/') {
$settings['global']['repositoryPath'] .= '/';
}
}
define('REPOSITORY_PATH', ($settings['global']['repositoryPath'] ? $settings['global']['repositoryPath'] : '/config/repositories/'));

//-- INCLUDE FUNCTIONS
$dir = RELATIVE_PATH . 'functions';
$dir = ABSOLUTE_PATH . 'functions';
$handle = opendir($dir);
while ($file = readdir($handle)) {
if ($file[0] != '.' && !is_dir($dir . '/' . $file)) {
Expand All @@ -33,7 +45,7 @@
closedir($handle);

//-- INCLUDE CLASSES
$dir = RELATIVE_PATH . 'classes';
$dir = ABSOLUTE_PATH . 'classes';
$handle = opendir($dir);
while ($file = readdir($handle)) {
if ($file[0] != '.' && !is_dir($dir . '/' . $file)) {
Expand All @@ -42,4 +54,5 @@
}
closedir($handle);

startup();
$repositories = getRepositoryList();

0 comments on commit 33edd85

Please sign in to comment.