Skip to content

Commit

Permalink
Add page to add user groups from user details and updated buttons' URL
Browse files Browse the repository at this point in the history
  • Loading branch information
catdesu committed Jul 4, 2024
1 parent 8b394bf commit 66ae854
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
3 changes: 2 additions & 1 deletion orif/timbreuse/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// User groups routes
$routes->group('user-groups', function($routes) {
$routes->get('', '\Timbreuse\Controllers\UserGroups');
$routes->get('(:num)', '\Timbreuse\Controllers\UserGroups::displayByUserId/$1');

$routes->get('create', '\Timbreuse\Controllers\UserGroups::create');
$routes->get('create/(:num)', '\Timbreuse\Controllers\UserGroups::create/$1');
Expand All @@ -22,6 +21,7 @@

// Select
$routes->get('select', '\Timbreuse\Controllers\UserGroups::selectUserGroup');
$routes->get('select/(:num)', '\Timbreuse\Controllers\UserGroups::selectUserGroup/$1');
$routes->get('select-parent', '\Timbreuse\Controllers\UserGroups::selectUserGroup');
$routes->get('select-parent/(:num)', '\Timbreuse\Controllers\UserGroups::selectUserGroup/$1');

Expand Down Expand Up @@ -91,6 +91,7 @@

$routes->get('user-groups', '\Timbreuse\Controllers\UserGroups::displayByUserId');
$routes->get('user-groups/(:num)', '\Timbreuse\Controllers\UserGroups::displayByUserId/$1');
$routes->get('user-groups/select/(:num)', '\Timbreuse\Controllers\UserGroups::selectGroupsLinkToUser/$1');

// Personal event planning
$routes->group('event-plannings', function($routes) {
Expand Down
78 changes: 71 additions & 7 deletions orif/timbreuse/Controllers/UserGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class UserGroups extends BaseController
{
// todo: manage rights to methods and add link and unlink user from a group
// Class properties
private UserGroupsModel $userGroupsModel;
private UserSyncGroupsModel $userSyncGroupsModel;
Expand Down Expand Up @@ -61,8 +62,23 @@ public function index() : string {
$data['title'] = lang('tim_lang.user_group_list');

$userGroups = $this->userGroupsModel->findAll();

$userGroups = array_map(function($userGroup) {
if (is_null($userGroup['fk_parent_user_group_id'])) {
$userGroup['class'] = 'bg-light';
}

$userGroup['updateUrl'] = base_url("admin/user-groups/update/{$userGroup['id']}");
$userGroup['deleteUrl'] = base_url("admin/user-groups/delete/{$userGroup['id']}");

return $userGroup;
}, $userGroups);

$data['userGroups'] = $this->formatForListView($userGroups);

$data['createUrl'] = base_url('admin/user-groups/create');
$data['updateUrl'] = base_url('admin/user-groups/update');

return $this->display_view('Timbreuse\Views\userGroups\list', $data);
}

Expand All @@ -73,7 +89,6 @@ public function index() : string {
* @return array
*/
private function getParents(?int $groupId): array|null {
// todo: change the logic to get parents on the same level
if (is_null($groupId)) {
return null;
}
Expand Down Expand Up @@ -132,6 +147,10 @@ public function displayByUserId(?int $timUserId = null) : string {
$timUserId = get_tim_user_id();
}

if (is_admin()) {
$data['createUrl'] = base_url("user-groups/select/$timUserId");
}

$user = $this->userSyncModel->find($timUserId);

$data['title'] = lang('tim_lang.title_user_group_of', [
Expand All @@ -145,24 +164,69 @@ public function displayByUserId(?int $timUserId = null) : string {
->where('fk_user_sync_id', $timUserId)
->findAll();

foreach ($userGroups as &$userGroup) {
$userGroups = array_map(function($userGroup) use ($timUserId) {
$parentGroups = $this->getParents($userGroup['fk_parent_user_group_id']);



if (!is_null($parentGroups)) {
$userGroup['name'] = $this->formatBreadCrumb($userGroup, $parentGroups);
}
}

$data['userGroups'] = $userGroups;
if (is_admin()) {
$userGroup['deleteUrl'] = base_url("admin/user-groups/{$userGroup['id']}/unlink-user/$timUserId");
}

return $userGroup;
}, $userGroups);

$data['userGroups'] = $userGroups;

$data['buttons'] = $this->persoLogsController->get_buttons_for_log_views(Time::today(), 'day', $timUserId)['buttons'];

return $this->display_view([
'Timbreuse\Views\period_menu',
'Timbreuse\Views\userGroups\list'
], $data);
}


/**
* Display page to link groups to a user
*
* @param int $timUserId
* @return string
*/
public function selectGroupsLinkToUser(int $timUserId) : string {
$user = $this->userSyncModel->find($timUserId);
$data['title'] = lang('tim_lang.title_add_groups_to', [
'firstname' => $user['name'],
'lastname' => $user['surname']
]);
$linkedUserGroups = $this->userGroupsModel
->select('user_group.id, fk_parent_user_group_id, name')
->join('user_sync_group', 'fk_user_group_id = user_group.id')
->where('fk_user_sync_id', $timUserId)
->findAll();

$allUserGroups = $this->userGroupsModel->findAll();

$allUserGroups = array_map(function($userGroup) use ($linkedUserGroups, $timUserId) {
if (in_array($userGroup, $linkedUserGroups)) {
$userGroup['class'] = 'bg-secondary';
} else {
$userGroup['addUrl'] = base_url("admin/user-groups/{$userGroup['id']}/link-user/$timUserId");
}

return $userGroup;
}, $allUserGroups);

$data['userGroups'] = $this->formatForListView($allUserGroups);
$data['buttons'] = $this->persoLogsController->get_buttons_for_log_views(Time::today(), 'day', $timUserId)['buttons'];

return $this->display_view([
'Timbreuse\Views\period_menu',
'Timbreuse\Views\userGroups\list'
], $data);
}

/**
* Format and sort user group array for display
*
Expand Down
4 changes: 2 additions & 2 deletions orif/timbreuse/Controllers/UserSyncGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function addLinkUserToGroup(int $groupId, int $userId) : RedirectResponse
'fk_user_group_id' => $groupId
]);

return redirect()->to(base_url("admin/user-groups/{$groupId}/link-user"));
return redirect()->to($_SESSION['_ci_previous_url']);
}

/**
Expand All @@ -104,7 +104,7 @@ public function deleteLinkToGroup(int $groupId, int $userId) : RedirectResponse
->where('fk_user_sync_id', $userId)
->delete();

return redirect()->to(base_url("admin/user-groups/{$groupId}/link-user"));
return redirect()->to($_SESSION['_ci_previous_url']);
}

/**
Expand Down
1 change: 1 addition & 0 deletions orif/timbreuse/Language/fr/tim_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,5 @@
'unknown_user' => 'Utilisateur inconnu',
'403_error_message' => 'Vous n\'avez pas la permission d\'accéder à cette page.',
'title_user_group_of' => 'Groupes d\'utilisateurs liés à {firstname} {lastname}',
'title_add_groups_to' => 'Ajouter des groupes d\'utilisateurs à {firstname} {lastname}'
];
23 changes: 16 additions & 7 deletions orif/timbreuse/Views/userGroups/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<div class="text-left col-12">
<h3><?= esc($title) ?></h3>
</div>
<?php if ($isAdminView): ?>
<?php if (isset($createUrl)): ?>
<div class="col-sm-6 text-left">
<a class="btn btn-primary" href="<?= current_url() . '/create' ?>"><?= lang('common_lang.btn_add') ?></a>
<a class="btn btn-primary" href="<?= $createUrl ?>"><?= lang('common_lang.btn_add') ?></a>
</div>
<?php endif; ?>
</div>
Expand All @@ -18,17 +18,26 @@
</thead>
<tbody id="table-body">
<?php foreach($userGroups as $userGroup): ?>
<tr class="<?= is_null($userGroup['fk_parent_user_group_id']) ? 'bg-light' : '' ?>">
<tr class="<?= isset($userGroup['class']) ? $userGroup['class'] : '' ?>">
<td>
<?= (isset($linkedUserGroups)
&& in_array($userGroup['id'], $linkedUserGroups) ? '<i class="bi bi-circle-fill"></i> ': '') . $userGroup['name'] ?>
</td>
<td class="text-right">
<?php if ($isAdminView): ?>
<a href="<?= current_url() . "/update/{$userGroup['id']}" ?>">
<i class="bi bi-pencil" style="font-size: 20px;"></i>
<?php if (isset($userGroup['addUrl'])): ?>
<a href="<?= $userGroup['addUrl'] ?>">
<i class="bi bi-plus-circle" style="font-size: 20px;"></i>
</a>
<a href="<?= current_url() . "/delete/{$userGroup['id']}" ?>">
<?php endif; ?>

<?php if (isset($userGroup['updateUrl'])): ?>
<a href="<?= $userGroup['updateUrl'] ?>">
<i class="bi bi-pencil" style="font-size: 20px;"></i>
</a>
<?php endif; ?>

<?php if (isset($userGroup['deleteUrl'])): ?>
<a href="<?= $userGroup['deleteUrl'] ?>">
<i class="bi bi-trash" style="font-size: 20px;"></i>
</a>
<?php endif; ?>
Expand Down

0 comments on commit 66ae854

Please sign in to comment.