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