Skip to content

Commit

Permalink
🚧 Inertia resource
Browse files Browse the repository at this point in the history
  • Loading branch information
juzaweb committed Sep 2, 2023
1 parent 7ad513c commit a14b2b7
Show file tree
Hide file tree
Showing 23 changed files with 524 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CommentController extends BackendController
ResourceController::getDataForIndex as DataForIndex;
}

protected string $viewPrefix = 'cms::backend.comment';
protected string $template = 'inertia';

protected function validator(array $attributes, ...$params): Validator|array
{
Expand Down
16 changes: 5 additions & 11 deletions modules/Backend/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,14 @@ protected function getModel(...$params): string
*/
protected function getTitle(...$params): string
{
$postType = $params[0];
$taxonomy = $params[1];

$setting = $this->getSetting($postType, $taxonomy);
[$postType, $taxonomy] = $params;

return $setting->get('label');
return $this->getSetting($postType, $taxonomy)->get('label');
}

protected function getDataForIndex(...$params): array
{
$postType = $params[0];
$taxonomy = $params[1];

[$postType, $taxonomy] = $params;
$data = $this->DataForIndex($postType, $taxonomy);
$data['taxonomy'] = $taxonomy;
$data['setting'] = $this->getSetting($postType, $taxonomy);
Expand All @@ -157,7 +152,7 @@ protected function getDataForForm(Model $model, ...$params): array
return $data;
}

protected function checkPermission($ability, $arguments = [], ...$params)
protected function checkPermission($ability, $arguments = [], ...$params): void
{
if (!is_array($arguments)) {
$arguments = [$arguments];
Expand All @@ -177,7 +172,6 @@ protected function hasPermission($ability, $arguments = [], ...$params): bool
$arguments[] = $params[0];
$arguments[] = $params[1];

$response = Gate::inspect($ability, $arguments);
return $response->allowed();
return Gate::inspect($ability, $arguments)->allowed();
}
}
16 changes: 7 additions & 9 deletions modules/Backend/Http/Controllers/Backend/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

namespace Juzaweb\Backend\Http\Controllers\Backend;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Juzaweb\Backend\Http\Datatables\UserDataTable;
use Juzaweb\CMS\Abstracts\DataTable;
use Juzaweb\CMS\Http\Controllers\BackendController;
use Juzaweb\Backend\Http\Datatables\UserDataTable;
use Juzaweb\CMS\Models\Model;
use Juzaweb\CMS\Models\User;
use Juzaweb\CMS\Traits\ResourceController;
use Illuminate\Support\Facades\Validator;

class UserController extends BackendController
{
use ResourceController {
getDataForForm as DataForForm;
afterSave as tAfterSave;
}

protected string $viewPrefix = 'cms::backend.user';
Expand Down Expand Up @@ -88,17 +87,15 @@ protected function getDataTable(...$params): UserDataTable|DataTable
return new UserDataTable();
}

protected function getDataForForm(\Illuminate\Database\Eloquent\Model $model, ...$params): array
protected function getDataForForm(Model $model, ...$params): array
{
$data = $this->DataForForm($model);
$data['allStatus'] = User::getAllStatus();
return $data;
}

protected function afterSave(array $data, Model $model, ...$params)
protected function afterSave(array $data, Model $model, ...$params): void
{
$this->tAfterSave($data, $model);

do_action('user.after_save', $data, $model);
}

Expand All @@ -107,9 +104,10 @@ protected function afterSave(array $data, Model $model, ...$params)
*
* @param array $data
* @param Model $model
* @param mixed ...$params
* @throws ValidationException
*/
protected function beforeSave(&$data, &$model, ...$params)
protected function beforeSave(array &$data, Model $model, ...$params): void
{
if ($password = Arr::get($data, 'password')) {
Validator::make(
Expand Down
39 changes: 39 additions & 0 deletions modules/Backend/Support/ElementBuilders/IndexResourceBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\Backend\Support\ElementBuilders;

use Juzaweb\CMS\Interfaces\ElementBuilders\ElementBuilder as ElementBuilderInterface;
use Juzaweb\CMS\Abstracts\ElementBuilders\ElementBuilder;

class IndexResourceBuilder extends ElementBuilder implements ElementBuilderInterface
{
public function __construct(protected array $params = [])
{
//
}

public function toArray(): array
{
$builder = $this->builder();
$builder->row()
->col(['cols' => 12])
->buttonGroup()->addClass('float-right')
->link(['href' => $this->params['linkCreate']])
->text(trans('cms::app.add_new'))
->addClass('btn btn-success');

$builder->row()
->col(['cols' => 12])
->dataTable($this->params['dataTable']);

return $builder->toArray();
}
}
13 changes: 9 additions & 4 deletions modules/CMS/Abstracts/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function ($item, $key) {
$item['key'] = $key;
return $item;
}
)->values();
)->values()->toArray();

$columns = collect($this->columns())->map(
function ($item, $key) {
Expand All @@ -305,15 +305,20 @@ function ($item, $key) {
unset($item['formatter']);
return $item;
}
)->values();
)->values()->toArray();

$actions = collect($this->actions())->map(
function ($label, $key) {
$item['key'] = $key;
$item['label'] = $label;
if (is_string($label)) {
$item['label'] = $label;
} else {
$item['label'] = $label['label'];
}

return $item;
}
)->values();
)->values()->toArray();

return [
'columns' => $columns,
Expand Down
23 changes: 23 additions & 0 deletions modules/CMS/Abstracts/ElementBuilders/ElementBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\CMS\Abstracts\ElementBuilders;

use Juzaweb\CMS\Support\Element\Contracts\ElementBuilder as ElementBuilderContract;

abstract class ElementBuilder
{
public function builder(): ElementBuilderContract
{
return app()->make(ElementBuilderContract::class);
}

abstract public function toArray(): array;
}
21 changes: 21 additions & 0 deletions modules/CMS/Interfaces/ElementBuilders/ElementBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\CMS\Interfaces\ElementBuilders;

use Illuminate\Contracts\Support\Arrayable;
use Juzaweb\CMS\Support\Element\Contracts\ElementBuilder as ElementBuilderContract;

interface ElementBuilder extends Arrayable
{
public function builder(): ElementBuilderContract;

public function toArray(): array;
}
4 changes: 2 additions & 2 deletions modules/CMS/Support/Element/ElementBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function elementByType(string $element, array $configs = []): Element
{
return match ($element) {
'form' => new Form($configs),
'input' => new Inputs\Input($configs),
'textarea' => new Inputs\Textarea($configs),
'input' => new Elements\Inputs\Input($configs),
'textarea' => new Elements\Inputs\Textarea($configs),
'row' => new Elements\Row($configs),
default => throw new \Exception("Element type {$element} not found"),
};
Expand Down
38 changes: 38 additions & 0 deletions modules/CMS/Support/Element/Elements/Buttons/ButtonGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\CMS\Support\Element\Elements\Buttons;

use Juzaweb\CMS\Support\Element\Interfaces\Element;
use Juzaweb\CMS\Support\Element\Traits\HasChildren;
use Juzaweb\CMS\Support\Element\Traits\HasClass;

class ButtonGroup implements Element
{
use HasClass, HasChildren;

protected string $element = 'button-group';

protected string $class = 'btn-group';

public function toArray(): array
{
$data = get_object_vars($this);

$data['className'] = $this->class;

return $data;
}

public function render(): string
{
return '';
}
}
7 changes: 6 additions & 1 deletion modules/CMS/Support/Element/Elements/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Str;
use Juzaweb\CMS\Abstracts\Action;
use Juzaweb\CMS\Abstracts\DataTable as DataTableAbstract;
use Juzaweb\CMS\Support\Element\Interfaces\Element;
use Juzaweb\CMS\Support\Element\Traits\HasClass;
use Juzaweb\CMS\Support\Element\Traits\HasId;
Expand Down Expand Up @@ -99,8 +100,12 @@ class DataTable implements Element

protected array $rowActions = [];

public function __construct(array $configs)
public function __construct(array|DataTableAbstract $configs)
{
if ($configs instanceof DataTableAbstract) {
$configs = $configs->toArray();
}

foreach ($configs as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Juzaweb\CMS\Support\Element\Inputs;
namespace Juzaweb\CMS\Support\Element\Elements\Inputs;

use Juzaweb\CMS\Support\Element\Abstracts\InputAbstract;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Juzaweb\CMS\Support\Element\Inputs;
namespace Juzaweb\CMS\Support\Element\Elements\Inputs;

use Juzaweb\CMS\Support\Element\Abstracts\InputAbstract;

Expand Down
51 changes: 51 additions & 0 deletions modules/CMS/Support/Element/Elements/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\CMS\Support\Element\Elements;

use Juzaweb\CMS\Support\Element\Interfaces\Element;
use Juzaweb\CMS\Support\Element\Traits\HasChildren;
use Juzaweb\CMS\Support\Element\Traits\HasClass;
use Juzaweb\CMS\Support\Element\Traits\HasId;

class Link implements Element
{
use HasClass, HasId, HasChildren;

protected string $element = 'link';

protected string $href;

protected string $target;

protected string $text;

public function __construct(array $configs = [])
{
//
}

public function text(string $text): static
{
$this->text = $text;

return $this;
}

public function toArray(): array
{
return get_object_vars($this);
}

public function render(): string
{
return '';
}
}
25 changes: 25 additions & 0 deletions modules/CMS/Support/Element/Traits/HasButtonElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* JUZAWEB CMS - Laravel CMS for Your Project
*
* @package juzaweb/cms
* @author The Anh Dang
* @link https://juzaweb.com
* @license GNU V2
*/

namespace Juzaweb\CMS\Support\Element\Traits;

use Juzaweb\CMS\Support\Element\Elements\Buttons\ButtonGroup;

trait HasButtonElement
{
public function buttonGroup(): ButtonGroup
{
$btn = new ButtonGroup();

$this->pushChild($btn);

return $btn;
}
}
Loading

0 comments on commit a14b2b7

Please sign in to comment.