add nova
This commit is contained in:
39
nova/src/Fields/Attachments/Attachment.php
Normal file
39
nova/src/Fields/Attachments/Attachment.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* @property string $attachment
|
||||
* @property string $disk
|
||||
*/
|
||||
class Attachment extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'nova_field_attachments';
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* Purge the attachment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function purge()
|
||||
{
|
||||
Storage::disk($this->disk)->delete($this->attachment);
|
||||
|
||||
$this->delete();
|
||||
}
|
||||
}
|
||||
52
nova/src/Fields/Attachments/DeleteAttachments.php
Normal file
52
nova/src/Fields/Attachments/DeleteAttachments.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DeleteAttachments
|
||||
{
|
||||
/**
|
||||
* The field instance.
|
||||
*
|
||||
* @var \Laravel\Nova\Fields\Field&\Laravel\Nova\Contracts\Storable
|
||||
*/
|
||||
public $field;
|
||||
|
||||
/**
|
||||
* The attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\Attachment>
|
||||
*/
|
||||
public static $model = Attachment::class;
|
||||
|
||||
/**
|
||||
* Create a new class instance.
|
||||
*
|
||||
* @param \Laravel\Nova\Fields\Field&\Laravel\Nova\Contracts\Storable $field
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($field)
|
||||
{
|
||||
$this->field = $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the attachments associated with the field.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param mixed $model
|
||||
* @return array
|
||||
*/
|
||||
public function __invoke(Request $request, $model)
|
||||
{
|
||||
static::$model::query()
|
||||
->where('attachable_type', $model->getMorphClass())
|
||||
->where('attachable_id', $model->getKey())
|
||||
->get()
|
||||
->each
|
||||
->purge();
|
||||
|
||||
return [$this->field->attribute => ''];
|
||||
}
|
||||
}
|
||||
18
nova/src/Fields/Attachments/DetachAnyAttachment.php
Normal file
18
nova/src/Fields/Attachments/DetachAnyAttachment.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DetachAnyAttachment
|
||||
{
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
call_user_func(new DetachAttachment, $request);
|
||||
call_user_func(new DetachPendingAttachment, $request);
|
||||
}
|
||||
}
|
||||
29
nova/src/Fields/Attachments/DetachAttachment.php
Normal file
29
nova/src/Fields/Attachments/DetachAttachment.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
|
||||
class DetachAttachment
|
||||
{
|
||||
/**
|
||||
* The attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\Attachment>
|
||||
*/
|
||||
public static $model = Attachment::class;
|
||||
|
||||
/**
|
||||
* Delete an attachment from the field.
|
||||
*
|
||||
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(NovaRequest $request)
|
||||
{
|
||||
static::$model::where('url', $request->attachmentUrl)
|
||||
->get()
|
||||
->each
|
||||
->purge();
|
||||
}
|
||||
}
|
||||
31
nova/src/Fields/Attachments/DetachPendingAttachment.php
Normal file
31
nova/src/Fields/Attachments/DetachPendingAttachment.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DetachPendingAttachment
|
||||
{
|
||||
/**
|
||||
* The pending attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\PendingAttachment>
|
||||
*/
|
||||
public static $model = PendingAttachment::class;
|
||||
|
||||
/**
|
||||
* Delete an attachment from the field.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
static::$model::where('draft_id', $request->draftId)
|
||||
->when($request->has('attachment'), function ($query) use ($request) {
|
||||
return $query->where('attachment', $request->attachment);
|
||||
})->get()
|
||||
->each
|
||||
->purge();
|
||||
}
|
||||
}
|
||||
29
nova/src/Fields/Attachments/DiscardPendingAttachments.php
Normal file
29
nova/src/Fields/Attachments/DiscardPendingAttachments.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DiscardPendingAttachments
|
||||
{
|
||||
/**
|
||||
* The pending attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\PendingAttachment>
|
||||
*/
|
||||
public static $model = PendingAttachment::class;
|
||||
|
||||
/**
|
||||
* Discard pending attachments on the field.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
static::$model::where('draft_id', $request->draftId)
|
||||
->get()
|
||||
->each
|
||||
->purge();
|
||||
}
|
||||
}
|
||||
117
nova/src/Fields/Attachments/PendingAttachment.php
Normal file
117
nova/src/Fields/Attachments/PendingAttachment.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Prunable;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Laravel\Nova\Contracts\Storable;
|
||||
|
||||
/**
|
||||
* @property string $attachment
|
||||
* @property string $disk
|
||||
*/
|
||||
class PendingAttachment extends Model
|
||||
{
|
||||
use Prunable;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'nova_pending_field_attachments';
|
||||
|
||||
/**
|
||||
* The attributes that aren't mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* The persist attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\Attachment>
|
||||
*/
|
||||
protected static $persistModel = Attachment::class;
|
||||
|
||||
/**
|
||||
* Get persist model instance.
|
||||
*
|
||||
* @return \Laravel\Nova\Fields\Attachments\Attachment
|
||||
*/
|
||||
public function getPersistModel()
|
||||
{
|
||||
return new static::$persistModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the given draft's pending attachments.
|
||||
*
|
||||
* @param string $draftId
|
||||
* @param \Laravel\Nova\Contracts\Storable $field
|
||||
* @param mixed $model
|
||||
* @return void
|
||||
*
|
||||
* @phpstan-param \Laravel\Nova\Fields\Field&\Laravel\Nova\Contracts\Storable $field
|
||||
*/
|
||||
public static function persistDraft($draftId, Storable $field, $model)
|
||||
{
|
||||
static::where('draft_id', $draftId)->get()->each->persist($field, $model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the pending attachment.
|
||||
*
|
||||
* @param \Laravel\Nova\Contracts\Storable $field
|
||||
* @param mixed $model
|
||||
* @return void
|
||||
*
|
||||
* @phpstan-param \Laravel\Nova\Fields\Field&\Laravel\Nova\Contracts\Storable $field
|
||||
*/
|
||||
public function persist(Storable $field, $model)
|
||||
{
|
||||
$disk = $field->getStorageDisk() ?? $field->getDefaultStorageDisk();
|
||||
|
||||
static::$persistModel::create([
|
||||
'attachable_type' => $model->getMorphClass(),
|
||||
'attachable_id' => $model->getKey(),
|
||||
'attachment' => $this->attachment,
|
||||
'disk' => $disk,
|
||||
'url' => Storage::disk($disk)->url($this->attachment),
|
||||
]);
|
||||
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the prunable model query.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Builder
|
||||
*/
|
||||
public function prunable()
|
||||
{
|
||||
return static::where('created_at', '<=', now()->subDays(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the model for pruning.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function pruning()
|
||||
{
|
||||
Storage::disk($this->disk)->delete($this->attachment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge the attachment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function purge()
|
||||
{
|
||||
$this->prune();
|
||||
}
|
||||
}
|
||||
28
nova/src/Fields/Attachments/PruneStaleAttachments.php
Normal file
28
nova/src/Fields/Attachments/PruneStaleAttachments.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
class PruneStaleAttachments
|
||||
{
|
||||
/**
|
||||
* The pending attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\PendingAttachment>
|
||||
*/
|
||||
public static $model = PendingAttachment::class;
|
||||
|
||||
/**
|
||||
* Prune the stale attachments from the system.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke()
|
||||
{
|
||||
Artisan::call('model:prune', [
|
||||
'--model' => static::$model,
|
||||
'--chunk' => 100,
|
||||
]);
|
||||
}
|
||||
}
|
||||
63
nova/src/Fields/Attachments/StorePendingAttachment.php
Normal file
63
nova/src/Fields/Attachments/StorePendingAttachment.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Laravel\Nova\Fields\Attachments;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Laravel\Nova\Contracts\Storable;
|
||||
|
||||
class StorePendingAttachment
|
||||
{
|
||||
/**
|
||||
* The field instance.
|
||||
*
|
||||
* @var \Laravel\Nova\Fields\Field&\Laravel\Nova\Contracts\Storable
|
||||
*/
|
||||
public $field;
|
||||
|
||||
/**
|
||||
* The pending attachment model.
|
||||
*
|
||||
* @var class-string<\Laravel\Nova\Fields\Attachments\PendingAttachment>
|
||||
*/
|
||||
public static $model = PendingAttachment::class;
|
||||
|
||||
/**
|
||||
* Create a new invokable instance.
|
||||
*
|
||||
* @param \Laravel\Nova\Contracts\Storable $field
|
||||
* @return void
|
||||
*
|
||||
* @phpstan-param \Laravel\Nova\Fields\Field&\Laravel\Nova\Contracts\Storable $field
|
||||
*/
|
||||
public function __construct(Storable $field)
|
||||
{
|
||||
$this->field = $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a pending attachment to the field.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array{path: string, url: string}
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'attachment' => ['required', 'file'],
|
||||
]);
|
||||
|
||||
$disk = $this->field->getStorageDisk() ?? $this->field->getDefaultStorageDisk();
|
||||
|
||||
static::$model::create([
|
||||
'draft_id' => $request->draftId,
|
||||
'attachment' => $path = $request->file('attachment')->store($this->field->getStorageDir(), $disk),
|
||||
'disk' => $disk,
|
||||
]);
|
||||
|
||||
return [
|
||||
'path' => $path,
|
||||
'url' => Storage::disk($disk)->url($path),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user