Skip to content

Commit

Permalink
Merge pull request #488 from code4romania/401-super-admin-nomenclatoa…
Browse files Browse the repository at this point in the history
…re-implement-reorder-rows-feature-in-tables-repeater-in-edit-mode

Sort service interventions and benefit types
  • Loading branch information
gheorghelupu17 authored Jan 13, 2025
2 parents 2b10dfc + 7a60c0f commit e0ca42e
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/Filament/Admin/Resources/BenefitResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\Benefit;
use App\Models\BenefitService;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
Expand Down Expand Up @@ -41,10 +42,15 @@ public static function form(Form $form): Form
TableRepeater::make('benefitTypes')
->relationship('benefitTypes')
->label(__('nomenclature.headings.benefit_types'))
->helperText(__('nomenclature.helper_texts.benefit_types'))
->reorderable()
->orderColumn()
->columnSpanFull()
->hideLabels()
->addActionLabel(__('nomenclature.actions.add_benefit_type'))
->schema([
Hidden::make('sort'),

TextInput::make('name')
->label(__('nomenclature.labels.benefit_type_name'))
->maxLength(200),
Expand Down
15 changes: 13 additions & 2 deletions app/Filament/Admin/Resources/ServiceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Models\Service;
use App\Models\ServiceIntervention;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
Expand Down Expand Up @@ -77,17 +78,23 @@ public static function form(Form $form): Form
TableRepeater::make('serviceInterventions')
->relationship('serviceInterventions')
->label(__('nomenclature.headings.service_intervention'))
->helperText(__('nomenclature.helper_texts.service_interventions'))
->reorderable()
->orderColumn()
->columnSpanFull()
->addActionLabel(__('nomenclature.actions.add_intervention'))
->schema([
Placeholder::make('id')
Hidden::make('sort'),

Placeholder::make('index')
->label(__('nomenclature.labels.nr'))
->content(function () {
static $index = 1;

return $index++;
})
->hiddenLabel(),

TextInput::make('name')
->columnSpanFull()
->hiddenLabel()
Expand All @@ -107,7 +114,11 @@ public static function form(Form $form): Form
])
->deleteAction(
fn (Action $action) => $action
->disabled(function (array $arguments, TableRepeater $component): bool {
->disabled(function (array $arguments, TableRepeater $component, string $operation): bool {
if ($operation === 'create') {
return false;
}

$items = $component->getState();
$currentItem = $items[$arguments['item']];

Expand Down
8 changes: 8 additions & 0 deletions app/Models/BenefitType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use App\Concerns\HasGeneralStatus;
use App\Models\Scopes\SortOrder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -17,8 +18,15 @@ class BenefitType extends Model
protected $fillable = [
'benefit_id',
'name',
'sort',
];

protected static function booted()
{
parent::booted();
static::addGlobalScope(new SortOrder);
}

public function benefit(): BelongsTo
{
return $this->belongsTo(Benefit::class);
Expand Down
34 changes: 34 additions & 0 deletions app/Models/Scopes/SortOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class SortOrder implements Scope
{
private string $column;

private string $direction;

public function __construct(string $column = 'sort', string $direction = 'asc')
{
$this->column = $column;
$this->direction = $direction;
}

/**
* Apply the scope to a given Eloquent query builder.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function apply(Builder $builder, Model $model): void
{
$builder->orderBy($this->column, $this->direction);
}
}
8 changes: 8 additions & 0 deletions app/Models/ServiceIntervention.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Models;

use App\Concerns\HasGeneralStatus;
use App\Models\Scopes\SortOrder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -19,8 +20,15 @@ class ServiceIntervention extends Model
protected $fillable = [
'service_id',
'name',
'sort',
];

protected static function booted()
{
parent::booted();
static::addGlobalScope(new SortOrder);
}

public function service(): BelongsTo
{
return $this->belongsTo(Service::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('service_interventions', function (Blueprint $table) {
$table->integer('sort')->default(0);
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('benefit_types', function (Blueprint $table) {
$table->integer('sort')->default(0);
});
}
};
2 changes: 2 additions & 0 deletions lang/ro/nomenclature.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
'inactivate_result_modal' => 'Prin inactivarea rezultatului, acesta nu va mai fi disponibil pentru fi adăugat în nomenclatoarele de rezultate ale organizațiilor Sunrise. Pentru organizațiile care au folosit deja acest rezultat, acesta va fi retras din nomenclatoare, fără să se șteargă din istoricul planului de interventie pentru care a fost folosit.',
'delete_benefit' => 'Beneficiile sociale utilizate deja în fișe de beneficiar nu mai pot fi șterse. Aveți doar opțiunea de a le deactiva.',
'result' => 'Definește un rezultat care va deveni disponibil pentru a fi inclus în nomenclatoarele tuturor organizațiilor Sunrise.',
'service_interventions' => 'Ordinea intervențiilor din această listă va fi păstrată în platforma Sunrise pentru afișarea opțiunilor în selectorul de intervenții',
'benefit_types' => 'Ordinea beneficiilor din această listă va fi păstrată în platforma Sunrise pentru afișarea opțiunilor în selectorul de beneficii sociale.',
],

'actions' => [
Expand Down

0 comments on commit e0ca42e

Please sign in to comment.