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 Rinvex\Tenants\Contracts\TenantContract; |
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 $owner_id |
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 string $thumbnail |
38
|
|
|
* @property string $cover_photo |
39
|
|
|
* @property \Carbon\Carbon|null $created_at |
40
|
|
|
* @property \Carbon\Carbon|null $updated_at |
41
|
|
|
* @property \Carbon\Carbon|null $deleted_at |
42
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $owner |
43
|
|
|
* |
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 whereOwnerId($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 implements TenantContract |
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
|
|
|
'email', |
81
|
|
|
'website', |
82
|
|
|
'phone', |
83
|
|
|
'language_code', |
84
|
|
|
'country_code', |
85
|
|
|
'state', |
86
|
|
|
'city', |
87
|
|
|
'address', |
88
|
|
|
'postal_code', |
89
|
|
|
'launch_date', |
90
|
|
|
'group', |
91
|
|
|
'is_active', |
92
|
|
|
'thumbnail', |
93
|
|
|
'cover_photo', |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected $casts = [ |
100
|
|
|
'slug' => 'string', |
101
|
|
|
'owner_id' => 'integer', |
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
|
|
|
'thumbnail' => 'string', |
115
|
|
|
'cover_photo' => 'string', |
116
|
|
|
'deleted_at' => 'datetime', |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
protected $observables = [ |
123
|
|
|
'validating', |
124
|
|
|
'validated', |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The attributes that are translatable. |
129
|
|
|
* |
130
|
|
|
* @var array |
131
|
|
|
*/ |
132
|
|
|
public $translatable = [ |
133
|
|
|
'name', |
134
|
|
|
'description', |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* The default rules that the model will validate against. |
139
|
|
|
* |
140
|
|
|
* @var array |
141
|
|
|
*/ |
142
|
|
|
protected $rules = []; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Whether the model should throw a |
146
|
|
|
* ValidationException if it fails validation. |
147
|
|
|
* |
148
|
|
|
* @var bool |
149
|
|
|
*/ |
150
|
|
|
protected $throwValidationExceptions = true; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create a new Eloquent model instance. |
154
|
|
|
* |
155
|
|
|
* @param array $attributes |
156
|
|
|
*/ |
157
|
|
|
public function __construct(array $attributes = []) |
158
|
|
|
{ |
159
|
|
|
parent::__construct($attributes); |
160
|
|
|
|
161
|
|
|
// Get users model |
162
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
163
|
|
|
|
164
|
|
|
$this->setTable(config('rinvex.tenants.tables.tenants')); |
165
|
|
|
$this->setRules([ |
166
|
|
|
'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.tenants.tables.tenants').',slug', |
167
|
|
|
'name' => 'required|string|max:150', |
168
|
|
|
'description' => 'nullable|string|max:10000', |
169
|
|
|
'owner_id' => 'required|integer|exists:'.(new $userModel())->getTable().',id', |
170
|
|
|
'email' => 'required|email|min:3|max:150|unique:'.config('rinvex.tenants.tables.tenants').',email', |
171
|
|
|
'website' => 'nullable|string|max:150', |
172
|
|
|
'phone' => 'nullable|numeric|min:4', |
173
|
|
|
'country_code' => 'required|alpha|size:2|country', |
174
|
|
|
'language_code' => 'required|alpha|size:2|language', |
175
|
|
|
'state' => 'nullable|string', |
176
|
|
|
'city' => 'nullable|string', |
177
|
|
|
'address' => 'nullable|string', |
178
|
|
|
'postal_code' => 'nullable|string', |
179
|
|
|
'launch_date' => 'nullable|date_format:Y-m-d', |
180
|
|
|
'group' => 'nullable|string|max:150', |
181
|
|
|
'is_active' => 'sometimes|boolean', |
182
|
|
|
'thumbnail' => 'nullable|string|max:150', |
183
|
|
|
'cover_photo' => 'nullable|string|max:150', |
184
|
|
|
]); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get all attached models of the given class to the tenant. |
189
|
|
|
* |
190
|
|
|
* @param string $class |
191
|
|
|
* |
192
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
193
|
|
|
*/ |
194
|
|
|
public function entries(string $class): MorphToMany |
195
|
|
|
{ |
196
|
|
|
return $this->morphedByMany($class, 'tenantable', config('rinvex.tenants.tables.tenantables'), 'tenant_id', 'tenantable_id'); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the options for generating the slug. |
201
|
|
|
* |
202
|
|
|
* @return \Spatie\Sluggable\SlugOptions |
203
|
|
|
*/ |
204
|
|
|
public function getSlugOptions(): SlugOptions |
205
|
|
|
{ |
206
|
|
|
return SlugOptions::create() |
207
|
|
|
->doNotGenerateSlugsOnUpdate() |
208
|
|
|
->generateSlugsFrom('name') |
209
|
|
|
->saveSlugsTo('slug'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* A tenant always belongs to an owner. |
214
|
|
|
* |
215
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
216
|
|
|
*/ |
217
|
|
|
public function owner() |
218
|
|
|
{ |
219
|
|
|
$userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model'); |
|
|
|
|
220
|
|
|
|
221
|
|
|
return $this->belongsTo($userModel, 'owner_id', 'id'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Activate the tenant. |
226
|
|
|
* |
227
|
|
|
* @return $this |
228
|
|
|
*/ |
229
|
|
|
public function activate() |
230
|
|
|
{ |
231
|
|
|
$this->update(['is_active' => true]); |
232
|
|
|
|
233
|
|
|
return $this; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Deactivate the tenant. |
238
|
|
|
* |
239
|
|
|
* @return $this |
240
|
|
|
*/ |
241
|
|
|
public function deactivate() |
242
|
|
|
{ |
243
|
|
|
$this->update(['is_active' => false]); |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
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.