diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 8b0af5e9..f44d921c 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -80,7 +80,9 @@ class SettingsController extends Controller $setting_value = $data->value; } - $user->settings()->updateExistingPivot($setting->id, ['value' => $setting_value]); + $user->settings()->detach($setting->id); + $user->settings()->save($setting, ['uservalue' => $setting_value]); + $route = route('settings.index', [], false); return redirect($route) ->with([ diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 621e43f0..2bfb9d2f 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -6,6 +6,8 @@ use Illuminate\Support\ServiceProvider; use Artisan; use Schema; use App\Setting; +use App\User; +use Session; class AppServiceProvider extends ServiceProvider { @@ -32,7 +34,9 @@ class AppServiceProvider extends ServiceProvider } if(is_file(database_path('app.sqlite'))) { if(Schema::hasTable('settings')) { - if($bg_image = Setting::fetch('background_image')) { + die("s: ".\Session::get('current_user')); + //die("c: ".User::currentUser()); + if($bg_image = Setting::_fetch('background_image', User::currentUser())) { $alt_bg = ' style="background-image: url(/storage/'.$bg_image.')"'; } diff --git a/app/Setting.php b/app/Setting.php index dfe8d592..e7ee4cce 100644 --- a/app/Setting.php +++ b/app/Setting.php @@ -157,10 +157,10 @@ class Setting extends Model */ public static function _fetch($key, $user=null) { - $cachekey = ($user === null) ? $key : $key.'-'.$user->id; - if (Setting::cached($cachekey)) { - return Setting::$cache[$cachekey]; - } else { + #$cachekey = ($user === null) ? $key : $key.'-'.$user->id; + #if (Setting::cached($cachekey)) { + # return Setting::$cache[$cachekey]; + #} else { $find = self::where('key', '=', $key)->first(); if (!is_null($find)) { @@ -168,25 +168,33 @@ class Setting extends Model $value = $find->value; } else { // not system variable so use user specific value // check if user specified value has been set - $usersetting = $user->settings->where('id', $find->id)->first(); - //die(print_r($usersetting)); + //print_r($user); + $usersetting = $user->settings()->where('id', $find->id)->first(); + //print_r($user->settings); + //die(var_dump($usersetting)); //->pivot->value; + //echo "user: ".$user->id." --- ".$usersettings; if(isset($usersetting) && !empty($usersetting)) { - $value = $usersetting->pivot->value; + $value = $usersetting->pivot->uservalue; } else { // if not get default from base setting //$user->settings()->save($find, ['value' => $find->value]); - $user->settings()->updateExistingPivot($find->id, ['value' => $find->value]); + #$has_setting = $user->settings()->where('id', $find->id)->exists(); + #if($has_setting) { + # $user->settings()->updateExistingPivot($find->id, ['uservalue' => (string)$find->value]); + #} else { + # $user->settings()->save($find, ['uservalue' => (string)$find->value]); + #} $value = $find->value; } } - Setting::add($cachekey, $value); + #Setting::add($cachekey, $value); return $value; } else { return false; } - } + #} } /** @@ -267,7 +275,7 @@ class Setting extends Model */ public function users() { - return $this->belongsToMany('App\User')->withPivot('value'); + return $this->belongsToMany('App\User')->using('App\SettingUser')->withPivot('uservalue'); } public static function user() diff --git a/app/SettingUser.php b/app/SettingUser.php new file mode 100644 index 00000000..05c3949b --- /dev/null +++ b/app/SettingUser.php @@ -0,0 +1,10 @@ +belongsToMany('App\Setting')->withPivot('value'); + return $this->belongsToMany('App\Setting')->withPivot('uservalue'); } public static function currentUser()