Skip to content

Commit 71f9497

Browse files
committed
Finish readme
1 parent 6b17798 commit 71f9497

File tree

2 files changed

+195
-1
lines changed

2 files changed

+195
-1
lines changed

README.md

+194
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,200 @@ Image album edition page example looks like this:
467467

468468
![MFU db](https://github.com/itstructure/laravel-media-file-uploader/blob/dev/mfu_db.png)
469469

470+
#### 5.3.2 Short architecture structure and request way for uploading process in simple words
471+
472+
1. Call `UploadController` method.
473+
2. Call static method from `Itstructure\MFU\Facades\Uploader` facade in controller method.
474+
3. Get instance of Uploader service `Itstructure\MFU\Services\Uploader::getInstance($config)` and call here a facade's method.
475+
4. Get instance of needed processor, with set config data from service to this:
476+
477+
`Itstructure\MFU\Processors\UploadProcessor` or
478+
479+
`Itstructure\MFU\Processors\UpdateProcessor` or
480+
481+
`Itstructure\MFU\Processors\DeleteProcessor`.
482+
483+
5. Set process parameters and then call it's `run()` method.
484+
485+
See inside core.
486+
487+
#### 5.3.3 Link Media files with parent owner
488+
489+
Shortly, without extra words.
490+
491+
For example you use `Product` eloquent model, which contains **albums** and **media files** both.
492+
493+
Albums and media files can be linked with Product, after Product is saved, through `owners_albums` and `owners_mediafiles` DB relations.
494+
495+
This relations are set by `BehaviorMediafile` and `BehaviorAlbum` classes automatically.
496+
497+
```php
498+
499+
namespace App\Models;
500+
501+
use Illuminate\Database\Eloquent\Model;
502+
use Itstructure\MFU\Interfaces\BeingOwnerInterface;
503+
use Itstructure\MFU\Behaviors\Owner\{BehaviorMediafile, BehaviorAlbum};
504+
use Itstructure\MFU\Processors\SaveProcessor;
505+
use Itstructure\MFU\Models\Albums\AlbumTyped;
506+
use Itstructure\MFU\Traits\{OwnerBehavior, Thumbnailable};
507+
508+
class Product extends Model implements BeingOwnerInterface
509+
{
510+
use Thumbnailable, OwnerBehavior;
511+
512+
protected $table = 'products';
513+
514+
protected $fillable = ['title', 'alias', 'description', 'price', 'category_id'];
515+
516+
public function getItsName(): string
517+
{
518+
return $this->getTable();
519+
}
520+
521+
public function getPrimaryKey()
522+
{
523+
return $this->getKey();
524+
}
525+
526+
public static function getBehaviorMadiafileAttributes(): array
527+
{
528+
return [SaveProcessor::FILE_TYPE_THUMB, SaveProcessor::FILE_TYPE_IMAGE];
529+
}
530+
531+
public static function getBehaviorAlbumAttributes(): array
532+
{
533+
return [AlbumTyped::ALBUM_TYPE_IMAGE];
534+
}
535+
536+
public static function getBehaviorAttributes(): array
537+
{
538+
return array_merge(static::getBehaviorMadiafileAttributes(), static::getBehaviorAlbumAttributes());
539+
}
540+
541+
protected static function booted(): void
542+
{
543+
$behaviorMediafile = BehaviorMediafile::getInstance(static::getBehaviorMadiafileAttributes());
544+
$behaviorAlbum = BehaviorAlbum::getInstance(static::getBehaviorAlbumAttributes());
545+
546+
static::saved(function (Model $ownerModel) use ($behaviorMediafile, $behaviorAlbum) {
547+
if ($ownerModel->wasRecentlyCreated) {
548+
$behaviorMediafile->link($ownerModel);
549+
$behaviorAlbum->link($ownerModel);
550+
} else {
551+
$behaviorMediafile->refresh($ownerModel);
552+
$behaviorAlbum->refresh($ownerModel);
553+
}
554+
});
555+
556+
static::deleted(function (Model $ownerModel) use ($behaviorMediafile, $behaviorAlbum) {
557+
$behaviorMediafile->clear($ownerModel);
558+
$behaviorAlbum->clear($ownerModel);
559+
});
560+
}
561+
}
562+
```
563+
564+
The main rules:
565+
566+
- It is very important to be implemented from `BeingOwnerInterface`!
567+
568+
- It is very important to use `OwnerBehavior` trait. Some required BASE methods by `BeingOwnerInterface` are already existing in this trait.
569+
570+
- It is very important to make the next methods: `getItsName()`, `getPrimaryKey()`.
571+
572+
- It is very important to add method `booted()` with behaviour instances.
573+
574+
- It is very important to set **behavior attributes**!
575+
576+
See deeper in to core and imagine how it works :-)
577+
578+
Go next...
579+
580+
It is very important to use MFU partials correctly in your application blade forms!
581+
582+
Short cut example for the blade form:
583+
584+
```blade
585+
<form action="{{ route('admin_product_store') }}" method="post">
586+
587+
<div class="row">
588+
<div class="col-12 col-sm-10 col-md-8 col-lg-6 col-xl-4">
589+
@include('uploader::partials.thumbnail', ['model' => $model ?? null, 'ownerParams' => $ownerParams ?? null])
590+
</div>
591+
</div>
592+
593+
<div class="row">
594+
<div class="col-12 col-sm-10 col-md-8 col-lg-6 col-xl-4">
595+
<div class="form-group">
596+
<label for="id_title">Title</label>
597+
<input id="id_title" type="text" class="form-control @if ($errors->has('title')) is-invalid @endif"
598+
name="title" value="{{ old('title', !empty($model) ? $model->title : null) }}" required autofocus>
599+
@if ($errors->has('title'))
600+
<div class="invalid-feedback">
601+
<strong>{{ $errors->first('title') }}</strong>
602+
</div>
603+
@endif
604+
</div>
605+
</div>
606+
</div>
607+
608+
..........
609+
610+
..........
611+
612+
<hr />
613+
<h5>{{ trans('uploader::main.new_files') }}</h5>
614+
<div class="row mb-3">
615+
@include('uploader::partials.new-mediafiles', [
616+
'fileType' => \Itstructure\MFU\Processors\SaveProcessor::FILE_TYPE_IMAGE,
617+
'ownerParams' => $ownerParams ?? null
618+
])
619+
</div>
620+
621+
@if(!empty($edition))
622+
<hr />
623+
<h5>{{ trans('uploader::main.existing_files') }}</h5>
624+
<div class="row mb-3">
625+
@include('uploader::partials.existing-mediafiles', [
626+
'edition' => true,
627+
'fileType' => \Itstructure\MFU\Processors\SaveProcessor::FILE_TYPE_IMAGE,
628+
'ownerParams' => $ownerParams ?? null,
629+
'mediaFiles' => $mediaFiles ?? []
630+
])
631+
</div>
632+
@endif
633+
634+
@if(!empty($allImageAlbums) && !$allImageAlbums->isEmpty())
635+
<hr />
636+
<h5>{{ trans('uploader::main.image_albums') }}</h5>
637+
<div class="row mb-3">
638+
@include('uploader::partials.albums-form-list', [
639+
'albums' => $allImageAlbums,
640+
'edition' => true
641+
])
642+
</div>
643+
@endif
644+
645+
<button class="btn btn-primary" type="submit">Create</button>
646+
<input type="hidden" value="{!! csrf_token() !!}" name="_token">
647+
648+
</form>
649+
```
650+
651+
To clarify:
652+
653+
By `fileType` there will be set a field `image[]`, which will be set by `fill()` method in `Itstructure\MFU\Traits\OwnerBehavior` trait using `getBehaviorAttributes()`,
654+
and then it's value will be put in to the `BehaviorMediafile` object during `booted()` calling after for example `Product` is saved. Then a table `owners_mediafiles` will be filled.
655+
Link between `Product` and `Mediafile` will be created.
656+
657+
658+
To see more, how that example works in global, see real example here: [Laravel Microshop Simple](https://github.com/itstructure/laravel-microshop-simple).
659+
660+
I hope you will be happy with this package. Good luck with your development!
661+
662+
With all respect, Andrey!
663+
470664
## License
471665

472666
Copyright © 2024 Andrey Girnik [email protected].

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"require": {
1616
"php": ">=7.1.0",
17-
"laravel/framework": "^10.0|^11.0",
17+
"laravel/framework": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
1818
"itstructure/laravel-grid-view": "~1.1.1"
1919
},
2020
"autoload": {

0 commit comments

Comments
 (0)