This commit is contained in:
2023-11-27 08:29:48 +10:00
parent c432b32d08
commit 0772010119
195 changed files with 7153 additions and 9787 deletions

View File

@@ -0,0 +1,73 @@
<?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

@@ -0,0 +1,44 @@
<?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

@@ -0,0 +1,43 @@
<?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

@@ -0,0 +1,86 @@
<?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));
}
}

51
app.old/Models/Event.php Normal file
View File

@@ -0,0 +1,51 @@
<?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

@@ -0,0 +1,45 @@
<?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);
}
}

107
app.old/Models/Gallery.php Normal file
View File

@@ -0,0 +1,107 @@
<?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));
}
}

1064
app.old/Models/Media.php Normal file

File diff suppressed because it is too large Load Diff

239
app.old/Models/MediaJob.php Normal file
View File

@@ -0,0 +1,239 @@
<?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

@@ -0,0 +1,35 @@
<?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);
}
}

View File

@@ -0,0 +1,37 @@
<?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',
];
}

View File

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

246
app.old/Models/User.php Normal file
View File

@@ -0,0 +1,246 @@
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Traits\Uuids;
use Illuminate\Database\Eloquent\Collection;
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;
class User extends Authenticatable implements Auditable
{
use HasApiTokens;
use HasFactory;
use Notifiable;
use Uuids;
use \OwenIt\Auditing\Auditable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'first_name',
'last_name',
'email',
'phone',
'password',
'display_name',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
'permissions'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// protected $hidden = [
// 'permissions'
// ];
/**
* The attributes to append.
*
* @var string[]
*/
protected $appends = [
'permissions'
];
/**
* The default attributes.
*
* @var string[]
*/
protected $attributes = [
'phone' => '',
];
/**
* Boot the model.
*
* @return void
*/
protected static function boot(): void
{
parent::boot();
$clearCache = function ($user) {
Cache::forget(
"user:{$user->id}",
"user:{$user->id}:permissions"
);
};
static::saving($clearCache);
static::deleting($clearCache);
}
/**
* 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();
});
}
/**
* Set the permission attribute
*
* @param array $newPermissions The new permissions to set to the user.
* @return void
*/
public function setPermissionsAttribute(array $newPermissions): void
{
$existingPermissions = $this->permissions->pluck('permission')->toArray();
$this->revokePermission(array_diff($this->permissions, $newPermissions));
$this->givePermission(array_diff($newPermissions, $this->permissions));
$cacheKey = "user:{$this->id}:permissions";
Cache::delete($cacheKey);
}
/**
* Test if user has permission
*
* @param string $permission Permission to test.
* @return boolean
*/
public function hasPermission(string $permission): bool
{
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];
}
$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);
}
/**
* Revoke permissions from the user
*
* @param string|array $permissions The permission(s) to revoke.
* @return integer
*/
public function revokePermission($permissions): int
{
if (is_array($permissions) === false) {
$permissions = [$permissions];
}
$cacheKey = "user:{$this->id}:permissions";
Cache::forget($cacheKey);
return $this->permissions()
->whereIn('permission', $permissions)
->delete();
}
/**
* Get the list of files of the user
*
* @return HasMany
*/
public function media(): HasMany
{
return $this->hasMany(Media::class);
}
/**
* Get the list of files of the user
*
* @return HasMany
*/
public function articles(): HasMany
{
return $this->hasMany(Article::class);
}
/**
* Get associated user codes
*
* @return HasMany
*/
public function codes(): HasMany
{
return $this->hasMany(UserCode::class);
}
/**
* Get the list of logins of the user
*
* @return HasMany
*/
public function logins(): HasMany
{
return $this->hasMany(UserLogins::class);
}
/**
* Get the events associated with the user.
*
* @return BelongsToMany
*/
public function events(): BelongsToMany
{
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id');
}
}

View File

@@ -0,0 +1,83 @@
<?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

@@ -0,0 +1,39 @@
<?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);
}
}