Files
YMS/webgui/projects.php
T
2025-05-09 10:22:08 +02:00

274 lines
8.8 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024-2025 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
require 'globals.inc';
$pagetitle = _('Projects');
$id = gpc_get_int($_REQUEST, 'id', 0);
$sql = "SELECT userid, displayname FROM user WHERE userid>0";
$sth = $pdo->query($sql);
$opt_user = array();
foreach ($sth->fetchAll() as $row) {
$opt_user[$row['userid']] = $row['displayname'];
}
$opt_state = db_load_enum('project', 'projstate', 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['txt'] = gpc_get_string($_POST, 'flt_txt');
db_save_filter($flt, 205);
$action = ACT_DEFAULT;
break;
case 'insert':
$p[':vid'] = $user->vid;
$p[':projname'] = gpc_get_string($_POST, 'projname');
$p[':responsible'] = gpc_get_int($_POST, 'responsible');
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
$id = db_exec_insert('project', $p);
$action = ACT_VIEW;
break;
case 'update':
$p[':projid'] = $id;
$p[':projname'] = gpc_get_string($_POST, 'projname');
$p[':startdate'] = gpc_get_date($_POST, 'startdate');
$p[':duration'] = gpc_get_int($_POST, 'duration');
$p[':projstate'] = gpc_get_string($_POST, 'projstate');
$p[':remarks'] = gpc_get_string($_POST, 'remarks', 150);
db_exec_update('project', $p, 'projid');
$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']);
// TBD
$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);
$sql = "SELECT projid, projname, startdate, duration, remarks "
. "FROM project "
. "WHERE vid=? "
. "ORDER BY projid";
$sth = $pdo->prepare($sql);
$sth->execute([$user->vid]);
$res = $sth->fetchAll();
echo '<table class="table table-striped">'. "\n";
echo "<thead>\n";
echo "<tr>";
echo "<th>" . _('Name') . "</th>";
echo "<th>" . _('Start date') . "</th>";
echo "<th>" . _('Duration') . "</th>";
echo "<th>" . _('Remarks') . "</th>";
echo "<th>&nbsp;</th>";
echo "</tr>\n";
echo "</thead>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>". $row['projname'] ."</td>\n";
echo "<td>". $row['startdate'] ."</td>\n";
echo "<td>". $row['duration'] ."</td>\n";
echo "<td>". $row['remarks'] ."</td>\n";
// Action buttons
form_action_buttons($g_scriptname, $row['projid']);
echo "</tr>\n";
}
echo "<tfoot>\n";
echo '<tr><td colspan="5">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
form_add_button($g_scriptname);
echo "<h1>", _('Generic Tasks'), "</h1>\n";
echo "<p>", _('Tasks not assigned to any project'), "</p>\n";
$sql = "SELECT taskid, taskname "
. "FROM task "
. "WHERE vid=? AND projid IS NULL "
. "ORDER BY taskid";
$sth = $pdo->prepare($sql);
$sth->execute([$user->vid]);
$res = $sth->fetchAll();
echo '<table class="table table-striped">', "\n";
echo "<thead>\n";
echo "<tr>";
echo "<th>" . _('Name') . "</th>";
echo "</tr>\n";
echo "</thead>\n";
foreach ($res as $row) {
echo "<tr>\n";
echo "<td>". $row['taskname'] ."</td>\n";
form_action_buttons('task.php', $row['taskid']);
echo "</tr>\n";
}
// Table summary
echo "<tfoot>\n";
echo '<tr><td colspan="2">', sprintf(_('%d records'), count($res)), '</td></tr>', "\n";
echo "</tfoot>\n";
echo "</table>\n";
form_add_button('task.php');
elseif ($action == ACT_ADD):
// ========== VARIANT: add record =============================================
echo '<h2>', _('Add Project'), "</h2>\n";
?>
<form method="post" action="<?=$g_scriptname?>">
<div class="mb-3">
<label for="projname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="projname" name="projname">
</div>
<?php
form_create_select('responsible', _('Responsible'), $opt_user, $project->responsible);
?>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"></textarea>
</div>
<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 projname, startdate, duration, responsible, projstate, remarks "
. "FROM project "
. "WHERE projid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$project = $sth->fetch(PDO::FETCH_OBJ);
echo "<h2>", $project->projname, "</h2>\n";
echo '<table class="table">', "\n";
echo '<tr><th scope="row" style="width:20%">', _('Name'),"</th><td>", $project->projname, "</td></tr>\n";
echo '<tr><th scope="row">', _('Responsible'),"</th><td>", $opt_user[$project->responsible], "</td></tr>\n";
echo '<tr><th scope="row">', _('Start date'),"</th><td>", $project->startdate, "</td></tr>\n";
echo '<tr><th scope="row">', _('Duration'),"</th><td>", $project->duration, "</td></tr>\n";
echo '<tr><th scope="row">', _('State'),"</th><td>", $project->projstate, "</td></tr>\n";
echo '<tr><th scope="row">', _('Remarks'),"</th><td>", $project->remarks, "</td></tr>\n";
echo "</table>\n";
form_view_buttons($g_scriptname, $id);
// assigned tasks
echo "<h2>", _('Assigned tasks'), "</h2>\n";
$sql = "SELECT taskid, taskname FROM task WHERE projid=? ORDER BY taskname";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
?>
<table class="table table-sm">
<thead>
<tr>
<th><?=_('Task')?></th>
</tr>
</thead>
<?php foreach ($sth->fetchAll() as $row): ?>
<tr>
<td><?=$row['taskname']?></td>
<?php form_action_buttons('task.php', $row['taskid']); ?>
</tr>
<?php endforeach; ?>
</table>
<?php
form_add_button('task.php', $id, 'projid');
elseif ($action == ACT_EDIT):
// ========== VARIANT: edit single record =====================================
echo '<h2>', _('Edit Project'), "</h2>\n";
$sql = "SELECT projname, responsible, startdate, duration, projstate, remarks "
. "FROM project "
. "WHERE projid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$id]);
$project = $sth->fetch(PDO::FETCH_OBJ);
?>
<form method="post" action="<?=$g_scriptname?>">
<input type="hidden" name="id" value="<?=$id?>">
<div class="mb-3">
<label for="projname" class="form-label"><?=_('Name')?></label>
<input type="text" class="form-control" id="projname" name="projname" value="<?=$project->projname ?>">
</div>
<?php
form_create_select('responsible', _('Responsible'), $opt_user, $project->responsible);
?>
<div class="mb-3">
<label for="startdate"><?=_('Startdate')?></label>
<input type="date" class="form-control" id="startdate" name="startdate" value="<?=$project->startdate ?>">
</div>
<div class="mb-3">
<label for="duration"><?=_('Duration, days')?></label>
<input type="number" class="form-control" id="duration" name="duration" min="1" value="<?=$project->duration ?>">
</div>
<?php
form_create_select('projstate', _('Status'), $opt_state, $project->projstate);
?>
<div class="mb-3">
<label for="remarks" class="form-label"><?=_('Remarks')?></label>
<textarea class="form-control" id="remarks" name="remarks" rows="3"><?=$project->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 ==========================================
echo '<h2>', _('Delete Project'), "</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';