Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a CRUD Generator to the admin options #228

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e428f96
Adding a CRUD Generator to the admin options
DeveloperOnCall Jan 4, 2019
de57c24
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
e67c1a6
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
e6a6a4f
Merge remote-tracking branch 'origin/master'
DeveloperOnCall Jan 4, 2019
fdce64f
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
0d0e3a6
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
7b4e28f
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
357a6ed
Merge remote-tracking branch 'origin/master'
DeveloperOnCall Jan 4, 2019
a0df875
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
d833899
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
e521f27
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
58253e1
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
cc24313
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
9b50f55
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
c81a1c2
Adding a CRUD Generator to the admin options, Fixed Code Style Issues
DeveloperOnCall Jan 4, 2019
07c24cd
Add missing language entries and route
DeveloperOnCall Jan 5, 2019
06ac56c
Fix Page Title
DeveloperOnCall Jan 5, 2019
93877f6
Change to include summernote properly to avoid insecure file inclusio…
DeveloperOnCall Jan 5, 2019
9a0089f
Adding Dynamic Menu From generated CRUD Module
DeveloperOnCall Jan 5, 2019
d61a1f1
Route cleanup
DeveloperOnCall Jan 5, 2019
fe48de3
Route cleanup
DeveloperOnCall Jan 5, 2019
8fe8c55
Merge remote-tracking branch 'origin/master'
DeveloperOnCall Jan 5, 2019
734b6f9
Added description of new feature and instructions for changes related…
DeveloperOnCall Jan 5, 2019
59aff70
Fixed Missing artisan command - rookie mistake not synching the compo…
DeveloperOnCall Jan 7, 2019
e325d74
Fixed Missing files
DeveloperOnCall Jan 10, 2019
10946ea
Fixing extra trailing slash in stub path
DeveloperOnCall Jan 10, 2019
7dbe459
Reversing change to stub path - seems the crudgenerator package has s…
DeveloperOnCall Jan 10, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
130 changes: 130 additions & 0 deletions app/Http/Controllers/CrudController.php
@@ -0,0 +1,130 @@
<?php

namespace App\Http\Controllers;

use Artisan;
use File;
use Illuminate\Http\Request;
use Redirect;
use Response;
use View;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be one blank line after the last USE statement; 2 found;



class CrudController extends Controller
{
/**
* Display generator.
*
* @return Response
*/
public function getGenerator()
{
return view('crudmanagement.add-crud');
}

/**
* Process generator.
*
* @param \Illuminate\Http\Request $request
*
* @return Response
*/
public function postGenerator(Request $request)
{
$commandArg = [];
$commandArg['name'] = $request->crud_name;


// Make sure we do not already have this module
$directories = glob(base_path().'/resources/views/*', GLOB_ONLYDIR);
foreach ($directories as $dir){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected 1 space after closing parenthesis; found 0

$this_name = str_replace(base_path().'/resources/views/','',$dir);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space found after comma in function call

$used_names[] = $this_name;
}

if (in_array(strtolower($request->crud_name), $used_names)) {
return Redirect::back()->withErrors(['Name Exists or Restricted']);
}

if ($request->has('fields')) {
$fieldsArray = [];
$validationsArray = [];
$x = 0;
foreach ($request->fields as $field) {
if ($request->fields_required[$x] == 1) {
$validationsArray[] = $field;
}

$fieldsArray[] = $field.'#'.$request->fields_type[$x];

$x++;
}

$commandArg['--fields'] = implode(';', $fieldsArray);
}

if (!empty($validationsArray)) {
$commandArg['--validations'] = implode('#required;', $validationsArray).'#required';
}

if ($request->has('route')) {
$commandArg['--route'] = $request->route;
}

if ($request->has('view_path')) {
$commandArg['--view-path'] = $request->view_path;
}

if ($request->has('controller_namespace')) {
$commandArg['--controller-namespace'] = $request->controller_namespace;
}

if ($request->has('model_namespace')) {
$commandArg['--model-namespace'] = $request->model_namespace;
}

if ($request->has('route_group')) {
$commandArg['--route-group'] = $request->route_group;
}

if ($request->has('relationships')) {
$commandArg['--relationships'] = $request->relationships;
}

if ($request->has('form_helper')) {
$commandArg['--form-helper'] = $request->form_helper;
}

if ($request->has('soft_deletes')) {
$commandArg['--soft-deletes'] = $request->soft_deletes;
}

try {
Artisan::call('crud:generate', $commandArg);

$menus = json_decode(File::get(base_path('resources/laravel-admin/menus.json')));

$name = $commandArg['name'];
$routeName = ($commandArg['--route-group']) ? $commandArg['--route-group'].'/'.snake_case($name, '-') : snake_case($name, '-');

$menus->menus = array_map(function ($menu) use ($name, $routeName) {
if ($menu->section == 'Resources') {
array_push($menu->items, (object) [
'title' => $name,
'url' => '/'.$routeName,
]);
}

return $menu;
}, $menus->menus);

File::put(base_path('resources/laravel-admin/menus.json'), json_encode($menus));

Artisan::call('migrate');
} catch (\Exception $e) {
return Response::make($e->getMessage(), 500);
}

return redirect('crud')->with('flash_message', 'Your CRUD has been generated. See on the menu.');
}
}
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -6,7 +6,6 @@
"type": "project",
"require": {
"php": "^7.1.3",
"barryvdh/laravel-debugbar": "^3.0",
"creativeorange/gravatar": "~1.0",
"doctrine/dbal": "^2.5",
"fideloper/proxy": "^4.0",
Expand All @@ -33,6 +32,7 @@
"socialiteproviders/youtube": "^3.0"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.0",
"filp/whoops": "^2.0",
"fzaninotto/faker": "^1.4",
"mockery/mockery": "^1.0",
Expand Down
70 changes: 70 additions & 0 deletions config/crudgenerator.php
@@ -0,0 +1,70 @@
<?php

return [

'custom_template' => true,

/*
|--------------------------------------------------------------------------
| Crud Generator Template Stubs Storage Path
|--------------------------------------------------------------------------
|
| Here you can specify your custom template path for the generator.
|
*/

'path' => base_path('resources/crud-generator/'),

/*
* Columns number to show in view's table.
*/
'view_columns_number' => 3,

/*
* Delimiter for template vars
*/
'custom_delimiter' => ['%%', '%%'],

/*
|--------------------------------------------------------------------------
| Dynamic templating
|--------------------------------------------------------------------------
|
| Here you can specify your customs templates for the generator.
| You can set new templates or delete some templates if you do not want them.
| You can also choose which values are passed to the views and you can specify a custom delimiter for all templates
|
| Those values are available :
|
| formFields
| formFieldsHtml
| varName
| crudName
| crudNameCap
| crudNameSingular
| primaryKey
| modelName
| modelNameCap
| viewName
| routePrefix
| routePrefixCap
| routeGroup
| formHeadingHtml
| formBodyHtml
|
|
*/
'dynamic_view_template' => [
'index' => ['formHeadingHtml', 'formBodyHtml', 'crudName', 'crudNameCap', 'modelName', 'viewName', 'routeGroup', 'primaryKey'],
'form' => ['formFieldsHtml'],
'create' => ['crudName', 'crudNameCap', 'modelName', 'modelNameCap', 'viewName', 'routeGroup', 'viewTemplateDir'],
'edit' => ['crudName', 'crudNameSingular', 'crudNameCap', 'modelNameCap', 'modelName', 'viewName', 'routeGroup', 'primaryKey', 'viewTemplateDir'],
'show' => ['formHeadingHtml', 'formBodyHtml', 'formBodyHtmlForShowView', 'crudName', 'crudNameSingular', 'crudNameCap', 'modelName', 'viewName', 'routeGroup', 'primaryKey'],
/*
* Add new stubs templates here if you need to, like action, datatable...
* custom_template needs to be activated for this to work
*/
],


];
36 changes: 36 additions & 0 deletions resources/crud-generator/views/html/create.blade.stub
@@ -0,0 +1,36 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">


<div class="col-md-12">
<div class="card">
<div class="card-header">Create New %%modelName%%</div>
<div class="card-body">
<a href="{{ url('/%%routeGroup%%%%viewName%%') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
<br />
<br />

@if ($errors->any())
<ul class="alert alert-danger">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif

<form method="POST" action="{{ url('/%%routeGroup%%%%viewName%%') }}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}

@include ('%%viewTemplateDir%%.form', ['formMode' => 'create'])

</form>

</div>
</div>
</div>
</div>
</div>
@endsection
37 changes: 37 additions & 0 deletions resources/crud-generator/views/html/edit.blade.stub
@@ -0,0 +1,37 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">


<div class="col-md-12">
<div class="card">
<div class="card-header">Edit %%modelName%% #{{ $%%crudNameSingular%%->%%primaryKey%% }}</div>
<div class="card-body">
<a href="{{ url('/%%routeGroup%%%%viewName%%') }}" title="Back"><button class="btn btn-warning btn-sm"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
<br />
<br />

@if ($errors->any())
<ul class="alert alert-danger">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif

<form method="POST" action="{{ url('/%%routeGroup%%%%viewName%%/' . $%%crudNameSingular%%->%%primaryKey%%) }}" accept-charset="UTF-8" class="form-horizontal" enctype="multipart/form-data">
{{ method_field('PATCH') }}
{{ csrf_field() }}

@include ('%%viewTemplateDir%%.form', ['formMode' => 'edit'])

</form>

</div>
</div>
</div>
</div>
</div>
@endsection
@@ -0,0 +1,6 @@
<div class="checkbox">
<label><input name="{{ %1$s }}" type="checkbox" value="1" {{ (isset($%%crudNameSingular%%) && 1 == $%%crudNameSingular%%->%1$s) ? 'checked' : '' }}> Yes</label>
</div>
<div class="checkbox">
<label><input name="{{ %1$s }}" type="checkbox" value="0" {{ (isset($%%crudNameSingular%%) && 0 == $%%crudNameSingular%%->%1$s) ? 'checked' : '' }}> No</label>
</div>
@@ -0,0 +1 @@
<input class="form-control" name="%%itemName%%" type="%%fieldType%%" id="%%itemName%%" value="{{ isset($%%crudNameSingular%%->%%itemName%%) ? $%%crudNameSingular%%->%%itemName%% : ''}}" %%required%%>
@@ -0,0 +1 @@
<input class="form-control" name="%%itemName%%" type="%%fieldType%%" id="%%itemName%%" value="{{ isset($%%crudNameSingular%%->%%itemName%%) ? $%%crudNameSingular%%->%%itemName%% : ''}}" %%required%%>
@@ -0,0 +1 @@
<input class="form-control" name="%%itemName%%" type="password" id="%%itemName%%" %%required%%>
@@ -0,0 +1,6 @@
<div class="radio">
<label><input name="%%itemName%%" type="radio" value="1" {{ (isset($%%crudNameSingular%%) && 1 == $%%crudNameSingular%%->%%itemName%%) ? 'checked' : '' }}> Yes</label>
</div>
<div class="radio">
<label><input name="%%itemName%%" type="radio" value="0" @if (isset($%%crudNameSingular%%)) {{ (0 == $%%crudNameSingular%%->%%itemName%%) ? 'checked' : '' }} @else {{ 'checked' }} @endif> No</label>
</div>
@@ -0,0 +1,5 @@
<select name="%%itemName%%" class="form-control" id="%%itemName%%" %%required%%>
@foreach (json_decode('%%options%%', true) as $optionKey => $optionValue)
<option value="{{ $optionKey }}" {{ (isset($%%crudNameSingular%%->%%itemName%%) && $%%crudNameSingular%%->%%itemName%% == $optionKey) ? 'selected' : ''}}>{{ $optionValue }}</option>
@endforeach
</select>
@@ -0,0 +1 @@
<textarea class="form-control summernote" rows="5" name="%%itemName%%" type="%%fieldType%%" id="%%itemName%%" %%required%%>{{ isset($%%crudNameSingular%%->%%itemName%%) ? $%%crudNameSingular%%->%%itemName%% : ''}}</textarea>
@@ -0,0 +1,5 @@
<div class="form-group {{ $errors->has('%1$s') ? 'has-error' : ''}}">
<label for="%1$s" class="control-label">{{ %2$s }}</label>
%3$s
{!! $errors->first('%1$s', '<p class="help-block">:message</p>') !!}
</div>
7 changes: 7 additions & 0 deletions resources/crud-generator/views/html/form.blade.stub
@@ -0,0 +1,7 @@
%%formFieldsHtml%%

<div class="form-group">
<input class="btn btn-primary" type="submit" value="{{ $formMode === 'edit' ? 'Update' : 'Create' }}">
</div>

@include('scripts.summernote')