Skip to content

Commit 233f18b

Browse files
authored
Merge pull request #85 from balajidharma/2.x-Changes
Added dynamic CRUD
2 parents ddbb433 + 4d1936d commit 233f18b

21 files changed

+956
-318
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace App\Grid\Admin;
4+
5+
use BalajiDharma\LaravelCrud\CrudBuilder;
6+
use BalajiDharma\LaravelCategory\Models\Category;
7+
8+
class CategoryItemGrid extends CrudBuilder
9+
{
10+
public $title = 'Categories';
11+
12+
public $description = 'Manage Categories';
13+
14+
public $model = Category::class;
15+
16+
public $route = 'admin.category.type.item';
17+
18+
public function columns()
19+
{
20+
return [
21+
[
22+
'attribute' => 'name',
23+
'label' => 'Name',
24+
'sortable' => true,
25+
'filter' => 'like',
26+
'searchable' => true,
27+
],
28+
[
29+
'attribute' => 'slug',
30+
'label' => __('Slug'),
31+
'form_options' => function ($model) {
32+
return [
33+
'type' => 'text',
34+
'label' => __('Slug'),
35+
'help_block' => [
36+
'text' => 'The “slug” is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.',
37+
],
38+
];
39+
},
40+
],
41+
[
42+
'attribute' => 'description',
43+
'label' => __('Description'),
44+
],
45+
[
46+
'attribute' => 'enabled',
47+
'label' => 'Enabled',
48+
'form_options' => function ($model) {
49+
return [
50+
'type' => 'checkbox',
51+
'label' => __('Enabled'),
52+
'value' => 1,
53+
'default_value' => 1,
54+
];
55+
},
56+
],
57+
[
58+
'attribute' => 'parent_id',
59+
'label' => __('Parent Item'),
60+
'form_options' => function ($model) {
61+
$item_options = Category::selectOptions($this->addtional['type']->id, null, true);
62+
return [
63+
'type' => 'choice',
64+
'choices' => $item_options,
65+
'label' => __('Parent Item'),
66+
'selected' => $this->model->parent_id ?? null,
67+
'empty_value' => '-ROOT-',
68+
'help_block' => [
69+
'text' => 'The maximum depth for a link and all its children is fixed. Some type links may not be available as parents if selecting them would exceed this limit.',
70+
],
71+
];
72+
},
73+
],
74+
[
75+
'attribute' => 'weight',
76+
'label' => __('Weight'),
77+
'form_options' => function ($model) {
78+
return [
79+
'type' => 'number',
80+
'wrapper' => ['class' => 'form-control py-2 w-40'],
81+
'label' => __('Weight'),
82+
];
83+
},
84+
],
85+
[
86+
'attribute' => 'created_at',
87+
'sortable' => true
88+
],
89+
[
90+
'attribute' => 'updated_at',
91+
'sortable' => true
92+
]
93+
];
94+
}
95+
96+
public function buildRoutes($mainRoute = null)
97+
{
98+
if(!$mainRoute){
99+
$routeName = request()->route()->getName();
100+
$mainRoute = substr($routeName, 0, strrpos($routeName, '.'));
101+
}
102+
103+
return [
104+
'index' => route($mainRoute.'.index', ['type' => $this->addtional['type']->id]),
105+
'create' => route($mainRoute.'.create', ['type' => $this->addtional['type']->id]),
106+
'store' => route($mainRoute.'.store', ['type' => $this->addtional['type']->id]),
107+
'edit' => function ($id) use ($mainRoute) {
108+
return route($mainRoute.'.edit', ['type' => $this->addtional['type']->id, 'item' => $id]);
109+
},
110+
'update' => function ($id) use ($mainRoute) {
111+
return route($mainRoute.'.update', ['type' => $this->addtional['type']->id, 'item' => $id]);
112+
},
113+
'show' => function ($id) use ($mainRoute) {
114+
return route($mainRoute.'.show', ['type' => $this->addtional['type']->id, 'item' => $id]);
115+
},
116+
'destroy' => function ($id) use ($mainRoute) {
117+
return route($mainRoute.'.destroy', ['type' => $this->addtional['type']->id, 'item' => $id]);
118+
},
119+
];
120+
}
121+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace App\Grid\Admin;
4+
5+
use BalajiDharma\LaravelCrud\CrudBuilder;
6+
use BalajiDharma\LaravelCategory\Models\CategoryType;
7+
8+
class CategoryTypeGrid extends CrudBuilder
9+
{
10+
public $title = 'Category Types';
11+
12+
public $description = 'Manage Category Types';
13+
14+
public $model = CategoryType::class;
15+
16+
public $route = 'admin.category.type';
17+
18+
public function columns()
19+
{
20+
return [
21+
[
22+
'attribute' => 'id',
23+
'label' => 'ID',
24+
'sortable' => true,
25+
'searchable' => true,
26+
'form_options' => function($model) {
27+
return [
28+
'hide' => true
29+
];
30+
}
31+
],
32+
[
33+
'attribute' => 'name',
34+
'label' => 'Name',
35+
'sortable' => true,
36+
'filter' => 'like',
37+
'searchable' => true,
38+
],
39+
[
40+
'attribute' => 'machine_name',
41+
'label' => __('Machine name'),
42+
'sortable' => true,
43+
'filter' => 'like',
44+
'searchable' => true,
45+
'form_options' => function($model) {
46+
return [
47+
'label' => __('Machine-readable name'),
48+
'attr' => $model->machine_name ? ['disabled' => 'disabled'] : [],
49+
];
50+
}
51+
],
52+
[
53+
'attribute' => 'description',
54+
'label' => __('Description'),
55+
'list' => false
56+
],
57+
[
58+
'attribute' => 'items',
59+
'label' => '# of Items',
60+
'list' => [
61+
'class' => 'BalajiDharma\LaravelCrud\Column\LinkColumn',
62+
'route' => 'admin.category.type.item.index',
63+
'route_params' => ['type' => 'id'],
64+
'attr' => ['class' => 'link link-primary'],
65+
'value' => function ($model) {
66+
return count($model->categories);
67+
},
68+
]
69+
],
70+
[
71+
'attribute' => 'is_flat',
72+
'label' => __('Use Flat Category'),
73+
'sortable' => true,
74+
'searchable' => true,
75+
'value' => function($model) {
76+
return $model->is_flat ? __('Yes') : __('No');
77+
},
78+
'form_options' => function($model) {
79+
return [
80+
'type' => 'checkbox',
81+
'label' => __('Use Flat Category'),
82+
'value' => 1,
83+
'checked' => $model ? $model->is_flat : false,
84+
];
85+
}
86+
],
87+
[
88+
'attribute' => 'created_at',
89+
'sortable' => true
90+
],
91+
[
92+
'attribute' => 'updated_at',
93+
'sortable' => true
94+
]
95+
];
96+
}
97+
}

app/Grid/Admin/MediaGrid.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace App\Grid\Admin;
4+
5+
use BalajiDharma\LaravelCrud\CrudBuilder;
6+
use Plank\Mediable\Media;
7+
8+
class MediaGrid extends CrudBuilder
9+
{
10+
public $title = 'Media';
11+
12+
public $description = 'Manage Media';
13+
14+
public $model = Media::class;
15+
16+
public $route = 'admin.media';
17+
18+
public function columns()
19+
{
20+
return [
21+
[
22+
'attribute' => 'type',
23+
'filable' => true,
24+
'label' => __('Type'),
25+
'value' => function ($model) {
26+
return $model->variant_name;
27+
},
28+
'form_options' => function ($model) {
29+
$type = media_type_as_options();
30+
return [
31+
'type' => 'select',
32+
'choices' => $type,
33+
'label' => __('Type'),
34+
'default_value' => $model ? $model->variant_name : null,
35+
];
36+
},
37+
],
38+
[
39+
'attribute' => 'filename',
40+
'filable' => true,
41+
'label' => 'Name',
42+
'sortable' => true,
43+
'filter' => 'like',
44+
'searchable' => true,
45+
'list' => [
46+
'class' => 'BalajiDharma\LaravelCrud\Column\LinkColumn',
47+
'route' => 'admin.media.show',
48+
'route_params' => ['medium' => 'id'],
49+
'attr' => ['class' => 'link link-primary']
50+
],
51+
'form_options' => function ($model) {
52+
return [
53+
'attribute' => 'name',
54+
'type' => 'text',
55+
'label' => __('Name'),
56+
'default_value' => $model ? $model->filename : null,
57+
];
58+
}
59+
],
60+
[
61+
'attribute' => 'alt',
62+
'label' => __('Alternative Text'),
63+
'filable' => true,
64+
'list' => false,
65+
'form_options' => function ($model) {
66+
return [
67+
'type' => 'text',
68+
'label' => __('Alternative Text'),
69+
];
70+
}
71+
],
72+
[
73+
'attribute' => 'file',
74+
'label' => __('File'),
75+
'filable' => true,
76+
'value' => function ($model) {
77+
if($model->aggregate_type != 'image'){
78+
$file = media_type_icon($model);
79+
} else {
80+
$file = '<image src="'.$model->getUrl().'" alt="'.$model->alt.'">';
81+
}
82+
return '<div class="avatar"><div class="w-32 rounded">'.$file.'</div><div>';
83+
},
84+
'form_options' => function ($model) {
85+
return [
86+
'type' => 'file',
87+
];
88+
}
89+
],
90+
[
91+
'attribute' => 'created_at',
92+
'sortable' => true
93+
],
94+
[
95+
'attribute' => 'updated_at',
96+
'sortable' => true
97+
]
98+
];
99+
}
100+
}

0 commit comments

Comments
 (0)