- Add CacheHelper for file-based caching with TTL support - Add Database helper for centralized connection management - Update WorkflowModel to cache status transitions (10 min TTL) - Update UserPreferencesModel to cache user prefs (5 min TTL) - Update manage_workflows.php to clear cache on changes - Update get_users.php to use Database helper (example migration) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* UserPreferencesModel
|
|
* Handles user-specific preferences and settings with caching
|
|
*/
|
|
require_once dirname(__DIR__) . '/helpers/CacheHelper.php';
|
|
|
|
class UserPreferencesModel {
|
|
private $conn;
|
|
private static $CACHE_PREFIX = 'user_prefs';
|
|
private static $CACHE_TTL = 300; // 5 minutes
|
|
|
|
public function __construct($conn) {
|
|
$this->conn = $conn;
|
|
}
|
|
|
|
/**
|
|
* Get all preferences for a user (with caching)
|
|
* @param int $userId User ID
|
|
* @return array Associative array of preference_key => preference_value
|
|
*/
|
|
public function getUserPreferences($userId) {
|
|
return CacheHelper::remember(self::$CACHE_PREFIX, $userId, function() use ($userId) {
|
|
$sql = "SELECT preference_key, preference_value
|
|
FROM user_preferences
|
|
WHERE user_id = ?";
|
|
$stmt = $this->conn->prepare($sql);
|
|
$stmt->bind_param("i", $userId);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
$prefs = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$prefs[$row['preference_key']] = $row['preference_value'];
|
|
}
|
|
$stmt->close();
|
|
return $prefs;
|
|
}, self::$CACHE_TTL);
|
|
}
|
|
|
|
/**
|
|
* Set or update a preference for a user
|
|
* @param int $userId User ID
|
|
* @param string $key Preference key
|
|
* @param string $value Preference value
|
|
* @return bool Success status
|
|
*/
|
|
public function setPreference($userId, $key, $value) {
|
|
$sql = "INSERT INTO user_preferences (user_id, preference_key, preference_value)
|
|
VALUES (?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE preference_value = VALUES(preference_value)";
|
|
$stmt = $this->conn->prepare($sql);
|
|
$stmt->bind_param("iss", $userId, $key, $value);
|
|
$result = $stmt->execute();
|
|
$stmt->close();
|
|
|
|
// Invalidate cache for this user
|
|
if ($result) {
|
|
CacheHelper::delete(self::$CACHE_PREFIX, $userId);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Get a single preference value for a user
|
|
* @param int $userId User ID
|
|
* @param string $key Preference key
|
|
* @param mixed $default Default value if preference doesn't exist
|
|
* @return mixed Preference value or default
|
|
*/
|
|
public function getPreference($userId, $key, $default = null) {
|
|
$prefs = $this->getUserPreferences($userId);
|
|
return $prefs[$key] ?? $default;
|
|
}
|
|
|
|
/**
|
|
* Delete a preference for a user
|
|
* @param int $userId User ID
|
|
* @param string $key Preference key
|
|
* @return bool Success status
|
|
*/
|
|
public function deletePreference($userId, $key) {
|
|
$sql = "DELETE FROM user_preferences
|
|
WHERE user_id = ? AND preference_key = ?";
|
|
$stmt = $this->conn->prepare($sql);
|
|
$stmt->bind_param("is", $userId, $key);
|
|
$result = $stmt->execute();
|
|
$stmt->close();
|
|
|
|
// Invalidate cache for this user
|
|
if ($result) {
|
|
CacheHelper::delete(self::$CACHE_PREFIX, $userId);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Delete all preferences for a user
|
|
* @param int $userId User ID
|
|
* @return bool Success status
|
|
*/
|
|
public function deleteAllPreferences($userId) {
|
|
$sql = "DELETE FROM user_preferences WHERE user_id = ?";
|
|
$stmt = $this->conn->prepare($sql);
|
|
$stmt->bind_param("i", $userId);
|
|
$result = $stmt->execute();
|
|
$stmt->close();
|
|
|
|
// Invalidate cache for this user
|
|
if ($result) {
|
|
CacheHelper::delete(self::$CACHE_PREFIX, $userId);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Clear all user preferences cache
|
|
*/
|
|
public static function clearCache() {
|
|
CacheHelper::delete(self::$CACHE_PREFIX);
|
|
}
|
|
}
|