updated to laravel 11

This commit is contained in:
2024-04-22 18:16:33 +10:00
parent 5fbca80a3c
commit 5b7da699bd
503 changed files with 9672 additions and 73262 deletions

View File

@@ -1,73 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class AnalyticsItemRequest extends Model
{
use HasFactory;
/**
* The table name
*
* @var string
*/
protected $table = 'analytics_requests';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'type',
'path'
];
/**
* Model Boot.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::creating(function (AnalyticsItemRequest $analytics) {
if (isset($analytics->session_id) !== true) {
$request = request();
if ($request !== null) {
$session = AnalyticsSession::where('ip', $request->ip())
->where('useragent', $request->userAgent())
->where('ended_at', '>=', now()->subMinutes(30))
->first();
if ($session === null) {
$session = AnalyticsSession::create([
'ip' => $request->ip(),
'useragent' => $request->userAgent(),
'ended_at' => now()
]);
} else {
$session->update(['ended_at' => now()]);
}
$analytics->session_id = $session->id;
}
}//end if
});
}
/**
* Return the Analytics Session model.
*
* @return BelongsTo
*/
public function session(): BelongsTo
{
return $this->belongsTo(AnalyticsSession::class, 'id', 'session_id');
}
}

View File

@@ -1,44 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class AnalyticsSession extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'ip',
'useragent',
'ended_at'
];
/**
* Set the "useragent" attribute.
*
* @param mixed $value
* @return void
*/
public function setUseragentAttribute($value)
{
$this->attributes['useragent'] = $value !== null ? $value : '';
}
/**
* Returns the related requests for this session.
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function requests(): HasMany {
return $this->hasMany(AnalyticsItemRequest::class, 'session_id', 'id');
}
}

View File

@@ -1,43 +0,0 @@
<?php
namespace App\Models;
use App\Traits\HasAttachments;
use App\Traits\HasGallery;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Article extends Model
{
use HasFactory;
use Uuids;
use HasGallery;
use HasAttachments;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'slug',
'publish_at',
'content',
'user_id',
'hero'
];
/**
* Get the article user
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View File

@@ -1,86 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Cache;
class Attachment extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'media_id',
'private',
];
/**
* The default attributes.
*
* @var string[]
*/
protected $attributes = [
'private' => false,
];
/**
* Get the media for this attachment.
*
* @return BelongsTo
*/
public function media(): BelongsTo
{
return $this->belongsTo(Media::class);
}
/**
* Get associated Media object.
*
* @return null|Media
*/
public function getMediaAttribute(): ?Media
{
$mediaId = '0';
$media = null;
if (Cache::has("attachment:{$this->id}:media") === true) {
$mediaId = Cache::get("attachment:{$this->id}:media");
} else {
$media = $this->media()->first();
if ($media === null) {
return null;
}
$mediaId = $media->id;
Cache::put("attachment:{$this->id}:media", $mediaId, now()->addDays(28));
}
return Cache::remember("media:{$mediaId}", now()->addDays(28), function () use ($media) {
if ($media !== null) {
return $media;
}
return $this->media()->first();
});
}
/**
* Set the media for this item.
*
* @param Media $media The media model.
* @return void
*/
public function setMediaAttribute(Media $media): void
{
$this->media()->associate($media)->save();
Cache::put("attachment:{$this->id}:media", $media->id, now()->addDays(28));
}
}

View File

@@ -2,21 +2,20 @@
namespace App\Models;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Subscription extends Model
class EmailSubscriptions extends Model
{
use HasFactory;
use Uuids;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
* @var array
*/
protected $fillable = [
'email',
'confirmed'
];
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class EmailUpdate extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'email',
'token'
];
/**
* Get the user that owns the email update.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}

View File

@@ -1,51 +0,0 @@
<?php
namespace App\Models;
use App\Traits\HasAttachments;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
class Event extends Model
{
use HasAttachments;
use HasFactory;
use Uuids;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'title',
'location',
'location_url',
'address',
'start_at',
'end_at',
'publish_at',
'status',
'registration_type',
'registration_data',
'hero',
'content',
'price',
'ages',
'open_at',
];
/**
* Get all the associated users.
*
* @return BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id');
}
}

View File

@@ -1,45 +0,0 @@
<?php
namespace App\Models;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class EventUser extends Model
{
use HasFactory;
use Uuids;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'event_id',
'user_id',
];
/**
* Get the event for this attachment.
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
/**
* Get the user for this attachment.
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View File

@@ -1,107 +0,0 @@
<?php
namespace App\Models;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Cache;
class Gallery extends Model
{
use HasFactory;
use Uuids;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'media_id',
];
/**
* Boot the model.
*
* @return void
*/
protected static function boot(): void
{
parent::boot();
$clearCache = function ($gallery) {
Cache::forget("gallery:{$gallery->id}:media");
};
static::saving($clearCache);
static::deleting($clearCache);
}
/**
* Get gallery addendum model.
*
* @return Illuminate\Database\Eloquent\Relations\MorphTo Addenum model.
*/
public function addendum(): MorphTo
{
return $this->morphTo();
}
/**
* Get the media for this item.
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo The media model.
*/
public function media(): BelongsTo
{
return $this->belongsTo(Media::class);
}
/**
* Get the media for this item.
*
* @return null|Media The media model.
*/
public function getMediaAttribute(): ?Media
{
$mediaId = '0';
$media = null;
if (Cache::has("gallery:{$this->id}:media") === true) {
$mediaId = Cache::get("gallery:{$this->id}:media");
} else {
$media = $this->media()->first();
if ($media === null) {
return null;
}
$mediaId = $media->id;
Cache::put("gallery:{$this->id}:media", $mediaId, now()->addDays(28));
}
return Cache::remember("media:{$mediaId}", now()->addDays(28), function () use ($media) {
if ($media !== null) {
return $media;
}
return $this->media()->first();
});
}
/**
* Set the media for this item.
*
* @param Media $media The media model.
* @return void
*/
public function setMediaAttribute(Media $media): void
{
$this->media()->associate($media)->save();
Cache::put("gallery:{$this->id}:media", $media->id, now()->addDays(28));
}
}

14
app/Models/Location.php Normal file
View File

@@ -0,0 +1,14 @@
<?php
namespace App\Models;
use App\Traits\UUID;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
use HasFactory, UUID;
protected $fillable = ['name', 'address', 'address_url', 'url'];
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,239 +0,0 @@
<?php
namespace App\Models;
use App\Jobs\MediaWorkerJob;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class MediaJob extends Model
{
use HasFactory;
use Uuids;
/**
* The default attributes.
*
* @var string[]
*/
protected $attributes = [
'user_id' => null,
'media_id' => null,
'status' => '',
'status_text' => '',
'progress' => 0,
'progress_max' => 0,
'data' => '',
];
/**
* Set MediaJob status to failed.
*
* @param string $statusText The failed reason.
* @return void
*/
public function setStatusFailed(string $statusText = ''): void
{
$this->setStatus('failed', $statusText, 0);
}
/**
* Set MediaJob status to queued.
*
* @return void
*/
public function setStatusQueued(): void
{
$this->setStatus('queued', '', 0);
}
/**
* Set MediaJob status to waiting.
*
* @return void
*/
public function setStatusWaiting(): void
{
$this->setStatus('waiting', '', 0);
}
/**
* Set MediaJob status to processing.
*
* @param integer $progress The processing progress value.
* @param integer $progressMax The processing progress maximum value.
* @param string $statusText The processing status text.
* @return void
*/
public function setStatusProcessing(int $progress = 0, int $progressMax = 0, string $statusText = ''): void
{
if ($statusText === '') {
$statusText = $this->status_text;
}
$this->setStatus('processing', $statusText, $progress, $progressMax);
}
/**
* Set MediaJob status to complete.
*
* @return void
*/
public function setStatusComplete(): void
{
$this->setStatus('complete');
}
/**
* Set MediaJon status to invalid.
*
* @param string $text The status text.
* @return void
*/
public function setStatusInvalid(string $text = ''): void
{
$this->setStatus('invalid', $text);
}
/**
* Set MediaJob status details.
*
* @param string $status The status string.
* @param string $text The status text.
* @param integer $progress The status progress value.
* @param integer $progress_max The status progress maximum value.
* @return void
*/
protected function setStatus(string $status, string $text = '', int $progress = 0, int $progress_max = 0): void
{
$this->status = $status;
$this->status_text = $text;
$this->progress = $progress;
$this->progress_max = $progress_max;
$this->save();
}
/**
* Process the MediaJob.
*
* @return void
*/
public function process(): void
{
$data = json_decode($this->data, true);
if ($data !== null) {
if (array_key_exists('chunks', $data) === true) {
if (array_key_exists('chunk_count', $data) === false) {
$this->setStatusInvalid('chunk_count is missing');
return;
}
if (array_key_exists('name', $data) === false) {
$this->setStatusInvalid('name is missing');
return;
}
$numChunks = count($data['chunks']);
$maxChunks = intval($data['chunk_count']);
if ($numChunks >= $maxChunks) {
// merge file and dispatch
$this->setStatusProcessing(0, $maxChunks, 'combining chunks');
$newFile = generateTempFilePath(pathinfo($data['name'], PATHINFO_EXTENSION));
$failed = false;
for ($index = 1; $index <= $maxChunks; $index++) {
if (array_key_exists($index, $data['chunks']) === false) {
$failed = `{$index} chunk is missing`;
} else {
$tempFileName = $data['chunks'][$index];
if ($failed === false) {
$chunkContents = file_get_contents($tempFileName);
if ($chunkContents === false) {
$failed = `{$index} chunk is empty`;
} else {
file_put_contents($newFile, $chunkContents, FILE_APPEND);
}
}
unlink($tempFileName);
$this->setStatusProcessing($index, $maxChunks);
}
}
unset($data['chunks']);
$this->data = json_encode($data);
if ($failed !== false) {
$this->setStatusInvalid($failed);
} else {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $newFile);
finfo_close($finfo);
$data['file'] = $newFile;
$data['size'] = filesize($newFile);
$data['mime_type'] = $mime;
if (
array_key_exists('storage', $data) === true &&
array_key_exists('security_type', $data) === true &&
array_key_exists('mime_type', $data) === true &&
$data['mime_type'] !== ""
) {
$error = Media::verifyStorage($data['mime_type'], $data['security_type'], $data['storage']);
switch ($error) {
case Media::STORAGE_VALID:
break;
case Media::STORAGE_MIME_MISSING:
$this->setStatusInvalid('The file type cannot be determined.');
return;
case Media::STORAGE_NOT_FOUND:
$this->setStatusInvalid('Storage was not found.');
return;
case Media::STORAGE_INVALID_SECURITY:
$this->setStatusInvalid('Storage invalid for security value.');
return;
default:
$this->setStatusInvalid('Storage verification error occurred.');
return;
}
}
$this->data = json_encode($data);
$this->setStatusQueued();
MediaWorkerJob::dispatch($this)->onQueue('media');
}//end if
}//end if
} else {
$this->setStatusQueued();
MediaWorkerJob::dispatch($this)->onQueue('media');
}//end if
}//end if
}
/**
* Return the job owner
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Return the media item
*
* @return BelongsTo
*/
public function media(): BelongsTo
{
return $this->belongsTo(Media::class);
}
}

View File

@@ -1,35 +0,0 @@
<?php
namespace App\Models;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Permission extends Model
{
use HasFactory;
use Uuids;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'permission',
'user',
];
/**
* Get the User associated with this model
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

26
app/Models/Post.php Normal file
View File

@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use App\Helpers;
use App\Traits\HasFiles;
use App\Traits\Slug;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory, Slug, HasFiles;
protected $fillable = ['title', 'content', 'user_id', 'status', 'published_at', 'hero_media_name'];
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
public function hero()
{
return $this->belongsTo(Media::class, 'hero_media_name');
}
}

View File

@@ -1,37 +0,0 @@
<?php
namespace App\Models;
use App\Enum\HttpResponseCodes;
use App\Jobs\OptimizeMediaJob;
use App\Traits\Uuids;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\StreamedResponse;
class Shortlink extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'code',
'url',
];
}

11
app/Models/Ticket.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Ticket extends Model
{
use HasFactory;
}

View File

@@ -2,26 +2,21 @@
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Collection;
use App\Mail\LoginLink;
use App\Traits\UUID;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
use Laravel\Sanctum\HasApiTokens;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
use PharIo\Manifest\Email;
class User extends Authenticatable implements Auditable
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens;
use HasFactory;
use Notifiable;
use Uuids;
use \OwenIt\Auditing\Auditable;
use HasFactory, Notifiable, UUID;
/**
* The attributes that are mass assignable.
@@ -29,12 +24,24 @@ class User extends Authenticatable implements Auditable
* @var array<int, string>
*/
protected $fillable = [
'first_name',
'last_name',
'admin',
'firstname',
'surname',
'email',
'phone',
'password',
'display_name',
'home_address',
'home_address2',
'home_city',
'home_postcode',
'home_state',
'home_country',
'billing_address',
'billing_address2',
'billing_city',
'billing_postcode',
'billing_state',
'billing_country',
'subscribed'
];
/**
@@ -45,202 +52,144 @@ class User extends Authenticatable implements Auditable
protected $hidden = [
'password',
'remember_token',
'permissions'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// protected $hidden = [
// 'permissions'
// ];
/**
* The attributes to append.
*
* @var string[]
*/
protected $appends = [
'permissions'
'password' => 'hashed',
];
/**
* The default attributes.
*
* @var string[]
*/
protected $attributes = [
'phone' => '',
];
/**
* Boot the model.
* The "booted" method of the model.
*
* @return void
*/
protected static function boot(): void
protected $appends = [
'subscribed',
'email_update_pending'
];
public static function boot()
{
parent::boot();
$clearCache = function ($user) {
Cache::forget(
"user:{$user->id}",
"user:{$user->id}:permissions"
);
};
static::updating(function ($user) {
if ($user->isDirty('email')) {
EmailSubscriptions::where('email', $user->getOriginal('email'))->update(['email' => $user->email]);
static::saving($clearCache);
static::deleting($clearCache);
}
// remove duplicate email subscriptions, favoring those with confirmed dates
$subscriptions = EmailSubscriptions::where('email', $user->email)->orderBy('created_at', 'asc')->get();
$confirmed = EmailSubscriptions::where('email', $user->email)->whereNotNull('confirmed')->orderBy('confirmed', 'asc')->first();
if($subscriptions->count() > 1) {
// if there is a confirmed, then delete all the others
if($confirmed) {
$subscriptions->each(function($subscription) use ($confirmed) {
if($subscription->id !== $confirmed->id) {
$subscription->delete();
}
});
} else {
// if there is no confirmed, then delete all but the most recent
$subscriptions->each(function($subscription) use ($subscriptions) {
if($subscription->id !== $subscriptions->last()->id) {
$subscription->delete();
}
});
}
}
}
});
/**
* Get the list of permissions of the user
*
* @return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function permissions(): HasMany
{
return $this->hasMany(Permission::class);
}
/**
* Get the permission attribute
*
* @return array
*/
public function getPermissionsAttribute(): array
{
$cacheKey = "user:{$this->id}:permissions";
return Cache::remember($cacheKey, now()->addDays(28), function () {
return $this->permissions()->pluck('permission')->toArray();
static::deleting(function ($user) {
EmailSubscriptions::where('email', $user->email)->delete();
});
}
/**
* Set the permission attribute
*
* @param array $newPermissions The new permissions to set to the user.
* @return void
*/
public function setPermissionsAttribute(array $newPermissions): void
public function createLoginToken($redirect = null)
{
$existingPermissions = $this->permissions->pluck('permission')->toArray();
// Generate a unique token
$token = Str::random(60);
$this->revokePermission(array_diff($this->permissions, $newPermissions));
$this->givePermission(array_diff($newPermissions, $this->permissions));
// Store the token in the database
DB::table('login_tokens')->insert([
'email' => $this->email,
'token' => $token,
'intended_url' => $redirect,
]);
$cacheKey = "user:{$this->id}:permissions";
Cache::delete($cacheKey);
return $token;
}
/**
* Test if user has permission
*
* @param string $permission Permission to test.
* @return boolean
*/
public function hasPermission(string $permission): bool
public function softDelete()
{
return in_array($permission, $this->permissions);
}
/**
* Give permissions to the user
*
* @param string|array $permissions The permission(s) to give.
* @return Illuminate\Database\Eloquent\Collection
*/
public function givePermission($permissions): Collection
{
if (is_array($permissions) === false) {
$permissions = [$permissions];
foreach ($this->fillable as $field) {
if ($field === 'email_verified_at') {
$this->email_verified_at = null;
} else if ($field !== 'email') {
$this->{$field} = '';
}
}
$newPermissions = array_map(function ($permission) {
return ['permission' => $permission];
}, array_diff($permissions, $this->permissions));
$cacheKey = "user:{$this->id}:permissions";
Cache::forget($cacheKey);
return $this->permissions()->createMany($newPermissions);
$this->save();
}
/**
* Revoke permissions from the user
*
* @param string|array $permissions The permission(s) to revoke.
* @return integer
*/
public function revokePermission($permissions): int
public function getName(): string
{
if (is_array($permissions) === false) {
$permissions = [$permissions];
$name = '';
if($this->firstname || $this->surname) {
$name = implode(' ', [$this->firstname, $this->surname]);
} else {
$name = substr($this->email, 0, strpos($this->email, '@'));
}
$cacheKey = "user:{$this->id}:permissions";
Cache::forget($cacheKey);
return $this->permissions()
->whereIn('permission', $permissions)
->delete();
return $name;
}
/**
* Get the list of files of the user
*
* @return HasMany
*/
public function media(): HasMany
public function tickets()
{
return $this->hasMany(Media::class);
return $this->hasMany(Ticket::class);
}
/**
* Get the list of files of the user
*
* @return HasMany
*/
public function articles(): HasMany
public function getSubscribedAttribute()
{
return $this->hasMany(Article::class);
return EmailSubscriptions::where('email', $this->email)
->whereNotNull('confirmed')
->exists();
}
/**
* Get associated user codes
*
* @return HasMany
*/
public function codes(): HasMany
public function setSubscribedAttribute($value)
{
return $this->hasMany(UserCode::class);
if ($value) {
$subscription = EmailSubscriptions::where('email', $this->email)->first();
if ($subscription) {
if($subscription->confirmed === null) {
$subscription->update(['confirmed' => now()]);
$subscription->save();
}
} else {
EmailSubscriptions::Create([
'email' => $this->email,
'confirmed' => now()
]);
}
} else {
EmailSubscriptions::where('email', $this->email)->delete();
}
}
/**
* Get the list of logins of the user
*
* @return HasMany
*/
public function logins(): HasMany
public function emailUpdate()
{
return $this->hasMany(UserLogins::class);
return $this->hasOne(EmailUpdate::class);
}
/**
* Get the events associated with the user.
*
* @return BelongsToMany
*/
public function events(): BelongsToMany
public function getEmailUpdatePendingAttribute()
{
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id');
return $this->emailUpdate()->exists();
}
}

View File

@@ -1,83 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserCode extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'action',
'user_id',
'data',
];
/**
* Boot function from Laravel.
*
* @return void
*/
protected static function boot(): void
{
parent::boot();
static::creating(function ($model) {
UserCode::clearExpired();
if (empty($model->{'code'}) === true) {
while (true) {
$code = random_int(100000, 999999);
if (UserCode::where('code', $code)->count() === 0) {
$model->{'code'} = $code;
break;
}
}
}
});
}
/**
* Generate new code
*
* @return void
*/
public function regenerate(): void
{
while (true) {
$code = random_int(100000, 999999);
if (UserCode::where('code', $code)->count() === 0) {
$this->code = $code;
break;
}
}
}
/**
* Clear expired user codes
*
* @return void
*/
public static function clearExpired(): void
{
UserCode::where('updated_at', '<=', now()->subDays(5))->delete();
}
/**
* Get associated user
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View File

@@ -1,39 +0,0 @@
<?php
namespace App\Models;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserLogins extends Model
{
use HasFactory;
use Uuids;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'token',
'login',
'logout',
'ip_address',
'user_agent',
];
/**
* Get the file user
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

52
app/Models/Workshop.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
namespace App\Models;
use App\Traits\HasFiles;
use App\Traits\Slug;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Workshop extends Model
{
use HasFactory, Slug, HasFiles;
protected $fillable = [
'title',
'content',
'starts_at',
'ends_at',
'publish_at',
'closes_at',
'status',
'price',
'ages',
'registration',
'registration_data',
'location_id',
'user_id',
'hero_media_name'
];
protected $casts = [
'starts_at' => 'datetime',
'ends_at' => 'datetime',
'publish_at' => 'datetime',
'closes_at' => 'datetime',
];
public function author()
{
return $this->belongsTo(User::class, 'user_id');
}
public function hero()
{
return $this->belongsTo(Media::class, 'hero_media_name');
}
public function location()
{
return $this->belongsTo(Location::class, 'location_id');
}
}