Skip to content

Commit

Permalink
Finished create group event planning and made user-groups/select reus…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
catdesu committed Mar 6, 2024
1 parent 56f7e1a commit 29f8ab8
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 75 deletions.
6 changes: 4 additions & 2 deletions orif/timbreuse/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
$routes->get('update/(:num)', '\Timbreuse\Controllers\UserGroups::update/$1');
$routes->get('update/(:num)/(:num)', '\Timbreuse\Controllers\UserGroups::update/$1/$2');
$routes->post('update/(:num)', '\Timbreuse\Controllers\UserGroups::update/$1');
$routes->get('select-parent', '\Timbreuse\Controllers\UserGroups::selectParent');
$routes->get('select-parent/(:num)', '\Timbreuse\Controllers\UserGroups::selectParent/$1');
$routes->get('select-parent', '\Timbreuse\Controllers\UserGroups::selectUserGroup');
$routes->get('select', '\Timbreuse\Controllers\UserGroups::selectUserGroup');
$routes->get('select-parent/(:num)', '\Timbreuse\Controllers\UserGroups::selectUserGroup/$1');
});

// Event plannings
Expand All @@ -23,6 +24,7 @@
$routes->get('personal/create', '\Timbreuse\Controllers\EventPlannings::createPersonal');
$routes->post('personal/create', '\Timbreuse\Controllers\EventPlannings::createPersonal');
$routes->get('group/create', '\Timbreuse\Controllers\EventPlannings::createGroup');
$routes->get('group/create/(:num)', '\Timbreuse\Controllers\EventPlannings::createGroup/$1');
$routes->post('group/create', '\Timbreuse\Controllers\EventPlannings::createGroup');
$routes->get('delete/(:num)', '\Timbreuse\Controllers\EventPlannings::delete/$1');
$routes->post('delete/(:num)/(:num)', '\Timbreuse\Controllers\EventPlannings::delete/$1/$2');
Expand Down
89 changes: 52 additions & 37 deletions orif/timbreuse/Controllers/EventPlannings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
use Psr\Log\LoggerInterface;
use Timbreuse\Models\EventPlanningsModel;
use Timbreuse\Models\EventTypesModel;
use Timbreuse\Models\UserGroupsModel;

class EventPlannings extends BaseController
{
// Class properties
private EventPlanningsModel $eventPlanningsModel;
private EventTypesModel $eventTypesModel;
private UserGroupsModel $userGroupsModel;

/**
* Constructor
Expand All @@ -35,6 +37,7 @@ public function initController(
// Load required models
$this->eventPlanningsModel = new EventPlanningsModel();
$this->eventTypesModel = new EventTypesModel();
$this->userGroupsModel = new UserGroupsModel();
}

/**
Expand Down Expand Up @@ -75,7 +78,13 @@ public function createPersonal() : string|RedirectResponse {
'eventTypes' => $this->mapForSelectForm($eventTypes)
];

session()->remove('eventPlanningPostData');

if (isset($_POST) && !empty($_POST)) {
if ($this->checkButtonClicked('select_linked_user')) {
return redirect()->to(base_url('admin/users/select'));
}

$data['errors'] = $this->getPostDataAndSaveEventPlanning();

if (empty($data['errors'])) {
Expand All @@ -93,19 +102,28 @@ public function createPersonal() : string|RedirectResponse {
*
* @return string|RedirectResponse
*/
public function createGroup() : string|RedirectResponse {
public function createGroup(?int $userGroupId = null) : string|RedirectResponse {
$eventTypes = $this->eventTypesModel->where('is_group_event_type', true)->findAll();
$userGroup = $this->userGroupsModel->find($userGroupId);
$data = [
'title' => lang('tim_lang.create_event_planning_title'),
'eventPlanning' => null,
'eventTypes' => $this->mapForSelectForm($eventTypes)
'sessionEventPlanning' => session()->get('eventPlanningPostData'),
'eventTypes' => $this->mapForSelectForm($eventTypes),
'userGroup' => $userGroup
];

session()->remove('eventPlanningPostData');

if (isset($_POST) && !empty($_POST)) {
if ($this->checkButtonClicked('select_user_group')) {
return redirect()->to(base_url('admin/user-groups/select?path=admin/event-plannings/group/create/'));
}

$data['errors'] = $this->getPostDataAndSaveEventPlanning();

if (empty($data['errors'])) {
return redirect()->to(base_url('admin/event-types'));
return redirect()->to(base_url('admin/event-plannings'));
}
}

Expand All @@ -114,35 +132,6 @@ public function createGroup() : string|RedirectResponse {
'Timbreuse\Views\eventPlannings\get_event_series_form'], $data);
}

/**
* Retrieves the corresponding event type and display the update form
*
* @param int $id
* @return string|RedirectResponse
*/
public function update(int $id) : string|RedirectResponse {
$eventPlanning = $this->eventPlanningsModel->find($id);

if (is_null($eventPlanning)) {
return redirect()->to(base_url('admin/event-types'));
}

$data = [
'title' => lang('tim_lang.update_event_planning_title'),
'eventPlanning' => $eventPlanning
];

if (isset($_POST) && !empty($_POST)) {
$data['errors'] = $this->getPostDataAndSaveEventPlanning();

if (empty($data['errors'])) {
return redirect()->to(base_url('admin/event-types'));
}
}

return $this->display_view(['Timbreuse\Views\eventPlannings\save_form'], $data);
}

/**
* Display the delete form and delete the corresponding event type
*
Expand Down Expand Up @@ -177,7 +166,7 @@ public function delete(int $id, int $action = 0) : string|RedirectResponse {
break;
}

return redirect()->to(base_url('admin/event-types'));
return redirect()->to(base_url('admin/event-plannings'));
}

/**
Expand All @@ -186,16 +175,17 @@ public function delete(int $id, int $action = 0) : string|RedirectResponse {
* @return array Validation errors encountered during the saving process
*/
private function getPostDataAndSaveEventPlanning() : array {
// todo: Save event serie and get id of saved + errors
$eventPlanning = [
'id' => $this->request->getPost('id'),
'fk_event_series_id' => $this->request->getPost('fk_event_series_id'),
'fk_user_group_id' => $this->request->getPost('fk_user_group_id'),
'fk_user_sync_id' => $this->request->getPost('fk_user_sync_id'),
'fk_event_series_id' => null,
'fk_user_group_id' => $this->request->getPost('linked_user_group_id') ?? null,
'fk_user_sync_id' => $this->request->getPost('linked_user_id') ?? null,
'fk_event_type_id' => $this->request->getPost('fk_event_type_id'),
'event_date' => $this->request->getPost('event_date'),
'start_time' => $this->request->getPost('start_time'),
'end_time' => $this->request->getPost('end_time'),
'is_work_time' => (bool)$this->request->getPost('isPersonalEventType'),
'is_work_time' => (bool)$this->request->getPost('is_work_time'),
];

$this->eventPlanningsModel->save($eventPlanning);
Expand All @@ -207,4 +197,29 @@ private function mapForSelectForm($array) : array {
return $row['name'];
}, $array));
}

/**
* Check if selectParentButton has been clicked
* Save post data on true
*
* @return bool
*/
private function checkButtonClicked(string $buttonName) : bool {
$selectParentUserGroup = boolval($this->request->getPost($buttonName));

if ($selectParentUserGroup) {
$this->savePostDataToSession('eventPlanningPostData');
}

return $selectParentUserGroup;
}

/**
* Save post data in the user session
*
* @return void
*/
private function savePostDataToSession(string $key) : void {
session()->set($key, $this->request->getPost());
}
}
18 changes: 6 additions & 12 deletions orif/timbreuse/Controllers/UserGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function create(int $parentId = null) : string|RedirectResponse {

if (isset($_POST) && !empty($_POST)) {
if ($this->checkSelectParent()) {
return redirect()->to(base_url('admin/user-groups/select-parent'));
return redirect()->to(base_url('admin/user-groups/select-parent?path=admin/user-groups/create/'));
}

$data['errors'] = $this->getPostDataAndSaveUserGroup();
Expand Down Expand Up @@ -118,7 +118,7 @@ public function update(int $id, int $parentId = null) : string|RedirectResponse

if (isset($_POST) && !empty($_POST)) {
if ($this->checkSelectParent()) {
return redirect()->to(base_url('admin/user-groups/select-parent/' . $id ?? ''));
return redirect()->to(base_url('admin/user-groups/select-parent/' . $id . '?path=admin/user-groups/update/' . $id . '/'));
}

$data['errors'] = $this->getPostDataAndSaveUserGroup();
Expand All @@ -132,19 +132,13 @@ public function update(int $id, int $parentId = null) : string|RedirectResponse
}

/**
* Display select parent user group page
* Display select user group page
*
* @param int $id
* @return string
*/
public function selectParent(?int $id = null) : string {
$pathToUserGroupForm = 'admin/user-groups/';

if (is_null($id)) {
$pathToUserGroupForm = "{$pathToUserGroupForm}create/";
} else {
$pathToUserGroupForm = "{$pathToUserGroupForm}update/{$id}/";
}
public function selectUserGroup(?int $id = null) : string {
$filters = $_GET;

$data['title'] = lang('tim_lang.user_group_list');
$data['list_title'] = ucfirst(lang('tim_lang.user_group_list'));
Expand All @@ -155,7 +149,7 @@ public function selectParent(?int $id = null) : string {

$data['items'] = $this->userGroupsModel->where('id !=', $id)->findAll();

$data['url_update'] = $pathToUserGroupForm;
$data['url_update'] = $filters['path'];

return $this->display_view('Common\Views\items_list', $data);
}
Expand Down
2 changes: 1 addition & 1 deletion orif/timbreuse/Language/fr/tim_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
'field_linked_user' => 'Utilisateur concerné',
'field_linked_user_group' => 'Groupe concerné',
'btn_select_linked_user' => 'Sélectionner un utilisateur',
'btn_select_linked_user_group' => 'Sélectionner un groupe d’utilisateurs',
'btn_select_user_group' => 'Sélectionner un groupe d’utilisateurs',
'field_start_date' => 'Date de début',
'field_end_date' => 'Date de fin',
'field_recurrence_frequency' => 'Fréquence',
Expand Down
12 changes: 6 additions & 6 deletions orif/timbreuse/Models/EventPlanningsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public function __construct(ConnectionInterface &$db = null, ValidationInterface
'fk_user_group_id' =>
[
'label' => lang('tim_lang.field_user_group_id'),
'rules' => 'required_without[fk_user_sync_id]|numeric'
'rules' => 'required_without[fk_user_sync_id]|permit_empty|numeric'
],
'fk_user_sync_id' =>
[
'label' => lang('tim_lang.field_user_sync_id'),
'rules' => 'required_without[fk_user_group_id]|numeric'
'rules' => 'required_without[fk_user_group_id]|permit_empty|numeric'
],
'fk_event_type_id' =>
[
Expand All @@ -61,22 +61,22 @@ public function __construct(ConnectionInterface &$db = null, ValidationInterface
'event_date' =>
[
'label' => lang('tim_lang.field_event_date'),
'rules' => 'required'
'rules' => 'required|valid_date'
],
'start_time' =>
[
'label' => lang('tim_lang.field_event_start_time'),
'rules' => 'required'
'rules' => 'required|valid_date'
],
'end_time' =>
[
'label' => lang('tim_lang.field_event_end_time'),
'rules' => 'required'
'rules' => 'required|valid_date'
],
'is_work_time' =>
[
'label' => lang('tim_lang.field_is_work_time'),
'rules' => 'required|is_bool'
'rules' => 'is_bool'
],
];

Expand Down
32 changes: 17 additions & 15 deletions orif/timbreuse/Views/eventPlannings/group/save_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,20 @@
<div class="col-sm-6">
<div class="form-group">
<?= form_label(lang('tim_lang.event_type'), 'event_type', ['class' => 'form-label']); ?>
<?= form_dropdown('event_type', $eventTypes, [], [
<?= form_dropdown('fk_event_type_id', $eventTypes, $sessionEventPlanning['fk_event_type_id'] ?? [], [
'class' => 'form-control', 'id' => 'event_type'
]); ?>
<span class="text-danger"><?= isset($errors['end_time']) ? esc($errors['end_time']) : ''; ?></span>
<span class="text-danger"><?= isset($errors['fk_event_type_id']) ? esc($errors['fk_event_type_id']) : ''; ?></span>
</div>
</div>

<div class="col-sm-6">
<div class="form-group">
<?= form_label(lang('tim_lang.field_linked_user_group'), 'linked_user_group', ['class' => 'form-label']); ?>
<?= form_input('', $eventPlanning['linked_user_group'] ?? set_value('linked_user_group'), [
<?= form_input('', $userGroup['name'] ?? '', [
'class' => 'form-control', 'id' => 'linked_user_group', 'disabled' => ''
]); ?>
<span class="text-danger"><?= isset($errors['end_time']) ? esc($errors['end_time']) : ''; ?></span>
<?= form_submit('select_linked_user_group', lang('tim_lang.btn_select_linked_user_group'), ['class' => 'mt-3 w-100 btn btn-secondary']); ?>
<?= form_submit('select_user_group', lang('tim_lang.btn_select_user_group'), ['class' => 'mt-3 w-100 btn btn-secondary']); ?>
</div>
</div>
</div>
Expand All @@ -48,26 +47,26 @@
<div class="col-sm-4">
<div class="form-group">
<?= form_label(lang('tim_lang.field_event_date'), 'event_date', ['class' => 'form-label']); ?>
<?= form_input('event_date', $eventPlanning['event_date'] ?? set_value('event_date'), [
'class' => 'form-control', 'id' => 'event_date', 'required' => ''
<?= form_input('event_date', $sessionEventPlanning['event_date'] ?? $eventPlanning['event_date'] ?? set_value('event_date'), [
'class' => 'form-control', 'id' => 'event_date',
], 'date'); ?>
<span class="text-danger"><?= isset($errors['event_date']) ? esc($errors['event_date']) : ''; ?></span>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<?= form_label(lang('tim_lang.field_start_time'), 'start_time', ['class' => 'form-label']); ?>
<?= form_input('start_time', $eventPlanning['start_time'] ?? set_value('start_time'), [
'class' => 'form-control', 'id' => 'start_time', 'required' => ''
<?= form_input('start_time', $sessionEventPlanning['start_time'] ?? $eventPlanning['start_time'] ?? set_value('start_time'), [
'class' => 'form-control', 'id' => 'start_time',
], 'time'); ?>
<span class="text-danger"><?= isset($errors['start_time']) ? esc($errors['start_time']) : ''; ?></span>
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<?= form_label(lang('tim_lang.field_end_time'), 'end_time', ['class' => 'form-label']); ?>
<?= form_input('end_time', $eventPlanning['end_time'] ?? set_value('end_time'), [
'class' => 'form-control', 'id' => 'end_time', 'required' => ''
<?= form_input('end_time', $sessionEventPlanning['end_time'] ?? $eventPlanning['end_time'] ?? set_value('end_time'), [
'class' => 'form-control', 'id' => 'end_time',
], 'time'); ?>
<span class="text-danger"><?= isset($errors['end_time']) ? esc($errors['end_time']) : ''; ?></span>
</div>
Expand All @@ -80,7 +79,7 @@
<?= form_radio(
'is_work_time',
'1',
$update ? $eventPlanning['is_work_time'] : true,
$update ? $eventPlanning['is_work_time'] : (!is_null($sessionEventPlanning['is_work_time']) ? $sessionEventPlanning['is_work_time'] : true),
['class' => 'form-check-input', 'id' => 'is_work_time_yes']
) ?>
<?= form_label(lang('common_lang.yes'), 'is_work_time_yes', ['class' => 'form-check-label']); ?>
Expand All @@ -89,7 +88,10 @@
<?= form_radio(
'is_work_time',
'0',
$update ? !$eventPlanning['is_work_time'] : false,
$update ?
!$eventPlanning['is_work_time'] : (!is_null($sessionEventPlanning['is_work_time']) ?
!$sessionEventPlanning['is_work_time'] :
false),
['class' => 'form-check-input', 'id' => 'is_work_time_no']
) ?>
<?= form_label(lang('common_lang.no'), 'is_work_time_no', ['class' => 'form-check-label']); ?>
Expand All @@ -100,9 +102,9 @@
</div>
</div>

<?= form_input('linked_user_group_id', '', ['hidden' => '']) ?>
<?= form_input('linked_user_group_id', $userGroup['id'] ?? '', ['hidden' => '']) ?>

<?php if (!$update): ?>
<?php if (!$update) : ?>
<?= form_button('', lang('tim_lang.btn_create_series'), ['class' => 'btn btn-primary', 'id' => 'create_series']) ?>
<div id="create_series_form"></div>
<?php endif; ?>
Expand Down
4 changes: 2 additions & 2 deletions orif/timbreuse/Views/eventPlannings/personal/save_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="col-sm-6">
<div class="form-group">
<?= form_label(lang('tim_lang.event_type'), 'event_type', ['class' => 'form-label']); ?>
<?= form_dropdown('event_type', $eventTypes, [], [
<?= form_dropdown('fk_event_type_id', $eventTypes, [], [
'class' => 'form-control', 'id' => 'event_type'
]); ?>
<span class="text-danger"><?= isset($errors['end_time']) ? esc($errors['end_time']) : ''; ?></span>
Expand All @@ -35,7 +35,7 @@
<div class="col-sm-6">
<div class="form-group">
<?= form_label(lang('tim_lang.field_linked_user'), 'linked_user', ['class' => 'form-label']); ?>
<?= form_input('', $eventPlanning['linked_user'] ?? set_value('linked_user'), [
<?= form_input('', $eventPlanning['linked_user_name'] ?? set_value('linked_user'), [
'class' => 'form-control', 'id' => 'linked_user', 'disabled' => ''
]); ?>
<span class="text-danger"><?= isset($errors['end_time']) ? esc($errors['end_time']) : ''; ?></span>
Expand Down

0 comments on commit 29f8ab8

Please sign in to comment.