Archived
1
0
Fork 0

Formatting fixes and cleanup.

This commit is contained in:
Henrik Hautakoski 2023-01-31 07:34:02 +01:00
parent a1e14a3e60
commit 51fb71e469
41 changed files with 394 additions and 392 deletions

View file

@ -7,7 +7,7 @@ use App\Http\Livewire\Form\CardForm;
class CardController extends BaseController
{
protected $_datatable = [
protected $_datatable = [
'model' => Card::class,
'default_sort' => 'id',
'route_create' => 'admin.card.create',
@ -16,21 +16,21 @@ class CardController extends BaseController
'restore_enabled' => true,
'columns' => [
'id' => '#',
'body' => 'Body',
'subject' => 'Subject',
'subject_type' => 'Subject Type',
'body' => 'Body',
'subject' => 'Subject',
'subject_type' => 'Subject Type',
'raid.name' => 'Raid',
],
'sort_columns' => [
'id' => 'id',
'body' => 'body',
'body' => 'body',
'subject' => ['character.name', 'class', 'role'],
'raid.name' => 'raid.name',
]
];
static public function getForm() : string
{
return CardForm::class;
}
static public function getForm(): string
{
return CardForm::class;
}
}

View file

@ -9,13 +9,13 @@ use App\Http\Controllers\Controller;
class AuthController extends Controller
{
/**
/**
* Show login page
*/
public function create()
{
return view('auth.login');
}
public function create()
{
return view('auth.login');
}
/**
* Destroy an authenticated session.

View file

@ -31,18 +31,18 @@ class Kernel extends HttpKernel
protected $middlewareGroups = [
'web' => [
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
]
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class
];
}

View file

@ -7,45 +7,45 @@ use Livewire\Component;
class AlertContainer extends Component
{
/**
* List of alert types.
*/
protected array $_types = [
'info',
'success',
'warning',
'danger'
];
/**
* List of alert types.
*/
protected array $_types = [
'info',
'success',
'warning',
'danger'
];
/**
* Event listeners
*/
protected $listeners = [
// Add message when "alert" event is fired.
'alert' => 'addMessage'
];
/**
* Event listeners
*/
protected $listeners = [
// Add message when "alert" event is fired.
'alert' => 'addMessage'
];
/**
* Alert messages.
*/
public array $messages = [];
/**
* Alert messages.
*/
public array $messages = [];
public function mount()
{
// Load messages from session
foreach($this->_types as $type) {
if (Session::has($type)) {
$this->addMessage($type, Session::get($type));
}
}
// Load messages from session
foreach ($this->_types as $type) {
if (Session::has($type)) {
$this->addMessage($type, Session::get($type));
}
}
}
/**
* Add a message to the container.
*/
public function addMessage(string $type, string $message)
/**
* Add a message to the container.
*/
public function addMessage(string $type, string $message)
{
$this->messages[] = [ $type, $message ];
$this->messages[] = [$type, $message];
}
/**

View file

@ -5,197 +5,202 @@ namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Datatable extends Component
{
use WithPagination, Traits\WithSort, Traits\Alert;
use WithPagination, Traits\WithSort, Traits\Alert;
/**
* Query string settings.
*/
protected $queryString = [
'sort' => ['except' => 'id'],
'dir' => ['except' => 'asc'],
'page' => ['except' => 1],
'trashed' => ['except' => false]
];
/**
* Query string settings.
*/
protected $queryString = [
'sort' => ['except' => 'id'],
'dir' => ['except' => 'asc'],
'page' => ['except' => 1],
'trashed' => ['except' => false]
];
/**
* Model to get data from.
*/
public $model;
/**
* Model to get data from.
*/
public $model;
/**
* Array of columns to show
*/
public array $columns;
/**
* Array of columns to show
*/
public array $columns;
/**
* What colums are sortable
*/
public array $sort_columns;
/**
* What colums are sortable
*/
public array $sort_columns;
/**
* Default sorting column
*/
public string $default_sort;
/**
* Default sorting column
*/
public string $default_sort;
/**
* If only trashed records should be shown.
*/
public bool $trashed = false;
/**
* If only trashed records should be shown.
*/
public bool $trashed = false;
/**
* Route for creating a record (if null, link is omitted)
*/
public $route_create;
/**
* Route for creating a record (if null, link is omitted)
*/
public $route_create;
/**
* Route for editing a record (if null, link is omitted)
*/
public $route_edit;
/**
* Route for editing a record (if null, link is omitted)
*/
public $route_edit;
/**
* True if delete functionality is enabled.
*/
public $delete_enabled;
/**
* True if delete functionality is enabled.
*/
public $delete_enabled;
/**
* True if restore functionality is enabled.
*/
public $restore_enabled;
/**
* True if restore functionality is enabled.
*/
public $restore_enabled;
/**
* How many records should be displayed on one page.
*/
public int $itemsPerPage = 30;
/**
* How many records should be displayed on one page.
*/
public int $itemsPerPage = 30;
public function mount(string $model, array $columns,
array $sort_columns = [], $default_sort = '',
$route_create = null, $route_edit = null, $delete_enabled = false, $restore_enabled = false)
{
$this->model = app()->make($model);
$this->default_sort = $default_sort;
$this->sort = $this->default_sort;
$this->sort_columns = $sort_columns;
$this->route_create = $route_create;
$this->route_edit = $route_edit;
$this->delete_enabled = (bool) $delete_enabled;
$this->restore_enabled = (bool) $restore_enabled;
public function mount(
string $model,
array $columns,
array $sort_columns = [],
$default_sort = '',
$route_create = null,
$route_edit = null,
$delete_enabled = false,
$restore_enabled = false
) {
$this->model = app()->make($model);
$this->default_sort = $default_sort;
$this->columns = $columns;
$this->sort = $this->default_sort;
$this->sort_columns = $sort_columns;
$this->route_create = $route_create;
$this->route_edit = $route_edit;
$this->delete_enabled = (bool) $delete_enabled;
$this->restore_enabled = (bool) $restore_enabled;
$this->setTrashed(false);
$this->setTrashed(false);
}
/**
* Get the model name.
*/
public function getModelName() : string
{
return class_basename($this->model);
}
/**
* Get the model name.
*/
public function getModelName(): string
{
return class_basename($this->model);
}
/**
* Set trashed flag.
*/
public function setTrashed($value)
{
$this->trashed = (bool) $value;
/**
* Set trashed flag.
*/
public function setTrashed($value)
{
$this->trashed = (bool) $value;
if ($this->trashed) {
// Add deleted_at columns.
$this->columns['deleted_at'] = 'Deleted at';
$this->sort_columns['deleted_at'] = 'deleted_at';
} else {
// Unset deleted_at columns.
unset($this->columns['deleted_at']);
unset($this->sort_columns['deleted_at']);
if ($this->trashed) {
// Add deleted_at columns.
$this->columns['deleted_at'] = 'Deleted at';
$this->sort_columns['deleted_at'] = 'deleted_at';
} else {
// Unset deleted_at columns.
unset($this->columns['deleted_at']);
unset($this->sort_columns['deleted_at']);
// if delete_at was the sorting column
// revert back to default sorting.
if ($this->sort == 'deleted_at') {
$this->sort = $this->default_sort;
}
}
}
// if delete_at was the sorting column
// revert back to default sorting.
if ($this->sort == 'deleted_at') {
$this->sort = $this->default_sort;
}
}
}
/**
* Toggle trashed flag
*/
public function toggleTrashed()
{
$this->setTrashed(!$this->trashed);
}
/**
* Toggle trashed flag
*/
public function toggleTrashed()
{
$this->setTrashed(!$this->trashed);
}
/**
* Delete an record
*/
public function delete($id)
{
if (!$this->delete_enabled) {
return;
}
/**
* Delete an record
*/
public function delete($id)
{
if (!$this->delete_enabled) {
return;
}
$record = $this->model->find($id);
$record = $this->model->find($id);
if (!$record) {
$this->danger('Record not found');
return;
}
if (!$record) {
$this->danger('Record not found');
return;
}
$record->delete();
$record->delete();
$this->info(__($this->getModelName() . " #:id was deleted.", [ 'id' => $record->id ]));
}
$this->info(__($this->getModelName() . " #:id was deleted.", ['id' => $record->id]));
}
/**
* Restore a delted record
*/
public function restore($id)
{
if (!$this->restore_enabled) {
return;
}
/**
* Restore a delted record
*/
public function restore($id)
{
if (!$this->restore_enabled) {
return;
}
$record = $this->model->withTrashed()->find($id);
$record = $this->model->withTrashed()->find($id);
if (!$record) {
$this->danger('Record not found');
return;
}
if (!$record) {
$this->danger('Record not found');
return;
}
$record->restore();
$record->restore();
$this->info(__($this->getModelName() . " #:id was restored.", [ 'id' => $record->id ]));
}
$this->info(__($this->getModelName() . " #:id was restored.", ['id' => $record->id]));
}
/**
* Render the datatable
*/
public function render()
{
$query = $this->model->query();
$query = $this->model->query();
// Only include trashed if asked to.
if ($this->trashed) {
$query->onlyTrashed();
}
// Only include trashed if asked to.
if ($this->trashed) {
$query->onlyTrashed();
}
// Check colums for "dot-notated" strings.
// as we want to eager load those relationships
foreach ($this->columns as $col => $_) {
$p = strrpos($col, '.');
if ($p !== false) {
$rel = substr($col, 0, $p);
$query->with($rel);
}
}
// Check colums for "dot-notated" strings.
// as we want to eager load those relationships
foreach ($this->columns as $col => $_) {
$p = strrpos($col, '.');
if ($p !== false) {
$rel = substr($col, 0, $p);
$query->with($rel);
}
}
// Apply sorting.
$this->sortApply($query);
// Apply sorting.
$this->sortApply($query);
$items = $query->paginate($this->itemsPerPage);
$items = $query->paginate($this->itemsPerPage);
return view('livewire.datatable', ['items' => $items]);
}

View file

@ -14,7 +14,7 @@ class Authenticate extends Middleware
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if (!$request->expectsJson()) {
return route('login');
}
}