Files
controls-web/lasuca/grower-account.php
2026-02-17 09:29:34 -06:00

271 lines
12 KiB
PHP

<?php
// phpcs:ignoreFile
require_once __DIR__ . '/grower-session.php';
require_once __DIR__ . '/inc/dbconfig.php';
require_once __DIR__ . '/inc/opendb.php';
require_once __DIR__ . '/inc/auth.php';
require_once __DIR__ . '/inc/grower_helpers.php';
$username = $_SESSION['myusername'];
$growerId = isset($_SESSION['growerid']) ? $_SESSION['growerid'] : '';
$profileMessage = '';
$profileSuccess = null;
$passwordMessage = '';
$passwordSuccess = null;
$member = auth_find_member($username);
$memberData = grower_member_defaults($member);
if ($member === null) {
$profileMessage = 'We could not locate your account record. Please contact support.';
$profileSuccess = false;
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$formType = isset($_POST['form_type']) ? $_POST['form_type'] : '';
if ($formType === 'profile') {
$emailInput = isset($_POST['email']) ? trim($_POST['email']) : '';
$farmInput = isset($_POST['growername']) ? trim($_POST['growername']) : '';
$phoneInput = isset($_POST['phone']) ? trim($_POST['phone']) : '';
$phoneDigits = preg_replace('/[^0-9A-Za-z\-\+\(\)\.\s]/', '', $phoneInput);
if ($emailInput !== '' && filter_var($emailInput, FILTER_VALIDATE_EMAIL) === false) {
$profileMessage = 'Please enter a valid email address or leave the field blank.';
$profileSuccess = false;
} else {
if (auth_update_profile($username, $emailInput, $farmInput, $phoneDigits)) {
$profileMessage = 'Profile updated successfully.';
$profileSuccess = true;
$member = auth_find_member($username) ?: $member;
$memberData = grower_member_defaults($member);
} else {
$profileMessage = 'Unable to update your profile. Please try again.';
$profileSuccess = false;
}
}
} elseif ($formType === 'password') {
$currentPassword = isset($_POST['current_password']) ? $_POST['current_password'] : '';
$newPassword = isset($_POST['new_password']) ? $_POST['new_password'] : '';
$confirmPassword = isset($_POST['confirm_password']) ? $_POST['confirm_password'] : '';
list($passwordSuccess, $passwordMessage) = auth_change_password($username, $currentPassword, $newPassword, $confirmPassword);
if ($passwordSuccess) {
$member = auth_find_member($username) ?: $member;
$memberData = grower_member_defaults($member);
}
}
}
$emailValue = isset($_POST['email']) ? trim($_POST['email']) : $memberData['email'];
$phoneValue = isset($_POST['phone']) ? trim($_POST['phone']) : $memberData['phone'];
$farmValue = isset($_POST['growername']) ? trim($_POST['growername']) : $memberData['growername'];
$lastLogin = $memberData['last_login_at'];
$passwordChanged = $memberData['password_last_changed'];
$recentFiles = $member !== null ? grower_recent_files($username, 6) : array();
require_once __DIR__ . '/inc/closedb.php';
$pageTitle = 'Grower Account';
$metaDescription = 'Update your grower portal contact information, review recent files, and manage passwords.';
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<title><?php echo htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8'); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="<?php echo htmlspecialchars($metaDescription, ENT_QUOTES, 'UTF-8'); ?>" />
<link rel="icon" type="image/x-icon" href="/images/favicon.ico" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/new/css/styles.css" />
<link rel="stylesheet" href="/new/css/lasuca-theme.css" />
<link rel="stylesheet" href="/new/css/pages.css" />
<link rel="stylesheet" href="/new/css/grower-portal.css" />
<link rel="stylesheet" href="/new/css/grower-dashboard.css" />
</head>
<body class="lasuca-theme theme-dark grower-portal">
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container">
<a class="navbar-brand" href="/grower-dashboard.php">
<img src="/images/logo2.png" alt="LASUCA logo" />
<span>Grower Portal</span>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#growerNav" aria-controls="growerNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="growerNav">
<ul class="navbar-nav ms-auto mb-2 mb-lg-0">
<li class="nav-item"><a class="nav-link" href="/home.php">Home</a></li>
<li class="nav-item"><a class="nav-link" href="/grower-dashboard.php">Dashboard</a></li>
<li class="nav-item"><a class="nav-link" href="/grower-files.php">Daily Reports</a></li>
<li class="nav-item"><a class="nav-link" href="/grower-account.php">Manage Account</a></li>
<li class="nav-item"><a class="nav-link" href="/grower-logout.php">Log Out</a></li>
<?php include __DIR__ . '/inc/theme-toggle.php'; ?>
</ul>
</div>
</div>
</nav>
<main>
<header class="hero hero-sub">
<div class="container">
<span class="badge">Grower portal</span>
<h1>Manage your account</h1>
<p>
Keep contact details current, review recent downloads, and update your password so the LASUCA team can
reach you quickly during harvest.
</p>
</div>
</header>
<section class="page-section py-5">
<div class="container">
<?php if ($profileMessage !== '') {
$profileClass = $profileSuccess ? 'success' : 'warning'; ?>
<div class="alert alert-<?php echo $profileClass; ?>" role="alert">
<?php echo htmlspecialchars($profileMessage, ENT_QUOTES, 'UTF-8'); ?>
</div>
<?php }
if ($passwordMessage !== '') {
$passwordClass = $passwordSuccess ? 'success' : 'warning'; ?>
<div class="alert alert-<?php echo $passwordClass; ?>" role="alert">
<?php echo htmlspecialchars($passwordMessage, ENT_QUOTES, 'UTF-8'); ?>
</div>
<?php } ?>
<div class="row g-4 align-items-start">
<div class="col-lg-4">
<div class="grower-account-card grower-account-summary h-100">
<h3 class="mb-3">Account snapshot</h3>
<dl class="row">
<dt class="col-sm-6">Grower ID</dt>
<dd class="col-sm-6 text-sm-end"><?php echo htmlspecialchars((string) $growerId, ENT_QUOTES, 'UTF-8'); ?></dd>
<dt class="col-sm-6">Username</dt>
<dd class="col-sm-6 text-sm-end"><?php echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); ?></dd>
<dt class="col-sm-6">Last login</dt>
<dd class="col-sm-6 text-sm-end"><?php echo htmlspecialchars(grower_format_datetime($lastLogin), ENT_QUOTES, 'UTF-8'); ?></dd>
<dt class="col-sm-6">Password updated</dt>
<dd class="col-sm-6 text-sm-end"><?php echo htmlspecialchars(grower_format_datetime($passwordChanged), ENT_QUOTES, 'UTF-8'); ?></dd>
<dt class="col-sm-6">File portal</dt>
<dd class="col-sm-6 text-sm-end"><a href="/grower-files.php" class="link-success">Open browser</a></dd>
</dl>
<?php if ($member !== null) {
if (!empty($recentFiles)) { ?>
<div class="mt-4">
<h3 class="mb-3">Recent files</h3>
<ul class="recent-file-list">
<?php foreach ($recentFiles as $recent) {
$recentName = htmlspecialchars($recent['name'], ENT_QUOTES, 'UTF-8');
$recentPath = htmlspecialchars($recent['path'], ENT_QUOTES, 'UTF-8');
$recentDate = htmlspecialchars(grower_format_datetime(date('Y-m-d H:i:s', $recent['modified'])), ENT_QUOTES, 'UTF-8'); ?>
<li>
<a href="<?php echo $recentPath; ?>" target="_blank" rel="noopener"><?php echo $recentName; ?></a>
<span class="activity-date"><?php echo $recentDate; ?></span>
</li>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<p class="text-muted mt-4 mb-0">We haven&rsquo;t spotted any files in your grower folder yet.</p>
<?php }
} ?>
</div>
</div>
<div class="col-lg-4">
<div class="grower-account-card h-100">
<h3 class="mb-3">Contact information</h3>
<form method="post" action="" novalidate>
<input type="hidden" name="form_type" value="profile" />
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input
type="email"
class="form-control"
name="email"
id="email"
value="<?php echo htmlspecialchars($emailValue, ENT_QUOTES, 'UTF-8'); ?>"
placeholder="you@example.com"
/>
</div>
<div class="mb-4">
<label for="phone" class="form-label">Phone number</label>
<input
type="text"
class="form-control"
name="phone"
id="phone"
value="<?php echo htmlspecialchars($phoneValue, ENT_QUOTES, 'UTF-8'); ?>"
placeholder="(000) 000-0000"
/>
</div>
<div class="text">
<label for="growername" class="form-label">Farm Name</label>
<input
type="text"
class="form-control"
name="growername"
id="growername"
value="<?php echo htmlspecialchars($farmValue, ENT_QUOTES, 'UTF-8'); ?>"
placeholder="Farm Name"
/>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-success">Save contact info</button>
</div>
</form>
</div>
</div>
<div class="col-lg-4">
<div class="grower-account-card h-100">
<h3 class="mb-3">Change password</h3>
<form method="post" action="" novalidate>
<input type="hidden" name="form_type" value="password" />
<div class="mb-3">
<label for="current_password" class="form-label">Current password</label>
<input
type="password"
class="form-control"
name="current_password"
id="current_password"
autocomplete="current-password"
/>
</div>
<div class="mb-3">
<label for="new_password" class="form-label">New password</label>
<input
type="password"
class="form-control"
name="new_password"
id="new_password"
autocomplete="new-password"
/>
</div>
<div class="mb-4">
<label for="confirm_password" class="form-label">Confirm new password</label>
<input
type="password"
class="form-control"
name="confirm_password"
id="confirm_password"
autocomplete="new-password"
/>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-outline-success">Update password</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</main>
<?php
require __DIR__ . '/inc/theme-footer.php';
?>