229 lines
7.7 KiB
PHP
229 lines
7.7 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************/
|
|
|
|
require 'globals.inc';
|
|
$pagetitle = _('Note');
|
|
|
|
$id = gpc_get_int($_REQUEST, 'id', 0);
|
|
|
|
$opt_notetype = db_load_enum('note', 'notetype');
|
|
|
|
// ========== 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':
|
|
$p[':notetype'] = gpc_get_enum($_POST, 'notetype', $opt_notetype, 'task');
|
|
$p[':refid'] = gpc_get_int($_POST, 'refid');
|
|
$p[':annotation'] = gpc_get_string($_POST, 'annotation');
|
|
$id = db_exec_insert('note', $p);
|
|
$caller = gpc_get_string($_POST, 'caller');
|
|
$callid = $p[':refid'];
|
|
$action = ACT_VIEW;
|
|
break;
|
|
|
|
case 'update':
|
|
$p[':noteid'] = $id;
|
|
$p[':annotation'] = gpc_get_string($_POST, 'annotation');
|
|
db_exec_update('note', $p, 'noteid');
|
|
$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']);
|
|
// get information for redirect
|
|
$sth = $pdo->prepare("SELECT notetype, refid FROM note WHERE noteid=?");
|
|
$sth->execute([$id]);
|
|
$note = $sth->fetch(PDO::FETCH_OBJ);
|
|
if ($note->notetype == 'task') {
|
|
$backlink = 'task.php';
|
|
} else {
|
|
$backlink = 'maintenance.php';
|
|
}
|
|
$backlink .= '?f=view&id='. $note->refid;
|
|
// now really delete
|
|
$sth = $pdo->prepare("DELETE FROM note WHERE noteid=?");
|
|
$sth->execute([$id]);
|
|
header_location($backlink, ['info' => _('Note deleted.')]);
|
|
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 =======================================
|
|
|
|
echo '<h1>', $pagetitle, "</h1>\n";
|
|
|
|
$sql = "SELECT n.noteid, n.notetype, n.refid,"
|
|
. " IF(CHAR_LENGTH(n.annotation) > 60, CONCAT(LEFT(n.annotation, 60), '…'), n.annotation) AS annotation "
|
|
. "FROM note AS n JOIN task AS t ON (n.notetype='task' AND n.refid=t.taskid AND t.vid=:vid) "
|
|
. "UNION "
|
|
. "SELECT n.noteid, n.notetype, n.refid,"
|
|
. " IF(CHAR_LENGTH(n.annotation) > 60, CONCAT(LEFT(n.annotation, 60), '…'), n.annotation) AS annotation "
|
|
. "FROM note AS n JOIN maintenance AS m ON (n.notetype='maint' AND n.refid=m.maintid AND m.vid=:vid) "
|
|
. "ORDER BY noteid";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([':vid' => $user->vid]);
|
|
$res = $sth->fetchAll();
|
|
|
|
?>
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th><?=_('ID')?></th>
|
|
<th><?=_('Reference')?></th>
|
|
<th><?=_('Type')?></th>
|
|
<th><?=_('Note')?></th>
|
|
</tr>
|
|
</thead>
|
|
<?php
|
|
|
|
foreach ($res as $row) {
|
|
echo "<tr>\n";
|
|
echo "<td>", $row['noteid'], "</td>\n";
|
|
echo "<td>", $row['refid'], "</td>\n";
|
|
echo "<td>", $row['notetype'], "</td>\n";
|
|
echo "<td>", $row['annotation'], "</td>\n";
|
|
form_action_buttons($g_scriptname, $row['noteid']);
|
|
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";
|
|
|
|
elseif ($action == ACT_ADD):
|
|
// ========== VARIANT: add record =============================================
|
|
|
|
echo '<h2>', _('Add Note'), "</h2>\n";
|
|
|
|
$notetype = gpc_get_enum($_REQUEST, 't', $opt_notetype);
|
|
|
|
// get task info for reference
|
|
if ($notetype == 'task') {
|
|
$sql = "SELECT taskname FROM task WHERE taskid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$taskname = $sth->fetchColumn();
|
|
echo '<h3>', sprintf(_('Task: %s'), $taskname), "</h3>\n";
|
|
} elseif ($notetype == 'maint') {
|
|
$sql = "SELECT activities FROM maintenance WHERE maintid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$activities = $sth->fetchColumn();
|
|
echo '<h3>', sprintf(_('Maintenance: %s'), $activities), "</h3>\n";
|
|
}
|
|
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<input type="hidden" name="refid" value="<?=$id?>">
|
|
<input type="hidden" name="notetype" value="<?=$notetype?>">
|
|
<input type="hidden" name="caller" value="<?=$g_referrer?>">
|
|
<div class="mb-3">
|
|
<label for="annotation" class="form-label"><?=_('Annotation')?></label>
|
|
<textarea class="form-control" id="annotation" name="annotation" rows="5" autofocus></textarea>
|
|
</div>
|
|
<div class="container-fluid px-0 my-3">
|
|
<button type="submit" name="submit[insert]" class="btn btn-primary"><?=_('Save')?></button>
|
|
<a href="<?=$g_referrer?>?f=view&id=<?=$id?>" class="btn btn-secondary"><?=_('Back')?></a>
|
|
</div>
|
|
<?php
|
|
|
|
elseif ($action == ACT_VIEW):
|
|
// ========== VARIANT: view single record =====================================
|
|
|
|
echo '<h2>', _('View Note'), "</h2>\n";
|
|
|
|
$sql = "SELECT refid, notetype, annotation FROM note WHERE noteid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$note = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
// TODO
|
|
// view annotation
|
|
// edit button
|
|
// list where annotaion used
|
|
echo '<table class="table">', "\n";
|
|
echo '<tr><th>', _('Referenz'),"</th><td>", $note->refid, "</td></tr>\n";
|
|
echo '<tr><th>', _('Annotation'),"</th><td>", $note->annotation, "</td></tr>\n";
|
|
echo "</table>\n";
|
|
|
|
|
|
$link = $note->notetype == 'task' ? 'task.php' : 'maintenance.php';
|
|
// echo '<p><a href="', $link, '?f=view&id=', $note->refid, '">', _('Parent'), "</a></p>\n";
|
|
form_view_buttons($g_scriptname, $id, $link.'?f=view&id='.$note->refid);
|
|
|
|
elseif ($action == ACT_EDIT):
|
|
// ========== VARIANT: edit single record =====================================
|
|
|
|
$sql = "SELECT refid, notetype, annotation FROM note WHERE noteid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$note = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
echo '<h2>', _('Edit Note'), "</h2>\n";
|
|
?>
|
|
<form method="post" action="<?=$g_scriptname?>">
|
|
<input type="hidden" name="id" value="<?=$id?>">
|
|
<div class="mb-3">
|
|
<label for="annotation" class="form-label"><?=_('Annotation')?></label>
|
|
<textarea class="form-control" id="annotation" name="annotation" rows="5" autofocus><?=$note->annotation;?></textarea>
|
|
</div>
|
|
<?php
|
|
form_edit_buttons($g_scriptname, $id);
|
|
echo "</form>\n";
|
|
|
|
elseif ($action == ACT_DELETE):
|
|
// ========== VARIANT: delete record ==========================================
|
|
|
|
$sql = "SELECT notetype, annotation FROM note WHERE noteid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$id]);
|
|
$note = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
echo '<h2>', _('Delete Note'), "</h2>\n";
|
|
echo '<p>', sprintf(_('Record no. %d'), $id), "</p>\n";
|
|
echo '<p>', $note->annotation, "</p>";
|
|
|
|
echo '<p>', _('Deleting an note item 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';
|