-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from khaledAbodaif/feature/formBuilder
Feature/form builder
- Loading branch information
Showing
8 changed files
with
320 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace TomatoPHP\TomatoPHP\Services\Generator\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
use ProtoneMedia\Splade\FormBuilder\Text; | ||
|
||
trait GenerateFormView | ||
{ | ||
private function generateFormView(): void | ||
{ | ||
$folders = []; | ||
if ($this->moduleName) { | ||
$folders[] = module_path($this->moduleName) . "/Resources/views/{$this->tableName}"; | ||
} else { | ||
$folders[] = resource_path("views/{$this->tableName}"); | ||
} | ||
|
||
$this->generateStubs( | ||
"vendor/tomatophp/tomato-php/stubs/FormBuilder/Form.stub", | ||
$this->moduleName ? module_path($this->moduleName) . "/Resources/views/" . str_replace('_', '-', $this->tableName) . "/form.blade.php" : resource_path("views/admin/{$this->tableName}/form.blade.php"), | ||
[ | ||
"title" => $this->modelName, | ||
"table" => str_replace('_', '-', $this->tableName), | ||
"cols" => $this->generateForm() | ||
], | ||
$folders | ||
); | ||
} | ||
|
||
private function generateFormBuilderClass() | ||
{ | ||
$this->generateStubs( | ||
"vendor/tomatophp/tomato-php/stubs/FormBuilder/FormClass.stub", | ||
$this->moduleName ? module_path($this->moduleName) . "/Forms/{$this->modelName}Form.php" : app_path("Forms/{$this->modelName}Form.php"), | ||
[ | ||
"name" => "{$this->modelName}Form", | ||
"route" => str_replace('_', '-', $this->tableName), | ||
"cols" => $this->generateFormElements(), | ||
"namespace" => $this->moduleName ? "Modules\\" . $this->moduleName . "\\Forms" : "App\\Forms", | ||
], | ||
[ | ||
$this->moduleName ? module_path($this->moduleName) . "/Forms" : app_path("Forms") | ||
] | ||
); | ||
} | ||
|
||
private function generateFormElements(): string | ||
{ | ||
// $item['type'] === 'tel' || color | ||
$types = ["string" => "Text", "email" => "Email", "tel" => "Text", "password" => "Password", "textarea" => "Textarea", "int" => "Number", "date" => "Date", "datetime" => "Datetime", "time" => "Time"]; | ||
$form = ""; | ||
foreach ($this->cols as $key => $item) { | ||
|
||
if (array_key_exists($item['type'],$types)){ | ||
$form .= " \ProtoneMedia\Splade\FormBuilder\\".$types[$item['type']]."::make('" . $item['name'] . "')->label(__('" . Str::ucfirst(str_replace('_', ' ', $item['name'])) . "')),"; | ||
$form .= PHP_EOL; | ||
} | ||
|
||
if ($item['type']== 'boolean'){ | ||
$form .= " \ProtoneMedia\Splade\FormBuilder\Checkbox::make('" . $item['name'] . "')->label(__('" . Str::ucfirst(str_replace('_', ' ', $item['name'])) . "'))->value(1),"; | ||
$form .= PHP_EOL; | ||
} | ||
|
||
if ($item['type'] === 'relation') { | ||
|
||
$itemLable = ($item['relation']['relationColumnType'] == 'json') ? 'name.' . app()->getLocale() : 'name'; | ||
$form .= " \ProtoneMedia\Splade\FormBuilder\Select::make('".$item['name']."') | ||
->label(__('".Str::remove('_id',$item['name'])."')) | ||
->choices() | ||
->remoteUrl('/admin/".$item['relation']['table']."/api') | ||
->remoteRoute('model.data') | ||
->optionLabel('".$itemLable."') | ||
->optionValue('id'),"; | ||
$form .= PHP_EOL; | ||
} | ||
|
||
if ($item['type'] === 'json' && ($item['name'] == 'name' || $item['name'] == 'title' || $item['name'] == 'description')) { | ||
|
||
$form .= " \ProtoneMedia\Splade\FormBuilder\Text::make('" . $item['name'] . ".ar')->label(__('" . Str::ucfirst(str_replace('_', ' ', $item['name'])) . "_ar')),"; | ||
$form .= PHP_EOL; | ||
$form .= " \ProtoneMedia\Splade\FormBuilder\Text::make('" . $item['name'] . ".en')->label(__('" . Str::ucfirst(str_replace('_', ' ', $item['name'])) . "_en')),"; | ||
$form .= PHP_EOL; | ||
} | ||
|
||
|
||
if ($key !== count($this->cols) - 1) { | ||
$form .= PHP_EOL; | ||
} | ||
} | ||
return $form; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\View\View; | ||
use TomatoPHP\TomatoPHP\Services\Tomato; | ||
use {{ FormNamespace }}; | ||
class {{ name }} extends Controller | ||
{ | ||
/** | ||
* @param Request $request | ||
* @return View | ||
*/ | ||
public function index(Request $request): View | ||
{ | ||
return Tomato::index( | ||
request: $request, | ||
view: '{{ modulePath }}{{ table }}.index', | ||
table: {{ tableClass }}::class, | ||
); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @return JsonResponse | ||
*/ | ||
public function api(Request $request): JsonResponse | ||
{ | ||
return Tomato::json( | ||
request: $request, | ||
model: {{ model }}::class, | ||
); | ||
} | ||
|
||
/** | ||
* @return View | ||
*/ | ||
public function create(): View | ||
{ | ||
return Tomato::create( | ||
view: '{{ modulePath }}{{ table }}.form', | ||
data: ['form'=>{{ title }}Form::class] | ||
|
||
); | ||
} | ||
|
||
/** | ||
* @param {{ requestNamespace }}{{ title }}StoreRequest $request | ||
* @return RedirectResponse | ||
*/ | ||
public function store({{ requestNamespace }}{{ title }}StoreRequest $request): RedirectResponse | ||
{ | ||
$response = Tomato::store( | ||
request: $request, | ||
model: {{ model }}::class, | ||
message: __('{{ title }} created successfully'), | ||
redirect: 'admin.{{ table }}.index', | ||
); | ||
|
||
return $response['redirect']; | ||
} | ||
|
||
/** | ||
* @param {{ model }} $model | ||
* @return View | ||
*/ | ||
public function show({{ model }} $model): View | ||
{ | ||
return Tomato::get( | ||
model: $model, | ||
view: '{{ modulePath }}{{ table }}.show', | ||
); | ||
} | ||
|
||
/** | ||
* @param {{ model }} $model | ||
* @return View | ||
*/ | ||
public function edit({{ model }} $model): View | ||
{ | ||
|
||
{{ title }}Form::$model=$model; | ||
{{ title }}Form::$route="admin.{{ table }}.update"; | ||
|
||
return Tomato::get( | ||
model: $model, | ||
view: '{{ modulePath }}{{ table }}.form', | ||
data: ['form'=>{{ title }}Form::class] | ||
|
||
); | ||
} | ||
|
||
/** | ||
* @param {{ requestNamespace }}{{ title }}UpdateRequest $request | ||
* @param {{ model }} $user | ||
* @return RedirectResponse | ||
*/ | ||
public function update({{ requestNamespace }}{{ title }}UpdateRequest $request, {{ model }} $model): RedirectResponse | ||
{ | ||
$response = Tomato::update( | ||
request: $request, | ||
model: $model, | ||
message: __('{{ title }} updated successfully'), | ||
redirect: 'admin.{{ table }}.index', | ||
); | ||
|
||
return $response['redirect']; | ||
} | ||
|
||
/** | ||
* @param {{ model }} $model | ||
* @return RedirectResponse | ||
*/ | ||
public function destroy({{ model }} $model): RedirectResponse | ||
{ | ||
return Tomato::destroy( | ||
model: $model, | ||
message: __('{{ title }} deleted successfully'), | ||
redirect: 'admin.{{ table }}.index', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<x-splade-modal class="font-main"> | ||
@if(isset($model)) | ||
<h1 class="text-2xl font-bold mb-4">{{__('Edit {{ title }}')}} #{{$model->id}}</h1> | ||
@else | ||
<h1 class="text-2xl font-bold mb-4">{{__('Create {{ title }}')}} </h1> | ||
@endif | ||
|
||
<x-splade-form :for="$form" /> | ||
|
||
</x-splade-modal> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
use ProtoneMedia\Splade\FormBuilder\Submit; | ||
use Illuminate\Database\Eloquent\Model; | ||
use ProtoneMedia\Splade\SpladeForm; | ||
use ProtoneMedia\Splade\AbstractForm; | ||
|
||
|
||
class {{ name }} extends AbstractForm | ||
{ | ||
public static Model|array $model=[]; | ||
public static string $route="admin.{{ route }}.store"; | ||
|
||
public function configure(SpladeForm $form) | ||
{ | ||
|
||
$form | ||
->action(route(self::$route,self::$model)) | ||
->method('POST') | ||
->class('space-y-4') | ||
->fill(self::$model); | ||
|
||
} | ||
|
||
public function fields(): array | ||
{ | ||
return [ | ||
{{ cols }} | ||
Submit::make() | ||
->label(__('Save')), | ||
]; | ||
} | ||
|
||
} |