Skip to content

Commit b18dbea

Browse files
committed
wip
1 parent 93695fa commit b18dbea

File tree

17 files changed

+903
-5
lines changed

17 files changed

+903
-5
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"javaabu/translatable": "^1.1",
2323
"javaabu/menu-builder": "^1.6",
2424
"kalnoy/nestedset": "^6.0",
25-
"javaabu/auth": "^1.16"
25+
"javaabu/auth": "^1.16",
26+
"diglactic/laravel-breadcrumbs": "^10.0",
27+
"lorisleiva/laravel-actions": "^2.9"
2628
},
2729
"require-dev": {
2830
"laravel/pint": "^1.14",

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@
4949
],
5050

5151
'should_translate' => false,
52+
53+
'use_default_view_for' => ['downloads', 'announcements', 'publications', 'jobs', 'blogs'],
5254
];

database/factories/PostFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Carbon;
7+
use Javaabu\Cms\Models\Post;
8+
use Javaabu\Cms\Models\PostType;
9+
use Javaabu\Cms\Support\Faker\Factory\ContentBlockFactory;
10+
11+
class PostFactory extends ContentBlockFactory
12+
{
13+
protected $model = Post::class;
14+
15+
public function definition(): array
16+
{
17+
return [
18+
'title' => $this->faker->word(),
19+
'slug' => $this->faker->slug(),
20+
'content' => $this->getContentBlock(),
21+
'excerpt' => $this->faker->word(),
22+
'menu_order' => $this->faker->randomNumber(),
23+
'status' => $this->faker->word(),
24+
'published_at' => Carbon::now(),
25+
'created_at' => Carbon::now(),
26+
'updated_at' => Carbon::now(),
27+
'document_no' => $this->faker->word(),
28+
'expire_at' => Carbon::now(),
29+
'format' => $this->faker->word(),
30+
'video_url' => $this->faker->url(),
31+
'page_style' => $this->faker->word(),
32+
'ref_no' => $this->faker->word(),
33+
'recently_updated' => $this->faker->boolean(),
34+
'coords' => $this->faker->word(),
35+
'city_id' => $this->faker->randomNumber(),
36+
37+
'type' => random_id_or_generate(PostType::class, 'slug'),
38+
];
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Illuminate\Support\Carbon;
7+
use Javaabu\Cms\Models\CategoryType;
8+
use Javaabu\Cms\Models\PostType;
9+
10+
class PostTypeFactory extends Factory
11+
{
12+
protected $model = PostType::class;
13+
14+
public function definition(): array
15+
{
16+
return [
17+
'name' => $this->faker->name(),
18+
'singular_name' => $this->faker->name(),
19+
'slug' => $this->faker->slug(),
20+
'icon' => $this->faker->word(),
21+
'features' => $this->faker->words(),
22+
'og_description' => $this->faker->text(),
23+
'order_column' => $this->faker->randomNumber(),
24+
'created_at' => Carbon::now(),
25+
'updated_at' => Carbon::now(),
26+
27+
'category_type_id' => random_id_or_generate(CategoryType::class),
28+
];
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('tag_model', function (Blueprint $table) {
17+
$table->id();
18+
$table->unique(['model_id', 'model_type', 'tag_id'], 'unique_model_tag');
19+
$table->morphs('model');
20+
21+
$table->foreignId('tag_id');
22+
$table->timestamps();
23+
24+
$table->foreign('tag_id')
25+
->references('id')
26+
->on('tags')
27+
->onDelete('cascade');
28+
});
29+
}
30+
31+
/**
32+
* Reverse the migrations.
33+
*
34+
* @return void
35+
*/
36+
public function down()
37+
{
38+
Schema::dropIfExists('tag_model');
39+
}
40+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
return new class extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('tags', function (Blueprint $table) {
16+
$table->id();
17+
$table->string('name')->unique();
18+
$table->timestamps();
19+
$table->jsonTranslatable();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('tags');
31+
}
32+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Javaabu\Cms\Actions\Posts;
4+
5+
use Javaabu\Cms\Http\Requests\PostRequest;
6+
use Javaabu\Cms\Models\Post;
7+
8+
class CreatePostAction {
9+
public function handle(PostRequest $request)
10+
{
11+
$post = Post::create($request->validated());
12+
13+
return $post;
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Javaabu\Cms\Http\Requests\PostRequest;
4+
use Javaabu\Cms\Models\Post;
5+
6+
class EditPostAction {
7+
public function handle(Post $post, PostRequest $request)
8+
{
9+
$post->update($request->validated());
10+
11+
if ($request->hasFile('media')) {
12+
$post->addMedia($request->file('media'))->toMediaCollection('media');
13+
}
14+
15+
return $post;
16+
}
17+
}

src/Cms.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ public function registerPostTypes($postTypes): void
3333
$type->slug = $slug;
3434
$type->icon = $data['icon'];
3535

36+
$category_type = CategoryType::whereSlug($slug . '-categories')->first();
37+
38+
if (! $category_type) {
39+
$category_type = new CategoryType();
40+
}
41+
42+
$category_type->name = $data['name'] ?? $name . ' Categories';
43+
$category_type->singular_name = $data['name_singular'] ?? Str::singular($data['name'] ?? $name) . ' Category';
44+
$category_type->slug = $slug . '_categories';
45+
46+
$category_type->save();
3647

37-
$category_type = CategoryType::whereSlug(Str::singular($slug) . '-categories')->first();
3848
$type->categoryType()->associate($category_type);
3949

4050
$type->features = $data['features'];
@@ -194,5 +204,85 @@ public function adminMenuItems(array $menus = [])
194204
return $menus;
195205
}
196206

207+
/**
208+
* Load up permissions for all the Post Types
209+
*
210+
* @return array
211+
*/
212+
public function seedPostTypePermissions(array $existing_permissions = []): array
213+
{
214+
$data = [];
215+
$all_post_types = PostType::all();
216+
217+
foreach ($all_post_types as $post_type) {
218+
$permissions = $this->constructPostTypePermissions($post_type);
219+
$existing = $existing_permissions[$post_type->permission_slug] ?? [];
220+
$data[$post_type->permission_slug] = array_merge($existing, $permissions);
221+
}
222+
223+
return array_merge($existing_permissions, $data);
224+
}
197225

226+
/**
227+
* Permission template for the post types
228+
*
229+
* @param PostType $post_type
230+
* @return string[]
231+
*/
232+
protected function constructPostTypePermissions(PostType $post_type): array
233+
{
234+
$slug = $post_type->permission_slug;
235+
$title = Str::lower($post_type->name);
236+
237+
return [
238+
'edit_' . $slug => 'Edit own ' . $title,
239+
'edit_others_' . $slug => 'Edit all ' . $title,
240+
'delete_' . $slug => 'Delete own ' . $title,
241+
'delete_others_' . $slug => 'Delete all ' . $title,
242+
'view_' . $slug => 'View own ' . $title,
243+
'view_others_' . $slug => 'View all ' . $title,
244+
'force_delete_' . $slug => 'Force delete own ' . $title,
245+
'force_delete_others_' . $slug => 'Force delete all ' . $title,
246+
'publish_' . $slug => 'Publish own ' . $title,
247+
'publish_others_' . $slug => 'Publish all ' . $title,
248+
'import_' . $slug => 'Import ' . $title,
249+
];
250+
}
251+
252+
/**
253+
* Load up permissions for all the Category Types
254+
*
255+
* @returns array;
256+
*/
257+
public function seedCategoryTypePermissions(array $existing_permissions = []): array
258+
{
259+
$data = [];
260+
$all_category_types = CategoryType::all();
261+
262+
foreach ($all_category_types as $category_type) {
263+
$permissions = $this->constructCategoryTypePermissions($category_type);
264+
$existing = $existing_permissions[$category_type->permission_slug] ?? [];
265+
$data[$category_type->permission_slug] = array_merge($existing, $permissions);
266+
}
267+
return array_merge($existing_permissions, $data);
268+
}
269+
270+
/**
271+
* Permission template for the category types
272+
*
273+
* @param CategoryType $category_type
274+
* @return string[]
275+
*/
276+
protected function constructCategoryTypePermissions(CategoryType $category_type): array
277+
{
278+
$slug = $category_type->permission_slug;
279+
$title = Str::lower($category_type->name);
280+
281+
return [
282+
'edit_' . $slug => 'Edit ' . $title,
283+
'delete_' . $slug => 'Delete ' . $title,
284+
'view_' . $slug => 'View ' . $title,
285+
'import_' . $slug => 'Import ' . $title,
286+
];
287+
}
198288
}

src/CmsServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function boot()
2929

3030
$this->registerPolicies();
3131

32+
$this->loadViewsFrom(__DIR__.'/../resources/views', 'cms');
33+
3234
$this->registerRouteModelBindings();
3335
}
3436

@@ -193,4 +195,9 @@ private function getPolicies(): array
193195

194196
return $policies;
195197
}
198+
199+
public function registerBreadcrumbs()
200+
{
201+
202+
}
196203
}

0 commit comments

Comments
 (0)