First web gui prototype
This commit is contained in:
@@ -0,0 +1,338 @@
|
||||
<?php
|
||||
/******************************************************************************
|
||||
* YMS - Yacht Management Software
|
||||
* Copyright (C) 2024 Thomas Hooge
|
||||
*
|
||||
* SPDX-License-Identifier: WTFPL
|
||||
******************************************************************************/
|
||||
|
||||
require 'globals.inc';
|
||||
$pagetitle=_('Company');
|
||||
|
||||
$id = gpc_get_int($_REQUEST, 'id', 0);
|
||||
$opt_comptype = db_get_options(2);
|
||||
|
||||
// ========== ACTIONS START ===================================================
|
||||
|
||||
switch ($submit = form_get_action()) {
|
||||
|
||||
case NULL: break;
|
||||
|
||||
case 'add': $action = ACT_ADD; break;
|
||||
case 'view': $action = ACT_VIEW; break;
|
||||
case 'edit': $action = ACT_EDIT; break;
|
||||
case 'del': $action = ACT_DELETE; break;
|
||||
|
||||
case 'insert':
|
||||
$compname = gpc_get_string($_POST, 'compname');
|
||||
$comptype = gpc_get_int($_POST, 'comptype');
|
||||
$sql = "INSERT INTO company "
|
||||
. " (compname, comptype) "
|
||||
. "VALUES "
|
||||
. " (:compname, :comptype)";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->bindValue(':compname', $compname, PDO::PARAM_STR);
|
||||
$sth->bindValue(':comptype', $comptype, PDO::PARAM_INT);
|
||||
$sth->execute();
|
||||
$id = $pdo->lastInsertId();
|
||||
$action = ACT_VIEW;
|
||||
break;
|
||||
|
||||
case 'update':
|
||||
$compname = gpc_get_string($_POST, 'compname');
|
||||
$comptype = gpc_get_int($_POST, 'comptype');
|
||||
$street = gpc_get_string($_POST, 'street', 30);
|
||||
$zip = gpc_get_string($_POST, 'zip', 10);
|
||||
$city = gpc_get_string($_POST, 'city', 30);
|
||||
$country = gpc_get_string($_POST, 'country', 30);
|
||||
$contact = gpc_get_string($_POST, 'contact', 30);
|
||||
$phone = gpc_get_string($_POST, 'phone', 30);
|
||||
$email = gpc_get_string($_POST, 'email', 50);
|
||||
$web = gpc_get_string($_POST, 'web', 40);
|
||||
$customerno = gpc_get_string($_POST, 'customerno', 20);
|
||||
$contractno = gpc_get_string($_POST, 'contractno', 20);
|
||||
$remarks = gpc_get_string($_POST, 'remarks', 150);
|
||||
$sql = "UPDATE company "
|
||||
. "SET compname=:compname,"
|
||||
. " comptype=:comptype,"
|
||||
. " street=:street,"
|
||||
. " zip=:zip,"
|
||||
. " city=:city,"
|
||||
. " country=:country,"
|
||||
. " contact=:contact,"
|
||||
. " phone=:phone,"
|
||||
. " email=:email,"
|
||||
. " web=:web,"
|
||||
. " customerno=:customerno,"
|
||||
. " contractno=:contractno,"
|
||||
. " remarks=:remarks "
|
||||
. "WHERE compid=:compid";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->bindValue(':compid', $id, PDO::PARAM_INT);
|
||||
$sth->bindValue(':compname', $compname, PDO::PARAM_STR);
|
||||
$sth->bindValue(':comptype', $comptype, PDO::PARAM_INT);
|
||||
$sth->bindValue(':street', $street, PDO::PARAM_STR);
|
||||
$sth->bindValue(':zip', $zip, PDO::PARAM_STR);
|
||||
$sth->bindValue(':city', $city, PDO::PARAM_STR);
|
||||
$sth->bindValue(':country', $country, PDO::PARAM_STR);
|
||||
$sth->bindValue(':contact', $contact, PDO::PARAM_STR);
|
||||
$sth->bindValue(':phone', $phone, PDO::PARAM_STR);
|
||||
$sth->bindValue(':email', $email, PDO::PARAM_STR);
|
||||
$sth->bindValue(':web', $web, PDO::PARAM_STR);
|
||||
$sth->bindValue(':customerno', $customerno, PDO::PARAM_STR);
|
||||
$sth->bindValue(':contractno', $contractno, PDO::PARAM_STR);
|
||||
$sth->bindValue(':remarks', $remarks, PDO::PARAM_STR);
|
||||
$sth->execute();
|
||||
$action = ACT_VIEW;
|
||||
break;
|
||||
|
||||
case 'delete':
|
||||
// Security token needed!
|
||||
if (gpc_get_string($_POST, 'token', 16) != $_SESSION['token']) {
|
||||
$g_error->Add(_('Delete prohibited, invalid security token!'));
|
||||
$action = ACT_VIEW;
|
||||
break;
|
||||
}
|
||||
unset($_SESSION['token']);
|
||||
$sth = $pdo->prepare("DELETE FROM company WHERE compid=?");
|
||||
$sth->execute([$id]);
|
||||
$g_message->Add(sprintf(_('Deleted company no. %d'), $id));
|
||||
$action = ACT_DEFAULT;
|
||||
break;
|
||||
|
||||
default:
|
||||
$g_error->Add(sprintf(_('Unknown function!'), $submit));
|
||||
$valid = FALSE;
|
||||
|
||||
}
|
||||
|
||||
// ========== ACTIONS END =====================================================
|
||||
|
||||
require 'header.php';
|
||||
|
||||
// ========== PAGE CONTENT ====================================================
|
||||
|
||||
if ($action == ACT_DEFAULT):
|
||||
// ========== VARIANT: default behavior =======================================
|
||||
|
||||
echo '<h1>', $pagetitle , "</h1>\n";
|
||||
|
||||
$sql = "SELECT compid, compname, comptype, remarks "
|
||||
. "FROM company "
|
||||
. "ORDER BY comptype, compname";
|
||||
$sth = $pdo->query($sql);
|
||||
$res = $sth->fetchAll();
|
||||
echo '<table class="table table-striped">', "\n";
|
||||
echo "<thead>\n";
|
||||
echo "<tr>";
|
||||
echo "<th>" . _('Name') . "</th>";
|
||||
echo "<th>" . _('Type') . "</th>";
|
||||
echo "<th>" . _('Remarks') . "</th>";
|
||||
echo "<th> </th>";
|
||||
echo "</tr>\n";
|
||||
echo "</thead>\n";
|
||||
foreach ($res as $row) {
|
||||
echo "<tr>\n";
|
||||
echo "<td>". $row['compname'] ."</td>\n";
|
||||
echo "<td>". $opt_comptype[$row['comptype']] ."</td>\n";
|
||||
echo "<td>". $row['remarks'] ."</td>\n";
|
||||
// Action buttons
|
||||
echo "<td>";
|
||||
echo '<a title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['compid'], '"><i class="bi-eye"></i></a>', "\n";
|
||||
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['compid'], '"><i class="bi-pencil"></i></a>';
|
||||
echo "</td>\n";
|
||||
echo "</tr>\n";
|
||||
}
|
||||
// Table summary
|
||||
echo "<tfoot>\n";
|
||||
echo '<tr><td colspan="6">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
|
||||
echo "</tfoot>\n";
|
||||
echo "</table>\n";
|
||||
|
||||
form_add_button($g_scriptname);
|
||||
|
||||
|
||||
elseif ($action == ACT_ADD):
|
||||
// ========== VARIANT: add record =============================================
|
||||
|
||||
echo '<h2>', _('Add Company'), "</h2>\n";
|
||||
?>
|
||||
<form method="post">
|
||||
<div class="mb-3">
|
||||
<label for="compname" class="form-label"><?=_('Name')?></label>
|
||||
<input type="text" class="form-control" id="compname" name="compname">
|
||||
</div>
|
||||
<?php form_create_select('comptype', _('Company type'), $opt_comptype); ?>
|
||||
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
|
||||
<a href="<?=$g_scriptname?>" class="btn btn-secondary"><?=_('Back')?></a>
|
||||
</form>
|
||||
<?php
|
||||
|
||||
elseif ($action == ACT_VIEW):
|
||||
// ========== VARIANT: view single record =====================================
|
||||
|
||||
$sql = "SELECT compname, comptype, street, zip, city, country,"
|
||||
. " contact, phone, email, web, customerno, contractno, remarks "
|
||||
. "FROM company "
|
||||
. "WHERE compid=?";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([$id]);
|
||||
$company = $sth->fetch(PDO::FETCH_OBJ);
|
||||
|
||||
echo "<h2>", $company->compname, "</h2>\n";
|
||||
|
||||
echo '<table class="table">', "\n";
|
||||
echo '<tr><th scope="row">', _('Name'),"</th><td>", $company->compname, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Type'),"</th><td>", $opt_comptype[$company->comptype], "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Street'),"</th><td>", $company->street, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Zip, City'),"</th><td>", $company->zip, ' ', $company->city, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Country'),"</th><td>", $company->country, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Contact'),"</th><td>", $company->contact, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Phone'),"</th><td>", $company->phone, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Email'),"</th><td>", $company->email, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Web'),"</th><td>", $company->web, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Customer no.'),"</th><td>", $company->customerno, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Contract no.'),"</th><td>", $company->contractno, "</td></tr>\n";
|
||||
echo '<tr><th scope="row">', _('Remarks'),"</th><td>", $company->remarks, "</td></tr>\n";
|
||||
echo "</table>\n";
|
||||
|
||||
form_view_buttons($g_scriptname, $id);
|
||||
|
||||
// show referenced equipment
|
||||
$sql = "SELECT ename FROM equipment WHERE supplier=:id OR manufacturer=:id";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$sth->execute();
|
||||
if ($sth->rowCount > 0) {
|
||||
echo "<p>Referenced in equipment:</p>\n";
|
||||
echo "<ul>\n";
|
||||
foreach ($sth->fetchAll() as $row) {
|
||||
echo "<li>", $row['ename'], "</li>\n";
|
||||
}
|
||||
echo "</ul>\n";
|
||||
} else {
|
||||
echo '<p>', _('Not referenced in any equipment'), "<p>\n";
|
||||
}
|
||||
|
||||
elseif ($action == ACT_EDIT):
|
||||
// ========== VARIANT: edit single record =====================================
|
||||
|
||||
echo '<h2>', _('Edit Company'), "</h2>\n";
|
||||
|
||||
$sql = "SELECT compname, comptype, street, zip, city, country,"
|
||||
. " contact, phone, email, web, customerno, contractno, remarks "
|
||||
. "FROM company "
|
||||
. "WHERE compid=?";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([$id]);
|
||||
$company = $sth->fetch(PDO::FETCH_OBJ);
|
||||
|
||||
?>
|
||||
<form method="post" action="<?=$g_scriptname?>">
|
||||
<input type="hidden" name="id" value="<?=$id?>">
|
||||
<div class="mb-3">
|
||||
<label for="compname" class="form-label"><?=_('Name')?></label>
|
||||
<input type="text" class="form-control" id="compname" name="compname" value="<?=$company->compname ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="comptype" class="form-label"><?=_('Company type')?></label>
|
||||
<select name="comptype" class="form-control">
|
||||
<?php
|
||||
foreach ($opt_comptype as $k => $v) {
|
||||
echo '<option value="', $k ,'"';
|
||||
if ($company->comptype == $k) {
|
||||
echo ' selected';
|
||||
}
|
||||
echo '>', $v, "</option>\n";
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="street" class="form-label"><?=_('Street')?></label>
|
||||
<input type="text" class="form-control" id="street" name="street" value="<?=$company->street ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="zip" class="form-label"><?=_('Zip')?></label>
|
||||
<input type="text" class="form-control" id="zip" name="zip" value="<?=$company->zip ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="city" class="form-label"><?=_('City')?></label>
|
||||
<input type="text" class="form-control" id="city" name="city" value="<?=$company->city ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="country" class="form-label"><?=_('Country')?></label>
|
||||
<input type="text" class="form-control" id="country" name="country" value="<?=$company->country ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="contact" class="form-label"><?=_('Contact')?></label>
|
||||
<input type="text" class="form-control" id="contact" name="contact" value="<?=$company->contact ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="phone" class="form-label"><?=_('Phone')?></label>
|
||||
<input type="text" class="form-control" id="phone" name="phone" value="<?=$company->phone ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="email" class="form-label"><?=_('Email')?></label>
|
||||
<input type="text" class="form-control" id="email" name="email" value="<?=$company->email ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="web" class="form-label"><?=_('Web')?></label>
|
||||
<input type="text" class="form-control" id="web" name="web" value="<?=$company->web ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="customerno" class="form-label"><?=_('Customer no.')?></label>
|
||||
<input type="text" class="form-control" id="customerno" name="customerno" value="<?=$company->customerno ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="contractno" class="form-label"><?=_('Contract no.')?></label>
|
||||
<input type="text" class="form-control" id="contractno" name="contractno" value="<?=$company->contractno ?>">
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
|
||||
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$company->remarks ?></textarea>
|
||||
</div>
|
||||
<button type="submit" name="submit[update]" class="btn btn-primary"><?=_('Save')?></button>
|
||||
<a href="<?=$g_scriptname?>?f=view&id=<?=$id?>" class="btn btn-secondary"><?=_('Back')?></a>
|
||||
</form>
|
||||
<?php
|
||||
|
||||
elseif ($action == ACT_DELETE):
|
||||
// ========== VARIANT: delete record ==========================================
|
||||
|
||||
$sql = "SELECT compname, remarks FROM company WHERE compid=?";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([$id]);
|
||||
$company = $sth->fetch(PDO::FETCH_OBJ);
|
||||
|
||||
echo '<h2>', _('Delete Company'), "</h2>\n";
|
||||
echo '<p>', sprintf(_('Record no. %d'), $id), "</p>\n";
|
||||
echo '<p>Name: ', $company->compname, "</p>";
|
||||
echo '<p>Remarks: ', $company->remarks, "</p>";
|
||||
|
||||
// Still used as a supplier or manufacturer for equipment?
|
||||
$sql = "SELECT COUNT(*) FROM equipment WHERE supplier=:id OR manufacturer=:id";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->bindValue(':id', $id, PDO::PARAM_INT);
|
||||
$sth->execute();
|
||||
$usecount = $sth->fetchColumn();
|
||||
|
||||
if ($usecount > 0) {
|
||||
echo '<p class="alert alert-warning">', _('Company can not be deleted as there are equipment-references!'), "<p>\n";
|
||||
echo '<a href="', $g_scriptname, '?f=view&id=', $id, '" class="btn btn-secondary">', _('Back'), "</a>\n";
|
||||
|
||||
} else {
|
||||
echo '<p>', _('Deleting a company is final. There is no way back. Only delete if you are absolute sure.'), "</p>\n";
|
||||
$_SESSION['token'] = bin2hex(random_bytes(8));
|
||||
form_delete_buttons($g_scriptname, $id, $_SESSION['token']);
|
||||
}
|
||||
|
||||
else:
|
||||
// ========== ERROR UNKNOWN VARIANT ===========================================
|
||||
|
||||
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
|
||||
|
||||
endif; // $action == ...
|
||||
// ========== END OF VARIANTS =================================================
|
||||
|
||||
include 'footer.php';
|
||||
Reference in New Issue
Block a user