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

Add user groups page to user's details #31

Merged
merged 7 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions orif/timbreuse/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,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 @@ -88,6 +89,11 @@
});
});

// User groups
$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) {
$routes->get('', '\Timbreuse\Controllers\PersonalEventPlannings::index');
Expand Down
15 changes: 14 additions & 1 deletion orif/timbreuse/Controllers/PersoLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ protected function create_event_planning_link(?int $timUserId=null): array
$button['label'] = ucfirst(lang('tim_lang.event_plannings_list'));
return $button;
}

public function create_user_group_link(?int $timUserId=null): array
{
helper('UtilityFunctions');
if ($timUserId === get_tim_user_id()
&& $_SESSION['user_access'] < config('\User\Config\UserConfig')->access_lvl_admin) {
$button['link'] = base_url('user-groups');
} else {
$button['link'] = base_url("user-groups/$timUserId");
}
$button['label'] = ucfirst(lang('tim_lang.user_group_list'));
return $button;
}

public function get_buttons_for_log_views($day, string $period,
?int $timUserId=null): array
Expand All @@ -212,7 +225,7 @@ public function get_buttons_for_log_views($day, string $period,
$data['buttons'] = array_merge($this->create_time_links($day, $period), $data['buttons']);
array_push($data['buttons'], $this->create_planning_link($timUserId));
array_push ($data['buttons'], $this->create_event_planning_link($timUserId));

array_push ($data['buttons'], $this->create_user_group_link($timUserId));
return $data;
}

Expand Down
2 changes: 1 addition & 1 deletion orif/timbreuse/Controllers/PersonalEventPlannings.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function index(?int $timUserId = null) : string|RedirectResponse {
$data['url_delete'] = $eventPlannigRoute . 'delete/serie-or-occurence/';

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

Expand Down
206 changes: 202 additions & 4 deletions orif/timbreuse/Controllers/UserGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
use Timbreuse\Models\UserSyncGroupsModel;
use Timbreuse\Models\EventPlanningsModel;
use Timbreuse\Controllers\Users;
use Timbreuse\Models\UsersModel;
use Timbreuse\Controllers\PersoLogs;
use CodeIgniter\I18n\Time;

class UserGroups extends BaseController
{
// Class properties
private UserGroupsModel $userGroupsModel;
private UserSyncGroupsModel $userSyncGroupsModel;
private EventPlanningsModel $eventPlanningsModel;
private UsersModel $userSyncModel;
private Users $userSyncController;
private PersoLogs $persoLogsController;

/**
* Constructor
Expand All @@ -30,19 +35,22 @@ public function initController(
): void {
// Set Access level before calling parent constructor
// Accessibility reserved to admin users
$this->access_level = config('\User\Config\UserConfig')->access_lvl_admin;
$this->access_level = config('\User\Config\UserConfig')->access_lvl_registered;
parent::initController($request, $response, $logger);

// Load required helpers
helper('form');
helper('UtilityFunctions');

// Load required models
$this->userGroupsModel = new UserGroupsModel();
$this->userSyncGroupsModel = new UserSyncGroupsModel();
$this->eventPlanningsModel = new EventPlanningsModel();
$this->userSyncModel = new UsersModel();

// Load required controllers
$this->userSyncController = new Users();
$this->persoLogsController = new PersoLogs();
}

/**
Expand All @@ -51,14 +59,188 @@ public function initController(
* @return string
*/
public function index() : string {
if (!is_admin()) {
return redirect()->to(base_url('user-groups'));
}

$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);
}

/**
* Get all parents recursively
*
* @param mixed $groupId
* @return array
*/
private function getParents(?int $groupId): array|null {
if (is_null($groupId)) {
return null;
}

$allParentGroups = [];

$parentGroup = $this->userGroupsModel->find($groupId);

if ($parentGroup && !is_null($parentGroup['fk_parent_user_group_id'])) {
$parents = $this->getParents($parentGroup['fk_parent_user_group_id']);

foreach($parents as $parent) {
array_push($allParentGroups, $parent);
}
}

array_push($allParentGroups, $parentGroup);

return $allParentGroups;
}

/**
* Format parent and child hierarchy as a breadcrumb
*
* @param array $group
* @param array $parentGroups
* @return string
*/
private function formatBreadCrumb(array $group, array $parentGroups) : string {
$groupBreadCrumb = '';
$arrow = ' <i class="bi bi-arrow-right"></i> ';

foreach ($parentGroups as $i => $parentGroup) {
if ($i > 0) {
$groupBreadCrumb .= $arrow;
}

$groupBreadCrumb .= esc($parentGroup['name']);
}

$groupBreadCrumb .= $arrow . esc($group['name']);

return $groupBreadCrumb;
}

/**
* Display user groups by corresponding user id
*
* @param mixed $timUserId
* @return string
*/
public function displayByUserId(?int $timUserId = null) : string|RedirectResponse {
if (is_null($timUserId)) {
$timUserId = get_tim_user_id();
}

if ($timUserId != get_tim_user_id() && !is_admin()) {
return redirect()->to(base_url('user-groups'));
}

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

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

if (is_null($user)) {
return redirect()->to($_SESSION['_ci_previous_url']);
}

$data['title'] = lang('tim_lang.title_user_group_of', [
'firstname' => $user['name'],
'lastname' => $user['surname']
]);

$userGroups = $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();

$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);
}

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|RedirectResponse {
if (!is_admin()) {
return redirect()->to(base_url('user-groups'));
}

$user = $this->userSyncModel->find($timUserId);
$data['title'] = lang('tim_lang.title_manage_user_groups', [
'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 Expand Up @@ -102,7 +284,7 @@ private function displayHierarchyRecursive(array $array, ?string $parentId = nul
$prefix .= str_repeat('<i class="bi bi-chevron-right"></i>', $depth) . ' ';
}

$item['name'] = $prefix . $item['name'];
$item['name'] = $prefix . esc($item['name']);
$result[] = $item;

// Recursively process children
Expand All @@ -123,6 +305,10 @@ private function displayHierarchyRecursive(array $array, ?string $parentId = nul
* @return string|RedirectResponse
*/
public function create(int $parentId = null) : string|RedirectResponse {
if (!is_admin()) {
return redirect()->to(base_url('user-groups'));
}

$parentUserGroup = $this->userGroupsModel->find($parentId);

$data = [
Expand Down Expand Up @@ -156,6 +342,10 @@ public function create(int $parentId = null) : string|RedirectResponse {
* @return string|RedirectResponse
*/
public function update(int $id, int $parentId = null) : string|RedirectResponse {
if (!is_admin()) {
return redirect()->to(base_url('user-groups'));
}

$userGroup = $this->userGroupsModel->find($id);
$parentUserGroupId = $parentId ?? $userGroup['fk_parent_user_group_id'] ?? null;

Expand Down Expand Up @@ -201,6 +391,10 @@ public function update(int $id, int $parentId = null) : string|RedirectResponse
* @return string|RedirectResponse
*/
public function delete(int $id, int $action = 0) : string|RedirectResponse {
if (!is_admin()) {
return redirect()->to(base_url('user-groups'));
}

$userGroup = $this->userGroupsModel->find($id);

if (!$userGroup) {
Expand Down Expand Up @@ -240,9 +434,13 @@ public function delete(int $id, int $action = 0) : string|RedirectResponse {
* Display select user group page
*
* @param int $id
* @return string
* @return string|RedirectResponse
*/
public function selectUserGroup(?int $id = null) : string {
public function selectUserGroup(?int $id = null) : string|RedirectResponse {
if (!is_admin()) {
return redirect()->to(base_url('user-groups'));
}

$filters = $_GET;

$data['route'] = $filters['path'];
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
2 changes: 2 additions & 0 deletions orif/timbreuse/Language/fr/tim_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,6 @@
'msg_err_end_date_greater_than' => 'La date de fin doit être supérieure à la date de début.',
'unknown_user' => 'Utilisateur inconnu',
'403_error_message' => 'Vous n\'avez pas la permission d\'accéder à cette page.',
'title_user_group_of' => 'Groupes dont {firstname} {lastname} est membre',
'title_manage_user_groups' => 'Gestion des groupes dont {firstname} {lastname} est membre'
];
Loading
Loading