Files
YMS/webgui/maintenance.php
T

378 lines
11 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024-2026 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
require 'lib/gpc.inc';
$pagetitle = _('Maintenance');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_equipment = db_get_opt_equip($user->vid, [0 => _('*** unassigned ***')]);
$opt_state = db_load_enum('maintenance', 'maintstate', true, true);
// ========== 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 'filter':
$flt = array();
$flt['equip'] = gpc_get_int($_POST, 'flt_equip');
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
db_save_filter($flt, 204);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(204);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':vid'] = $user->vid;
$p[':eid'] = gpc_get_int($_POST, 'eid');
$p[':activities'] = gpc_get_string($_POST, 'activities', 80);
$id = db_exec_insert('maintenance', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':maintid'] = $id;
$p[':eid'] = gpc_get_int($_POST, 'eid');
$p[':activities'] = gpc_get_string($_POST, 'activities', 80);
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
$p[':series'] = gpc_get_int($_POST, 'series');
$p[':maintstate'] = gpc_get_string($_POST, 'maintstate');
db_exec_update('maintenance', $p, 'maintid');
$action = ACT_VIEW;
break;
case 'delete':
break;
default:
$g_error->Add(sprintf(_("Unknown function '%s'!"), $submit));
$valid = FALSE;
}
// ========== ACTIONS END =====================================================
require 'header.php';
// ========== PAGE CONTENT ====================================================
if ($action == ACT_DEFAULT):
// ========== VARIANT: default behavior =======================================
page_caption_search($pagetitle);
// load filter from db
$flt = db_get_filter($user->id, 204, ['equip','txt']);
// Filter
$opt_special = array(
-2 => _('&horbar; all &horbar;'),
-1 => _('&horbar; none &horbar;')
);
?>
<form method="post" action="<?=$g_scriptname?>">
<div id="filter" class="card">
<div class="card-header"><i class="bi bi-funnel me-2"></i>Filter</div>
<div class="card-body d-flex flex-wrap gap-3">
<div class="d-flex flex-nowrap gap-2">
<label for="flt_equip"><?=_('Equipment')?></label>
<select class="form-select" id="flt_equip" name="flt_equip">
<?php
foreach ($opt_special + $opt_equipment as $k => $v) {
echo '<option value="', $k,'"';
if (isset($flt->equip) and $k == $flt->equip) {
echo ' selected';
}
echo '>', $v, '</option>';
}
?>
</select>
</div>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt">Text</label>
<input type="text" class="form-control" name="flt_txt" maxlength="30" value="<?=$flt->txt ?? ''?>">
</div>
</div>
<div class="card-footer">
<button name="submit[filter]" class="btn btn-sm btn-primary" title="<?=_('Apply')?>"><?=_('Go')?></button>
<button name="submit[freset]" class="btn btn-sm btn-secondary float-end" title="<?=_('Reset filter to defaults')?>"><?=_('Clear')?></button>
</div>
</div>
</form>
<?php
$sort = array(
1 => 'm.maintid',
2 => 'm.maintid DESC'
);
$w = array('m.vid=:vid');
$p = array(':vid' => $user->vid);
if (isset($flt->equip) and ($flt->equip == -1 or $flt->equip == 0)) {
$w[] = 'm.eid IS NULL';
} elseif ($flt->equip > 0) {
$w[] = 'm.eid=:eid';
$p[':eid'] = $flt->equip;
}
if (strlen($flt->txt) > 1) {
$w[] = '(m.activities LIKE :txt OR m.remarks LIKE :txt)';
$p[':txt'] = '%'.$flt->txt.'%';
}
$where = join(' AND ', $w);
$order = ' ORDER BY m.maintid';
$sql = "SELECT COUNT(*) FROM maintenance AS m WHERE " . $where;
$sth = $pdo->prepare($sql);
try {
$sth->execute($p);
} catch(PDOException $e) {
$g_error->Add($e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($p, true));
$g_error->PrintOut();
}
$numrows = $sth->fetchColumn();
$page = gpc_get_int($_REQUEST, 'p', 1);
$rows_pp = gpc_get_int($_REQUEST, 'n', $user->rows_pp);
$lastpage = ceil($numrows/$rows_pp);
$sql = "SELECT m.maintid, m.activities, m.remarks, e.ename "
. "FROM maintenance AS m LEFT OUTER JOIN equipment AS e USING (eid)";
$sql .= ' WHERE ' . $where;
$sql .= $order;
// if pagination:
$sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp;
$sth = $pdo->prepare($sql);
$sth->execute($p);
$res = $sth->fetchAll();
// Pagination on top
$pagination = get_pagination($g_scriptname, '', $page, $lastpage);
echo $pagination;
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>";
echo '<th>#</th>';
echo "<th>", _('Activities'), "</th>";
echo "<th>", _('Equipment'), "</th>";
echo "<th>", _('Remarks'), "</th>";
echo "</tr>\n";
echo "</thead>\n";
$i = ($page - 1) * $rows_pp;
$i0 = $i + 1;
foreach ($res as $row) {
$i++; // record number
echo "<tr>\n";
echo '<td>', $i;
echo '<a title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['maintid'], '"><i class="bi-eye ms-2"></i></a>', "\n";
echo "</td>\n";
echo "<td>". $row['activities'] ."</td>\n";
echo "<td>". $row['ename'] ."</td>\n";
echo "<td>". $row['remarks'] ."</td>\n";
echo "<td>";
// Edit current record button
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['maintid'], '"><i class="bi-pencil"></i></a>', "\n";
echo "</td>\n";
echo "</tr>\n";
}
// Table summary
table_rec_summary($i0, $i, $numrows, 6);
echo "</table>\n";
echo $pagination;
form_add_button($g_scriptname);
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add Maintenance'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="activities" class="form-label"><?=_('Activities')?></label>
<input type="text" class="form-control" id="activities" name="activities">
</div>
<?php
// TODO
// Add link to equipment, perhaps later with modal dialog?
?>
<div class="mb-3">
<label for="eid" class="form-label"><?=_('Equipment')?></label>
<select name="eid" id="eid" class="form-select">
<?php
foreach ($opt_equipment as $k => $v) {
echo '<option value="', $k, '"';
if ($k == $id) {
echo ' selected';
}
echo '>', $v, "</option>\n";
}
?>
</select>
<div class="container-fluid px-0 my-3">
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
<a href="<?=$g_scriptname?>" class="btn btn-secondary"><?=_('Back')?></a>
</div>
<?php
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
echo '<h2>', _('View Maintenance'), "</h2>\n";
$sql = "SELECT m.activities, m.eid, m.remarks, m.series, m.maintstate, e.ename "
. "FROM maintenance AS m LEFT OUTER JOIN equipment AS e USING (eid) "
. "WHERE m.maintid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$maint = $sth->fetch(PDO::FETCH_OBJ);
if (isset($maint->eid)) {
$sql = "SELECT r.docid, d.filename, d.title, d.hash, d.extension "
. "FROM docref AS r INNER JOIN document AS d USING (docid) "
. "WHERE r.reftype='equipment' AND r.refid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$maint->eid]);
$docs = $sth->fetchAll();
} else {
$maint->ename = _('n/a');
unset($docs);
}
// Bootstrap grid
echo '<div class="container">';
echo '<div class="row">';
echo '<div class="col">';
echo '<table class="table">', "\n";
echo '<tr><th>', _('Activities'),"</th><td>", $maint->activities, "</td></tr>\n";
echo '<tr><th>', _('Equipment'),"</th><td>", $maint->ename;
if (isset($maint->eid) and $maint->eid>0) {
// Link to equipment
echo ' <a href="equipment.php?f=view&id=', $maint->eid, '"><i class="bi-eye"></i></a>';
}
echo "</td></tr>\n";
echo '<tr><th>', _('Remarks'),"</th><td>", $maint->remarks, "</td></tr>\n";
echo '<tr><th>', _('Series'),"</th><td>", $maint->series, "</td></tr>\n";
echo '<tr><th scope="row">', _('State'),"</th><td>", get_enum($maint->maintstate), "</td></tr>\n";
echo "</table>\n";
echo '</div>'; // Column break
echo '<div class="col">';
// TODO Show documents for equipment
echo "<h4>Documents</h4>\n";
if (isset($docs)) {
echo '<ul>', "\n";
foreach ($docs as $d) {
echo '<li>', $d['title'] ?? $d['filename'], ' (', $d['docid'], ")</li>\n";
}
echo "</ul>\n";
} else {
echo '<p>', _('No documents found.'), "</p>\n";
}
echo '</div>'; // col
echo '</div>'; // row
echo '</div>'; // container
form_view_buttons($g_scriptname, $id);
echo '<h3>', _('Annotations'), "</h3>\n";
$sql = "SELECT noteid, annotation "
. "FROM note "
. "WHERE notetype='maint' AND refid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
if ($sth->rowCount() > 0) {
$n = 0;
foreach ($sth->fetchAll() as $row) {
$n += 1;
echo "<p>($n) {$row['annotation']}";
echo ' <a title="', _('Edit'), '" href="note.php?f=edit&id=', $row['noteid'], '"><i class="bi-pencil"></i></a>';
echo "</p>\n";
}
} else {
echo '<p>', _('No annotations for this maintenance'), "</p>\n";
}
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<a href="note.php?f=add&t=maint&id=', $id, '" class="btn btn-primary" role="button">';
echo _('Add note'), "</a>\n";
echo "</div>\n";
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT activities, eid, remarks, series, maintstate "
. "FROM maintenance "
. "WHERE maintid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$maint = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Edit Maintenance'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="activities" class="form-label"><?=_('Activities')?></label>
<input type="text" class="form-control" id="activities" name="activities" value="<?=$maint->activities ?>">
</div>
<?php form_create_select('eid', _('Equipment'), $opt_equipment, $maint->eid); ?>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$maint->remarks ?></textarea>
</div>
<div class="mb-3">
<label for="series" class="form-label"><?=_('Series')?></label>
<input type="text" class="form-control" id="series" name="series" maxlength="4" value="<?=$maint->series ?>">
</div>
<?php
form_create_select('maintstate', _('Status'), $opt_state, $maint->maintstate);
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
echo '<h2>', _('Delete Maintenance'), "</h2>\n";
else:
// ========== ERROR UNKNOWN VARIANT ===========================================
echo '<p>', _('Unknown function call: Please report to system development!'), "</p>\n";
endif; // $action == ...
// ========== END OF VARIANTS =================================================
include 'footer.php';