Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post templates #907

Merged
merged 12 commits into from
Jul 5, 2024
8 changes: 8 additions & 0 deletions app/Http/Controllers/Entity/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ public function create(Campaign $campaign, Entity $entity, Post $post)
{
$this->authorize('post', [$entity->child, 'add']);
$parentRoute = $entity->pluralType();
$templates = Post::template()->pluck('name', 'id')->all();

$template = request()->input('template');
if (!empty($template) && $this->authorize('useTemplates', $campaign)) {
$template = Post::template()->where('id', $template)->first();
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
}

return view('entities.pages.posts.create', compact(
'campaign',
'post',
'entity',
'parentRoute',
'templates',
'template',
));
}

Expand Down
38 changes: 38 additions & 0 deletions app/Http/Controllers/Entity/Posts/TemplateController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers\Entity\Posts;

use App\Http\Controllers\Controller;
use App\Models\Campaign;
use App\Models\Post;
use App\Services\Entity\TemplateService;

class TemplateController extends Controller
{
protected TemplateService $service;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(TemplateService $templateService)
{
$this->middleware('auth');
$this->service = $templateService;
}

/**
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Campaign $campaign, Post $post)
{
$this->authorize('template', $post);
$this->service->post($post)->toggle();
return redirect()->back()
->with(
'success',
__('entities/actions.templates.success.' . ($post->is_template ? 'set' : 'unset'), ['name' => $post->name])
);
}
}
1 change: 1 addition & 0 deletions app/Models/CharacterRace.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function character(): BelongsTo
{
return $this->belongsTo(Character::class);
}

public function race(): BelongsTo
{
return $this->belongsTo(Race::class);
Expand Down
14 changes: 11 additions & 3 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @property string|null $marketplace_uuid
* @property bool|int $is_private
* @property int $deleted_by
* @property bool|int $is_pinned
* @property bool|int $is_template
* @property int $position
* @property array $settings
* @property Entity|null $entity
Expand Down Expand Up @@ -69,6 +69,7 @@ class Post extends Model
'settings',
'location_id',
'layout_id',
'is_template'
];

/** @var string[] Fields that can be used to order by */
Expand Down Expand Up @@ -187,14 +188,21 @@ public function getEntryForEditionAttribute()
}

/**
* @return Builder
*/
public function scopeOrdered(Builder $query)
public function scopeOrdered(Builder $query): Builder
{
return $query
->orderBy('position');
}

/**
*/
public function scopeTemplate(Builder $query): Builder
{
return $query->where('is_template', true);
}


/**
*/
public function collapsed(): bool
Expand Down
8 changes: 7 additions & 1 deletion app/Policies/CampaignPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,18 @@ public function galleryUpload(?User $user, Campaign $campaign): bool
);
}


/**
* @return bool
*/
protected function checkPermission(int $action, User $user, Campaign $campaign = null)
{
return EntityPermission::hasPermission(0, $action, $user, null, $campaign);
}

/**
*/
public function useTemplates(?User $user, Campaign $campaign): bool
{
return $this->isAdmin($user);
}
}
18 changes: 18 additions & 0 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Policies;

use App\Facades\UserCache;
use App\Models\Post;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PostPolicy
{
use HandlesAuthorization;

public function template(?User $user, Post $post): bool
{
return !$post->layout_id && $user && UserCache::user($user)->admin();
}
}
11 changes: 9 additions & 2 deletions app/Services/Entity/TemplateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@
namespace App\Services\Entity;

use App\Traits\EntityAware;
use App\Traits\PostAware;

class TemplateService
{
use EntityAware;
use PostAware;

public function toggle(): void
{
$this->entity->is_template = !$this->entity->is_template;
$this->entity->save();
if (isset($this->post)) {
$this->post->is_template = !$this->post->is_template;
$this->post->save();
} else {
$this->entity->is_template = !$this->entity->is_template;
$this->entity->save();
}
}
}
18 changes: 18 additions & 0 deletions app/Traits/PostAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Traits;

use App\Models\Post;

/**
*/
trait PostAware
{
public Post $post;

public function post(Post $post): self
{
$this->post = $post;
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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::table('posts', function (Blueprint $table) {
$table->boolean('is_template')->defaultValue(false);
$table->index('is_template');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('posts', function (Blueprint $table) {
$table->dropIndex('is_template');
$table->dropColumn('is_template');
});
}
};
5 changes: 4 additions & 1 deletion lang/en/posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

return [
'create' => [
'title' => 'New Post',
'title' => 'New Post',
'template' => [
'helper' => 'The campaigns admins have defined the following posts as templates that can be re-used.'
]
],
'fields' => [
'name' => 'Name',
Expand Down
2 changes: 1 addition & 1 deletion resources/views/cruds/lists/_create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<span class="sr-only">{{ __('crud.actions.actions') }}</span>
</button>
<div class="dropdown-menu hidden" role="menu" id="templates-submenu">
@if ($templates->isNotEmpty())
@if (auth()->user()->can('useTemplates', $campaign) && $templates->isNotEmpty())
@foreach ($templates as $entityTemplate)
<x-dropdowns.item
:link="route($route . '.create', [$campaign, 'copy' => $entityTemplate->entity_id, 'template' => true])"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/entities/components/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
<x-icon class="fa-solid fa-link"></x-icon>
{{ __('crud.actions.copy_mention') }}
</x-dropdowns.item>
@if (auth()->user()->isAdmin())
@if (auth()->user()->can('useTemplates', $campaign))
<x-dropdowns.item :link="route('entities.template', [$campaign, $entity])">
@if($entity->is_template)
<x-icon class="fa-regular fa-star"></x-icon>
Expand Down
9 changes: 9 additions & 0 deletions resources/views/entities/pages/posts/_actions.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
{{ __('entities/notes.move.move') }}
</x-dropdowns.item>
@endif
@if (auth()->check() && auth()->user()->can('useTemplates', $campaign))
<x-dropdowns.item :link="route('posts.template', [$campaign, 'post' => $post])" icon="@if($post->is_template) fa-solid @else fa-regular @endif fa-star">
@if ($post->is_template)
{{ __('entities/actions.templates.unset') }}
@else
{{ __('entities/actions.templates.set') }}
@endif
</x-dropdowns.item>
@endif
<hr class="m-0" />
<x-dropdowns.item :link="route('entities.story.reorder', [$campaign, 'entity' => $entity])" icon="fa-solid fa-arrows-v">
{{ __('entities/story.reorder.icon_tooltip') }}
Expand Down
9 changes: 8 additions & 1 deletion resources/views/entities/pages/posts/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
}

$options = $entity->postPositionOptions(!empty($model->position) ? $model->position : null);
if (isset($template)) {
$model = $template;
$model->name = '';
}
$last = array_key_last($options);

$bragiName = $entity->isCharacter() ? $entity->name : null;
Expand All @@ -45,6 +49,9 @@
@can('permission', $entity->child)
<x-tab.tab target="permissions" :title="__('entities/notes.show.advanced')"></x-tab.tab>
@endcan
@if (auth()->user()->can('useTemplates', $campaign) && !empty($templates))
<x-tab.tab target="templates" :title="__('entities/attributes.template.load.title')"></x-tab.tab>
@endif
</ul>
</div>
@include('entities.pages.posts._save-options')
Expand Down Expand Up @@ -99,8 +106,8 @@ class="html-editor"
</x-forms.field>
</x-grid>
</div>

@includeWhen(auth()->user()->can('permission', $entity->child), 'entities.pages.posts._permissions')
@includeWhen(auth()->user()->isAdmin() && !empty($templates), 'entities.pages.posts._templates')
</div>
</div>

Expand Down
16 changes: 16 additions & 0 deletions resources/views/entities/pages/posts/_templates.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="tab-pane" id="form-templates">
<x-grid type="1/1">
<x-helper>
{{ __('posts.create.template.helper') }}
</x-helper>
<ul>
@foreach ($templates as $id => $name)
<li>
<a href="{{ route('entities.posts.create', [$campaign, $entity, 'template' => $id]) }}">
<span>{{$name}}</span>
</a>
</li>
@endforeach
</ul>
</x-grid>
</div>
2 changes: 2 additions & 0 deletions routes/campaigns/entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@


Route::get('/w/{campaign}/entities/{entity}/template', 'Entity\TemplateController@update')->name('entities.template');
Route::get('/w/{campaign}/posts/{post}/template', 'Entity\Posts\TemplateController@update')->name('posts.template');


// Attribute template
Route::get('/w/{campaign}/entities/{entity}/attribute-template', 'Entity\AttributeTemplateController@index')->name('entities.attributes.template');
Expand Down
Loading