Skip to content

Commit

Permalink
Merge pull request #8 from PortableStudios/feature/FILACMS-10_Add_tax…
Browse files Browse the repository at this point in the history
…onomy_relationships

Feature/filacms 10 add taxonomy relationships
  • Loading branch information
kyoungportable authored Mar 4, 2024
2 parents 9e17ba7 + f0d6cec commit 4edfdeb
Show file tree
Hide file tree
Showing 45 changed files with 1,000 additions and 96 deletions.
1 change: 1 addition & 0 deletions config/fila-cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return [
'use_admin_panel' => true,
'admin_prefix' => 'admin',
'publish_content_routes' => true,
'admin_plugins' => [
\Portable\FilaCms\Plugins\UsersPlugin::class,
\Portable\FilaCms\Plugins\PermissionsPlugin::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

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

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('taxonomy_resources', function (Blueprint $table) {
$table->string('resource_class');
$table->foreignId('taxonomy_id')->constrained()->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('taxonomy_resources');
}
};
28 changes: 28 additions & 0 deletions database/migrations/2024_02_23_222545_create_taxonomyables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('taxonomyables', function (Blueprint $table) {
$table->foreignId('taxonomy_term_id')->references('id')->on('taxonomy_terms')->cascadeOnDelete();
$table->foreignId('taxonomyable_id');
$table->string('taxonomyable_type');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('taxonomyables');
}
};
71 changes: 71 additions & 0 deletions resources/css/filacms.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

.main-content {
@apply md:pl-12 md:pr-12;
}

.page-header {
@apply bg-gray-200 md:pl-12 md:pr-12 pt-4 pb-4 mb-4;
}

.logo-wrapper {
@apply border bg-white p-4 pt-2 pb-2 inline-block rounded-lg;
}

.page-footer {
@apply bg-gray-200 md:pl-12 md:pr-12 pt-4 pb-4 mt-4 text-center;
}

h1 {
@apply text-3xl font-bold mb-4 pb-2 border-b;
}

.filters-column {
@apply w-1/5;
}

.listing-column {
@apply w-4/5 pl-6 w-full;
}

.btn-primary {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}

.authors {
@apply italic mb-2;
}

.taxonomies {
@apply text-sm pt-4;
}

.taxonomy-term {
@apply inline-block rounded bg-gray-100 p-1;
}

.content-listing-card {
@apply md:w-1/5 w-full border rounded-lg inline-block;
}

.content-listing-card__image {
@apply w-full h-32 rounded-t-lg overflow-hidden;
}

.content-listing-card__content {
@apply p-4;
}

.content-listing-card__title {
@apply text-lg font-bold mb-2;
}

.content-listing-card__excerpt {
@apply text-sm;
}

.content-listing-card a {
@apply italic;
}
5 changes: 5 additions & 0 deletions routes/frontend-routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Portable\FilaCms\Facades\FilaCms;

FilaCms::contentRoutes();
25 changes: 25 additions & 0 deletions src/Casts/DynamicTermIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Portable\FilaCms\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class DynamicTermIds implements CastsAttributes
{
protected $taxonomyId;

public function __construct($taxonomyId)
{
$this->taxonomyId = $taxonomyId;
}

public function get($model, $key, $value, $attributes)
{
return $model->terms()->where('taxonomy_id', $this->taxonomyId)->pluck('id');
}

public function set($model, $key, $value, $attributes)
{
return [$key => $value];
}
}
25 changes: 25 additions & 0 deletions src/Casts/DynamicTermList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Portable\FilaCms\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class DynamicTermList implements CastsAttributes
{
protected $taxonomyId;

public function __construct($taxonomyId)
{
$this->taxonomyId = $taxonomyId;
}

public function get($model, $key, $value, $attributes)
{
return $model->terms()->where('taxonomy_id', $this->taxonomyId)->get();
}

public function set($model, $key, $value, $attributes)
{
return [$key => $value];
}
}
2 changes: 1 addition & 1 deletion src/Events/ContentUpdating.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class ContentUpdating
*/
public function __construct(public Page $order)
{
$order->updated_user_id = auth()->user()->id;
$order->updated_user_id = auth()->user() ? auth()->user()->id : $order->created_user_id;
}
}
13 changes: 13 additions & 0 deletions src/Facades/FilaCms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Portable\FilaCms\Facades;

use Illuminate\Support\Facades\Facade;

class FilaCms extends Facade
{
protected static function getFacadeAccessor()
{
return 'fila-cms';
}
}
74 changes: 74 additions & 0 deletions src/FilaCms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Portable\FilaCms;

use Filament\Facades\Filament;
use Illuminate\Support\Facades\Route;
use Portable\FilaCms\Filament\Resources\AbstractContentResource;
use Portable\FilaCms\Livewire\ContentResourceList;
use Portable\FilaCms\Livewire\ContentResourceShow;
use ReflectionClass;

class FilaCms
{
protected static $contentResources = null;

protected static $contentModels = null;

public function getContentModelResource($modelClass)
{
if (is_null(self::$contentModels)) {
$this->getContentModels();
}

return isset(self::$contentModels[$modelClass]) ? self::$contentModels[$modelClass] : null;
}

public function getContentModels()
{
if (! is_null(self::$contentResources) && ! is_null(self::$contentModels)) {
return self::$contentResources;
}

$options = [];
static::$contentModels = [];
foreach (Filament::getPanels() as $panel) {
foreach ($panel->getResources() as $resourceClass) {
$reflectionObject = new ReflectionClass($resourceClass);
if ($reflectionObject->isSubclassOf(AbstractContentResource::class)) {
$options[$resourceClass] = $resourceClass::getNavigationLabel();
static::$contentModels[$resourceClass::getModel()] = $resourceClass;
}
}
}
static::$contentResources = $options;

return $options;
}

public function contentRoutes()
{
$this->getContentModels();
foreach (static::$contentModels as $modelClass => $resourceClass) {
$prefix = method_exists($resourceClass, 'getFrontendRoutePrefix') ? $resourceClass::getFrontendRoutePrefix() : $resourceClass::getRoutePrefix();
$registerIndex = method_exists($resourceClass, 'registerIndexRoute') ? $resourceClass::registerIndexRoute() : true;
$registerShow = method_exists($resourceClass, 'registerShowRoute') ? $resourceClass::registerShowRoute() : true;
$feIndexComponent = method_exists($resourceClass, 'getFrontendIndexComponent') ? $resourceClass::getFrontendIndexComponent() : ContentResourceList::class;
$feShowComponent = method_exists($resourceClass, 'getFrontendShowComponent') ? $resourceClass::getFrontendShowComponent() : ContentResourceShow::class;

Route::group(
['prefix' => $prefix, 'middleware' => 'web'],
function () use ($feShowComponent, $prefix, $registerIndex, $registerShow, $feIndexComponent, $modelClass) {
if ($registerIndex) {
Route::get('/', $feIndexComponent)
->name($prefix.'.index')
->defaults('model', $modelClass);
}
if ($registerShow) {
Route::get('/{slug}', $feShowComponent)->name($prefix.'.show')->defaults('model', $modelClass);
}
}
);
}
}
}
Loading

0 comments on commit 4edfdeb

Please sign in to comment.