Files
YMS/webgui/task.php
T

421 lines
14 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 = _('Task');
$id = gpc_get_int($_REQUEST, 'id', 0);
$opt_projects = db_get_opt_proj($user->vid, array(-1 => _('unassigned')));
$opt_priority = db_load_enum('task', 'priority', true, true);
$opt_state = db_load_enum('task', 'taskstate', 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['prio'] = gpc_get_enum($_POST, 'flt_prio', array_merge(array_keys($opt_priority), ['-2', '-1']));
$flt['txt'] = gpc_get_string($_POST, 'flt_txt');
$allowed = db_load_enum('task', 'taskstate');
$flt['stat'] = gpc_get_set($_POST, 'flt_stat', $allowed);
db_save_filter($flt, 206);
$action = ACT_DEFAULT;
break;
case 'freset':
db_clear_filter(206);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':vid'] = $user->vid;
$p[':projid'] = gpc_get_int($_POST, 'projid');
if ($p[':projid'] == -1) {
$p[':projid'] = NULL;
}
$p[':taskname'] = gpc_get_string($_POST, 'taskname');
$p[':responsible'] = $user->id;
$id = db_exec_insert('task', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':taskid'] = $id;
$p[':projid'] = gpc_get_int($_POST, 'projid');
$p[':plandate'] = gpc_get_date($_POST, 'plandate');
$p[':duedate'] = gpc_get_date($_POST, 'duedate');
if ($p[':projid'] == -1) {
$p[':projid'] = NULL;
}
$p[':taskname'] = gpc_get_string($_POST, 'taskname');
$p[':priority'] = gpc_get_string($_POST, 'priority', 10);
if ($p[':priority'] == '-1') {
$p[':priority'] = NULL;
}
$p[':started'] = gpc_get_datetime($_POST, 'started');
$p[':finished'] = gpc_get_datetime($_POST, 'finished');
$p[':taskstate'] = gpc_get_enum($_POST, 'taskstate', array_keys($opt_state));
$p[':responsible'] = gpc_get_int($_POST, 'responsible');
db_exec_update('task', $p, 'taskid');
$action = ACT_VIEW;
break;
case 'delete':
// Only captain is allowed to delete
if ($user->role != 'captain') {
$g_warning->Add('Only captain is allowed to delete tasks.');
$action = ACT_VIEW;
break;
}
// 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 possible redirect
$sth = $pdo->prepare("SELECT projid FROM task WHERE taskid=?");
$sth->execute([$id]);
$task = $sth->fetch(PDO::FETCH_OBJ);
if (isset($task->projid)) {
$backlink = 'project.php?id='.$task->projid;
}
// now really delete
$sth = $pdo->prepare("DELETE FROM task WHERE taskid=?");
$sth->execute([$id]);
$g_success->Add(_('Task deleted'));
if (isset($backlink)) {
header_location($backlink, ['info' => _('Note deleted.')]);
}
$action = ACT_DEFAULT;
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, 206, ['txt','prio', 'stat']);
$w = array('(t.vid=:vid OR t.vid IS NULL)');
$p = array(':vid' => $user->vid);
if (strlen($flt->txt) > 1) {
$w[] = '(taskname LIKE :txt)';
$p[':txt'] = '%'.$flt->txt.'%';
}
if (strlen($flt->prio) > 2 ) {
$w[] = 'priority=:priority';
$p[':priority'] = $flt->prio;
} elseif ($flt->prio == -1) {
$w[] = 'priority IS NULL';
}
/*if (strlen($flt->stat) > 2 ) {
$w[] = 'taskstate=:taskstate';
$p[':taskstate'] = $flt->stat;
} */
$parts = ["taskstate=''", 'taskstate IS NULL'];
foreach ($flt->stat as $f) {
$parts[] = "FIND_IN_SET('$f', taskstate)";
}
if (!empty($parts)) {
if (count($parts) > 1) {
$w[] = '(' . join(' OR ', $parts) . ')';
} else {
$w[] = $parts[0];
}
}
$where = join(' AND ', $w);
$order = ' ORDER BY t.taskid';
// get total recond count for pagination and limit
$sql = "SELECT COUNT(*) FROM task AS t";
if ($where) $sql .= " 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', $g_rows_pp);
$lastpage = ceil($numrows/$rows_pp);
$sql = "SELECT t.taskid, t.vid, t.projid, t.taskname, t.priority, t.taskstate,"
. " p.projname "
. "FROM task AS t LEFT JOIN project AS p USING (projid) "
. "WHERE $where";
if ($order) $sql .= $order;
// if pagination:
$sql .= ' LIMIT ' . ($page - 1) * $rows_pp . ',' . $rows_pp;
$sth = $pdo->prepare($sql);
try {
$sth->execute($p);
} catch (PDOexception $e) {
$g_error->Add('SQL-Error: '. $e->getMessage());
$g_error->Add($sql);
$g_error->Add(print_r($p, true));
$g_error->Printout();
}
$res = $sth->fetchAll();
?>
<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">
<?php
filter_create_select('flt_prio', _('Priority'), $g_opt_all + $g_opt_none + $opt_priority, $flt->prio);
//filter_create_select('flt_stat', _('State'), $g_opt_all + $opt_state, $flt->stat);
?>
<div class="d-flex flex-nowrap gap-2">
<label for="flt_txt">Text</label>
<input type="text" class="form-control" name="flt_txt" size="15" maxlength="30" value="<?=$flt->txt ?>">
</div>
<?php
$opt_status = db_load_enum('task', 'taskstate', true, true, false);
filter_create_checks('flt_stat', _('Status'), $opt_status, $flt->stat);
?>
</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
// Pagination on top
$pagination = get_pagination($g_scriptname, '', $page, $lastpage);
echo $pagination;
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>', _('Priority'), "</th>";
echo '<th>', _('Status'), "</th>";
echo "<th></th>";
echo "<th></th>\n";
echo "</tr>\n";
echo "</thead>\n";
$i = ($page - 1) * $rows_pp;
$i0 = $i + 1;
foreach ($res as $row) {
$i++; // record number
echo "<tr>\n";
// mark generic tasks with braces
echo "<td>", $row['vid'] ? $i : "($i)";
echo '<a class="text-nowrap" title="', _('View'), '" href="', $g_scriptname, '?f=view&id=', $row['taskid'], '"><i class="bi bi-eye ms-2"></i></a>';
echo "</td>\n";
echo "<td>", $row['taskname'], "</td>\n";
echo "<td>", $row['projname'], "</td>\n";
echo "<td>", ($opt_priority[$row['priority']] ?? ''), "</td>\n";
echo "<td>", ($opt_state[$row['taskstate']]), "</td>\n";
// Edit current record button
echo "<td>";
echo '<a title="', _('Edit'), '" href="', $g_scriptname, '?f=edit&id=', $row['taskid'], '"><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";
// additional pagination at bottom
echo $pagination;
form_add_button($g_scriptname);
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');
$opt_none = [-1 => '&horbar; none &horbar;'];
form_create_select('projid', _('Project'), $opt_none + $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, t.priority, t.plandate, t.duedate,"
. " t.started, t.finished, t.taskstate, t.responsible,"
. " u.displayname, p.projname "
. "FROM task AS t LEFT JOIN project AS p USING (projid) "
. " LEFT JOIN user AS u ON (t.responsible=u.userid) "
. "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 style="width:20%;">', _('Task'),"</th><td>", $task->taskname, "</td></tr>\n";
echo '<tr><th>', _('Project'),"</th><td>", ($task->projname ?? _('unassigned')), "</td></tr>\n";
echo '<tr><th>', _('Priority'),"</th><td>", ($task->priority ?? _('none')), "</td></tr>\n";
echo '<tr><th>', _('Plan date'),"</th><td>", $task->plandate, "</td></tr>\n";
echo '<tr><th>', _('Due date'),"</th><td>", $task->duedate, "</td></tr>\n";
echo '<tr><th>', _('Started at'),"</th><td>", $task->started, "</td></tr>\n";
echo '<tr><th>', _('Finished at'),"</th><td>", $task->finished, "</td></tr>\n";
echo '<tr><th>', _('Status'),"</th><td>", $opt_state[$task->taskstate], "</td></tr>\n";
echo '<tr><th>', _('Responsible'),"</th><td>", $task->displayname, "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
echo '<h3>', _('Annotations'), "</h3>";
$sql = "SELECT noteid, annotation "
. "FROM note "
. "WHERE notetype='task' 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) ", nl2br($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 task'), "</p>\n";
}
echo '<div class="container-fluid px-0 my-3">', "\n";
echo '<a href="note.php?f=add&t=task&id=', $id, '" class="btn btn-primary" role="button">';
echo _('Add note'), "</a>\n";
echo "</div>\n";
echo '<h3>', _('Documents'), "</h3>";
echo '<p>', _('Not yet implemented'), "</p>";
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
$opt_user = db_get_opt_user($user->id); // only used here
$sql = "SELECT taskname, projid, priority, plandate, duedate, started, finished,"
. " taskstate "
. "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
$opt_none = [-1 => '&horbar; none &horbar;'];
form_create_select('projid', _('Project'), $opt_none + $opt_projects, $task->projid);
form_create_select('priority', _('Priority'), $opt_none + $opt_priority, $task->priority ?? 'X');
?>
<div class="mb-3">
<label for="plandate" class="form-label"><?=_('Plan date')?></label>
<input type="date" class="form-control" id="plandate" name="plandate" value="<?=$task->plandate ?>">
</div>
<div class="mb-3">
<label for="duedate" class="form-label"><?=_('Due date')?></label>
<input type="date" class="form-control" id="duedate" name="duedate" value="<?=$task->duedate ?>">
</div>
<div class="mb-3">
<label for="started" class="form-label"><?=_('Started at')?></label>
<input type="datetime-local" class="form-control" id="started" name="started" value="<?=$task->started ?>">
</div>
<div class="mb-3">
<label for="finished" class="form-label"><?=_('Finished at')?></label>
<input type="datetime-local" class="form-control" id="finished" name="finished" value="<?=$task->finished ?>">
</div>
<?php
form_create_select('taskstate', _('Status'), $opt_state, $task->taskstate);
form_create_select('responsible', _('Responsible'), $opt_user, $user->id);
form_edit_buttons($g_scriptname, $id);
echo "</form>\n";
elseif ($action == ACT_DELETE):
// ========== VARIANT: delete record ==========================================
$sql = "SELECT taskname FROM task WHERE taskid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$task = $sth->fetch(PDO::FETCH_OBJ);
echo '<h2>', _('Delete Task'), "</h2>\n";
echo '<p>', sprintf(_('Task no. %d'), $id), "</p>\n";
echo '<p>', $task->taskname, "</p>";
echo '<p>', _('Deleting a task 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';