Skip to content

Commit

Permalink
first commit 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Oct 30, 2024
1 parent 8ea800b commit 44fd974
Show file tree
Hide file tree
Showing 13 changed files with 251 additions and 56 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
"keywords": [
"php",
"laravel",
"template"
"meta",
"filamentphp",
"model",
"relation",
"relation manager",
"pluggable model"
],
"license": "MIT",
"autoload": {
Expand Down Expand Up @@ -48,7 +53,9 @@
},
"require": {
"php": "^8.1|^8.2",
"tomatophp/console-helpers": "^1.1"
"tomatophp/console-helpers": "^1.1",
"filament/filament": "^3.2",
"genealabs/laravel-model-caching": "^11.0"
},
"require-dev": {
"laravel/pint": "^1.18",
Expand Down
42 changes: 42 additions & 0 deletions database/migrations/2010_02_13_134649_create_metas_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('metas', function (Blueprint $table) {
$table->id();

$table->string('model_id');
$table->string('model_type');

$table->string('connected_id')->nullable();
$table->string('connected_type')->nullable();

$table->string('key')->index();
$table->json('value')->nullable();

$table->timestamps();
});

}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('metas');
}
};

This file was deleted.

9 changes: 8 additions & 1 deletion resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

return [

"label" => "المعلومات",
"single" => "معلومة",
"create" => "إضافة معلومة",
"columns" => [
"model" => "النموذج",
"key" => "المفتاح",
"value" => "القيمة"
],
];
9 changes: 8 additions & 1 deletion resources/lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

return [

"label" => "Metas",
"single" => "Meta",
"create" => "Create Meta",
"columns" => [
"model" => "Model",
"key" => "Key",
"value" => "Value"
],
];
8 changes: 8 additions & 0 deletions resources/views/table-columns/value.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<span class="fi-ta-text-item-label text-sm leading-6 text-gray-950 dark:text-white">
@if(is_array($getState()))
{{ json_encode($getState(), JSON_PRETTY_PRINT) }}
@else
{{ $getState() }}
@endif

</span>
Empty file removed src/Filament/Pages/.gitkeep
Empty file.
59 changes: 59 additions & 0 deletions src/Filament/RelationManager/MetaRelationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace TomatoPHP\FilamentMeta\Filament\RelationManager;

use Filament\Forms\Form;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Table;
use Filament\Tables;

class MetaRelationManager extends RelationManager
{
protected static string $relationship = 'modelMeta';

public function form(Form $form): Form
{
return $form->schema([
Forms\Components\TextInput::make('key')
->label(trans('filament-meta::messages.columns.key'))
->required()
->maxLength(255),
Forms\Components\TextInput::make('value')
->label(trans('filament-meta::messages.columns.value'))
]);
}

public function table(Table $table): Table
{
return $table
->headerActions([
CreateAction::make()
])
->columns([
Tables\Columns\TextColumn::make('key')
->label(trans('filament-meta::messages.columns.key'))
->sortable(),
Tables\Columns\TextColumn::make('value')
->label(trans('filament-meta::messages.columns.value'))
->view('filament-meta::table-columns.value')
])
->headerActions([
Tables\Actions\CreateAction::make()
])
->filters([

])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Empty file removed src/Filament/Resources/.gitkeep
Empty file.
Empty file removed src/Filament/Widgets/.gitkeep
Empty file.
23 changes: 14 additions & 9 deletions src/FilamentMetaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace TomatoPHP\FilamentMeta;

use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;
use TomatoPHP\FilamentMeta\Filament\RelationManager\MetaRelationManager;


class FilamentMetaServiceProvider extends ServiceProvider
Expand All @@ -13,41 +15,44 @@ public function register(): void
$this->commands([
\TomatoPHP\FilamentMeta\Console\FilamentMetaInstall::class,
]);

//Register Config file
$this->mergeConfigFrom(__DIR__.'/../config/filament-meta.php', 'filament-meta');

//Publish Config
$this->publishes([
__DIR__.'/../config/filament-meta.php' => config_path('filament-meta.php'),
], 'filament-meta-config');

//Register Migrations
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

//Publish Migrations
$this->publishes([
__DIR__.'/../database/migrations' => database_path('migrations'),
], 'filament-meta-migrations');
//Register views
$this->loadViewsFrom(__DIR__.'/../resources/views', 'filament-meta');

//Publish Views
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/filament-meta'),
], 'filament-meta-views');

//Register Langs
$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'filament-meta');

//Publish Lang
$this->publishes([
__DIR__.'/../resources/lang' => base_path('lang/vendor/filament-meta'),
], 'filament-meta-lang');

//Register Routes
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');



Livewire::component('tomato-p-h-p.filament-meta.filament.relation-manager.meta-relation-manage', MetaRelationManager::class);

}

public function boot(): void
Expand Down
60 changes: 60 additions & 0 deletions src/Models/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace TomatoPHP\FilamentMeta\Models;

use GeneaLabs\LaravelModelCaching\CachedModel;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* @property integer $id
* @property string $model_id
* @property string $model_type
* @property string $connected_id
* @property string $connected_type
* @property string $key
* @property mixed $value
* @property string $created_at
* @property string $updated_at
*/
class Meta extends CachedModel
{
use Cachable;

protected $cachePrefix = "tomato_meta_";

/**
* @var array
*/
protected $fillable = [
'connected_id',
'connected_type',
'model_id',
'model_type',
'key',
'value',
'created_at',
'updated_at'
];

protected $casts = [
'value' => 'array',
];

/**
* @return MorphTo
*/
public function model(): MorphTo
{
return $this->morphTo();
}


/**
* @return MorphTo
*/
public function connected(): MorphTo
{
return $this->morphTo();
}
}
43 changes: 43 additions & 0 deletions src/Traits/HasMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace TomatoPHP\FilamentMeta\Traits;

use Illuminate\Database\Eloquent\Relations\MorphMany;

trait HasMeta
{
/**
* @return MorphMany
*/
public function modelMeta(): MorphMany
{
return $this->morphMany('TomatoPHP\FilamentMeta\Models\Meta', 'model');
}


/**
* @param string $key
* @param mixed $value
* @return mixed
*/
public function meta(string $key, mixed $value=null): mixed
{
if($value!==null){
if($value === 'null'){
return $this->modelMeta()->updateOrCreate(['key' => $key], ['value' => null]);
}
else {
return $this->modelMeta()->updateOrCreate(['key' => $key], ['value' => $value]);
}
}
else {
$meta = $this->modelMeta()->where('key', $key)->first();
if($meta){
return $meta->value;
}
else {
return $this->modelMeta()->updateOrCreate(['key' => $key], ['value' => null]);
}
}
}
}

0 comments on commit 44fd974

Please sign in to comment.