Skip to content

Commit

Permalink
Merge pull request #6 from queents/dev
Browse files Browse the repository at this point in the history
update media collection and media lib type
  • Loading branch information
3x1io authored Oct 27, 2022
2 parents 3d92a70 + 870edbc commit 54b3b9c
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 35 deletions.
20 changes: 20 additions & 0 deletions Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Controllers\Controller;
use Modules\Base\Services\Components\Base\Alert;
use Modules\Base\Services\Components\Base\Render;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class DashboardController extends Controller
{
Expand All @@ -21,6 +22,25 @@ public function logout()
return redirect()->route('login');
}

public function getMedia(){
$media = Media::all()->groupBy('model_type');
$mediaCollection = [];
foreach ($media as $key=>$value){
$getKey = collect(explode("\\",$key))->last();
$mediaCollection[$getKey] = $value->toArray();
}

return response()->json([
"data" =>$mediaCollection
]);
}

public function destroy(Media $media){
$media->delete();

return Alert::make(__('Your Media Item Has Been deleted'))->fire();
}

public function select(Request $request){
$request->validate([
"model_type" => "required",
Expand Down
3 changes: 3 additions & 0 deletions Routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@
Route::post('/select', [DashboardController::class, 'select'])->name('select');
Route::post('/create', [DashboardController::class, 'create'])->name('create');
Route::post('logout', [DashboardController::class, 'logout'])->name('logout');
Route::post('media/delete/{media}', [DashboardController::class, 'destroy'])->name('media.destroy');
Route::get('media/get-media', [DashboardController::class, 'getMedia'])->name('media.api');
});

8 changes: 0 additions & 8 deletions Services/Components/Base/Page.php

This file was deleted.

22 changes: 22 additions & 0 deletions Services/Concerns/HasCol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Modules\Base\Services\Concerns;

trait HasCol
{
/**
* @var int
* @example 12
*/
public int $col = 12;

/**
* @param int $col
* @return $this
*/
public function col(int $col): static
{
$this->col = $col;
return $this;
}
}
25 changes: 25 additions & 0 deletions Services/Concerns/HasSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Modules\Base\Services\Concerns;

trait HasSection
{
/**
* @var ?string
* @example 'groupId'
*/
public ?string $section = null;
public ?string $sectionLabel = null;

/**
* @param string $section
* @param ?string $sectionLabel
* @return $this
*/
public function section(string $section, ?string $sectionLabel): static
{
$this->section = $section;
$this->sectionLabel = $sectionLabel;
return $this;
}
}
62 changes: 42 additions & 20 deletions Services/Resource/Concerns/Process/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ public function processMediaOnCreate(Request $request, $record): void
{
foreach ($this->rows() as $field) {
if($field->vue === 'ViltMedia.vue') {
if ($request->{$field->name} && is_array($request->{$field->name})) {
foreach ($request->{$field->name} as $item) {
$record->addMedia($item)
if($field->type === 'lib' && is_string($request->{$field->name})){
if($request->{$field->name}){
$record->addMediaFromUrl($request->{$field->name})
->preservingOriginal()
->toMediaCollection($field->name);
}
}
else {
if ($request->{$field->name} && is_array($request->{$field->name})) {
foreach ($request->{$field->name} as $item) {
$record->addMedia($item)
->preservingOriginal()
->toMediaCollection($field->name);
}
}
}
}
}
}
Expand All @@ -27,27 +36,40 @@ public function processMediaOnUpdate(Request $request, $record): void
{
foreach ($this->rows() as $field) {
if (($field->vue === 'ViltMedia.vue')) {
if ($request->{$field->name} && is_array($request->{$field->name})) {
$record->clearMediaCollection($field->name);
foreach ($request->{$field->name} as $key=>$item) {
if(!is_string($item)){
if($item->getClientOriginalName() === 'blob'){
$record->addMedia($item)
->usingFileName(strtolower(Str::random(10).'_'.$key.'.'.$item->extension()))
->preservingOriginal()
->toMediaCollection($field->name);
}
else {
$record->addMedia($item)
->preservingOriginal()
->toMediaCollection($field->name);
}
if($field->type === 'lib' && is_string($request->{$field->name})){
if($request->{$field->name}){
if(!$record->getMedia($field->name)->first() || $record->getMedia($field->name)->first()->getUrl() !== $request->{$field->name}){
$record->clearMediaCollection($field->name);
$record->addMediaFromUrl($request->{$field->name})
->preservingOriginal()
->toMediaCollection($field->name);
}
}
}
else if(empty($request->get($field->name))){
$record->clearMediaCollection($field->name);
else {
if ($request->{$field->name} && is_array($request->{$field->name})) {
$record->clearMediaCollection($field->name);
foreach ($request->{$field->name} as $key=>$item) {
if(!is_string($item)){
if($item->getClientOriginalName() === 'blob'){
$record->addMedia($item)
->usingFileName(strtolower(Str::random(10).'_'.$key.'.'.$item->extension()))
->preservingOriginal()
->toMediaCollection($field->name);
}
else {
$record->addMedia($item)
->preservingOriginal()
->toMediaCollection($field->name);
}
}
}
}
else if(empty($request->get($field->name))){
$record->clearMediaCollection($field->name);
}
}

}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Services/Rows/Abstracts/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Modules\Base\Services\Concerns\HasAdd;
use Modules\Base\Services\Concerns\HasApi;
use Modules\Base\Services\Concerns\HasCol;
use Modules\Base\Services\Concerns\HasColor;
use Modules\Base\Services\Concerns\HasDefault;
use Modules\Base\Services\Concerns\HasDescription;
use Modules\Base\Services\Concerns\HasHint;
use Modules\Base\Services\Concerns\HasIcon;
use Modules\Base\Services\Concerns\HasId;
use Modules\Base\Services\Concerns\HasLabel;
use Modules\Base\Services\Concerns\HasMax;
Expand All @@ -23,6 +25,7 @@
use Modules\Base\Services\Concerns\HasRelation;
use Modules\Base\Services\Concerns\HasRoles;
use Modules\Base\Services\Concerns\HasRows;
use Modules\Base\Services\Concerns\HasSection;
use Modules\Base\Services\Concerns\HasTab;
use Modules\Base\Services\Concerns\HasTrack;
use Modules\Base\Services\Concerns\HasType;
Expand Down Expand Up @@ -70,6 +73,9 @@ abstract class Base implements Component
use HasValidation;
use HasRows;
use HasAdd;
use HasSection;
use HasCol;
use HasIcon;
use IsUrl;
use IsMulti;
use IsOver;
Expand Down
20 changes: 20 additions & 0 deletions Services/Rows/Section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Modules\Base\Services\Rows;

use Modules\Base\Services\Rows\Abstracts\Base;

class Section extends Base
{

public string $vue = 'ViltSection.vue';

/**
* @param string $name
* @return static
*/
public static function make(string $name): self
{
return (new self)->name($name)->over()->list(false);
}
}
7 changes: 0 additions & 7 deletions Services/Widget.php

This file was deleted.

0 comments on commit 54b3b9c

Please sign in to comment.