feat(preferences): added preferences model and helper to get and set them
This commit is contained in:
parent
d5abe92eb2
commit
25a1fc0c37
2 changed files with 122 additions and 0 deletions
99
app/Helpers/Preferences.php
Normal file
99
app/Helpers/Preferences.php
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
<?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();
|
||||||
|
}
|
||||||
|
}
|
23
app/Models/Preference.php
Normal file
23
app/Models/Preference.php
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Preference extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $table = 'cc_pref';
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'subjid',
|
||||||
|
'keystr',
|
||||||
|
'valstr'
|
||||||
|
];
|
||||||
|
|
||||||
|
public function user() {
|
||||||
|
return $this->belongsTo(User::class, 'subjid');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue