2026-01-01 18:57:23 -05:00
|
|
|
<?php
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2026-01-01 18:57:23 -05:00
|
|
|
/**
|
|
|
|
|
* WorkflowModel - Handles status transition workflows and validation
|
2026-01-29 10:53:26 -05:00
|
|
|
*
|
|
|
|
|
* Uses caching for frequently accessed transition rules since they rarely change.
|
2026-01-01 18:57:23 -05:00
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
require_once dirname(__DIR__) . '/helpers/CacheHelper.php';
|
|
|
|
|
|
2026-04-13 20:56:10 -04:00
|
|
|
class WorkflowModel
|
|
|
|
|
{
|
2026-01-29 11:04:36 -05:00
|
|
|
private mysqli $conn;
|
|
|
|
|
private static string $CACHE_PREFIX = 'workflow';
|
|
|
|
|
private static int $CACHE_TTL = 600; // 10 minutes
|
2026-01-01 18:57:23 -05:00
|
|
|
|
2026-04-13 20:56:10 -04:00
|
|
|
public function __construct(mysqli $conn)
|
|
|
|
|
{
|
2026-01-01 18:57:23 -05:00
|
|
|
$this->conn = $conn;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
/**
|
|
|
|
|
* Get all active transitions (with caching)
|
|
|
|
|
*
|
|
|
|
|
* @return array All active transitions indexed by from_status
|
|
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
private function getAllTransitions(): array
|
|
|
|
|
{
|
2026-07-10 10:56:52 -04:00
|
|
|
$cached = CacheHelper::get(self::$CACHE_PREFIX, 'all_transitions', self::$CACHE_TTL);
|
|
|
|
|
if ($cached !== null) {
|
|
|
|
|
return $cached;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT from_status, to_status, requires_comment, requires_admin
|
|
|
|
|
FROM status_transitions
|
|
|
|
|
WHERE is_active = TRUE";
|
|
|
|
|
$result = $this->conn->query($sql);
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
// A transient DB failure must NOT be cached as "no transitions" — that
|
|
|
|
|
// would block every status change for the whole TTL. Fail safe by
|
|
|
|
|
// returning empty without storing it, so the next call retries.
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2026-03-17 23:22:24 -04:00
|
|
|
|
2026-07-10 10:56:52 -04:00
|
|
|
$transitions = [];
|
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
|
$from = $row['from_status'];
|
|
|
|
|
if (!isset($transitions[$from])) {
|
|
|
|
|
$transitions[$from] = [];
|
2026-01-29 10:53:26 -05:00
|
|
|
}
|
2026-07-10 10:56:52 -04:00
|
|
|
$transitions[$from][$row['to_status']] = [
|
|
|
|
|
'to_status' => $row['to_status'],
|
|
|
|
|
'requires_comment' => (bool)$row['requires_comment'],
|
|
|
|
|
'requires_admin' => (bool)$row['requires_admin']
|
|
|
|
|
];
|
|
|
|
|
}
|
2026-01-29 10:53:26 -05:00
|
|
|
|
2026-07-10 10:56:52 -04:00
|
|
|
CacheHelper::set(self::$CACHE_PREFIX, 'all_transitions', $transitions);
|
|
|
|
|
return $transitions;
|
2026-01-29 10:53:26 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-01 18:57:23 -05:00
|
|
|
/**
|
|
|
|
|
* Get allowed status transitions for a given status
|
|
|
|
|
*
|
|
|
|
|
* @param string $currentStatus Current ticket status
|
|
|
|
|
* @return array Array of allowed transitions with requirements
|
|
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
public function getAllowedTransitions(string $currentStatus): array
|
|
|
|
|
{
|
2026-01-29 10:53:26 -05:00
|
|
|
$allTransitions = $this->getAllTransitions();
|
|
|
|
|
|
|
|
|
|
if (!isset($allTransitions[$currentStatus])) {
|
|
|
|
|
return [];
|
2026-01-01 18:57:23 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
return array_values($allTransitions[$currentStatus]);
|
2026-01-01 18:57:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a status transition is allowed
|
|
|
|
|
*
|
|
|
|
|
* @param string $fromStatus Current status
|
|
|
|
|
* @param string $toStatus Desired status
|
|
|
|
|
* @param bool $isAdmin Whether user is admin
|
|
|
|
|
* @return bool True if transition is allowed
|
|
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
public function isTransitionAllowed(string $fromStatus, string $toStatus, bool $isAdmin = false): bool
|
|
|
|
|
{
|
2026-01-01 18:57:23 -05:00
|
|
|
// Allow same status (no change)
|
|
|
|
|
if ($fromStatus === $toStatus) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
$allTransitions = $this->getAllTransitions();
|
2026-01-01 18:57:23 -05:00
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
if (!isset($allTransitions[$fromStatus][$toStatus])) {
|
2026-01-01 18:57:23 -05:00
|
|
|
return false; // Transition not defined
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
$transition = $allTransitions[$fromStatus][$toStatus];
|
2026-01-01 18:57:23 -05:00
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
if ($transition['requires_admin'] && !$isAdmin) {
|
2026-01-01 18:57:23 -05:00
|
|
|
return false; // Admin required
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all possible statuses from transitions table
|
|
|
|
|
*
|
|
|
|
|
* @return array Array of unique status values
|
|
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
public function getAllStatuses(): array
|
|
|
|
|
{
|
2026-07-10 10:56:52 -04:00
|
|
|
$cached = CacheHelper::get(self::$CACHE_PREFIX, 'all_statuses', self::$CACHE_TTL);
|
|
|
|
|
if ($cached !== null) {
|
|
|
|
|
return $cached;
|
|
|
|
|
}
|
2026-03-17 23:22:24 -04:00
|
|
|
|
2026-07-10 10:56:52 -04:00
|
|
|
$sql = "SELECT DISTINCT from_status as status FROM status_transitions
|
|
|
|
|
UNION
|
|
|
|
|
SELECT DISTINCT to_status as status FROM status_transitions
|
|
|
|
|
ORDER BY status";
|
|
|
|
|
$result = $this->conn->query($sql);
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
// Do not cache an empty list on a transient DB failure.
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$statuses = [];
|
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
|
$statuses[] = $row['status'];
|
|
|
|
|
}
|
2026-01-29 10:53:26 -05:00
|
|
|
|
2026-07-10 10:56:52 -04:00
|
|
|
CacheHelper::set(self::$CACHE_PREFIX, 'all_statuses', $statuses);
|
|
|
|
|
return $statuses;
|
2026-01-01 18:57:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get transition requirements
|
|
|
|
|
*
|
|
|
|
|
* @param string $fromStatus Current status
|
|
|
|
|
* @param string $toStatus Desired status
|
|
|
|
|
* @return array|null Transition requirements or null if not found
|
|
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
public function getTransitionRequirements(string $fromStatus, string $toStatus): ?array
|
|
|
|
|
{
|
2026-01-29 10:53:26 -05:00
|
|
|
$allTransitions = $this->getAllTransitions();
|
|
|
|
|
|
|
|
|
|
if (!isset($allTransitions[$fromStatus][$toStatus])) {
|
2026-01-01 18:57:23 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
$transition = $allTransitions[$fromStatus][$toStatus];
|
|
|
|
|
return [
|
|
|
|
|
'requires_comment' => $transition['requires_comment'],
|
|
|
|
|
'requires_admin' => $transition['requires_admin']
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 10:56:52 -04:00
|
|
|
/**
|
|
|
|
|
* Whether a given transition requires a comment.
|
|
|
|
|
*
|
|
|
|
|
* Convenience accessor so callers (e.g. the update-ticket endpoint) can
|
|
|
|
|
* enforce requires_comment server-side without inspecting the full row.
|
|
|
|
|
* Returns false for an undefined transition or a no-op (same status).
|
|
|
|
|
*
|
|
|
|
|
* @param string $fromStatus Current status
|
|
|
|
|
* @param string $toStatus Desired status
|
|
|
|
|
* @return bool True if the transition requires a comment
|
|
|
|
|
*/
|
|
|
|
|
public function transitionRequiresComment(string $fromStatus, string $toStatus): bool
|
|
|
|
|
{
|
|
|
|
|
$requirements = $this->getTransitionRequirements($fromStatus, $toStatus);
|
|
|
|
|
return $requirements !== null && !empty($requirements['requires_comment']);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 10:53:26 -05:00
|
|
|
/**
|
|
|
|
|
* Clear workflow cache (call when transitions are modified)
|
|
|
|
|
*/
|
2026-04-13 20:56:10 -04:00
|
|
|
public static function clearCache(): void
|
|
|
|
|
{
|
2026-01-29 10:53:26 -05:00
|
|
|
CacheHelper::delete(self::$CACHE_PREFIX);
|
2026-01-01 18:57:23 -05:00
|
|
|
}
|
|
|
|
|
}
|