99 lines
2.7 KiB
PHP
99 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\Preference;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Preferences
|
|
{
|
|
private function setValue($key, $value, $isUserValue = false) {
|
|
try {
|
|
$userId = Auth::id();
|
|
|
|
if ($isUserValue && is_null($userId)) {
|
|
throw new Exception("User id can't be null for a user preference {$key}.");
|
|
}
|
|
|
|
$pref = Preference::where('key', $key);
|
|
if ($isUserValue) {
|
|
$pref = $pref->where('subjid', $userId);
|
|
}
|
|
|
|
if ($pref->count() > 0) {
|
|
$pref->valstr = $value;
|
|
} else {
|
|
if ($isUserValue) {
|
|
throw new Exception("Preference {$key} not found for user {$userId}.");
|
|
}
|
|
throw new Exception("Preference {$key} not found.");
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
header('HTTP/1.0 503 Service Unavailable');
|
|
Log::info('Database error: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function getValue($key, $isUserValue = false, $forceDefault = false) {
|
|
try {
|
|
$userId = Auth::id();
|
|
|
|
if ($isUserValue && is_null($userId)) {
|
|
throw new Exception("User id can't be null for a user preference {$key}.");
|
|
}
|
|
|
|
$pref = Preference::where('key', $key);
|
|
if ($isUserValue) {
|
|
$pref = $pref->where('subjid', $userId);
|
|
}
|
|
|
|
if ($pref->count() > 0) {
|
|
return $pref->first()->valstr;
|
|
} else {
|
|
if ($isUserValue) {
|
|
throw new Exception("Preference {$key} not found for user {$userId}.");
|
|
}
|
|
throw new Exception("Preference {$key} not found.");
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
header('HTTP/1.0 503 Service Unavailable');
|
|
Log::info('Database error: ' . $e->getMessage());
|
|
die();
|
|
}
|
|
}
|
|
|
|
public function getDefaultTimeZone() {
|
|
return config('app.timezone');
|
|
}
|
|
|
|
public static function setUserTimezone($timezone = null)
|
|
{
|
|
self::setValue('user_timezone', $timezone, true);
|
|
}
|
|
|
|
public static function getUserTimezone()
|
|
{
|
|
$timezone = self::getValue('user_timezone', true);
|
|
if (!$timezone) {
|
|
return self::getDefaultTimezone();
|
|
}
|
|
|
|
return $timezone;
|
|
}
|
|
|
|
// Always attempts to returns the current user's personal timezone setting
|
|
public static function getTimezone()
|
|
{
|
|
$userId = Auth::id();
|
|
|
|
if (!is_null($userId)) {
|
|
return self::getUserTimezone();
|
|
}
|
|
|
|
return self::getDefaultTimezone();
|
|
}
|
|
}
|