feat: inversion relation User→Projects, les projets pointent vers les utilisateurs
All checks were successful
Deploy Preprod / Build and Deploy to Preprod (push) Successful in 31s
All checks were successful
Deploy Preprod / Build and Deploy to Preprod (push) Successful in 31s
Le champ `users` est désormais sur le blueprint projet. Les blueprints pochet/client perdent leur champ `projects`. La logique PHP (user-projects, managers, controller, template, mark-all-read) lit project.users au lieu de user.projects. Script de migration inclus. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
075e511a6a
commit
ea90f512cf
10 changed files with 101 additions and 25 deletions
|
|
@ -85,6 +85,10 @@ tabs:
|
|||
query: page.logo.toFile
|
||||
layout: cardlets
|
||||
required: true
|
||||
users:
|
||||
label: Utilisateurs assignés
|
||||
type: users
|
||||
multiple: true
|
||||
|
||||
- width: 2/3
|
||||
sections:
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ sections:
|
|||
fr: Projet(s) en cours
|
||||
en: Current project(s)
|
||||
link: https://designtopack.morphozbygroupepochet.com/
|
||||
value: "{{ user.projects.toPages.count }}"
|
||||
value: "{{ user.currentProjects.count }}"
|
||||
icon: folder
|
||||
content:
|
||||
label: ' '
|
||||
|
|
@ -52,8 +52,3 @@ sections:
|
|||
layout: cardlets
|
||||
required: true
|
||||
width: 1/2
|
||||
projects:
|
||||
label: Projets
|
||||
type: pages
|
||||
query: page('projects').children
|
||||
width: 1/2
|
||||
|
|
|
|||
|
|
@ -14,11 +14,6 @@ fields:
|
|||
- Sales Manager
|
||||
default: Project Panager
|
||||
width: 1/4
|
||||
projects:
|
||||
label: Projets
|
||||
type: pages
|
||||
query: page('projects').children
|
||||
width: 3/4
|
||||
hiddenProjects:
|
||||
label: Projets masqués
|
||||
type: pages
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ return [
|
|||
require(__DIR__ . '/routes/request-project-creation.php'),
|
||||
require(__DIR__ . '/routes/request-optimization-appointment.php'),
|
||||
require(__DIR__ . '/routes/migrate-notifications.php'),
|
||||
require(__DIR__ . '/routes/migrate-user-projects.php'),
|
||||
],
|
||||
'hooks' => [
|
||||
'page.create:after' => require_once(__DIR__ . '/hooks/create-steps.php'),
|
||||
|
|
|
|||
75
public/site/config/routes/migrate-user-projects.php
Normal file
75
public/site/config/routes/migrate-user-projects.php
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Script de migration : inverse la relation User→Projects en Project→Users.
|
||||
*
|
||||
* Pour chaque user (non-admin) ayant un champ projects non vide,
|
||||
* ajoute ce user dans le champ `users` du projet correspondant.
|
||||
*
|
||||
* Idempotent : pas de doublons si exécuté plusieurs fois.
|
||||
* À supprimer après migration.
|
||||
*
|
||||
* Usage: POST /migrate-user-projects.json
|
||||
*/
|
||||
|
||||
return [
|
||||
'pattern' => 'migrate-user-projects.json',
|
||||
'method' => 'POST',
|
||||
'action' => function () {
|
||||
$user = kirby()->user();
|
||||
|
||||
if (!$user || $user->role()->id() !== 'admin') {
|
||||
return [
|
||||
'status' => 'error',
|
||||
'message' => 'Cette action nécessite les droits administrateur.'
|
||||
];
|
||||
}
|
||||
|
||||
$migrated = [];
|
||||
$errors = [];
|
||||
|
||||
$nonAdminUsers = kirby()->users()->filter(fn($u) => $u->role()->id() !== 'admin');
|
||||
|
||||
foreach ($nonAdminUsers as $u) {
|
||||
if (!$u->projects()->exists() || $u->projects()->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$userProjects = $u->projects()->toPages();
|
||||
|
||||
foreach ($userProjects as $project) {
|
||||
try {
|
||||
$currentUsers = $project->users()->yaml();
|
||||
|
||||
$userUuid = $u->uuid()->toString();
|
||||
|
||||
if (in_array($userUuid, $currentUsers)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$currentUsers[] = $userUuid;
|
||||
$project->update(['users' => $currentUsers]);
|
||||
|
||||
$migrated[] = [
|
||||
'user' => $u->name()->value(),
|
||||
'email' => $u->email(),
|
||||
'project' => $project->title()->value(),
|
||||
];
|
||||
} catch (\Throwable $th) {
|
||||
$errors[] = [
|
||||
'user' => $u->email(),
|
||||
'project' => $project->title()->value(),
|
||||
'error' => $th->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'message' => count($migrated) . ' assignations migrées.',
|
||||
'migrated' => $migrated,
|
||||
'errors' => $errors
|
||||
];
|
||||
}
|
||||
];
|
||||
|
|
@ -27,8 +27,12 @@ return function ($page, $kirby, $site) {
|
|||
}
|
||||
}
|
||||
|
||||
if ($kirby->user()->projects()->exists() && $kirby->user()->projects()->isNotEmpty()) {
|
||||
$userData['projects'] = $kirby->user()->projects()->toPages()->map(function ($project) {
|
||||
$userProjects = $kirby->user()->currentProjects()->merge(
|
||||
$kirby->user()->archivedProjects()
|
||||
);
|
||||
|
||||
if ($userProjects->count() > 0) {
|
||||
$userData['projects'] = $userProjects->map(function ($project) {
|
||||
return [
|
||||
"title" => (string) $project->title(),
|
||||
"uri" => (string) $project->uri(),
|
||||
|
|
|
|||
|
|
@ -222,12 +222,12 @@ class ProjectPage extends NotificationsPage {
|
|||
// }
|
||||
|
||||
public function managers() {
|
||||
return kirby()->users()->filter(function($user) {
|
||||
if ($user->role() != 'admin' && $user->projects()->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user->role() == 'admin' || $user->projects()->toPages()->has($this);
|
||||
if ($this->users()->isEmpty()) {
|
||||
return kirby()->users()->filterBy('role', 'admin');
|
||||
}
|
||||
$projectUsers = $this->users()->toUsers();
|
||||
return kirby()->users()->filter(function($user) use ($projectUsers) {
|
||||
return $user->role() == 'admin' || $projectUsers->has($user);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ return [
|
|||
if ($user->role()->name() === 'admin') {
|
||||
$projects = page('projects')->children()->toArray();
|
||||
} else {
|
||||
$projects = $user->projects()->toPages()->toArray();
|
||||
$projects = $user->currentProjects()->toArray();
|
||||
}
|
||||
|
||||
$count = $collector->markAllAsRead($projects, $user);
|
||||
|
|
|
|||
|
|
@ -5,16 +5,18 @@ Kirby::plugin('adrienpayet/pdc-authorized-projects', [
|
|||
'currentProjects' => function() {
|
||||
if ($this->role() == 'admin') {
|
||||
return page('projects')->children()->listed();
|
||||
} else {
|
||||
return $this->projects()->toPages()->listed();
|
||||
}
|
||||
return page('projects')->children()->listed()->filter(function($project) {
|
||||
return $project->users()->toUsers()->has($this);
|
||||
});
|
||||
},
|
||||
'archivedProjects' => function() {
|
||||
if ($this->role() == 'admin') {
|
||||
return page('projects')->children()->unlisted();
|
||||
} else {
|
||||
return $this->projects()->toPages()->unlisted();
|
||||
}
|
||||
return page('projects')->children()->unlisted()->filter(function($project) {
|
||||
return $project->users()->toUsers()->has($this);
|
||||
});
|
||||
},
|
||||
]
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ $currentUser = $kirby->user();
|
|||
try {
|
||||
$children = $currentUser->role() == 'admin'
|
||||
? $page->childrenAndDrafts()->map(fn($project) => getProjectData($project, $currentUser))->values()
|
||||
: $currentUser->projects()->toPages()->map(fn($project) => getProjectData($project, $currentUser))->values();
|
||||
: $currentUser->currentProjects()->map(fn($project) => getProjectData($project, $currentUser))->values();
|
||||
} catch (\Throwable $th) {
|
||||
throw new Exception($th->getMessage() . ' line ' . $th->getLine() . ' in file ' . $th->getFile(), 1);
|
||||
$children = [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue