Skip to content

Commit

Permalink
Merge pull request #84 from lara-zeus/config-model
Browse files Browse the repository at this point in the history
use config for models
  • Loading branch information
atmonshi authored Apr 29, 2023
2 parents a670617 + 61b2d1c commit 916cbc5
Show file tree
Hide file tree
Showing 21 changed files with 71 additions and 64 deletions.
10 changes: 10 additions & 0 deletions config/zeus-sky.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
*/
'page_uri_prefix' => 'page',

/**
* customize the models
*/
'models' => [
'faq' => \LaraZeus\Sky\Models\Faq::class,
'post' => \LaraZeus\Sky\Models\Post::class,
'postStatus' => \LaraZeus\Sky\Models\PostStatus::class,
'tag' => \LaraZeus\Sky\Models\Tag::class,
],

/**
* enable or disable individual Resources.
*/
Expand Down
6 changes: 4 additions & 2 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use LaraZeus\Sky\Models\Post;

class PostFactory extends Factory
{
protected $model = Post::class;
public function getModel(): string
{
return config('zeus-sky.models.post');
}

/**
* Define the model's default state.
Expand Down
14 changes: 6 additions & 8 deletions database/seeders/SkySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use LaraZeus\Sky\Models\Post;
use LaraZeus\Sky\Models\Tag;

class SkySeeder extends Seeder
{
public function run()
{
Tag::create(['name' => ['en' => 'laravel', 'ar' => 'لارافل'], 'type' => 'category']);
Tag::create(['name' => ['en' => 'talks', 'ar' => 'اخبار'], 'type' => 'category']);
Tag::create(['name' => ['en' => 'dev', 'ar' => 'تطوير'], 'type' => 'category']);
config('zeus-sky.models.tag')::create(['name' => ['en' => 'laravel', 'ar' => 'لارافل'], 'type' => 'category']);
config('zeus-sky.models.tag')::create(['name' => ['en' => 'talks', 'ar' => 'اخبار'], 'type' => 'category']);
config('zeus-sky.models.tag')::create(['name' => ['en' => 'dev', 'ar' => 'تطوير'], 'type' => 'category']);

Post::factory()
config('zeus-sky.models.post')::factory()
->count(8)
->create();

foreach (Post::all() as $post) { // loop through all posts
$random_tags = Tag::all()->random(1)->first()->name;
foreach (config('zeus-sky.models.post')::all() as $post) { // loop through all posts
$random_tags = config('zeus-sky.models.tag')::all()->random(1)->first()->name;
$post->syncTagsWithType([$random_tags], 'category');
}
}
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"Navigations": "قوائم التنقل",
"Navigation": "قائمة التنقل",
"Select Post": "اختر التدوينة",
"Select Page": "اختر الصفحة"
"Select Page": "اختر الصفحة",
"Recent Post": "أحدث التدوينات"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@unless($recent->isEmpty())
<div class="my-4">
<h4 class="mb-4 text-xl font-bold text-gray-700 dark:text-gray-200">Recent Post</h4>
<h4 class="mb-4 text-xl font-bold text-gray-700 dark:text-gray-200">{{ __('Recent Post') }}</h4>
<div class="flex flex-col max-w-sm px-4 py-6 mx-auto bg-white dark:bg-gray-800 rounded-[2rem] ltr:rounded-br-none rtl:rounded-bl-none shadow-md">
@foreach($recent as $post)
<a href="{{ route('post',$post->slug) }}" class="border-b border-t border-white hover:border-primary-600 transition duration-300 px-1 py-4">
Expand Down
12 changes: 5 additions & 7 deletions src/Classes/RenderNavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

namespace LaraZeus\Sky\Classes;

use LaraZeus\Sky\Models\Post;

class RenderNavItem
{
public static function render($item, $class = '')
{
if ($item['type'] === 'page-link') {
$page = Post::page()->find($item['data']['page_id']) ?? '';
$page = config('zeus-sky.models.post')::page()->find($item['data']['page_id']) ?? '';

return '<a class="' . $class . '"
target="' . ($item['data']['target'] ?? '_self') . '"
target="' . ($item['data']['target'] ?? '_self') . '"
href="' . route('page', $page) . '"
>' .
$item['label'] .
'</a>';
} elseif ($item['type'] === 'post-link') {
$post = Post::find($item['data']['post_id']) ?? '';
$post = config('zeus-sky.models.post')::find($item['data']['post_id']) ?? '';

return '<a class="' . $class . '"
target="' . ($item['data']['target'] ?? '_self') . '"
target="' . ($item['data']['target'] ?? '_self') . '"
href="' . route('post', $post) . '"
>' .
$item['label'] .
'</a>';
} else {
return '<a class="' . $class . '"
target="' . ($item['data']['target'] ?? '_self') . '"
target="' . ($item['data']['target'] ?? '_self') . '"
href="' . $item['data']['url'] . '"
>' .
$item['label'] .
Expand Down
3 changes: 1 addition & 2 deletions src/Console/migrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LaraZeus\Sky\Console;

use Illuminate\Console\Command;
use LaraZeus\Sky\Models\Post;

class migrateCommand extends Command
{
Expand All @@ -28,7 +27,7 @@ class migrateCommand extends Command
*/
public function handle()
{
$posts = Post::get();
$posts = config('zeus-sky.models.post')::get();
foreach ($posts as $post) {
$post->translatable = [];

Expand Down
5 changes: 4 additions & 1 deletion src/Filament/Resources/FaqResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

class FaqResource extends SkyResource
{
protected static ?string $model = Faq::class;
public static function getModel(): string
{
return config('zeus-sky.models.faq') ?? Faq::class;
}

protected static ?string $navigationIcon = 'iconpark-folderwithdrawal-o';

Expand Down
14 changes: 8 additions & 6 deletions src/Filament/Resources/PageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@
use Illuminate\Support\Str;
use LaraZeus\Sky\Filament\Resources\PageResource\Pages;
use LaraZeus\Sky\Models\Post;
use LaraZeus\Sky\Models\PostStatus;
use Mohamedsabil83\FilamentFormsTinyeditor\Components\TinyEditor;

class PageResource extends SkyResource
{
protected static ?string $model = Post::class;
public static function getModel(): string
{
return config('zeus-sky.models.post') ?? Post::class;
}

protected static ?string $slug = 'pages';

protected static ?string $navigationIcon = 'iconpark-folder-o';

protected static function getNavigationBadge(): ?string
{
return (string) Post::query()->page()->count();
return (string) config('zeus-sky.models.post')::query()->page()->count();
}

public static function form(Form $form): Form
Expand Down Expand Up @@ -84,7 +86,7 @@ public static function form(Form $form): Form
->label(__('Post Slug')),

Select::make('parent_id')
->options(Post::where('post_type', 'page')->pluck('title', 'id'))
->options(config('zeus-sky.models.post')::where('post_type', 'page')->pluck('title', 'id'))
->label(__('Parent Page')),

TextInput::make('ordering')
Expand All @@ -102,7 +104,7 @@ public static function form(Form $form): Form
->default('publish')
->required()
->reactive()
->options(PostStatus::pluck('label', 'name')),
->options(config('zeus-sky.models.postStatus')::pluck('label', 'name')),

TextInput::make('password')
->label(__('Password'))
Expand Down Expand Up @@ -148,7 +150,7 @@ public static function table(Table $table): Table
SelectFilter::make('status')
->multiple()
->label(__('Status'))
->options(PostStatus::pluck('label', 'name')),
->options(config('zeus-sky.models.postStatus')::pluck('label', 'name')),

Filter::make('password')
->label(__('Password Protected'))
Expand Down
3 changes: 1 addition & 2 deletions src/Filament/Resources/PageResource/Pages/ListPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use LaraZeus\Sky\Filament\Resources\PageResource;
use LaraZeus\Sky\Models\Post;

class ListPosts extends ListRecords
{
Expand All @@ -15,6 +14,6 @@ class ListPosts extends ListRecords

protected function getTableQuery(): Builder
{
return Post::where('post_type', 'page');
return config('zeus-sky.models.post')::where('post_type', 'page');
}
}
12 changes: 7 additions & 5 deletions src/Filament/Resources/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@
use Illuminate\Support\Str;
use LaraZeus\Sky\Filament\Resources\PostResource\Pages;
use LaraZeus\Sky\Models\Post;
use LaraZeus\Sky\Models\PostStatus;
use Mohamedsabil83\FilamentFormsTinyeditor\Components\TinyEditor;

class PostResource extends SkyResource
{
protected static ?string $model = Post::class;
public static function getModel(): string
{
return config('zeus-sky.models.post');
}

protected static ?string $navigationIcon = 'iconpark-docdetail-o';

protected static function getNavigationBadge(): ?string
{
return (string) Post::query()->posts()->count();
return (string) config('zeus-sky.models.post')::query()->posts()->count();
}

public static function form(Form $form): Form
Expand Down Expand Up @@ -107,7 +109,7 @@ public static function form(Form $form): Form
->default('publish')
->required()
->reactive()
->options(PostStatus::pluck('label', 'name')),
->options(config('zeus-sky.models.postStatus')::pluck('label', 'name')),

TextInput::make('password')
->label(__('Password'))
Expand Down Expand Up @@ -185,7 +187,7 @@ public static function table(Table $table): Table
SelectFilter::make('status')
->multiple()
->label(__('Status'))
->options(PostStatus::pluck('label', 'name')),
->options(config('zeus-sky.models.postStatus')::pluck('label', 'name')),

Filter::make('password')
->label(__('Password Protected'))
Expand Down
3 changes: 1 addition & 2 deletions src/Filament/Resources/PostResource/Pages/ListPosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use LaraZeus\Sky\Filament\Resources\PostResource;
use LaraZeus\Sky\Models\Post;

class ListPosts extends ListRecords
{
Expand All @@ -15,6 +14,6 @@ class ListPosts extends ListRecords

protected function getTableQuery(): Builder
{
return Post::where('post_type', 'post');
return config('zeus-sky.models.post')::where('post_type', 'post');
}
}
8 changes: 5 additions & 3 deletions src/Filament/Resources/TagResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use LaraZeus\Sky\Filament\Resources\TagResource\Pages;
use LaraZeus\Sky\Models\Tag;

class TagResource extends SkyResource
{
protected static ?string $model = Tag::class;
public static function getModel(): string
{
return config('zeus-sky.models.tag');
}

protected static ?string $navigationIcon = 'iconpark-tag-o';

Expand All @@ -28,7 +30,7 @@ protected static function shouldRegisterNavigation(): bool

protected static function getNavigationBadge(): ?string
{
return (string) Tag::query()->count();
return (string) config('zeus-sky.models.tag')::query()->count();
}

public static function form(Form $form): Form
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Livewire/Faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LaraZeus\Sky\Http\Livewire;

use LaraZeus\Sky\Models\Faq as Faqs;
use Livewire\Component;

class Faq extends Component
Expand All @@ -19,7 +18,7 @@ public function render()
->twitter();

return view(app('theme') . '.addons.faq')
->with('faqs', Faqs::get())
->with('faqs', config('zeus-sky.models.faq')::get())
->layout(config('zeus-sky.layout'));
}
}
5 changes: 2 additions & 3 deletions src/Http/Livewire/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LaraZeus\Sky\Http\Livewire;

use LaraZeus\Sky\Models\Post;
use Livewire\Component;

class Page extends Component
Expand All @@ -11,7 +10,7 @@ class Page extends Component

public function mount($slug)
{
$this->page = Post::where('slug', $slug)->page()->firstOrFail();
$this->page = config('zeus-sky.models.post')::where('slug', $slug)->page()->firstOrFail();

if ($this->page->status !== 'publish') {
abort_if(! auth()->check(), 404);
Expand All @@ -38,7 +37,7 @@ public function render()
->with([
'post' => $this->page,
/** @phpstan-ignore-next-line */
'children' => Post::with('parent')->where('parent_id', $this->page->id)->get(),
'children' => config('zeus-sky.models.post')::with('parent')->where('parent_id', $this->page->id)->get(),
])
->layout(config('zeus-sky.layout'));
}
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Livewire/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace LaraZeus\Sky\Http\Livewire;

use LaraZeus\Sky\Models\Post as postModel;
use Livewire\Component;

class Post extends Component
Expand All @@ -11,7 +10,7 @@ class Post extends Component

public function mount($slug)
{
$this->post = postModel::where('slug', $slug)->firstOrFail();
$this->post = config('zeus-sky.models.post')::where('slug', $slug)->firstOrFail();

if ($this->post->status !== 'publish') {
abort_if(! auth()->check(), 404);
Expand All @@ -36,7 +35,7 @@ public function render()

return view(app('theme') . '.post')
->with('post', $this->post)
->with('related', postModel::related($this->post)->take(4)->get())
->with('related', config('zeus-sky.models.post')::related($this->post)->take(4)->get())
->layout(config('zeus-sky.layout'));
}
}
Loading

0 comments on commit 916cbc5

Please sign in to comment.