Skip to content

Commit

Permalink
#50 fixes with new templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulgolakiya committed Apr 5, 2016
1 parent 4ddbc29 commit 815a053
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
24 changes: 24 additions & 0 deletions src/Generators/Scaffold/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ class ControllerGenerator
/** @var string */
private $path;

/** @var string */
private $templateType;

public function __construct(CommandData $commandData)
{
$this->commandData = $commandData;
$this->path = $commandData->config->pathController;
$this->templateType = config('infyom.laravel_generator.path.templates', 'core-templates');
}

public function generate()
Expand Down Expand Up @@ -54,10 +58,30 @@ private function generateDataTable()

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$headerFieldTemplate = TemplateUtil::getTemplate('scaffold.views.datatable_column', $this->templateType);

$headerFields = [];

foreach ($this->commandData->inputFields as $field) {
if (!$field['inIndex']) {
continue;
}
$headerFields[] = $fieldTemplate = TemplateUtil::fillTemplateWithFieldData(
$this->commandData->dynamicVars,
$this->commandData->fieldNamesMapping,
$headerFieldTemplate,
$field
);
}

$path = $this->commandData->config->pathDataTables;

$fileName = $this->commandData->modelName.'DataTable.php';

$fields = implode(",".infy_nl_tab(1, 3), $headerFields);

$templateData = str_replace('$DATATABLE_COLUMNS$', $fields, $templateData);

FileUtil::createFile($path, $fileName, $templateData);

$this->commandData->commandComment("\n$fileName created: ");
Expand Down
25 changes: 1 addition & 24 deletions src/Generators/Scaffold/ViewGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,7 @@ private function generateDataTableBody()
{
$templateData = TemplateUtil::getTemplate('scaffold.views.datatable_body', $this->templateType);

$templateData = TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);

$templateData = str_replace('$FIELD_HEADERS$', $this->generateTableHeaderFields(), $templateData);

$cellFieldTemplate = TemplateUtil::getTemplate('scaffold.views.datatable_column', $this->templateType);

$tableBodyFields = [];

foreach ($this->commandData->inputFields as $field) {
if (!$field['inIndex']) {
continue;
}

$tableBodyFields[] = TemplateUtil::fillTemplateWithFieldData(
$this->commandData->dynamicVars,
$this->commandData->fieldNamesMapping,
$cellFieldTemplate,
$field
);
}

$tableBodyFields = implode(",".infy_nl_tab(1, 5), $tableBodyFields);

return str_replace('$DATATABLE_COLUMNS$', $tableBodyFields, $templateData);
return TemplateUtil::fillTemplate($this->commandData->dynamicVars, $templateData);
}

private function generateIndex()
Expand Down
53 changes: 42 additions & 11 deletions templates/scaffold/datatable.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace $NAMESPACE_DATATABLES$;

use App\Models\$MODEL_NAME$;
use $NAMESPACE_MODEL$\$MODEL_NAME$;
use Yajra\Datatables\Services\DataTable;

class $MODEL_NAME$DataTable extends DataTable
Expand All @@ -15,7 +15,21 @@ class $MODEL_NAME$DataTable extends DataTable
{
return $this->datatables
->eloquent($this->query())
->addColumn('action', 'path.to.action.view')
->addColumn('actions', function ($data) {
return '
' . Form::open(['route' => ['$MODEL_NAME_PLURAL_CAMEL$.destroy', $data->id], 'method' => 'delete']) . '
<div class=\'btn-group\'>
<a href="' . route('$MODEL_NAME_PLURAL_CAMEL$.show', [$data->id]) . '" class=\'btn btn-default btn-xs\'><i class="glyphicon glyphicon-eye-open"></i></a>
<a href="' . route('$MODEL_NAME_PLURAL_CAMEL$.edit', [$data->id]) . '" class=\'btn btn-default btn-xs\'><i class="glyphicon glyphicon-edit"></i></a>
' . Form::button('<i class="glyphicon glyphicon-trash"></i>', [
'type' => 'submit',
'class' => 'btn btn-danger btn-xs',
'onclick' => "return confirm('Are you sure?')"
]) . '
</div>
' . Form::close() . '
';
})
->make(true);
}

Expand All @@ -39,10 +53,30 @@ class $MODEL_NAME$DataTable extends DataTable
public function html()
{
return $this->builder()
->columns($this->getColumns())
->ajax('')
->addAction(['width' => '80px'])
->parameters($this->getBuilderParameters());
->columns(array_merge(
$this->getColumns(),
[
'actions' => [
'orderable' => false,
'searchable' => false,
'printable' => false,
'exportable' => false
]
]
))
->parameters([
'dom' => 'Bfrtip',
'scrollX' => true,
'buttons' => [
'create',
'csv',
'excel',
'pdf',
'print',
'reset',
'reload'
],
]);
}

/**
Expand All @@ -53,10 +87,7 @@ class $MODEL_NAME$DataTable extends DataTable
private function getColumns()
{
return [
'id',
// add your columns
'created_at',
'updated_at',
$DATATABLE_COLUMNS$
];
}

Expand All @@ -67,6 +98,6 @@ class $MODEL_NAME$DataTable extends DataTable
*/
protected function filename()
{
return '$$MODEL_NAME_PLURAL_CAMEL$';
return '$MODEL_NAME_PLURAL_CAMEL$';
}
}

0 comments on commit 815a053

Please sign in to comment.