1
|
|
|
<?php namespace Anomaly\PreferencesModule\Preference; |
2
|
|
|
|
3
|
|
|
use Anomaly\PreferencesModule\Preference\Command\GetValueFieldType; |
4
|
|
|
use Anomaly\PreferencesModule\Preference\Command\GetValuePresenter; |
5
|
|
|
use Anomaly\PreferencesModule\Preference\Command\ModifyValue; |
6
|
|
|
use Anomaly\PreferencesModule\Preference\Contract\PreferenceInterface; |
7
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldType; |
8
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter; |
9
|
|
|
use Anomaly\Streams\Platform\Model\Preferences\PreferencesPreferencesEntryModel; |
10
|
|
|
use Anomaly\UsersModule\User\Contract\UserInterface; |
11
|
|
|
use Illuminate\Database\Eloquent\Builder; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PreferenceModel |
15
|
|
|
* |
16
|
|
|
* @method Builder belongingToUser(UserInterface $user) |
17
|
|
|
* |
18
|
|
|
* @link http://pyrocms.com/ |
19
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
20
|
|
|
* @author Ryan Thompson <[email protected]> |
21
|
|
|
* @package Anomaly\PreferencesModule\PreferenceInterface |
22
|
|
|
*/ |
23
|
|
|
class PreferenceModel extends PreferencesPreferencesEntryModel implements PreferenceInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The cache minutes. |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $cacheMinutes = 99999; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Limit to preferences belonging to the provided user. |
35
|
|
|
* |
36
|
|
|
* @param Builder $query |
37
|
|
|
* @param UserInterface $user |
38
|
|
|
*/ |
39
|
|
|
public function scopeBelongingToUser(Builder $query, UserInterface $user) |
40
|
|
|
{ |
41
|
|
|
$query->where('user_id', $user->getId()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the key. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getKey() |
50
|
|
|
{ |
51
|
|
|
return $this->key; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the key. |
56
|
|
|
* |
57
|
|
|
* @param $key |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setKey($key) |
61
|
|
|
{ |
62
|
|
|
$this->key = $key; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the user. |
69
|
|
|
* |
70
|
|
|
* @return UserInterface |
71
|
|
|
*/ |
72
|
|
|
public function getUser() |
73
|
|
|
{ |
74
|
|
|
return $this->user; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the user. |
79
|
|
|
* |
80
|
|
|
* @param UserInterface $user |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function setUser(UserInterface $user) |
84
|
|
|
{ |
85
|
|
|
$this->user = $user; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the value. |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function getValue() |
96
|
|
|
{ |
97
|
|
|
return $this->value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set the value. |
102
|
|
|
* |
103
|
|
|
* @param $value |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setValue($value) |
107
|
|
|
{ |
108
|
|
|
$this->value = $value; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set the value. |
115
|
|
|
* |
116
|
|
|
* @param $value |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
protected function setValueAttribute($value) |
120
|
|
|
{ |
121
|
|
|
$this->attributes['value'] = $this->dispatch(new ModifyValue($this, $value)); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get the value attribute. |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
protected function getValueAttribute() |
132
|
|
|
{ |
133
|
|
|
/* @var FieldType $type */ |
134
|
|
|
$type = $this->dispatch(new GetValueFieldType($this)); |
135
|
|
|
|
136
|
|
|
if (!$type) { |
137
|
|
|
return $this->attributes['value']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $type->getValue(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the field type's presenter |
145
|
|
|
* for a given field slug. |
146
|
|
|
* |
147
|
|
|
* We're overriding this to catch |
148
|
|
|
* the "value" key. |
149
|
|
|
* |
150
|
|
|
* @param $fieldSlug |
151
|
|
|
* @return FieldTypePresenter |
152
|
|
|
*/ |
153
|
|
|
public function getFieldTypePresenter($fieldSlug) |
154
|
|
|
{ |
155
|
|
|
if ($fieldSlug == 'value') { |
156
|
|
|
return $this->dispatch(new GetValuePresenter($this)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return parent::getFieldTypePresenter($fieldSlug); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|