Files
thetool/application/IvtCrediting/IvtCreditingModel.php
2024-06-20 17:59:51 +02:00

118 lines
3.5 KiB
PHP

<?php
class IvtCreditingModel {
public static function create(array $data) {
throw new Exception("Please use IvtCreditingController to create IvtCreditings");
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$res = $db->select("crediting", "*", "1=1 ORDER BY id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtCrediting($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$res = $db->select("crediting", "*", "$where ORDER BY id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtCrediting($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function getLast($filter = []) {
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$res = $db->select("crediting", "*", "$where ORDER BY id DESC LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new IvtCrediting($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function search($filter, $limit = false) {
$items = [];
$db = FronkDB::singleton(IVT_DBHOST, IVT_DBUSER, IVT_DBPASS, IVT_DBNAME);
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM crediting WHERE $where ORDER by id";
mfLoghandler::singleton()->debug($sql);
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT " . $limit['start'] . ", " . $limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT " . $limit['count'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new IvtCrediting($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("cid", $filter)) {
$cid = $filter['cid'];
if(is_numeric($cid)) {
$where .= " AND cid=$cid";
}
}
if(array_key_exists("date_outgoing", $filter)) {
$date_outgoing = $filter['date_outgoing'];
if($date_outgoing) {
$where .= " AND date_outgoing='$date_outgoing'";
}
}
if(array_key_exists("date_outgoing>", $filter)) {
$date_outgoing = $filter['date_outgoing>'];
if($date_outgoing) {
$where .= " AND date_outgoing >= '$date_outgoing'";
}
}
if(array_key_exists("date_outgoing<", $filter)) {
$date_outgoing = $filter['date_outgoing<'];
if($date_outgoing) {
$where .= " AND date_outgoing <= '$date_outgoing'";
}
}
return $where;
}
}