Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/merges/tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
'chats' => 'umod_chats',
'chat_messages' => 'umod_chat_messages',
'spreads' => 'umod_spreads',
'spreadsheets' => 'umod_spreadsheets',

];
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public function up()
createDefaultTableFields($table);
$table->uuidMorphs('spreadable');
$table->json('content')->default(new Expression('(JSON_ARRAY())'));
$table->timestamps();
$table->softDeletes();
createDefaultExtraTableFields($table);
});

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

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Str;



class CreateSpreadsheetsTable extends Migration
{
public function up()
{
$modularitySpreadsheetsTable = modularityConfig('tables.spreadsheets', 'modularity_spreadsheets');

Schema::create($modularitySpreadsheetsTable, function (Blueprint $table) {
// this will create an id, name field
createDefaultTableFields($table);
$table->uuidMorphs('spreadsheetable');
$table->json('content')->default(new Expression('(JSON_ARRAY())'));
$table->string('role')->nullable();
$table->string('locale')->nullable();
// a "published" column, and soft delete and timestamps columns
createDefaultExtraTableFields($table);
});

Schema::create('spreadsheet_translations', function (Blueprint $table) {
// createDefaultTranslationsTableFields($table, Str::singular(modularityConfig('tables.spreadsheets', 'modularity_spreadsheets')));
createDefaultTranslationsTableFields($table, 'spreadsheet');

$table->json('content')->default(new Expression('(JSON_ARRAY())'));;
});


}

public function down()
{
Schema::dropIfExists('spreadsheet_translations');
Schema::dropIfExists(modularityConfig('tables.spreadsheets', 'modularity_spreadsheets'));

}
}
5 changes: 3 additions & 2 deletions operations/2025_01_23_203858_payments_table_operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\ConsoleOutput;
use TimoKoerber\LaravelOneTimeOperations\OneTimeOperation;
use Unusualify\Modularity\Facades\Modularity;

return new class extends OneTimeOperation
{
Expand Down Expand Up @@ -35,7 +36,6 @@ public function __construct()
*/
public function process(): void
{

if (! Schema::hasTable('umod_payments')
&& Schema::hasTable('unfy_payments')) {

Expand All @@ -50,8 +50,9 @@ public function process(): void
}else {

Artisan::call('migrate', [
'--path' => 'vendor/unusualify/modularity/database/migrations/default/2024_06_24_125121_create_payments_table.php'
'--path' => Modularity::getVendorPath('database/migrations/default/2024_06_24_125121_create_payments_table.php')
]);

$this->info("\tumod_payments created");

}
Expand Down
4 changes: 2 additions & 2 deletions src/Entities/Spread.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Spread extends Model
protected $fillable = [
'name',
'published',
'content',
'content'
];

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

// protected function casts(): array
Expand Down
43 changes: 43 additions & 0 deletions src/Entities/Spreadsheet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Unusualify\Modularity\Entities;

use Unusualify\Modularity\Entities\Model;
use Modules\PressRelease\Entities\Report;
use Unusualify\Modularity\Entities\Traits\HasTranslation;

class Spreadsheet extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
use HasTranslation;

public $translatedAttributes = [
'content'
];

protected $fillable = [
'published',
// 'content',
'role',
'locale'
];

protected $casts = [
'content' => 'array'
];

public function __construct(array $attributes = [])
{
$this->table = $this->getTable();
parent::__construct($attributes);
}

public function getTable()
{
return modularityConfig('tables.spreadsheets', 'modularity_spreads');
}
}
84 changes: 23 additions & 61 deletions src/Entities/Traits/HasSpreadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,112 +9,74 @@ trait HasSpreadable
{
use ManageEloquent;

protected $_pendingSpreadData;
protected $_pendingSpreadData = [];



public static function bootHasSpreadable()
{
// TODO: Keep the old spreadable data from model and remove attributes based on that don't remove all column fields
self::saving(static function (Model $model) {
// Store the spread data before cleaning
if (! $model->exists) {
// Set property to preserve data through events
$model->_pendingSpreadData = $model->_spread ?: $model->prepareSpreadableJson();
} elseif ($model->_spread) {
// Handle existing spread updates
self::saving(static function(Model $model) {

if (!$model->exists) {
// For new models, preserve the spread data for after creation.

$model->_pendingSpreadData = array_merge($model->_pendingSpreadData, $model->_spread);

} else if($model->_spread){
// For existing models, rebuild the _spread attribute from the spreadable fillable inputs,
// and clean those fillables from the model.

$model->spreadable()->update([
'content' => $model->_spread,
'content' => $model->_spread
]);
}

$model->cleanSpreadableAttributes();
$model->offsetUnset('_spread');
});

self::created(static function (Model $model) {
$model->spreadable()->create($model->_pendingSpreadData ?? []);
self::created(static function(Model $model) {
$model->spreadable()->create([
'content' => $model->_pendingSpreadData ?? []
]);
});

self::retrieved(static function (Model $model) {
// If there's a spread model, load its attributes
// dd('text');
// dd($model);
self::retrieved(static function(Model $model) {
if ($model->spreadable) {
$jsonData = $model->spreadable->content ?? [];

// Set spreadable attributes on model, excluding protected attributes
foreach ($jsonData as $key => $value) {
if (! $model->isProtectedAttribute($key)) {
$model->setAttribute($key, $value);
}
}

// Set _spread attribute
$model->setAttribute('_spread', $jsonData);
// dd($model->_spread);

} else {
// dd('here');
// Initialize empty _spread if no spreadable exists
$model->setAttribute('_spread', []);
}
});

}

public function initializeHasSpreadable()
{
$this->mergeFillable(['_spread']);
$spreadableFields = ['_spread'];
$this->mergeFillable($spreadableFields);
}

// TODO: rename relation to spread as well
public function spreadable(): \Illuminate\Database\Eloquent\Relations\MorphOne
{
return $this->morphOne(\Unusualify\Modularity\Entities\Spread::class, 'spreadable');
}

protected function isProtectedAttribute(string $key): bool
{

return in_array($key, $this->getReservedKeys());
}

public function getReservedKeys(): array
{

return array_merge(
$this->getColumns(), // Using ManageEloquent's getColumns
$this->definedRelations(), // Using ManageEloquent's definedRelations
array_keys($this->getMutatedAttributes()),
['spreadable', '_spread']
);
}

protected function prepareSpreadableJson(): array
{
$attributes = $this->getAttributes();
$protectedKeys = array_merge(
$this->getColumns(), // Using ManageEloquent's getColumns
$this->definedRelations(), // Using ManageEloquent's definedRelations
array_keys($this->getMutatedAttributes()),
['spreadable', '_spread']
);

return array_diff_key(
$attributes,
array_flip($protectedKeys)
);
}

protected function cleanSpreadableAttributes(): void
{
$columns = $this->getColumns();
$attributes = $this->getAttributes();
// TODO: Instead of removing any attribute remove the ones that you know that needs to be removed
// Remove any attributes that aren't database columns
foreach ($attributes as $key => $value) {
if (! in_array($key, $columns)) {
unset($this->attributes[$key]);
}
}

}
}
96 changes: 96 additions & 0 deletions src/Entities/Traits/HasSpreadsheetable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Unusualify\Modularity\Entities\Traits;

use Illuminate\Database\Eloquent\Model;

trait HasSpreadsheetable
{


protected $_pendingSpreadsheetData = [];

/**
* Perform any actions when booting the trait
*
* @return void
*/
public static function bootHasSpreadsheetable(): void
{
static::retrieved(function (Model $model) {
// Assuming you have one spreadsheet per locale
$spreadsheet = $model->spreadsheetable()->first();
if ($spreadsheet) {
// Load the spreadsheet's translation for the current locale
$translatedSpreadsheet = $spreadsheet->translateOrDefault(app()->getLocale());
$model->setAttribute('_spreadsheet', $translatedSpreadsheet->content);
} else {
$model->setAttribute('_spreadsheet', []);
}
});

static::creating(function (Model $model) {});

static::created(function (Model $model) {
$model->spreadsheetable()->create([
'content' => $model->_pendingSpreadsheetData ?? [],
'locale' => app()->getLocale(),
]);
});

static::updating(function (Model $model) {});

static::updated(function (Model $model) {});

static::saving(function (Model $model) {
if (!$model->exists) {
$model->_pendingSpreadsheetData = array_merge($model->_pendingSpreadsheetData, $model->_spreadsheet ?? []);
} else if ($model->_spreadsheet) {
$locale = app()->getLocale();
// Update or create the spreadsheet translation via the relationship
$model->spreadsheetable()->updateOrCreate(
['locale' => $locale],
['content' => $model->_spreadsheet]
);
}
$model->offsetUnset('_spreadsheet');
});

static::saved(function (Model $model) {});

static::restoring(function (Model $model) {});

static::restored(function (Model $model) {});

static::replicating(function (Model $model) {});

static::deleting(function (Model $model) {});

static::deleted(function (Model $model) {});

static::forceDeleting(function (Model $model) {});

static::forceDeleted(function (Model $model) {});
}

/**
* Laravel hook to initialize the trait
*
* @return void
*/
public function initializeHasSpreadsheetable(): void
{
$spreadsheetableFields = ['_spreadsheet'];
$this->mergeFillable($spreadsheetableFields);
}

public function spreadsheetable(): \Illuminate\Database\Eloquent\Relations\MorphMany
{
return $this->morphMany(\Unusualify\Modularity\Entities\Spreadsheet::class, 'spreadsheetable');
}

// protected function handleTranslatedSpreadsheet($data){

// }

}
17 changes: 17 additions & 0 deletions src/Entities/Translations/SpreadsheetTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Unusualify\Modularity\Entities\Translations;

use Unusualify\Modularity\Entities\Spreadsheet;
use Unusualify\Modularity\Entities\Model;

class SpreadsheetTranslation extends Model
{
protected $baseModuleModel = Spreadsheet::class;

protected $fillable = ['content', 'locale'];

protected $casts = [
'content' => 'array',
];
}
Loading