diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c1322dc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = false \ No newline at end of file diff --git a/.gitignore b/.gitignore index f712bc5..d41d005 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,12 @@ composer.phar /vendor/ +# ignore every .env file +.env + +# ignore .test folders +.test + # Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file # composer.lock diff --git a/app/controller/Controller.php b/app/controller/Controller.php new file mode 100644 index 0000000..2147aab --- /dev/null +++ b/app/controller/Controller.php @@ -0,0 +1,24 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +class Controller extends Nischcodes\ShiftCalc\Controller { + //TODO +} \ No newline at end of file diff --git a/app/model/Repository.php b/app/model/Repository.php new file mode 100644 index 0000000..3171fae --- /dev/null +++ b/app/model/Repository.php @@ -0,0 +1,24 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +class Repository extends Nischcodes\ShiftCalc\Controller { + //TODO +} \ No newline at end of file diff --git a/app/resources/layout/index.html b/app/resources/layout/index.html new file mode 100644 index 0000000..070b779 --- /dev/null +++ b/app/resources/layout/index.html @@ -0,0 +1,24 @@ + + + + + + Shift Calc + + + + + + \ No newline at end of file diff --git a/app/resources/template/index.html b/app/resources/template/index.html new file mode 100644 index 0000000..e69de29 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..26ab771 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "nischcodes/shiftcalc", + "description": "A small quick and dirty script to connect to a next cloud instance and read my registered working ours and display thema as a sheet and calculate the worked hours.", + "type": "project", + "license": "GPL-3.0", + "autoload": { + "psr-4": { + "Nischcodes\\Shiftcalc\\": "src/" + } + }, + "authors": [ + { + "name": "nisch.codes", + "email": "nischcodes@noreply.projects.nisch.codes" + } + ], + "require": {} +} diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..3c0d21b --- /dev/null +++ b/config/config.php @@ -0,0 +1,11 @@ +Shift Calc $date, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Holiday', 'desc' => $description]; + } + + // convert all entries to date/start/end-arrays + foreach($respData as $rpentry) { + $edate = ''; + $etime = ''; + $etype = 0; + $homeoffice = false; + + // extract date, time and type + foreach($rpentry['data'] as $i=>$rpdata) { + if($rpdata['columnId'] == 9) $edate = $rpdata['value']; + else if ($rpdata['columnId'] == 10) $etime = $rpdata['value']; + else if ($rpdata['columnId'] == 12) $etype = $rpdata['value']; + else if ($rpdata['columnId'] == 13) $homeoffice = $rpdata['value']; + } + + //print_r($edate); + //print_r($etime); + //print_r($etype); + //print_r($homeoffice); + + $found = false; + foreach($dates as $index=>$date) { + + //var_dump($date['date']); + //var_dump($edate); + + //var_dump($date['date'] == $edate); + + if($date['date'] == $edate) { + //var_dump($date); + + if($etype == 0) $dates[$index]['start'] = $etime; + else if($etype == 1) { + // add end time to entry + $dates[$index]['end'] = $etime; + + // calc shift in decimal + $dates[$index]['sdec'] = transferHoursToDecimal($dates[$index]['start']); + $dates[$index]['edec'] = transferHoursToDecimal($dates[$index]['end']); + $dates[$index]['worktime'] = $dates[$index]['edec'] - $dates[$index]['sdec']; + $dates[$index]['break'] = ($dates[$index]['worktime'] > $breakLimit ? $breakLarge : $breakSmall); + $dates[$index]['worktime_total'] = $dates[$index]['worktime'] - $dates[$index]['break']; + } + + $found = true; + break; + } + } + + if(!$found) { + if($etype == 0) $dates[] = ['date' => $edate, 'start' => $etime, 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 0, 'break' => 0.5, 'worktime_total' => 0, 'info' => ($homeoffice ? 'Homeoffice' : 'Office'), 'desc' => '']; + if($etype == 2) $dates[] = ['date' => $edate, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Vacation', 'desc' => '']; + if($etype == 3) $dates[] = ['date' => $edate, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Sickday', 'desc' => '']; + if($etype == 4) $dates[] = ['date' => $edate, 'start' => '', 'end' => '', 'sdec' => 0, 'edec' => 0, 'worktime' => 8, 'break' => 0, 'worktime_total' => 8, 'info' => 'Businesstrip', 'desc' => '']; + } + } + + // sort all dates + usort($dates, function($a, $b) { + return strcmp($a['date'], $b['date']); + }); + + // print_r($dates); + + $groupedDates = []; + + // group dates by week + foreach($dates as $entry) { + $date = new DateTime($entry['date']); + $year = $date->format('o'); + $week = $date->format('W'); + + $key = "$year-CW$week"; + + if(!isset($groupedDates[$key])) { + $groupedDates[$key] = []; + } + + $groupedDates[$key][] = $entry; + } + + //print_r($groupedDates); + + $homeofficedays = 0; + $sickdays = 0; + $vacations = 0; + $workdays = 0; + $currentWeek = date("W"); + $currentWeekIndex = "$currentYear-CW$currentWeek"; + + // build the current week + echo "

Current Week

"; + echo ""; + + echo ""; + echo ""; + + $currentWeekTotal = 0; + + foreach($groupedDates[$currentWeekIndex] as $id=>$data) { + $date = $data['date']; + $info = $data['info']; + $start = $data['start']; + $end = $data['end']; + $worktime = $data['worktime']; + $break = $data['break']; + $worktimeTotal = $data['worktime_total']; + $description = ($data['desc'] ? ' - '.$data['desc'] : ''); + + $class = strtolower($info); + + echo ""; + + $currentWeekTotal += $worktimeTotal; + } + + echo ""; + echo '
$currentWeekIndex
DateInfoStartEndWorktimeBreakWorktime Total
$date$info$description$start$end$worktime$break$worktimeTotal
Total: $currentWeekTotal
'; + + echo "

 

"; + echo "
"; + echo "

Full year table

"; + + // build the full year table + + echo ""; + + foreach($groupedDates as $index=>$group) { + $isCurrentWeek = (strcmp($index, $currentWeekIndex) === 0 ? true : false); + + if($isCurrentWeek) echo ""; + + echo ""; + echo ""; + + $weekTotal = 0; + + foreach($group as $id=>$data) { + $date = $data['date']; + $info = $data['info']; + $start = $data['start']; + $end = $data['end']; + $worktime = $data['worktime']; + $break = $data['break']; + $worktimeTotal = $data['worktime_total']; + $description = ($data['desc'] ? ' - '.$data['desc'] : ''); + + $class = strtolower($info); + + echo ""; + + $weekTotal += $worktimeTotal; + + if(strcmp($info, 'Sickday') === 0) $sickdays++; + if(strcmp($info, 'Homeoffice') === 0) $homeofficedays++; + if(strcmp($info, 'Vacation') === 0) $vacations++; + if(strcmp($info, 'Vacation') !== 0 && strcmp($info, 'Holiday') !== 0) $workdays++; + } + + + echo ""; + + if($isCurrentWeek) echo ""; + } + + echo ""; + + //$workdaystotal = count($dates); + echo ""; + + echo '
 
$index
DateInfoStartEndWorktimeBreakWorktime Total
$date$info$description$start$end$worktime$break$worktimeTotal
Total: $weekTotal
 
 
Sickdays: $sickdaysVacationdays: $vacations/30Homeoffice: $homeofficedays$workdays
'; +?> \ No newline at end of file diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 0000000..a05c59f --- /dev/null +++ b/src/Application.php @@ -0,0 +1,66 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +use Nischcodes\Shiftcalc\DotEnv; + +class Application { + + static function init() { + + // get the current root directory + $projectRoot = self::getProjectRoot(); + + // load the config file from the config folder + require_once $projectRoot . '/config/config.php'; + + // load the env file if the config file constant says so + if(SC_LOADDOTENV) { + // load .env file + DotEnv::load("$projectRoot/.env"); + } + } + + static function run() { + + //echo "run application"; + + } + + + static function getProjectRoot(?string $startDir = null): string { + $dir = realpath($startDir ?? __DIR__); + + while ($dir) { + if (file_exists($dir . '/composer.json')) { + return $dir; + } + + $parent = dirname($dir); + if ($parent === $dir) { + break; + } + + $dir = $parent; + } + + throw new RuntimeException('Project root folder not found.'); + } +} \ No newline at end of file diff --git a/src/Controller.php b/src/Controller.php new file mode 100644 index 0000000..19cef1f --- /dev/null +++ b/src/Controller.php @@ -0,0 +1,27 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +class Controller { + + function init($repository) { + + } +} \ No newline at end of file diff --git a/src/DotEnv.php b/src/DotEnv.php new file mode 100644 index 0000000..73b6654 --- /dev/null +++ b/src/DotEnv.php @@ -0,0 +1,64 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +class DotEnv { + + public static function load(string $path = '..\.env'): void { + + // check if given path contains a valid file + if (!file_exists($path)) { + throw new Exception("No valid file: $path"); + return; + } + + // get file content without new and empty lines + $lines = file($path, + FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES + ); + + // read line by line + foreach ($lines as $line) { + + // remove white characters + $line = trim($line); + + // skip comments + if (strpos($line, '#') === 0) { + continue; + } + + // split line into key and value + [$key, $value] = array_map( + 'trim', + explode('=', $line, 2) + ); + + // remove the single quotes from the value string + $value = trim($value, "\"'"); + + // only add to $_ENV if key doesn't already exist + if (!array_key_exists($key, $_ENV)) { + $_ENV[$key] = $value; + putenv("{\$key}={\$value}"); + } + } + } +} \ No newline at end of file diff --git a/src/HTTP.php b/src/HTTP.php new file mode 100644 index 0000000..b9ef10d --- /dev/null +++ b/src/HTTP.php @@ -0,0 +1,45 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +class HTTP { + + static function fetch(string $url, ?Array $headers = []): Mixed { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + if(!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $response = curl_exec($ch); + + curl_close($ch); + + if(curl_errno($ch)) { + throw new Exception('Curl-Error: ' . curl_error($ch)); + exit; + } + // returns the raw response of the curl request + return $response; + } + + static function fetchJSON(string $url, ?Array $headers = [], ?bool $associative = true): Mixed { + // converts the response of fetch into an JSON array/object + return json_decode(fetch($url, $headers), $associative); + } +} \ No newline at end of file diff --git a/src/Repository.php b/src/Repository.php new file mode 100644 index 0000000..de68062 --- /dev/null +++ b/src/Repository.php @@ -0,0 +1,27 @@ + + * @copyright 2021 nisch.codes + * @license https://projects.nisch.codes/nischcodes/shiftcalc/src/branch/main/LICENSE GPL-3 + * @version 1.0.0 + * @link https://projects.nisch.codes/nischcodes/shiftcalc + */ + +class Repository { + + function init() { + + } +} \ No newline at end of file