Skip to content

Commit ecc302f

Browse files
authored
Merge pull request #58 from balajidharma/2.x-Changes
Added media manager
2 parents a691b2c + fefe453 commit ecc302f

File tree

12 files changed

+736
-13
lines changed

12 files changed

+736
-13
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use BalajiDharma\LaravelAdminCore\Actions\Media\MediaCreateAction;
7+
use BalajiDharma\LaravelAdminCore\Actions\Media\MediaUpdateAction;
8+
use BalajiDharma\LaravelAdminCore\Data\Media\MediaCreateData;
9+
use BalajiDharma\LaravelAdminCore\Data\Media\MediaUpdateData;
10+
use BalajiDharma\LaravelAdminCore\Data\Media\MediaData;
11+
use Plank\Mediable\Media;
12+
use Illuminate\Support\Facades\Auth;
13+
use Inertia\Inertia;
14+
15+
class MediaController extends Controller
16+
{
17+
public function __construct()
18+
{
19+
$this->middleware('can:media list', ['only' => ['index']]);
20+
$this->middleware('can:media create', ['only' => ['create', 'store']]);
21+
$this->middleware('can:media edit', ['only' => ['edit', 'update']]);
22+
$this->middleware('can:media delete', ['only' => ['destroy']]);
23+
}
24+
25+
/**
26+
* Display a listing of the resource.
27+
*
28+
* @return \Inertia\Response
29+
*/
30+
public function index()
31+
{
32+
$mediaItems = (new Media)->newQuery();
33+
$mediaItems->whereIsOriginal();
34+
if (request()->has('search')) {
35+
$mediaItems->where('filename', 'Like', '%'.request()->input('search').'%');
36+
}
37+
38+
if (request()->query('sort')) {
39+
$attribute = request()->query('sort');
40+
$sort_order = 'ASC';
41+
if (strncmp($attribute, '-', 1) === 0) {
42+
$sort_order = 'DESC';
43+
$attribute = substr($attribute, 1);
44+
}
45+
$mediaItems->orderBy($attribute, $sort_order);
46+
} else {
47+
$mediaItems->latest();
48+
}
49+
50+
$mediaItems = $mediaItems->paginate(config('admin.paginate.per_page'))
51+
->onEachSide(config('admin.paginate.each_side'));
52+
53+
54+
return Inertia::render('Admin/Media/Index', [
55+
'items' => MediaData::collect($mediaItems),
56+
'filters' => request()->all('search'),
57+
'can' => [
58+
'create' => Auth::user()->can('media create'),
59+
'edit' => Auth::user()->can('media edit'),
60+
'delete' => Auth::user()->can('media delete'),
61+
],
62+
]);
63+
}
64+
65+
/**
66+
* Show the form for creating a new resource.
67+
*
68+
* @return \Inertia\Response
69+
*/
70+
public function create()
71+
{
72+
$typeOptions = media_type_as_options();
73+
return Inertia::render('Admin/Media/Create', [
74+
'typeOptions' => $typeOptions,
75+
]);
76+
}
77+
78+
/**
79+
* Store a newly created resource in storage.
80+
*
81+
* @return \Illuminate\Http\RedirectResponse
82+
*/
83+
public function store(MediaCreateData $data, MediaCreateAction $mediaCreateAction)
84+
{
85+
$mediaCreateAction->handle($data);
86+
87+
return redirect()->route('admin.media.index')
88+
->with('message', __('Media created successfully.'));
89+
}
90+
91+
/**
92+
* Display the specified resource.
93+
*
94+
* @return \Inertia\Response
95+
*/
96+
public function show($id)
97+
{
98+
$media = Media::findOrFail($id);
99+
100+
return Inertia::render('Admin/Media/Show', [
101+
'media' => MediaData::from($media),
102+
]);
103+
}
104+
105+
/**
106+
* Show the form for editing the specified resource.
107+
*
108+
* @return \Inertia\Response
109+
*/
110+
public function edit($id)
111+
{
112+
$media = Media::findOrFail($id);
113+
$typeOptions = media_type_as_options();
114+
115+
return Inertia::render('Admin/Media/Edit', [
116+
'media' => MediaData::from($media),
117+
'typeOptions' => $typeOptions,
118+
]);
119+
}
120+
121+
122+
/**
123+
* Update the specified resource in storage.
124+
*
125+
* @return \Illuminate\Http\RedirectResponse
126+
*/
127+
public function update(MediaUpdateData $mediaUpdateData, $id, MediaUpdateAction $mediaUpdateAction)
128+
{
129+
$media = Media::findOrFail($id);
130+
$mediaUpdateAction->handle($mediaUpdateData, $media);
131+
132+
return redirect()->route('admin.media.index')
133+
->with('message', __('Media updated successfully.'));
134+
}
135+
136+
/**
137+
* Remove the specified resource from storage.
138+
*
139+
* @return \Illuminate\Http\RedirectResponse
140+
*/
141+
public function destroy($id)
142+
{
143+
$media = Media::findOrFail($id);
144+
$media->getAllVariantsAndSelf()->each(function (Media $variant) {
145+
$variant->delete();
146+
});
147+
148+
return redirect()->route('admin.media.index')
149+
->with('message', __('Media deleted successfully.'));
150+
}
151+
}

resources/js/Pages/Admin/Category/Item/Create.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import { Head, Link, useForm } from "@inertiajs/vue3"
33
import {
4-
mdiMenu,
4+
mdiSelectGroup,
55
mdiArrowLeftBoldOutline
66
} from "@mdi/js"
77
import LayoutAuthenticated from "@/Layouts/LayoutAuthenticated.vue"
@@ -40,7 +40,7 @@ const form = useForm({
4040
<Head title="Create Category" />
4141
<SectionMain>
4242
<SectionTitleLineWithButton
43-
:icon="mdiMenu"
43+
:icon="mdiSelectGroup"
4444
title="Add Category"
4545
main
4646
>

resources/js/Pages/Admin/Category/Item/Edit.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import { Head, useForm } from "@inertiajs/vue3"
33
import {
4-
mdiMenu,
4+
mdiSelectGroup,
55
mdiArrowLeftBoldOutline
66
} from "@mdi/js"
77
import LayoutAuthenticated from "@/Layouts/LayoutAuthenticated.vue"
@@ -45,7 +45,7 @@ const form = useForm({
4545
<Head title="Update Category" />
4646
<SectionMain>
4747
<SectionTitleLineWithButton
48-
:icon="mdiMenu"
48+
:icon="mdiSelectGroup"
4949
title="Update Category"
5050
main
5151
>

resources/js/Pages/Admin/Category/Item/Index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import { Head } from "@inertiajs/vue3"
33
import {
4-
mdiLink,
4+
mdiSelectGroup,
55
mdiPlus,
66
mdiAlertBoxOutline,
77
mdiArrowLeftBoldOutline
@@ -37,7 +37,7 @@ const props = defineProps({
3737
<Head title="Categories" />
3838
<SectionMain>
3939
<SectionTitleLineWithButton
40-
:icon="mdiLink"
40+
:icon="mdiSelectGroup"
4141
title="Categories"
4242
main
4343
>

resources/js/Pages/Admin/Category/Type/Create.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import { Head, Link, useForm } from "@inertiajs/vue3"
33
import {
4-
mdiMenu,
4+
mdiSelectGroup,
55
mdiArrowLeftBoldOutline
66
} from "@mdi/js"
77
import LayoutAuthenticated from "@/Layouts/LayoutAuthenticated.vue"
@@ -17,7 +17,6 @@ const form = useForm({
1717
name: '',
1818
machine_name: '',
1919
description: ''
20-
2120
})
2221
</script>
2322

@@ -26,7 +25,7 @@ const form = useForm({
2625
<Head title="Create Category Type" />
2726
<SectionMain>
2827
<SectionTitleLineWithButton
29-
:icon="mdiMenu"
28+
:icon="mdiSelectGroup"
3029
title="Add Category Type"
3130
main
3231
>

resources/js/Pages/Admin/Category/Type/Edit.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import { Head, Link, useForm } from "@inertiajs/vue3"
33
import {
4-
mdiMenu,
4+
mdiSelectGroup,
55
mdiArrowLeftBoldOutline
66
} from "@mdi/js"
77
import LayoutAuthenticated from "@/Layouts/LayoutAuthenticated.vue"
@@ -32,7 +32,7 @@ const form = useForm({
3232
<Head title="Update Category Type" />
3333
<SectionMain>
3434
<SectionTitleLineWithButton
35-
:icon="mdiMenu"
35+
:icon="mdiSelectGroup"
3636
title="Update Category Type"
3737
main
3838
>

resources/js/Pages/Admin/Category/Type/Index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup>
22
import { Head, Link, useForm } from "@inertiajs/vue3"
33
import {
4-
mdiMenu,
4+
mdiSelectGroup,
55
mdiPlus,
66
mdiCogOutline,
77
mdiSquareEditOutline,
@@ -51,7 +51,7 @@ function destroy(id) {
5151
<Head title="category Types" />
5252
<SectionMain>
5353
<SectionTitleLineWithButton
54-
:icon="mdiMenu"
54+
:icon="mdiSelectGroup"
5555
title="Category Types"
5656
main
5757
>

0 commit comments

Comments
 (0)