202 lines
6.4 KiB
PHP
202 lines
6.4 KiB
PHP
<?php
|
|
|
|
class TimerecordingEmployeeWorkingHourModel
|
|
{
|
|
private $user_id;
|
|
private $day;
|
|
private $start;
|
|
private $end;
|
|
public static $days_definition = array(1 => "Montag", 2 => "Dienstag", 3 => "Mittwoch", 4 => "Donnerstag", 5 => "Freitag", 6 => "Samstag", 0 => "Sonntag");
|
|
public static $days_short = array(0 => "So", 1 => "Mo", 2 => "Di", 3 => "Mi", 4 => "Do", 5 => "Fr", 6 => "Sa");
|
|
|
|
public static function cleardays($days)
|
|
{
|
|
$days_short = TimerecordingEmployeeWorkingHourModel::$days_short;
|
|
foreach ($days_short as $day_short) {
|
|
$days = str_replace($day_short . " - " . $day_short, $day_short, $days);
|
|
}
|
|
return $days;
|
|
}
|
|
|
|
public static function find($data)
|
|
{
|
|
|
|
}
|
|
|
|
public static function create(array $data)
|
|
{
|
|
$model = new TimerecordingEmployeeWorkingHour();
|
|
|
|
foreach ($data as $field => $value) {
|
|
if (property_exists(get_called_class(), $field)) {
|
|
if (substr($field, 0, 5) == "vlan_" && !$value) {
|
|
$model->$field = null;
|
|
continue;
|
|
}
|
|
$model->$field = $value;
|
|
}
|
|
}
|
|
|
|
$me = mfValuecache::singleton()->get("me");
|
|
if (!$me) {
|
|
$me = new User();
|
|
$me->loadMe();
|
|
mfValuecache::singleton()->set("me", $me);
|
|
}
|
|
|
|
if ($model->create_by === null) {
|
|
$model->create_by = $me->id;
|
|
}
|
|
if ($model->edit_by === null) {
|
|
$model->edit_by = $me->id;
|
|
}
|
|
|
|
return $model;
|
|
}
|
|
|
|
public static function getOne($id)
|
|
{
|
|
if (!is_numeric($id) || !$id) {
|
|
throw new Exception("Invalid number", 400);
|
|
}
|
|
$item = [];
|
|
$db = FronkDB::singleton();
|
|
|
|
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "id=$id LIMIT 1");
|
|
if ($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new TimerecordingEmployeeWorkingHour($data);
|
|
}
|
|
return $item;
|
|
}
|
|
|
|
public static function getAll()
|
|
{
|
|
$items = [];
|
|
|
|
$db = FronkDB::singleton();
|
|
|
|
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "1=1");
|
|
if ($db->num_rows($res)) {
|
|
while ($data = $db->fetch_object($res)) {
|
|
$items[] = new TimerecordingEmployeeWorkingHour($data);
|
|
}
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function getAllArray()
|
|
{
|
|
$items = [];
|
|
$daysshort = TimerecordingEmployeeWorkingHourModel::$days_short;
|
|
$db = FronkDB::singleton();
|
|
|
|
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "1=1 ORDER by `user_id`,`day`");
|
|
if ($db->num_rows($res)) {
|
|
$olduser = 0;
|
|
$counter = 0;
|
|
$oldend = "";
|
|
$oldstart = "";
|
|
$oldday = "";
|
|
$datetimetext = "";
|
|
$secondcounter = 0;
|
|
|
|
while ($data = $db->fetch_array($res)) {
|
|
if ($olduser != $data['user_id']) {
|
|
if ($counter > 0) {
|
|
$secondcounter = 0;
|
|
$datetimetext .= " - " . $daysshort[$oldday] . " " . $oldstart . " - " . $oldend;
|
|
$datetimetext = TimerecordingEmployeeWorkingHourModel::cleardays($datetimetext);
|
|
$items[$olduser]['datetimetext'] = $datetimetext;
|
|
}
|
|
$datetimetext = "";
|
|
unset($oldstart);
|
|
unset($oldend);
|
|
unset($oldday);
|
|
$items[$data['user_id']] = $data;
|
|
}
|
|
$secondcounter = $secondcounter + strtotime(date("Y-m-d " . $data['end'] . ":00")) - strtotime(date("Y-m-d " . $data['start'] . ":00"));
|
|
|
|
if (!$datetimetext) {
|
|
$datetimetext = $daysshort[$data['day']];
|
|
}
|
|
echo $oldstart . $data['start'];
|
|
if (($oldstart != $data['start'] || $oldend != $data['end']) && $oldend) {
|
|
$datetimetext .= " - " . $daysshort[$oldday] . " " . $oldstart . " - " . $oldend;
|
|
$datetimetext .= "<br> " . $daysshort[$data['day']];
|
|
}
|
|
|
|
$items[$data['user_id']]['datetimetext'] = $datetimetext;
|
|
$items[$data['user_id']]['secondcounter'] = $secondcounter;
|
|
$olduser = $data['user_id'];
|
|
$oldstart = $data['start'];
|
|
$oldend = $data['end'];
|
|
$oldday = $data['day'];
|
|
$counter++;
|
|
}
|
|
$datetimetext .= " - " . $daysshort[$oldday] . " " . $oldstart . " - " . $oldend;
|
|
$datetimetext = TimerecordingEmployeeWorkingHourModel::cleardays($datetimetext);
|
|
$items[$olduser]['datetimetext'] = $datetimetext;
|
|
}
|
|
return $items;
|
|
|
|
}
|
|
|
|
public static function deleteUserHours($userid)
|
|
{
|
|
$db = FronkDB::singleton();
|
|
$sql = "DELETE FROM `TimerecordingEmployeeWorkingHour` WHERE `user_id`='" . $userid . "'";
|
|
$res = $db->query($sql);
|
|
}
|
|
|
|
public static function getFirst()
|
|
{
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "$where ");
|
|
if ($db->num_rows($res)) {
|
|
$data = $db->fetch_object($res);
|
|
$item = new TimerecordingEmployeeWorkingHour($data);
|
|
if ($item->id) {
|
|
return $item;
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function search($filter)
|
|
{
|
|
$items = [];
|
|
$db = FronkDB::singleton();
|
|
|
|
$where = self::getSqlFilter($filter);
|
|
$res = $db->select("TimerecordingEmployeeWorkingHour", "*", "$where ORDER by `day`,`start`");
|
|
if ($db->num_rows($res)) {
|
|
while ($data = $db->fetch_object($res)) {
|
|
$items[] = new TimerecordingEmployeeWorkingHour($data);
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function getSqlFilter($filter)
|
|
{
|
|
$where = "1=1 ";
|
|
|
|
if (array_key_exists("user_id", $filter)) {
|
|
$userid = $filter['user_id'];
|
|
if (is_numeric($userid)) {
|
|
$where .= " AND user_id=$userid";
|
|
}
|
|
}
|
|
|
|
//var_dump($filter, $where);exit;
|
|
return $where;
|
|
}
|
|
|
|
}
|