Merge pull request 'KAN-5 Create Template from Project & Create Project form Template' (#87) from koda/kan-5-create-template-from-project-create-project-form-template into epic/kan-3
Reviewed-on: OpenSource/KanbanGit#87
This commit was merged in pull request #87.
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Actions\Permissions\PermissionService;
|
||||
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||
use App\Enums\Permission;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -19,7 +20,7 @@ class CreateProject
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{key: string, name: string, creation_mode?: string, source_project_id?: int|string|null, copy_areas?: array<int, string>|null} $attributes
|
||||
* @param array{key: string, name: string, creation_mode?: string, source_project_id?: int|string|null, source_template_id?: int|string|null, copy_areas?: array<int, string>|null} $attributes
|
||||
*/
|
||||
public function handle(array $attributes, User $actor): Project
|
||||
{
|
||||
@@ -29,7 +30,9 @@ class CreateProject
|
||||
$this->initializeProjectTicketMetadata->handle($project);
|
||||
$this->permissionService->grant($actor, Permission::ProjectAdmin, $project);
|
||||
|
||||
if (($attributes['creation_mode'] ?? 'scratch') === 'duplicate' && isset($attributes['source_project_id'])) {
|
||||
$creationMode = $attributes['creation_mode'] ?? 'scratch';
|
||||
|
||||
if ($creationMode === 'duplicate' && isset($attributes['source_project_id'])) {
|
||||
$sourceProject = Project::query()->findOrFail((int) $attributes['source_project_id']);
|
||||
|
||||
$this->duplicateProjectConfiguration->handle(
|
||||
@@ -39,6 +42,15 @@ class CreateProject
|
||||
);
|
||||
}
|
||||
|
||||
if ($creationMode === 'template' && isset($attributes['source_template_id'])) {
|
||||
$template = ProjectTemplate::query()->findOrFail((int) $attributes['source_template_id']);
|
||||
|
||||
$this->duplicateProjectConfiguration->apply(
|
||||
$project,
|
||||
$template->configuration_snapshot ?? [],
|
||||
);
|
||||
}
|
||||
|
||||
return $project->fresh() ?? $project;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Projects;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class CreateProjectTemplate
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DuplicateProjectConfiguration $duplicateProjectConfiguration,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array{name: string, slug: string, copy_areas?: array<int, string>} $attributes
|
||||
*/
|
||||
public function handle(Project $sourceProject, array $attributes): ProjectTemplate
|
||||
{
|
||||
$copyAreas = array_values(Arr::get($attributes, 'copy_areas', []));
|
||||
|
||||
return ProjectTemplate::query()->create([
|
||||
'source_project_id' => $sourceProject->id,
|
||||
'name' => $attributes['name'],
|
||||
'slug' => $attributes['slug'],
|
||||
'copy_areas' => $this->duplicateProjectConfiguration->normalizeCopyAreas($copyAreas),
|
||||
'configuration_snapshot' => $this->duplicateProjectConfiguration->export($sourceProject, $copyAreas),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -90,13 +90,23 @@ class DuplicateProjectConfiguration
|
||||
|
||||
/**
|
||||
* @param list<string> $copyAreas
|
||||
* @return list<string>
|
||||
*/
|
||||
public function handle(Project $sourceProject, Project $targetProject, array $copyAreas): void
|
||||
public function normalizeCopyAreas(array $copyAreas): array
|
||||
{
|
||||
$selectedAreas = array_values(array_intersect(self::copyableAreas(), array_unique($copyAreas)));
|
||||
return array_values(array_intersect(self::copyableAreas(), array_unique($copyAreas)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $copyAreas
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function export(Project $sourceProject, array $copyAreas): array
|
||||
{
|
||||
$selectedAreas = $this->normalizeCopyAreas($copyAreas);
|
||||
|
||||
if ($selectedAreas === []) {
|
||||
return;
|
||||
return [];
|
||||
}
|
||||
|
||||
$sourceProject->loadMissing([
|
||||
@@ -110,141 +120,281 @@ class DuplicateProjectConfiguration
|
||||
'ticketTypes',
|
||||
]);
|
||||
|
||||
$configuration = [];
|
||||
|
||||
if (in_array(self::COPY_PROJECT_SETTINGS, $selectedAreas, true)) {
|
||||
$targetProject->forceFill([
|
||||
$configuration[self::COPY_PROJECT_SETTINGS] = [
|
||||
'show_author' => $sourceProject->show_author,
|
||||
'show_story_points' => $sourceProject->show_story_points,
|
||||
])->save();
|
||||
];
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_BOARD_WORKFLOW, $selectedAreas, true)) {
|
||||
$targetProject->ticketStatuses()->delete();
|
||||
$targetProject->boardColumns()->delete();
|
||||
$boardColumnIndexes = [];
|
||||
$boardColumns = [];
|
||||
|
||||
$boardColumnIdMap = [];
|
||||
|
||||
foreach ($sourceProject->boardColumns as $boardColumn) {
|
||||
$newBoardColumn = $targetProject->boardColumns()->create([
|
||||
foreach ($sourceProject->boardColumns as $index => $boardColumn) {
|
||||
$boardColumnIndexes[$boardColumn->id] = $index;
|
||||
$boardColumns[] = [
|
||||
'name' => $boardColumn->name,
|
||||
'position' => $boardColumn->position,
|
||||
]);
|
||||
|
||||
$boardColumnIdMap[$boardColumn->id] = $newBoardColumn->id;
|
||||
];
|
||||
}
|
||||
|
||||
$ticketStatusIdMap = [];
|
||||
$ticketStatusIndexes = [];
|
||||
$ticketStatuses = [];
|
||||
|
||||
foreach ($sourceProject->ticketStatuses as $ticketStatus) {
|
||||
$newTicketStatus = $targetProject->ticketStatuses()->create([
|
||||
'board_column_id' => $ticketStatus->board_column_id === null
|
||||
foreach ($sourceProject->ticketStatuses as $index => $ticketStatus) {
|
||||
$ticketStatusIndexes[$ticketStatus->id] = $index;
|
||||
$ticketStatuses[] = [
|
||||
'board_column_index' => $ticketStatus->board_column_id === null
|
||||
? null
|
||||
: ($boardColumnIdMap[$ticketStatus->board_column_id] ?? null),
|
||||
: ($boardColumnIndexes[$ticketStatus->board_column_id] ?? null),
|
||||
'name' => $ticketStatus->name,
|
||||
'position' => $ticketStatus->position,
|
||||
]);
|
||||
|
||||
$ticketStatusIdMap[$ticketStatus->id] = $newTicketStatus->id;
|
||||
'allowed_target_indexes' => [],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($sourceProject->ticketStatuses as $ticketStatus) {
|
||||
$newTicketStatusId = $ticketStatusIdMap[$ticketStatus->id] ?? null;
|
||||
|
||||
if ($newTicketStatusId === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$targetProject->ticketStatuses()
|
||||
->findOrFail($newTicketStatusId)
|
||||
->allowedTargetStatuses()
|
||||
->sync(
|
||||
$ticketStatus->allowedTargetStatuses
|
||||
->pluck('id')
|
||||
->map(fn (int $statusId): ?int => $ticketStatusIdMap[$statusId] ?? null)
|
||||
->filter()
|
||||
->values()
|
||||
->all(),
|
||||
);
|
||||
foreach ($sourceProject->ticketStatuses as $index => $ticketStatus) {
|
||||
$ticketStatuses[$index]['allowed_target_indexes'] = $ticketStatus->allowedTargetStatuses
|
||||
->pluck('id')
|
||||
->map(fn (int $statusId): ?int => $ticketStatusIndexes[$statusId] ?? null)
|
||||
->filter(static fn (?int $statusIndex): bool => $statusIndex !== null)
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
$configuration[self::COPY_BOARD_WORKFLOW] = [
|
||||
'board_columns' => $boardColumns,
|
||||
'ticket_statuses' => $ticketStatuses,
|
||||
];
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_TICKET_TYPES, $selectedAreas, true)) {
|
||||
$targetProject->ticketTypes()->delete();
|
||||
|
||||
foreach ($sourceProject->ticketTypes as $ticketType) {
|
||||
$targetProject->ticketTypes()->create([
|
||||
$configuration[self::COPY_TICKET_TYPES] = $sourceProject->ticketTypes
|
||||
->map(fn ($ticketType): array => [
|
||||
'name' => $ticketType->name,
|
||||
'position' => $ticketType->position,
|
||||
'is_parent' => $ticketType->is_parent,
|
||||
]);
|
||||
}
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_TICKET_PRIORITIES, $selectedAreas, true)) {
|
||||
$targetProject->ticketPriorities()->delete();
|
||||
|
||||
foreach ($sourceProject->ticketPriorities as $ticketPriority) {
|
||||
$targetProject->ticketPriorities()->create([
|
||||
$configuration[self::COPY_TICKET_PRIORITIES] = $sourceProject->ticketPriorities
|
||||
->map(fn ($ticketPriority): array => [
|
||||
'name' => $ticketPriority->name,
|
||||
'position' => $ticketPriority->position,
|
||||
]);
|
||||
}
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_TICKET_FIELDS, $selectedAreas, true)) {
|
||||
$targetProject->ticketFieldDefinitions()->delete();
|
||||
|
||||
foreach ($sourceProject->ticketFieldDefinitions as $ticketFieldDefinition) {
|
||||
$targetProject->ticketFieldDefinitions()->create([
|
||||
$configuration[self::COPY_TICKET_FIELDS] = $sourceProject->ticketFieldDefinitions
|
||||
->map(fn ($ticketFieldDefinition): array => [
|
||||
'field_type' => $ticketFieldDefinition->field_type,
|
||||
'label' => $ticketFieldDefinition->label,
|
||||
'position' => $ticketFieldDefinition->position,
|
||||
]);
|
||||
}
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_TICKET_LINK_TYPES, $selectedAreas, true)) {
|
||||
$targetProject->ticketLinkTypes()->delete();
|
||||
|
||||
foreach ($sourceProject->ticketLinkTypes as $ticketLinkType) {
|
||||
$targetProject->ticketLinkTypes()->create([
|
||||
$configuration[self::COPY_TICKET_LINK_TYPES] = $sourceProject->ticketLinkTypes
|
||||
->map(fn ($ticketLinkType): array => [
|
||||
'label' => $ticketLinkType->label,
|
||||
'counterpart_label' => $ticketLinkType->counterpart_label,
|
||||
'position' => $ticketLinkType->position,
|
||||
]);
|
||||
}
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_REPOSITORY_CONNECTIONS, $selectedAreas, true)) {
|
||||
$targetProject->repositoryConnections()->delete();
|
||||
|
||||
foreach ($sourceProject->repositoryConnections as $repositoryConnection) {
|
||||
$targetProject->repositoryConnections()->create([
|
||||
'provider' => $repositoryConnection->provider,
|
||||
$configuration[self::COPY_REPOSITORY_CONNECTIONS] = $sourceProject->repositoryConnections
|
||||
->map(fn ($repositoryConnection): array => [
|
||||
'provider' => $repositoryConnection->provider->value,
|
||||
'base_url' => $repositoryConnection->base_url,
|
||||
'provider_repository_id' => $repositoryConnection->provider_repository_id,
|
||||
'owner_name' => $repositoryConnection->owner_name,
|
||||
'repository_name' => $repositoryConnection->repository_name,
|
||||
'repository_full_name' => $repositoryConnection->repository_full_name,
|
||||
'repository_url' => $repositoryConnection->repository_url,
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_NOTIFICATION_ACTIONS, $selectedAreas, true)) {
|
||||
$configuration[self::COPY_NOTIFICATION_ACTIONS] = $sourceProject->notificationActions
|
||||
->map(fn ($notificationAction): array => [
|
||||
'name' => $notificationAction->name,
|
||||
'enabled' => $notificationAction->enabled,
|
||||
'event_type' => $notificationAction->event_type->value,
|
||||
'channel' => $notificationAction->channel->value,
|
||||
'channel_configuration' => $notificationAction->channel_configuration ?? [],
|
||||
'message_template' => $notificationAction->message_template,
|
||||
'delivery_url' => $notificationAction->delivery_url,
|
||||
'filters' => $notificationAction->filters ?? [],
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $configuration
|
||||
*/
|
||||
public function apply(Project $targetProject, array $configuration): void
|
||||
{
|
||||
if (array_key_exists(self::COPY_PROJECT_SETTINGS, $configuration)) {
|
||||
$projectSettings = $configuration[self::COPY_PROJECT_SETTINGS];
|
||||
|
||||
$targetProject->forceFill([
|
||||
'show_author' => (bool) ($projectSettings['show_author'] ?? $targetProject->show_author),
|
||||
'show_story_points' => (bool) ($projectSettings['show_story_points'] ?? $targetProject->show_story_points),
|
||||
])->save();
|
||||
}
|
||||
|
||||
if (array_key_exists(self::COPY_BOARD_WORKFLOW, $configuration)) {
|
||||
$targetProject->ticketStatuses()->delete();
|
||||
$targetProject->boardColumns()->delete();
|
||||
|
||||
$boardColumnIds = [];
|
||||
|
||||
foreach (($configuration[self::COPY_BOARD_WORKFLOW]['board_columns'] ?? []) as $boardColumn) {
|
||||
$newBoardColumn = $targetProject->boardColumns()->create([
|
||||
'name' => $boardColumn['name'],
|
||||
'position' => $boardColumn['position'],
|
||||
]);
|
||||
|
||||
$boardColumnIds[] = $newBoardColumn->id;
|
||||
}
|
||||
|
||||
$ticketStatusIds = [];
|
||||
|
||||
foreach (($configuration[self::COPY_BOARD_WORKFLOW]['ticket_statuses'] ?? []) as $ticketStatus) {
|
||||
$boardColumnIndex = $ticketStatus['board_column_index'] ?? null;
|
||||
$newTicketStatus = $targetProject->ticketStatuses()->create([
|
||||
'board_column_id' => $boardColumnIndex === null ? null : ($boardColumnIds[$boardColumnIndex] ?? null),
|
||||
'name' => $ticketStatus['name'],
|
||||
'position' => $ticketStatus['position'],
|
||||
]);
|
||||
|
||||
$ticketStatusIds[] = $newTicketStatus->id;
|
||||
}
|
||||
|
||||
foreach (($configuration[self::COPY_BOARD_WORKFLOW]['ticket_statuses'] ?? []) as $index => $ticketStatus) {
|
||||
$newTicketStatusId = $ticketStatusIds[$index] ?? null;
|
||||
|
||||
if ($newTicketStatusId === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$targetProject->ticketStatuses()->findOrFail($newTicketStatusId)
|
||||
->allowedTargetStatuses()
|
||||
->sync(
|
||||
collect($ticketStatus['allowed_target_indexes'] ?? [])
|
||||
->map(fn (int $statusIndex): ?int => $ticketStatusIds[$statusIndex] ?? null)
|
||||
->filter(static fn (?int $statusId): bool => $statusId !== null)
|
||||
->values()
|
||||
->all(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists(self::COPY_TICKET_TYPES, $configuration)) {
|
||||
$targetProject->ticketTypes()->delete();
|
||||
|
||||
foreach ($configuration[self::COPY_TICKET_TYPES] as $ticketType) {
|
||||
$targetProject->ticketTypes()->create([
|
||||
'name' => $ticketType['name'],
|
||||
'position' => $ticketType['position'],
|
||||
'is_parent' => $ticketType['is_parent'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array(self::COPY_NOTIFICATION_ACTIONS, $selectedAreas, true)) {
|
||||
if (array_key_exists(self::COPY_TICKET_PRIORITIES, $configuration)) {
|
||||
$targetProject->ticketPriorities()->delete();
|
||||
|
||||
foreach ($configuration[self::COPY_TICKET_PRIORITIES] as $ticketPriority) {
|
||||
$targetProject->ticketPriorities()->create([
|
||||
'name' => $ticketPriority['name'],
|
||||
'position' => $ticketPriority['position'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists(self::COPY_TICKET_FIELDS, $configuration)) {
|
||||
$targetProject->ticketFieldDefinitions()->delete();
|
||||
|
||||
foreach ($configuration[self::COPY_TICKET_FIELDS] as $ticketFieldDefinition) {
|
||||
$targetProject->ticketFieldDefinitions()->create([
|
||||
'field_type' => $ticketFieldDefinition['field_type'],
|
||||
'label' => $ticketFieldDefinition['label'],
|
||||
'position' => $ticketFieldDefinition['position'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists(self::COPY_TICKET_LINK_TYPES, $configuration)) {
|
||||
$targetProject->ticketLinkTypes()->delete();
|
||||
|
||||
foreach ($configuration[self::COPY_TICKET_LINK_TYPES] as $ticketLinkType) {
|
||||
$targetProject->ticketLinkTypes()->create([
|
||||
'label' => $ticketLinkType['label'],
|
||||
'counterpart_label' => $ticketLinkType['counterpart_label'],
|
||||
'position' => $ticketLinkType['position'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists(self::COPY_REPOSITORY_CONNECTIONS, $configuration)) {
|
||||
$targetProject->repositoryConnections()->delete();
|
||||
|
||||
foreach ($configuration[self::COPY_REPOSITORY_CONNECTIONS] as $repositoryConnection) {
|
||||
$targetProject->repositoryConnections()->create([
|
||||
'provider' => $repositoryConnection['provider'],
|
||||
'base_url' => $repositoryConnection['base_url'],
|
||||
'provider_repository_id' => $repositoryConnection['provider_repository_id'],
|
||||
'owner_name' => $repositoryConnection['owner_name'],
|
||||
'repository_name' => $repositoryConnection['repository_name'],
|
||||
'repository_full_name' => $repositoryConnection['repository_full_name'],
|
||||
'repository_url' => $repositoryConnection['repository_url'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists(self::COPY_NOTIFICATION_ACTIONS, $configuration)) {
|
||||
$targetProject->notificationActions()->delete();
|
||||
|
||||
foreach ($sourceProject->notificationActions as $notificationAction) {
|
||||
foreach ($configuration[self::COPY_NOTIFICATION_ACTIONS] as $notificationAction) {
|
||||
$targetProject->notificationActions()->create([
|
||||
'name' => $notificationAction->name,
|
||||
'enabled' => $notificationAction->enabled,
|
||||
'event_type' => $notificationAction->event_type,
|
||||
'channel' => $notificationAction->channel,
|
||||
'channel_configuration' => $notificationAction->channel_configuration,
|
||||
'message_template' => $notificationAction->message_template,
|
||||
'delivery_url' => $notificationAction->delivery_url,
|
||||
'filters' => $notificationAction->filters,
|
||||
'name' => $notificationAction['name'],
|
||||
'enabled' => $notificationAction['enabled'],
|
||||
'event_type' => $notificationAction['event_type'],
|
||||
'channel' => $notificationAction['channel'],
|
||||
'channel_configuration' => $notificationAction['channel_configuration'] ?? [],
|
||||
'message_template' => $notificationAction['message_template'] ?? null,
|
||||
'delivery_url' => $notificationAction['delivery_url'],
|
||||
'filters' => $notificationAction['filters'] ?? [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $copyAreas
|
||||
*/
|
||||
public function handle(Project $sourceProject, Project $targetProject, array $copyAreas): void
|
||||
{
|
||||
$this->apply($targetProject, $this->export($sourceProject, $copyAreas));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ use App\Http\Requests\StoreProjectRequest;
|
||||
use App\Http\Requests\SyncProjectPermissionsRequest;
|
||||
use App\Http\Resources\PermissionOptionResource;
|
||||
use App\Http\Resources\ProjectResource;
|
||||
use App\Http\Resources\ProjectTemplateResource;
|
||||
use App\Models\ProjectTemplate;
|
||||
use App\Http\Resources\TeamResource;
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Models\Project;
|
||||
@@ -52,11 +54,18 @@ class ProjectController extends Controller
|
||||
403,
|
||||
);
|
||||
|
||||
$sourceTemplates = ProjectTemplate::query()->with('sourceProject')->orderBy('name')->get();
|
||||
$selectedTemplateId = $request->integer('template');
|
||||
|
||||
return Inertia::render('projects/Create', [
|
||||
'copyAreaOptions' => DuplicateProjectConfiguration::copyAreaOptions(),
|
||||
'sourceProjects' => ProjectResource::collection(
|
||||
$this->listVisibleProjects->handle($request->user()),
|
||||
)->resolve(),
|
||||
'sourceTemplates' => ProjectTemplateResource::collection($sourceTemplates)->resolve(),
|
||||
'selectedSourceTemplateId' => $sourceTemplates->contains('id', $selectedTemplateId)
|
||||
? $selectedTemplateId
|
||||
: null,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Projects\CreateProjectTemplate;
|
||||
use App\Actions\Projects\DuplicateProjectConfiguration;
|
||||
use App\Http\Requests\StoreProjectTemplateRequest;
|
||||
use App\Http\Resources\ProjectResource;
|
||||
use App\Http\Resources\ProjectTemplateResource;
|
||||
use App\Models\Project;
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
|
||||
class ProjectTemplateController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CreateProjectTemplate $createProjectTemplate,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): Response
|
||||
{
|
||||
return Inertia::render('project-templates/Index', [
|
||||
'status' => session('status'),
|
||||
'templates' => ProjectTemplateResource::collection(
|
||||
ProjectTemplate::query()
|
||||
->with('sourceProject')
|
||||
->orderBy('name')
|
||||
->get(),
|
||||
)->resolve(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create(Project $project): Response
|
||||
{
|
||||
return Inertia::render('project-templates/Create', [
|
||||
'copyAreaOptions' => DuplicateProjectConfiguration::copyAreaOptions(),
|
||||
'project' => ProjectResource::make($project)->resolve(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StoreProjectTemplateRequest $request, Project $project): RedirectResponse
|
||||
{
|
||||
$template = $this->createProjectTemplate->handle($project, $request->validated());
|
||||
|
||||
return to_route('project-templates.index')
|
||||
->with('status', "Template {$template->name} was created.");
|
||||
}
|
||||
|
||||
public function destroy(ProjectTemplate $projectTemplate): RedirectResponse
|
||||
{
|
||||
$templateName = $projectTemplate->name;
|
||||
$projectTemplate->delete();
|
||||
|
||||
return to_route('project-templates.index')
|
||||
->with('status', "Template {$templateName} was deleted.");
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class StoreProjectRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'creation_mode' => ['required', 'string', Rule::in(['scratch', 'duplicate'])],
|
||||
'creation_mode' => ['required', 'string', Rule::in(['scratch', 'duplicate', 'template'])],
|
||||
'key' => [
|
||||
'required',
|
||||
'string',
|
||||
@@ -81,6 +81,12 @@ class StoreProjectRequest extends FormRequest
|
||||
}
|
||||
},
|
||||
],
|
||||
'source_template_id' => [
|
||||
Rule::requiredIf(fn (): bool => $this->input('creation_mode') === 'template'),
|
||||
'nullable',
|
||||
'integer',
|
||||
Rule::exists('project_templates', 'id'),
|
||||
],
|
||||
'copy_areas' => ['nullable', 'array'],
|
||||
'copy_areas.*' => [
|
||||
'string',
|
||||
@@ -96,6 +102,7 @@ class StoreProjectRequest extends FormRequest
|
||||
{
|
||||
return [
|
||||
'source_project_id.required' => 'Select a source project to duplicate from.',
|
||||
'source_template_id.required' => 'Select a template to create the project from.',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Actions\Projects\DuplicateProjectConfiguration;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreProjectTemplateRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()?->can('create-project') ?? false;
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
if ($this->filled('slug')) {
|
||||
$this->merge([
|
||||
'slug' => Str::slug((string) $this->input('slug')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'slug' => ['required', 'string', 'max:255', 'alpha_dash', Rule::unique('project_templates', 'slug')],
|
||||
'copy_areas' => ['required', 'array', 'min:1'],
|
||||
'copy_areas.*' => ['string', Rule::in(DuplicateProjectConfiguration::copyableAreas())],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\ProjectTemplate;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class ProjectTemplateResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
if ($this->resource instanceof ProjectTemplate) {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'source_project_id' => $this->source_project_id,
|
||||
'source_project' => $this->relationLoaded('sourceProject') && $this->sourceProject !== null
|
||||
? ProjectResource::make($this->sourceProject)->resolve()
|
||||
: null,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'copy_areas' => $this->copy_areas ?? [],
|
||||
'created_at' => $this->created_at?->toISOString(),
|
||||
'updated_at' => $this->updated_at?->toISOString(),
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => Arr::get($this->resource, 'id'),
|
||||
'source_project_id' => Arr::get($this->resource, 'source_project_id'),
|
||||
'source_project' => Arr::get($this->resource, 'source_project'),
|
||||
'name' => Arr::get($this->resource, 'name'),
|
||||
'slug' => Arr::get($this->resource, 'slug'),
|
||||
'copy_areas' => Arr::get($this->resource, 'copy_areas', []),
|
||||
'created_at' => Arr::get($this->resource, 'created_at'),
|
||||
'updated_at' => Arr::get($this->resource, 'updated_at'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Database\Factories\ProjectTemplateFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable(['source_project_id', 'name', 'slug', 'copy_areas', 'configuration_snapshot'])]
|
||||
class ProjectTemplate extends Model
|
||||
{
|
||||
/** @use HasFactory<ProjectTemplateFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'copy_areas' => 'array',
|
||||
'configuration_snapshot' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function sourceProject(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Project::class, 'source_project_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user