diff --git a/class/workinghours.class.php b/class/workinghours.class.php index 546a34d..195b598 100644 --- a/class/workinghours.class.php +++ b/class/workinghours.class.php @@ -57,7 +57,7 @@ class Workinghours extends CommonObject /** * @var string String with name of icon for workinghours. Must be the part after the 'object_' into object_workinghours.png */ - public $picto = 'workinghours@dolisirh'; + public $picto = 'fontawesome_fa-clock_fas_#d35968'; /** * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. diff --git a/view/workinghours_card.php b/view/workinghours_card.php index 57950e9..5712009 100644 --- a/view/workinghours_card.php +++ b/view/workinghours_card.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2023-2025 EVARISK * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,264 +16,161 @@ */ /** - * \file view/workinghours_card.php - * \ingroup dolisirh - * \brief Page to view Working Hours + * \file view/workinghours_card.php + * \ingroup dolisirh + * \brief Page to view working hours */ -// Load Dolibarr environment -$res = 0; -// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined) -if ( ! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"] . "/main.inc.php"; -// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME -$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME']; $tmp2 = realpath(__FILE__); $i = strlen($tmp) - 1; $j = strlen($tmp2) - 1; -while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) { $i--; $j--; } -if ( ! $res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1)) . "/main.inc.php")) $res = @include substr($tmp, 0, ($i + 1)) . "/main.inc.php"; -if ( ! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php")) $res = @include dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php"; -// Try main.inc.php using relative path -if ( ! $res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php"; -if ( ! $res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php"; -if ( ! $res) die("Include of main fails"); - -// Libraries -require_once DOL_DOCUMENT_ROOT . '/core/lib/company.lib.php'; +// Load DoliSIRH environment +$found = false; +for ($i = 1; $i <= 2; $i++) { + $filePath = __DIR__ . str_repeat('/..', $i) . '/dolisirh.main.inc.php'; + if (file_exists($filePath)) { + require_once $filePath; + $found = true; + break; + } +} +if (!$found) { + die('Include of dolisirh main fails'); +} + +// Load Dolibarr libraries require_once DOL_DOCUMENT_ROOT . '/core/lib/usergroups.lib.php'; -require_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php'; -require_once './../class/workinghours.class.php'; +// load DoliSIRH libraries +require_once __DIR__ . '/../class/workinghours.class.php'; // Global variables definitions global $db, $hookmanager, $langs, $user; -// Load translation files required by the page -$langs->loadLangs(array("dolisirh@dolisirh")); +// Load translation files required by the page. +saturne_load_langs(); -// Parameters -$action = (GETPOST('action', 'aZ09') ? GETPOST('action', 'aZ09') : 'view'); -$backtopage = GETPOST('backtopage', 'alpha'); +// Get parameters +$id = GETPOSTINT('id'); +$action = GETPOST('action', 'aZ09'); +$backToPage = GETPOST('backtopage', 'alpha'); -$socid = GETPOST('socid', 'int') ? GETPOST('socid', 'int') : GETPOST('id', 'int'); -if ($usertmp->socid) $socid = $usertmp->socid; -if (empty($socid) && $action == 'view') $action = 'create'; +// Initialize technical objects +$object = new Workinghours($db); +$userTmp = new User($db); -$usertmp = new User($db); +// Initialize view objects +$form = new Form($db); -// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context -$hookmanager->initHooks(array('userworkinghours', 'globalcard')); +$hookmanager->initHooks(['userworkinghours', 'globalcard']); // Note that conf->hooks_modules contains array -if ($action == 'view' && $usertmp->fetch($socid) <= 0) { - $langs->load("errors"); - print($langs->trans('ErrorRecordNotFound')); - exit; -} +// Load object +$userTmp->fetch($id); +$moreWhere = ' AND element_id = ' . $id . ' AND element_type = "' . $user->element . '" AND status = 1'; +$object->fetch(0, '', $moreWhere); -// Security check -$permissiontoadd = $usertmp->rights->societe->creer; +// Security check - Protection if external user -/* - * Actions - */ +// Build current user hierarchy +$userTmpHierarchy = []; +if ($userTmp->fk_user > 0) { + $userTmpHierarchy[] = $userTmp->fk_user; -$parameters = array(); -$reshook = $hookmanager->executeHooks('doActions', $parameters, $usertmp, $action); // Note that $action and $usertmp may have been modified by some hooks -if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); - -if (($action == 'update' && ! GETPOST("cancel", 'alpha')) || ($action == 'updateedit')) { - $object = new Workinghours($db); - $object->element_type = $usertmp->element; - $object->element_id = GETPOST('id'); - $object->status = 1; - $object->fk_user_creat = $user->id; - $object->schedule_monday = GETPOST('schedule_monday', 'string'); - $object->schedule_tuesday = GETPOST('schedule_tuesday', 'string'); - $object->schedule_wednesday = GETPOST('schedule_wednesday', 'string'); - $object->schedule_thursday = GETPOST('schedule_thursday', 'string'); - $object->schedule_friday = GETPOST('schedule_friday', 'string'); - $object->schedule_saturday = GETPOST('schedule_saturday', 'string'); - $object->schedule_sunday = GETPOST('schedule_sunday', 'string'); - - $object->workinghours_monday = GETPOST('workinghours_monday', 'integer'); - $object->workinghours_tuesday = GETPOST('workinghours_tuesday', 'integer'); - $object->workinghours_wednesday = GETPOST('workinghours_wednesday', 'integer'); - $object->workinghours_thursday = GETPOST('workinghours_thursday', 'integer'); - $object->workinghours_friday = GETPOST('workinghours_friday', 'integer'); - $object->workinghours_saturday = GETPOST('workinghours_saturday', 'integer'); - $object->workinghours_sunday = GETPOST('workinghours_sunday', 'integer'); - $result = $object->create($usertmp); - if ($result > 0) { - setEventMessages($langs->trans('UserWorkingHoursSaved'), null, 'mesgs'); - if (!empty($backtopage)) { - header('Location: ' . $backtopage); + $userTmpBoss = new User($db); + $userTmpBoss->fetch($userTmp->fk_user); + while ($userTmpBoss->fk_user > 0) { + $userTmpHierarchy[] = $userTmpBoss->fk_user; + $userTmpBoss->fetch($userTmpBoss->fk_user); + // We do not want to loop between two users who would be each other bosses + if (in_array($userTmpBoss->id, $userTmpHierarchy)) { + break; } - } else { - setEventMessages($langs->trans('UserWorkingHoursSave'), null, 'error'); - } + } } +$permissionToManageWorkingHours = $user->hasRight('dolisirh', $object->element, 'myworkinghours') + || $user->hasRight('dolisirh', $object->element, 'allworkinghours') + || in_array($user->id, $userTmpHierarchy) + || $user->admin == 1; + /* - * View + * Actions */ -$object = new Workinghours($db); - -$morewhere = ' AND element_id = ' . GETPOST('id'); -$morewhere .= ' AND element_type = ' . "'" . $usertmp->element . "'"; -$morewhere .= ' AND status = 1'; +$parameters = []; +$resHook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks +if ($resHook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); +} -$object->fetch(0, '', $morewhere); +if (empty($resHook)) { + if ($action == 'save_working_hours' && !empty($permissionToManageWorkingHours)) { + $object->element_type = $user->element; + $object->element_id = $id; + $object->status = 1; + $dayOfWeek = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']; + foreach ($dayOfWeek as $day) { + $object->{'schedule_' . $day} = GETPOST('schedule_' . $day); + $object->{'workinghours_' . $day} = GETPOSTINT('workinghours_' . $day); + } -if ($socid > 0 && empty($usertmp->id)) { - $result = $usertmp->fetch($socid); - if ($result <= 0) dol_print_error('', $usertmp->error); + $result = $object->create($user); + if ($result > 0) { + setEventMessages('UserWorkingHoursSaved', []); + } else { + setEventMessages('Error', [], 'error'); + } + header('Location: ' . (!empty($backToPage) ? $backToPage : $_SERVER['PHP_SELF'] . '?id=' . $id)); + exit; + } } -$title = $langs->trans("User"); -if ( ! empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/', $conf->global->MAIN_HTML_TITLE) && $usertmp->name) $title = $usertmp->name . " - " . $langs->trans('WorkingHours'); -$help_url = 'FR:Module_DoliSIRH'; - -$morecss = array("/dolisirh/css/dolisirh.css"); +/* + * View + */ -llxHeader('', $title, $help_url, '', '', '', array(), $morecss); +$title = $langs->trans(ucfirst($object->element)); +$helpUrl = 'FR:Module_DoliSIRH'; -if ( ! empty($usertmp->id)) $res = $usertmp->fetch_optionals(); +saturne_header(0, '', $title, $helpUrl); -// Object card -// ------------------------------------------------------------ -$morehtmlref = '
'; -$morehtmlref .= '
'; -$head = user_prepare_head($usertmp); -print dol_get_fiche_head($head, 'workinghours', $langs->trans("User"), 0, 'company'); -$linkback = '' . $langs->trans("BackToList") . ''; -dol_banner_tab($usertmp, 'socid', $linkback, ($usertmp->socid ? 0 : 1), 'rowid', 'nom'); +$head = user_prepare_head($userTmp); +print dol_get_fiche_head($head, 'workinghours', $langs->trans('User'), 0, 'user'); +$linkBack = '' . $langs->trans('BackToList') . ''; +dol_banner_tab($userTmp, 'id', $linkBack, 1, 'rowid', 'nom'); print dol_get_fiche_end(); -print load_fiche_titre($langs->trans('WorkingHours'), '', ''); +print load_fiche_titre($langs->trans('WorkingHours'), '', $object->picto); -//Show common fields - -if ( ! is_object($form)) $form = new Form($db); - -print '
'; +print ''; print ''; -print ''; -print ''; -if ($backtopage) { - print ''; +print ''; +if (!empty($backToPage)) { + print ''; } -print ''; - -print '' . "\n"; - -print ''; - -print ''; -print '' . "\n"; - -print ''; -print ''; -print '' . "\n"; - -print ''; -print ''; -print '' . "\n"; - -print ''; -print ''; -print '' . "\n"; - -print ''; -print ''; -print '' . "\n"; - -print ''; -print ''; -print '' . "\n"; - -print ''; -print ''; -print '' . "\n"; +print '
' . $langs->trans("Day") . '' . $langs->trans("Schedules") . ''. $langs->trans('WorkingHours(min)') .'
'; -print $form->textwithpicto($langs->trans("Monday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; -print $form->textwithpicto($langs->trans("Tuesday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; -print $form->textwithpicto($langs->trans("Wednesday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; -print $form->textwithpicto($langs->trans("Thursday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; -print $form->textwithpicto($langs->trans("Friday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; -print $form->textwithpicto($langs->trans("Saturday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; -print $form->textwithpicto($langs->trans("Sunday"), ''); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("OpeningHoursFormatDesc")); -print ''; -print 'global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '' : ' autofocus="autofocus"') . '>'; -print $form->textwithpicto('', $langs->trans("WorkingHoursFormatDesc")); -print '
'; +print ''; +print ''; +print ''; +print ''; +print ''; + +$dayOfWeek = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']; +foreach ($dayOfWeek as $day) { + print ''; +} print '
' . $langs->trans('Day') . '' . $langs->trans('Schedules') . '' . $langs->trans('WorkingHours(min)') . '
'; + print $langs->trans(ucfirst($day)); + print ''; + print ''; + print $form->textwithpicto('', $langs->trans('OpeningHoursFormatDesc')); + print ''; + print ''; + print $form->textwithpicto('', $langs->trans('WorkingHoursFormatDesc')); + print '
'; -// Buid current user hierarchy -$usertmphierarchy = []; -if ($usertmp->fk_user > 0) { - $usertmphierarchy = array($usertmp->fk_user); - $usertmpboss = new User($object->db); - $usertmpboss->fetch($usertmp->fk_user); - while ($usertmpboss->fk_user > 0) { - $usertmphierarchy[] = $usertmpboss->fk_user; - $usertmpboss->fetch($usertmpboss->fk_user); - // We do not want to loop between two users who would be each other bosses - if (in_array($usertmpboss->id, $usertmphierarchy)) { - break; - } - } -} - -if (($user->rights->dolisirh->workinghours->myworkinghours && $usertmp->id == $user->id) || ($user->rights->dolisirh->workinghours->allworkinghours && in_array($user->id, $usertmphierarchy)) || $user->admin == 1){ - print '
'; - print ''; - print '
'; - print '
'; +if (!empty($permissionToManageWorkingHours)) { + print $form->buttonsSaveCancel('Save', ''); } print '
';