Files
YMS/webgui/task.php
T
2024-04-15 07:34:31 +02:00

183 lines
5.4 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle = _('Task');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_projects = db_get_opt_proj($user->vid, array(-1 => _('unassigned')));
// ========== 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[':projid'] = gpc_get_int($_POST, 'projid');
if ($p[':projid'] == -1) {
$p[':projid'] = NULL;
}
$p[':taskname'] = gpc_get_string($_POST, 'taskname');
$id = db_exec_insert('task', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':taskid'] = $id;
$p[':projid'] = gpc_get_int($_POST, 'projid');
if ($p[':projid'] == -1) {
$p[':projid'] = NULL;
}
$p[':taskname'] = gpc_get_string($_POST, 'taskname');
db_exec_update('task', $p, 'taskid');
$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 =======================================
echo "<h1>$pagetitle</h1>\n";
$sql = "SELECT t.taskid, t.projid, t.taskname, p.projname "
. "FROM task AS t LEFT JOIN project AS p USING (projid) "
. "WHERE p.projid IS NULL OR p.vid=? "
. "ORDER BY t.taskid";
$sth = $pdo->prepare($sql);
try {
$sth->execute([$user->vid]);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
}
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>\n";
echo '<th>#</th>';
echo '<th>', _('Task'), "</th>";
echo '<th>', _('Project'), "</th>";
echo "<th></th>";
echo "<th></th>\n";
echo "</tr>\n";
echo "</thead>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>" . $row['taskid'] . "</td>\n";
echo "<td>" . $row['taskname'] . "</td>\n";
echo "<td>" . $row['projname'] . "</td>\n";
// Action buttons
form_action_buttons($g_scriptname, $row['taskid']);
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 Task'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<?php
// preselect project if parameter is set
$projid = gpc_get_int($_REQUEST, 'projid');
form_create_select('projid', _('Project'), $opt_projects, $projid);
?>
<div class="mb-3">
<label for="taskname" class="form-label"><?=_('Task')?></label>
<input type="text" class="form-control" id="taskname" name="taskname" required autofocus>
</div>
<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>
</form>
<?php
elseif ($action == ACT_VIEW):
// ========== VARIANT: view single record =====================================
$sql = "SELECT t.taskname, p.projname "
. "FROM task AS t LEFT JOIN project AS p USING (projid) "
. "WHERE taskid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$task = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('View Task'), "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th>', _('Task'),"</th><td>", $task->taskname, "</td></tr>\n";
echo '<tr><th>', _('Project'),"</th><td>", $task->projname ?? _('unassigned'), "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$sql = "SELECT taskname, projid "
. "FROM task "
. "WHERE taskid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$task = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Edit Task'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="taskname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="taskname" name="taskname" value="<?=$task->taskname ?>">
</div>
<?php
form_create_select('projid', _('Project'), $opt_projects, $task->projid);
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
echo '<h2>', _('Delete Task'), "</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';