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
9 changes: 9 additions & 0 deletions app/Http/Controllers/Entity/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ 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();
;
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved

$template = request()->input('template');
if (!empty($template)) {
$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
11 changes: 11 additions & 0 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @property bool|int $is_private
* @property int $deleted_by
* @property bool|int $is_pinned
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
* @property bool $is_template
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
* @property int $position
* @property array $settings
* @property Entity|null $entity
Expand Down Expand Up @@ -69,6 +70,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 @@ -195,6 +197,15 @@ public function scopeOrdered(Builder $query)
->orderBy('position');
}

/**
* @return Builder
*/
public function scopeTemplate(Builder $query)
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
{
return $query->where('is_template', true);
}


/**
*/
public function collapsed(): bool
Expand Down
21 changes: 21 additions & 0 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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
{
if ($post->exists === false) {
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
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
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('template', $post))
<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()->isAdmin() && !empty($templates))
spitfire305 marked this conversation as resolved.
Show resolved Hide resolved
<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