| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client1/web17/web/ |
Upload File : |
<?php
/**
* worktime-api.php – JSON-Endpunkt der integrierten Arbeitszeiterfassung.
* Hinter dem PaCIM-Login (Session). Nutzt die worktime-Klasse (parametrisierte Queries).
*
* Aktionen (?action=):
* GET events -> Kalender-Events (FullCalendar)
* GET workhour&id= -> einzelne Arbeitszeit
* POST add_hours / update_hours -> employee,date,start_time,end_time,break_duration[,id]
* POST delete_hours&id=
* POST add_employee -> first_name,last_name,max_hours
*/
require_once( __DIR__ . "/includes/config.inc.php" );
header( 'Content-Type: application/json; charset=utf-8' );
// --- Login-Schutz (gleiche Regel wie page::checkLogin) ---
if ( !isset( $_SESSION['user'] ) || $_SESSION['user'] != LOGIN_USER ) {
http_response_code( 403 );
echo json_encode( array( 'error' => 'unauthorized' ) );
exit();
}
$oWorktime = new worktime();
$action = isset( $_GET['action'] ) ? $_GET['action'] : '';
$isPost = $_SERVER['REQUEST_METHOD'] === 'POST';
switch ( $action ) {
case 'events':
echo json_encode( $oWorktime->calendarEvents() );
break;
case 'calendar':
echo json_encode( $oWorktime->calendarData() );
break;
case 'dashboard':
echo json_encode( $oWorktime->dashboard() );
break;
case 'employees':
echo json_encode( $oWorktime->employees() );
break;
case 'stats':
$sY = (int) ( isset( $_GET['year'] ) ? $_GET['year'] : date('Y') );
$sM = (int) ( isset( $_GET['month'] ) ? $_GET['month'] : date('n') );
$sEmps = $oWorktime->monthEmployeeHours( $sY, $sM );
$sNames = $oWorktime->monthNames();
$sTotal = 0; $sCount = 0;
foreach ( $sEmps as $se ) { $h = (float) ( is_array( $se ) ? $se['hours'] : $se->hours ); $sTotal += $h; if ( $h > 0 ) $sCount++; }
echo json_encode( array(
'year' => $sY,
'month' => $sM,
'label' => ( isset( $sNames[$sM] ) ? $sNames[$sM] : '' ) . ' ' . $sY,
'days' => $oWorktime->monthHours( $sY, $sM ),
'employees' => $sEmps,
'totals' => array(
'hours' => round( $sTotal, 2 ),
'employees' => $sCount,
'avg' => $sCount ? round( $sTotal / $sCount, 2 ) : 0
)
) );
break;
case 'day':
echo json_encode( $oWorktime->workHours( array( 'date' => isset( $_GET['date'] ) ? $_GET['date'] : '' ) ) );
break;
case 'employee_hours':
echo json_encode( array(
'employee' => $oWorktime->employee( isset( $_GET['id'] ) ? $_GET['id'] : 0 ),
'hours' => $oWorktime->workHours( array( 'employee_id' => isset( $_GET['id'] ) ? $_GET['id'] : 0 ) )
) );
break;
case 'workhour':
echo json_encode( $oWorktime->workHour( isset( $_GET['id'] ) ? $_GET['id'] : 0 ) );
break;
case 'update_hours':
if ( !$isPost ) { fail(); }
$ok = $oWorktime->updateWorkHour(
$_POST['id'] ?? 0, $_POST['employee'] ?? 0, $_POST['date'] ?? '',
$_POST['start_time'] ?? '', $_POST['end_time'] ?? '', $_POST['break_duration'] ?? 0
);
echo json_encode( array( 'success' => (bool) $ok ) );
break;
case 'add_hours':
if ( !$isPost ) { fail(); }
// Mehrere Mitarbeiter möglich (employees[]); gleiche Zeit für alle.
$emps = ( isset( $_POST['employees'] ) && is_array( $_POST['employees'] ) )
? $_POST['employees']
: array( $_POST['employee'] ?? 0 );
$ok = true; $n = 0;
foreach ( $emps as $eid ) {
if ( (int) $eid <= 0 ) continue;
$r = $oWorktime->addWorkHour( $eid, $_POST['date'] ?? '', $_POST['start_time'] ?? '', $_POST['end_time'] ?? '', $_POST['break_duration'] ?? 0 );
$ok = $ok && (bool) $r; $n++;
}
echo json_encode( array( 'success' => ( $n > 0 && $ok ), 'count' => $n ) );
break;
case 'add_hours_batch':
// Mehrere Mitarbeiter mit je EIGENEN Zeiten/Pause an einem Tag (aus dem Erfassungs-Modal).
if ( !$isPost ) { fail(); }
$date = (string) ( $_POST['date'] ?? '' );
$entries = json_decode( (string) ( $_POST['entries'] ?? '' ), true );
if ( ! is_array( $entries ) || ! count( $entries ) ) { echo json_encode( array( 'success' => false, 'error' => 'no_entries' ) ); break; }
$ok = true; $n = 0; $errors = array(); $seen = array();
foreach ( $entries as $en ) {
$eid = (int) ( $en['employee'] ?? 0 );
$st = trim( (string) ( $en['start_time'] ?? '' ) );
$et = trim( (string) ( $en['end_time'] ?? '' ) );
$br = max( 0, min( 600, (int) ( $en['break_duration'] ?? 0 ) ) );
if ( $eid <= 0 || $st === '' || $et === '' ) { $errors[] = 'Unvollständige Zeile.'; $ok = false; continue; }
if ( isset( $seen[ $eid ] ) ) { $errors[] = 'Mitarbeiter mehrfach gewählt.'; $ok = false; continue; }
$seen[ $eid ] = true;
$r = $oWorktime->addWorkHour( $eid, $date, $st, $et, $br );
if ( $r ) { $n++; } else { $ok = false; $errors[] = 'Speichern fehlgeschlagen (MA ' . $eid . ').'; }
}
echo json_encode( array( 'success' => ( $n > 0 && $ok ), 'count' => $n, 'errors' => $errors ) );
break;
case 'delete_hours':
if ( !$isPost ) { fail(); }
$ok = $oWorktime->deleteWorkHour( $_POST['id'] ?? 0 );
echo json_encode( array( 'success' => (bool) $ok ) );
break;
case 'add_employee':
if ( !$isPost ) { fail(); }
$ok = $oWorktime->addEmployee(
$_POST['first_name'] ?? '',
$_POST['last_name'] ?? '',
$_POST['max_hours'] ?? '0'
);
echo json_encode( array( 'success' => (bool) $ok ) );
break;
case 'update_employee':
if ( !$isPost ) { fail(); }
$ok = $oWorktime->updateEmployee(
$_POST['id'] ?? 0,
$_POST['first_name'] ?? '',
$_POST['last_name'] ?? '',
isset( $_POST['max_hours'] ) ? $_POST['max_hours'] : null
);
echo json_encode( array( 'success' => (bool) $ok ) );
break;
case 'set_active':
if ( !$isPost ) { fail(); }
$ok = $oWorktime->setEmployeeActive( $_POST['id'] ?? 0, ( ( $_POST['active'] ?? '1' ) == '1' ) );
echo json_encode( array( 'success' => (bool) $ok ) );
break;
default:
fail();
}
function fail() {
http_response_code( 400 );
echo json_encode( array( 'error' => 'bad_request' ) );
exit();
}