Issues (2963)

app/Models/UserPref.php (1 issue)

1
<?php
2
/**
3
 * UserPref.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Models;
27
28
class UserPref extends BaseModel
29
{
30
    public $timestamps = false;
31
    public $incrementing = false;
32
    protected $table = 'users_prefs';
33
    /** @var array */
34
    protected $primaryKey = ['user_id', 'pref'];
35
    protected $fillable = ['user_id', 'pref', 'value'];
36
37
    // ---- Helper Functions ----
38
    public static function getPref(User $user, $pref)
39
    {
40
        if ($user->relationLoaded('preferences')) {
41
            return optional($user->preferences->firstWhere('pref', $pref))->value;
42
        }
43
44
        return $user->preferences()->where('pref', $pref)->value('value');
45
    }
46
47
    public static function setPref(User $user, $pref, $value)
48
    {
49
        return UserPref::updateOrCreate(['user_id' => $user->user_id, 'pref' => $pref], ['value' => $value]);
50
    }
51
52
    public static function forgetPref(User $user, $pref)
53
    {
54
        return $user->preferences()->where('pref', $pref)->delete();
55
    }
56
57
    // ---- Accessors/Mutators ----
58
59
    public function getValueAttribute($value)
60
    {
61
        $decoded = json_decode($value, true);
62
        if (json_last_error() == JSON_ERROR_NONE) {
63
            return $decoded;
64
        }
65
66
        return $value;
67
    }
68
69
    public function setValueAttribute($value)
70
    {
71
        if (is_array($value)) {
72
            $this->attributes['value'] = json_encode($value);
73
        } else {
74
            $this->attributes['value'] = $value;
75
        }
76
    }
77
78
    // ---- Query Scopes ----
79
80
    public function scopePref($query, $pref)
81
    {
82
        return $query->where('pref', $pref);
83
    }
84
85
    // ---- Define Relationships ----
86
87
    public function user()
88
    {
89
        return $this->belongsTo(\App\Models\User::class, 'user_id');
90
    }
91
92
    /**
93
     * Set the keys for a save update query. (no primary key)
94
     *
95
     * @param  \Illuminate\Database\Eloquent\Builder  $query
96
     * @return \Illuminate\Database\Eloquent\Builder
97
     */
98
    protected function setKeysForSaveQuery($query)
99
    {
100
        /** @var array */
101
        $keys = $this->getKeyName();
102
        if (! is_array($keys)) {
0 ignored issues
show
The condition is_array($keys) is always false.
Loading history...
103
            return parent::setKeysForSaveQuery($query);
104
        }
105
106
        foreach ($keys as $keyName) {
107
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
108
        }
109
110
        return $query;
111
    }
112
113
    /**
114
     * Get the primary key value for a save query. (no primary key)
115
     *
116
     * @param  mixed  $keyName
117
     * @return mixed
118
     */
119
    protected function getKeyForSaveQuery($keyName = null)
120
    {
121
        if (is_null($keyName)) {
122
            $keyName = $this->getKeyName();
123
        }
124
125
        if (isset($this->original[$keyName])) {
126
            return $this->original[$keyName];
127
        }
128
129
        return $this->getAttribute($keyName);
130
    }
131
}
132