Files
thetool/application/FilestoreHistory/FilestoreHistoryModel.php
Spitzer_Daniel 1d394a7852 Filestore Fertiggestellt
Cpeprovisioning Performance Update
2023-04-19 17:27:48 +02:00

175 lines
4.7 KiB
PHP

<?php
class FilestoreHistoryModel
{
public $filestore_id;
public $file_id;
public $name;
public $description;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(array $data)
{
$model = new FilestoreHistory();
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
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("FilestoreHistory", "*", "id=$id LIMIT 1");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new FilestoreHistory($data);
}
return $item;
}
public static function getAll()
{
$items = [];
$db = FronkDB::singleton();
$sql = "SELECT FilestoreHistory.* FROM FilestoreHistory
";
$res = $db->query($sql);
if ($db->num_rows($res)) {
while ($data = $db->fetch_array($res)) {
$items[$data['filestore_id']] = 1;
}
}
return $items;
}
public static function getAllhistory($id)
{
$items = [];
$db = FronkDB::singleton();
$sql = "SELECT FilestoreHistory.*,Worker.name workername,Worker.id workerid FROM FilestoreHistory
LEFT JOIN File ON (FilestoreHistory.file_id = File.id)
INNER JOIN Worker ON (FilestoreHistory.edit_by=Worker.id)
WHERE `filestore_id` ='" . $id . "'
ORDER BY edit DESC";
$res = $db->query($sql);
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new FilestoreHistory($data);
}
}
return $items;
}
public static function getFirst()
{
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("FilestoreHistory", "*", "$where ORDER BY name, filename");
if ($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new FilestoreHistory($data);
if ($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter)
{
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT FilestoreHistory.* FROM FilestoreHistory
LEFT JOIN File ON (FilestoreHistory.file_id = File.id)
WHERE $where
ORDER BY name";
$res = $db->query($sql);
if ($db->num_rows($res)) {
while ($data = $db->fetch_object($res)) {
$items[] = new FilestoreHistory($data);
}
}
return $items;
}
private static function getSqlFilter($filter)
{
$where = "1=1 ";
if (array_key_exists("file_id", $filter)) {
$file_id = $filter['file_id'];
if (is_numeric($file_id)) {
$where .= " AND file_id=$file_id";
}
}
if (array_key_exists("filestore_id", $filter)) {
$filestore_id = $filter['filestore_id'];
if (is_numeric($filestore_id)) {
$where .= " AND filestore_id=$filestore_id";
}
}
//var_dump($filter);exit;
if (array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter['name']);
if ($name) {
$where .= " AND name='$name'";
}
}
if (array_key_exists("filename", $filter)) {
$filename = FronkDB::singleton()->escape($filter['filename']);
if ($filename) {
$where .= " AND File.filename='$filename'";
}
}
if (array_key_exists("subfolder", $filter)) {
$subfolder = FronkDB::singleton()->escape($filter['subfolder']);
if ($subfolder) {
$where .= " AND File.subfolder='$subfolder'";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}