Skip to content

Commit 9de09b3

Browse files
committed
wip
1 parent c0b5184 commit 9de09b3

File tree

10 files changed

+295
-38
lines changed

10 files changed

+295
-38
lines changed

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"kalnoy/nestedset": "^6.0",
2525
"javaabu/auth": "^1.16",
2626
"diglactic/laravel-breadcrumbs": "^10.0",
27-
"lorisleiva/laravel-actions": "^2.9"
27+
"lorisleiva/laravel-actions": "^2.9",
28+
"javaabu/mediapicker": "^1.0@dev"
2829
},
2930
"require-dev": {
3031
"laravel/pint": "^1.14",
@@ -55,5 +56,11 @@
5556
}
5657
},
5758
"minimum-stability": "dev",
58-
"prefer-stable": true
59+
"prefer-stable": true,
60+
"repositories": [
61+
{
62+
"type": "vcs",
63+
"url": "https://github.com/Javaabu/mediapicker.git"
64+
}
65+
]
5966
}

config/config.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@
5050

5151
'should_translate' => false,
5252

53-
'use_default_view_for' => ['downloads', 'announcements', 'publications', 'jobs', 'blogs'],
53+
'use_default_view_for' => [],
54+
55+
'views_folder' => 'post_type'
56+
5457
];

database/factories/PostFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Illuminate\Database\Eloquent\Factories\Factory;
66
use Illuminate\Support\Carbon;
7+
use Javaabu\Auth\User;
78
use Javaabu\Cms\Models\Post;
89
use Javaabu\Cms\Models\PostType;
910
use Javaabu\Cms\Support\Faker\Factory\ContentBlockFactory;
1011
use Javaabu\Helpers\Enums\PublishStatuses;
12+
use Javaabu\Mediapicker\Models\Attachment;
1113

1214
class PostFactory extends ContentBlockFactory
1315
{

src/Cms.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public function registerNormalRoutes(): void
8383
foreach ($post_types as $post_type) {
8484
Route::get($post_type->slug, [config('cms.web.controllers.posts'), 'index'])
8585
->defaults('web_post_type_slug', $post_type->slug)
86-
->name('web.posts.index.' . $post_type->slug);
86+
->name('index.' . $post_type->slug);
8787

8888
Route::get($post_type->slug . '/{post_slug}', [config('cms.web.controllers.posts'), 'show'])
8989
->defaults('web_post_type_slug', $post_type)
90-
->name('web.posts.show.' . $post_type->slug);
90+
->name('show.' . $post_type->slug);
9191

9292
// Route::get($post_type->slug . '/{post_slug}/files', [PostsController::class, 'downloadFiles'])
9393
// ->defaults('web_post_type_slug', $post_type)

src/CmsServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class CmsServiceProvider extends ServiceProvider
1818
'create_post_types_table',
1919
'create_posts_table',
2020
'create_categories_table',
21+
'create_tags_table',
22+
'create_tag_model_table',
2123
];
2224

2325
/**
@@ -69,9 +71,9 @@ public function offerPublishing(): void
6971
], 'cms-flags');
7072

7173
// Publish migrations
72-
foreach ($this->migrations as $migration) {
74+
foreach ($this->migrations as $i => $migration) {
7375
$vendorMigration = __DIR__ . '/../database/migrations/' . $migration . '.php';
74-
$appMigration = $this->generateMigrationName($migration, now()->addSecond());
76+
$appMigration = $this->generateMigrationName($migration, now()->addSeconds($i));
7577

7678
$this->publishes([
7779
$vendorMigration => $appMigration,
@@ -133,7 +135,7 @@ public function registerRouteModelBindings()
133135
try {
134136
return Post::where('type', $post_type_slug ?: -1)
135137
->publishedOrPreview()
136-
->notHiddenOfLocale($language)
138+
// ->notHiddenOfLocale($language)
137139
->whereSlug($value)
138140
->firstOrFail();
139141

@@ -150,7 +152,7 @@ public function registerRouteModelBindings()
150152
try {
151153
return Post::where('type', 'pages')
152154
->published()
153-
->notHiddenOfLocale($language)
155+
// ->notHiddenOfLocale($language)
154156
->whereSlug($value)
155157
->firstOrFail();
156158
} catch (ModelNotFoundException $e) {

src/Http/Controllers/Admin/PostsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function show(PostType $type, Post $post)
184184
public function edit(PostType $type, Post $post)
185185
{
186186
$this->authorize('update', $post);
187-
$post->dontShowTranslationFallbacks();
187+
// $post->dontShowTranslationFallbacks();
188188
return view('admin.posts.edit', compact('post', 'type'));
189189
}
190190

src/Http/Controllers/PostsController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public function index($post_type, Request $request)
6464
->onEachSide(1)
6565
->appends($request->except('page'));
6666

67-
6867
return view($post_type->getWebView(), compact('posts', 'post_type', 'title', 'search'));
6968
}
7069

@@ -76,12 +75,12 @@ public function show(Request $request, Post $post, PostType $post_type)
7675
$post_documents = $post->attachments_for_translation;
7776

7877
$related_posts = $post_type->posts()
79-
// ->similarToTags($post)
78+
->similarToTags($post)
8079
->published()
8180
->withRelations()
8281
->orderBy('tag_similarity', 'DESC')
8382
->latest('published_at')
84-
->limit($post_type->getRelatedPostsCount())
83+
// ->limit($post_type->getRelatedPostsCount())
8584
->get();
8685

8786
// Need to load full models. Above statement only gives specific fields.

src/Models/Post.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
use Illuminate\Support\Str;
1414
use Javaabu\Cms\Enums\GalleryTypes;
1515
use Javaabu\Cms\Enums\PageStyles;
16+
use Javaabu\Cms\Traits\IsTaggable;
1617
use Javaabu\Helpers\AdminModel\AdminModel;
1718
use Javaabu\Helpers\AdminModel\IsAdminModel;
1819
use Javaabu\Helpers\Enums\PublishStatuses;
1920
use Javaabu\Helpers\Traits\HasSlug;
2021
use Javaabu\Helpers\Traits\Publishable;
22+
use Javaabu\Mediapicker\Concerns\InteractsWithAttachments;
23+
use Javaabu\Mediapicker\Contracts\HasAttachments;
2124
use Javaabu\Translatable\Contracts\Translatable;
2225
use Javaabu\Translatable\JsonTranslatable\IsJsonTranslatable;
2326
use Javaabu\Translatable\Models\Language;
2427

2528
class Post extends Model implements
2629
AdminModel,
27-
Translatable
30+
Translatable,
31+
HasAttachments
2832
{
2933
use IsJsonTranslatable;
3034
use IsAdminModel;
@@ -33,6 +37,8 @@ class Post extends Model implements
3337
use HasSlug;
3438
use IsJsonTranslatable;
3539
use HasFactory;
40+
use InteractsWithAttachments;
41+
use IsTaggable;
3642

3743
protected static $status_class = PublishStatuses::class;
3844

@@ -179,10 +185,10 @@ public function getAdminLinkNameAttribute(): string
179185
* @param string $namespace
180186
* @return string
181187
*/
182-
public function url(string $action = 'show', string $locale = null, string $namespace = 'web'): string
188+
public function url(string $action = 'show', string $locale = null, string $namespace = 'admin'): string
183189
{
184190
$controller = Str::lower(Str::plural(Str::kebab(class_basename(get_class($this)))));
185-
$controller_action = $namespace . '.' . $controller . '.' . $action . '.' . $this->postType->slug;
191+
$controller_action = $namespace . '.' . $controller . '.' . $action;
186192

187193
$params = [
188194
'post_type' => $this->postType->slug,
@@ -214,6 +220,10 @@ public function postType(): BelongsTo
214220
return $this->belongsTo(PostType::class, 'type', 'slug');
215221
}
216222

223+
public function categories(): BelongsTo
224+
{
225+
return $this->belongsTo(Category::class);
226+
}
217227

218228
/**
219229
* A search scope
@@ -226,7 +236,9 @@ public function postType(): BelongsTo
226236
public function scopeSearch($query, $search, $locale = null): mixed
227237
{
228238
// fulltext search on db values or
229-
return $query->translationsSearch('title', $search, $locale);
239+
return config('cms.should_translate')
240+
? $query->translationsSearch('title', $search, $locale)
241+
: $query->where('title', 'LIKE', '%'.$search.'%');
230242
}
231243

232244
/**
@@ -314,16 +326,8 @@ public function scopeQueryPostType(Builder $query, $type): Builder
314326
* @param string|null $locale
315327
* @return string|null
316328
*/
317-
public function translatedPermalink(string $action = 'show', string $locale = null): ?string
329+
public function permalink(string $action = 'show'): ?string
318330
{
319-
if (! $locale) {
320-
$locale = app()->getLocale();
321-
}
322-
323-
if ($this->lang->value != $locale && (is_null($this->translations) || $this->hide_translation)) {
324-
return null;
325-
}
326-
327331
$post_type_slug = $this->postType->slug;
328332
$controller = Str::lower(Str::plural(Str::kebab(class_basename(get_class($this)))));
329333

@@ -333,7 +337,9 @@ public function translatedPermalink(string $action = 'show', string $locale = nu
333337
$controller_action = 'web.pages.' . $action;
334338
}
335339

336-
$params = [$locale, $this->slug];
340+
$params = [];
341+
342+
$params[] = $this->slug;
337343

338344
return URL::route($controller_action, $params);
339345
}
@@ -437,6 +443,16 @@ public function getContentBlocksAttribute(): array
437443
}
438444
}
439445

446+
/**
447+
* With relations scope
448+
*
449+
* @param $query
450+
*/
451+
public function scopeWithRelations($query)
452+
{
453+
return $query->withAttachments();
454+
}
455+
440456
protected static function newFactory()
441457
{
442458
return PostFactory::new();

src/Models/PostType.php

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Relations\BelongsTo;
77
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Illuminate\Support\Str;
89
use Javaabu\Cms\Database\Factories\PostTypeFactory;
10+
use Javaabu\Cms\Enums\PostTypeFeatures;
911
use Javaabu\Helpers\AdminModel\AdminModel;
1012
use Javaabu\Helpers\AdminModel\IsAdminModel;
1113
use Illuminate\Database\Eloquent\Model;
@@ -47,6 +49,16 @@ public function getAdminUrlAttribute(): string
4749
return 'post_type';
4850
}
4951

52+
public function getPermissionSlugAttribute(): string
53+
{
54+
return Str::slug($this->slug, '_');
55+
}
56+
57+
public function setSlugAttribute($value)
58+
{
59+
$this->attributes['slug'] = Str::slug($value);
60+
}
61+
5062
/**
5163
* Get the route key name
5264
*/
@@ -68,6 +80,30 @@ public function getTranslatables(): array
6880
];
6981
}
7082

83+
public function categoryType(): BelongsTo
84+
{
85+
return $this->belongsTo(CategoryType::class, 'category_type_id', 'id');
86+
}
87+
88+
public function getFeatureName($feature): ?string
89+
{
90+
if (! $this->hasFeature($feature)) {
91+
return null;
92+
}
93+
94+
$feature_title = $this->features[$feature];
95+
96+
if (gettype($feature_title) == 'boolean') {
97+
return PostTypeFeatures::getLabel($feature);
98+
}
99+
100+
if (gettype($feature_title) == 'string') {
101+
return Str::title($feature_title);
102+
}
103+
104+
return null;
105+
}
106+
71107
public function hasFeature($feature): bool
72108
{
73109
return array_key_exists($feature, $this->features);
@@ -79,22 +115,27 @@ public function hasFeature($feature): bool
79115
* @param string $action
80116
* @return string
81117
*/
82-
public function getWebView(string $action = 'index'): string
118+
public function getWebView(string $action = 'index', string $namespace = 'web'): string
83119
{
84120
$default_views = config('cms.use_default_view_for');
85121

86-
if (in_array($this->slug, $default_views)) {
87-
return 'web.post-type.default.' . $action;
88-
}
89-
90-
return 'web.post-type.' . $this->slug . '.' . $action;
122+
$view = in_array($this->slug, $default_views) ? 'default' : $this->slug;
123+
return $namespace . '.' . config('cms.views_folder') . '.' . $view . '.' . $action;
91124
}
92125

93126
public function getPaginatorCount(): int
94127
{
95128
return get_setting($this->slug . '_per_page') ?? get_setting('per_page');
96129
}
97130

131+
/**
132+
* A post type has many posts
133+
*/
134+
public function userVisiblePosts()
135+
{
136+
return $this->posts()->userVisibleForPostType($this);
137+
}
138+
98139
/**
99140
* A post type has many posts
100141
*
@@ -105,11 +146,6 @@ public function posts(): HasMany
105146
return $this->hasMany(Post::class, 'type', 'slug');
106147
}
107148

108-
public function categoryType(): BelongsTo
109-
{
110-
return $this->belongsTo(CategoryType::class, 'category_type_id', 'id');
111-
}
112-
113149
protected static function newFactory(): PostTypeFactory
114150
{
115151
return PostTypeFactory::new();

0 commit comments

Comments
 (0)