1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\Operations; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Route; |
6
|
|
|
|
7
|
|
|
trait CreateDummyOperation |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Define which routes are needed for this operation. |
11
|
|
|
* |
12
|
|
|
* @param string $segment Name of the current entity (singular). Used as first URL segment. |
13
|
|
|
* @param string $routeName Prefix of the route name. |
14
|
|
|
* @param string $controller Name of the current CrudController. |
15
|
|
|
*/ |
16
|
|
|
protected function setupCreateDummyRoutes($segment, $routeName, $controller) |
17
|
|
|
{ |
18
|
|
|
Route::post($segment.'/create-dummy', [ |
19
|
|
|
'as' => $routeName.'.createDummy', |
20
|
|
|
'uses' => $controller.'@createDummy', |
21
|
|
|
'operation' => 'createDummy', |
22
|
|
|
]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add the default settings, buttons, etc that this operation needs. |
27
|
|
|
*/ |
28
|
|
|
protected function setupCreateDummyDefaults() |
29
|
|
|
{ |
30
|
|
|
$this->crud->allowAccess('createDummy'); |
31
|
|
|
|
32
|
|
|
$this->crud->operation('createDummy', function () { |
33
|
|
|
$this->crud->loadDefaultOperationSettingsFromConfig(); |
34
|
|
|
}); |
35
|
|
|
|
36
|
|
|
$this->crud->operation('list', function () { |
37
|
|
|
if (method_exists($this->crud->getModel(), 'factory')) { |
38
|
|
|
$this->crud->addButton('top', 'create_dummy', 'view', 'crud::buttons.create_dummy'); |
39
|
|
|
} |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Show the form for creating inserting a new row. |
45
|
|
|
* |
46
|
|
|
* @return Response |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function createDummy() |
49
|
|
|
{ |
50
|
|
|
// Access |
51
|
|
|
$this->crud->hasAccessOrFail('createDummy'); |
52
|
|
|
|
53
|
|
|
// Check if Model has Factory trait |
54
|
|
|
if (! method_exists($this->crud->getModel(), 'factory')) { |
55
|
|
|
return response()->json([ |
|
|
|
|
56
|
|
|
'title' => trans('backpack::crud.create_dummy_error_title'), |
57
|
|
|
'message' => trans('backpack::crud.create_dummy_error_message'), |
58
|
|
|
], 500); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Options |
62
|
|
|
$TRUNCATE = 1; |
63
|
|
|
$CREATE = 2; |
64
|
|
|
|
65
|
|
|
$count = request()->input('count'); |
66
|
|
|
$option = request()->input('option'); |
67
|
|
|
|
68
|
|
|
// Truncate table |
69
|
|
|
if ($option & $TRUNCATE) { |
70
|
|
|
$this->crud->getModel()->truncate(); |
71
|
|
|
$messages[] = trans('backpack::crud.create_dummy_success_truncate'); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Create dummy entries |
75
|
|
|
if ($option & $CREATE) { |
76
|
|
|
$this->crud->getModel()::factory()->count($count)->create(); |
77
|
|
|
$messages[] = trans_choice('backpack::crud.create_dummy_sucess_message', $count, ['count' => $count]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return response()->json([ |
|
|
|
|
81
|
|
|
'title' => trans('backpack::crud.create_dummy_sucess_title'), |
82
|
|
|
'message' => join(' ', $messages), |
|
|
|
|
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|