Files
thetool/application/Timerecording/TimerecordingModel.php
2024-02-04 11:49:57 +01:00

169 lines
4.6 KiB
PHP

<?php
class TimerecordingModel
{
private $user_id;
private $start;
private $end;
private $timerecordingCategory_id;
private $comment;
private $approved;
private $completed;
public static function find($data)
{
}
public static function create(array $data)
{
$model = new Timerecording();
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("Timerecording", "*", "id=$id LIMIT 1");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Timerecording($data);
}
return $item;
}
public static function getAll()
{
$items = [];
$db = FronkDB::singleton();
$res = $db->select("Timerecording", "*", "1=1");
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Timerecording($data);
}
}
return $items;
}
public static function getAllPermits()
{
$items = [];
$db = FronkDB::singleton();
$sql = "SELECT Timerecording.* FROM `Timerecording`
INNER JOIN `TimerecordingCategory` ON (`Timerecording`.`timerecordingCategory_id` = `TimerecordingCategory`.`id`)
WHERE `TimerecordingCategory`.`approval`='1'";
$res = $db->query($sql);
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Timerecording($data);
}
}
return $items;
}
public static function getFirst()
{
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("Timerecording", "*", "$where ");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new Timerecording($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("Timerecording", "*", "$where");
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new Timerecording($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";
}
}
if (array_key_exists("start", $filter) && array_key_exists("end", $filter)) {
$start = $filter['start'];
$end = $filter['end'];
if (is_numeric($start) && is_numeric($end)) {
$where .= " AND `start` > $start AND `start` < $end";
}
}
if (array_key_exists("starttime", $filter) && array_key_exists("endtime", $filter)) {
$starttime = $filter['starttime'];
$endtime = $filter['endtime'];
$id = $filter['id'];
if (is_numeric($starttime) && is_numeric($endtime)) {
$where .= " AND ((`start` <= $starttime AND `end` >= $starttime ) || (`start` >= $endtime AND `end` <= $endtime) || (`start` >= $starttime AND `end` >= $starttime AND `start` <= $endtime AND `end` <= $endtime))";
if ($id && is_numeric($id)) {
$where .= " AND `id` != $id";
}
}
}
//
// var_dump($filter, $where);exit;
return $where;
}
}