Skip to content

Commit

Permalink
Merge pull request #345 from juzaweb/feature/inertia-resource
Browse files Browse the repository at this point in the history
feature/inertia-resource
  • Loading branch information
juzaweb committed Aug 6, 2023
2 parents 80d54de + dda1b50 commit e0be444
Show file tree
Hide file tree
Showing 26 changed files with 570 additions and 45 deletions.
5 changes: 3 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// phpcs:disable

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -27,7 +28,7 @@
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
| into the script here, so we don't need to manually load our classes.
|
*/

Expand All @@ -44,7 +45,7 @@
|
*/

$app = require_once __DIR__.'/bootstrap/app.php';
$app = require __DIR__.'/bootstrap/app.php';

$kernel = $app->make(Kernel::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ class TaxonomyController extends BackendController

protected string $viewPrefix = 'cms::backend.taxonomy';

//protected string $template = 'inertia';

protected function getDataTable(...$params): TaxonomyDataTable
{
$postType = $params[0];
$taxonomy = $params[1];
[$postType, $taxonomy] = $params;
$setting = $this->getSetting($postType, $taxonomy);
$dataTable = new TaxonomyDataTable();
$dataTable->mountData($setting->toArray());
Expand Down
11 changes: 3 additions & 8 deletions modules/Backend/Http/Datatables/TaxonomyDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ public function mount($taxonomy)
$this->taxonomy = $taxonomy;
}

/**
* Columns datatable
*
* @return array
*/
public function columns()
public function columns(): array
{
$columns = [
'name' => [
Expand Down Expand Up @@ -91,7 +86,7 @@ public function query($data)
return $query;
}

public function rowAction($row)
public function rowAction($row): array
{
$data = parent::rowAction($row);

Expand All @@ -104,7 +99,7 @@ public function rowAction($row)
return $data;
}

public function bulkActions($action, $ids)
public function bulkActions($action, $ids): void
{
foreach ($ids as $id) {
DB::beginTransaction();
Expand Down
99 changes: 89 additions & 10 deletions modules/CMS/Abstracts/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function getData(Request $request): array
*
* @param mixed ...$params The parameters to be mounted.
* @return void
* @throws \Exception
*/
public function mountData(...$params)
{
Expand All @@ -146,6 +147,11 @@ public function mountData(...$params)
$this->params = $params;
}

/**
* Renders the view for the PHP function.
*
* @return View
*/
public function render(): View
{
if (empty($this->currentUrl)) {
Expand All @@ -158,6 +164,11 @@ public function render(): View
);
}

/**
* Returns an array of actions.
*
* @return array Returns an array of actions.
*/
public function actions(): array
{
return [
Expand All @@ -168,10 +179,8 @@ public function actions(): array
/**
* A description of the entire PHP function.
*
* @param datatype $action description
* @param datatype $ids description
* @throws Some_Exception_Class description of exception
* @return Some_Return_Value
* @param string $action description
* @param array $ids description
*/
public function bulkActions($action, $ids)
{
Expand Down Expand Up @@ -215,6 +224,14 @@ public function rowAction($row)
];
}

/**
* Generates a formatted row action for the given value, row, and index.
*
* @param mixed $value The value for the row action.
* @param mixed $row The row object.
* @param int $index The index of the row.
* @return string The HTML rendered output of the row action.
*/
public function rowActionsFormatter($value, $row, $index): string
{
return view(
Expand All @@ -229,35 +246,74 @@ public function rowActionsFormatter($value, $row, $index): string
->render();
}

/**
* Sets the data URL for the object.
*
* @param string $url The URL to set as the data URL.
* @throws \Exception
* @return void
*/
public function setDataUrl(string $url): void
{
$this->dataUrl = $url;
}

/**
* Sets the action URL for the function.
*
* @param string $url The URL to set as the action URL.
* @return void
*/
public function setActionUrl(string $url): void
{
$this->actionUrl = $url;
}

/**
* Set the current URL.
*
* @param string $url The URL to set as the current URL.
* @return void
*/
public function setCurrentUrl(string $url): void
{
$this->currentUrl = $url;
}

/**
* Converts the object to an array.
*
* @return array The converted array.
*/
public function toArray(): array
{
$searchFields = $this->searchFields();
$searchFields = collect($this->searchFields())->map(
function ($item, $key) {
$item['key'] = $key;
return $item;
}
)->values();

$columns = collect($this->columns())->map(
function ($item, $key) {
$item['key'] = $key;
$item['sortable'] = Arr::get($item, 'sortable', true);
unset($item['formatter']);
return $item;
}
)->values();

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

return [
'columns' => $columns,
'actions' => $this->actions(),
'actions' => $actions,
'params' => $this->params,
'searchFields' => $searchFields,
'perPage' => $this->perPage,
Expand All @@ -269,6 +325,7 @@ function ($item, $key) {
'searchable' => $this->searchable,
'searchFieldTypes' => $this->getSearchFieldTypes(),
'table' => Crypt::encryptString(static::class),
'uniqueId' => $this->getUniqueId(),
];
}

Expand All @@ -279,7 +336,7 @@ function ($item, $key) {
*/
protected function getDataRender(): array
{
$uniqueId = 'juzaweb_' . Str::random(10);
$uniqueId = $this->getUniqueId();
$searchFields = $this->searchFields();

return [
Expand All @@ -300,22 +357,44 @@ protected function getDataRender(): array
];
}

private function paramsToArray($params)
/**
* Generate a unique ID.
*
* @return string The unique ID generated.
*/
protected function getUniqueId(): string
{
return 'juzaweb_' . Str::random(10);
}

/**
* Convert the given parameters into an array.
*
* @param mixed $params The parameters to convert.
* @throws \RuntimeException If the parameters contain unsupported types.
* @return mixed The converted parameters as an array.
*/
protected function paramsToArray($params)
{
foreach ($params as $key => $var) {
if (is_null($var)) {
continue;
}

if (! in_array(gettype($var), ['string', 'array', 'integer'])) {
throw new \Exception('Mount data can\'t support. Only supported string, array, integer');
throw new \RuntimeException('Mount data can\'t support. Only supported string, array, integer');
}
}

return $params;
}

private function getSearchFieldTypes()
/**
* Retrieves the search field types.
*
* @return array An array of search field types.
*/
protected function getSearchFieldTypes()
{
return apply_filters(Action::DATATABLE_SEARCH_FIELD_TYPES_FILTER, []);
}
Expand Down
Loading

0 comments on commit e0be444

Please sign in to comment.