1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Tenants\Models; |
6
|
|
|
|
7
|
|
|
use Spatie\Sluggable\SlugOptions; |
8
|
|
|
use Rinvex\Support\Traits\HasSlug; |
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
10
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
11
|
|
|
use Illuminate\Database\Eloquent\Builder; |
12
|
|
|
use Rinvex\Support\Traits\HasTranslations; |
13
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
14
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Rinvex\Tenants\Models\Tenant. |
19
|
|
|
* |
20
|
|
|
* @property int $id |
21
|
|
|
* @property string $slug |
22
|
|
|
* @property array $name |
23
|
|
|
* @property array $description |
24
|
|
|
* @property int $user_id |
25
|
|
|
* @property string $user_type |
26
|
|
|
* @property string $email |
27
|
|
|
* @property string $website |
28
|
|
|
* @property string $phone |
29
|
|
|
* @property string $language_code |
30
|
|
|
* @property string $country_code |
31
|
|
|
* @property string $state |
32
|
|
|
* @property string $city |
33
|
|
|
* @property string $address |
34
|
|
|
* @property string $postal_code |
35
|
|
|
* @property string $launch_date |
36
|
|
|
* @property string $group |
37
|
|
|
* @property bool $is_active |
38
|
|
|
* @property \Carbon\Carbon|null $created_at |
39
|
|
|
* @property \Carbon\Carbon|null $updated_at |
40
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
41
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $user |
42
|
|
|
* |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant ofUser(\Illuminate\Database\Eloquent\Model $user) |
|
|
|
|
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereAddress($value) |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCity($value) |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCountryCode($value) |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereCreatedAt($value) |
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereDeletedAt($value) |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereDescription($value) |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereEmail($value) |
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereGroup($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereId($value) |
53
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereIsActive($value) |
54
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereLanguageCode($value) |
55
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereLaunchDate($value) |
56
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereName($value) |
57
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereUserId($value) |
58
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereUserType($value) |
59
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePhone($value) |
60
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant wherePostalCode($value) |
61
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereSlug($value) |
62
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereState($value) |
63
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Tenants\Models\Tenant whereUpdatedAt($value) |
64
|
|
|
* @mixin \Eloquent |
65
|
|
|
*/ |
66
|
|
|
class Tenant extends Model |
67
|
|
|
{ |
68
|
|
|
use HasSlug; |
69
|
|
|
use HasTranslations; |
70
|
|
|
use ValidatingTrait; |
71
|
|
|
use CacheableEloquent; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
protected $fillable = [ |
77
|
|
|
'slug', |
78
|
|
|
'name', |
79
|
|
|
'description', |
80
|
|
|
'user_id', |
81
|
|
|
'user_type', |
82
|
|
|
'email', |
83
|
|
|
'website', |
84
|
|
|
'phone', |
85
|
|
|
'language_code', |
86
|
|
|
'country_code', |
87
|
|
|
'state', |
88
|
|
|
'city', |
89
|
|
|
'address', |
90
|
|
|
'postal_code', |
91
|
|
|
'launch_date', |
92
|
|
|
'group', |
93
|
|
|
'is_active', |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected $casts = [ |
100
|
|
|
'slug' => 'string', |
101
|
|
|
'user_id' => 'integer', |
102
|
|
|
'user_type' => 'string', |
103
|
|
|
'email' => 'string', |
104
|
|
|
'website' => 'string', |
105
|
|
|
'phone' => 'string', |
106
|
|
|
'country_code' => 'string', |
107
|
|
|
'language_code' => 'string', |
108
|
|
|
'state' => 'string', |
109
|
|
|
'city' => 'string', |
110
|
|
|
'address' => 'string', |
111
|
|
|
'postal_code' => 'string', |
112
|
|
|
'launch_date' => 'string', |
113
|
|
|
'group' => 'string', |
114
|
|
|
'is_active' => 'boolean', |
115
|
|
|
'deleted_at' => 'datetime', |
116
|
|
|
]; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
protected $observables = [ |
122
|
|
|
'validating', |
123
|
|
|
'validated', |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* The attributes that are translatable. |
128
|
|
|
* |
129
|
|
|
* @var array |
130
|
|
|
*/ |
131
|
|
|
public $translatable = [ |
132
|
|
|
'name', |
133
|
|
|
'description', |
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* The default rules that the model will validate against. |
138
|
|
|
* |
139
|
|
|
* @var array |
140
|
|
|
*/ |
141
|
|
|
protected $rules = []; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Whether the model should throw a |
145
|
|
|
* ValidationException if it fails validation. |
146
|
|
|
* |
147
|
|
|
* @var bool |
148
|
|
|
*/ |
149
|
|
|
protected $throwValidationExceptions = true; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Create a new Eloquent model instance. |
153
|
|
|
* |
154
|
|
|
* @param array $attributes |
155
|
|
|
*/ |
156
|
|
|
public function __construct(array $attributes = []) |
157
|
|
|
{ |
158
|
|
|
parent::__construct($attributes); |
159
|
|
|
|
160
|
|
|
$this->setTable(config('rinvex.tenants.tables.tenants')); |
161
|
|
|
$this->setRules([ |
162
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug', |
163
|
|
|
'name' => 'required|string|max:150', |
164
|
|
|
'description' => 'nullable|string|max:10000', |
165
|
|
|
'user_id' => 'required|integer', |
166
|
|
|
'user_type' => 'required|string', |
167
|
|
|
'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email', |
168
|
|
|
'website' => 'nullable|string|max:150', |
169
|
|
|
'phone' => 'nullable|numeric|min:4', |
170
|
|
|
'country_code' => 'required|alpha|size:2|country', |
171
|
|
|
'language_code' => 'required|alpha|size:2|language', |
172
|
|
|
'state' => 'nullable|string', |
173
|
|
|
'city' => 'nullable|string', |
174
|
|
|
'address' => 'nullable|string', |
175
|
|
|
'postal_code' => 'nullable|string', |
176
|
|
|
'launch_date' => 'nullable|date_format:Y-m-d', |
177
|
|
|
'group' => 'nullable|string|max:150', |
178
|
|
|
'is_active' => 'sometimes|boolean', |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get all attached models of the given class to the tenant. |
184
|
|
|
* |
185
|
|
|
* @param string $class |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
188
|
|
|
*/ |
189
|
|
|
public function entries(string $class): MorphToMany |
190
|
|
|
{ |
191
|
|
|
return $this->morphedByMany($class, 'tenantable', config('rinvex.tenants.tables.tenantables'), 'tenant_id', 'tenantable_id'); |
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the options for generating the slug. |
196
|
|
|
* |
197
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
198
|
|
|
*/ |
199
|
|
|
public function getSlugOptions(): SlugOptions |
200
|
|
|
{ |
201
|
|
|
return SlugOptions::create() |
202
|
|
|
->doNotGenerateSlugsOnUpdate() |
203
|
|
|
->generateSlugsFrom('name') |
204
|
|
|
->saveSlugsTo('slug'); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the owning user. |
209
|
|
|
* |
210
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
211
|
|
|
*/ |
212
|
|
|
public function user(): MorphTo |
213
|
|
|
{ |
214
|
|
|
return $this->morphTo(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get bookings of the given user. |
219
|
|
|
* |
220
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
221
|
|
|
* @param \Illuminate\Database\Eloquent\Model $user |
222
|
|
|
* |
223
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
224
|
|
|
*/ |
225
|
|
|
public function scopeOfUser(Builder $builder, Model $user): Builder |
226
|
|
|
{ |
227
|
|
|
return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Activate the tenant. |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function activate() |
236
|
|
|
{ |
237
|
|
|
$this->update(['is_active' => true]); |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Deactivate the tenant. |
244
|
|
|
* |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function deactivate() |
248
|
|
|
{ |
249
|
|
|
$this->update(['is_active' => false]); |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.