104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
<?php
|
|
|
|
//TODO: maybe convert this to use with TTCrudBaseModel
|
|
class WarehouseHistoryModel {
|
|
public int $id;
|
|
public string $table;
|
|
public int $row_id;
|
|
public string $key;
|
|
public ?string $old_value;
|
|
public ?string $new_value;
|
|
public ?string $note;
|
|
public ?string $data; // Added for version snapshots
|
|
public int $user_id;
|
|
public ?string $user_name;
|
|
public int $create;
|
|
|
|
public function __construct($data = []) {
|
|
if (empty($data)) return;
|
|
foreach ($data as $field => $value) {
|
|
if (property_exists(get_called_class(), $field)) {
|
|
$this->$field = $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function create($data) {
|
|
$FronkDB = FronkDB::singleton();
|
|
$db = $FronkDB->link;
|
|
|
|
// Define all possible columns to handle dynamic data insertion
|
|
$columns = ['table', 'row_id', 'key', 'old_value', 'new_value', 'note', 'data', 'user_id', 'create'];
|
|
$sqlColumns = [];
|
|
$sqlValues = [];
|
|
|
|
foreach ($columns as $column) {
|
|
if (isset($data[$column])) {
|
|
$sqlColumns[] = "`$column`";
|
|
$sqlValues[] = "'" . $db->real_escape_string($data[$column]) . "'";
|
|
}
|
|
}
|
|
|
|
if (empty($sqlColumns)) return false; // Nothing to insert
|
|
|
|
$sql = "INSERT INTO `WarehouseHistory` (" . implode(', ', $sqlColumns) . ") VALUES (" . implode(', ', $sqlValues) . ")";
|
|
$db->query($sql) or die($db->error);
|
|
|
|
return $db->insert_id;
|
|
}
|
|
|
|
/**
|
|
* Retrieves an array of WarehouseHistoryModel objects by row ID.
|
|
*
|
|
* @param int $id The row ID.
|
|
* @param string $table The table name.
|
|
* @return WarehouseHistoryModel[] Array of WarehouseHistoryModel objects.
|
|
*/
|
|
public static function getByRowId(int $id, string $table): array {
|
|
$db = FronkDB::singleton()->link;
|
|
$id = $db->real_escape_string($id);
|
|
$table = $db->real_escape_string($table);
|
|
$sql = "SELECT WH.*, W.name as user_name FROM `WarehouseHistory` WH
|
|
LEFT JOIN `Worker` W ON WH.user_id = W.id
|
|
WHERE WH.`row_id` = '$id' AND WH.`table` = '$table' ORDER BY WH.`create` DESC";
|
|
$result = $db->query($sql);
|
|
|
|
$rows = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$rows[] = new WarehouseHistoryModel($row);
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
/**
|
|
* Retrieves a single history entry for a specific version.
|
|
*
|
|
* @param int $rowId The ID of the row in the original table (e.g., offer ID).
|
|
* @param string $table The name of the original table (e.g., 'WarehouseOffer').
|
|
* @param int $version The version number to retrieve.
|
|
* @return ?self Returns a WarehouseHistoryModel object or null if not found.
|
|
*/
|
|
public static function getOneByVersion(int $rowId, string $table, int $version): ?self
|
|
{
|
|
$db = FronkDB::singleton()->link;
|
|
$rowId = $db->real_escape_string($rowId);
|
|
$table = $db->real_escape_string($table);
|
|
$version = $db->real_escape_string($version);
|
|
|
|
$sql = "SELECT * FROM `WarehouseHistory`
|
|
WHERE `row_id` = '$rowId'
|
|
AND `table` = '$table'
|
|
AND `key` = 'version'
|
|
AND `new_value` = '$version'
|
|
ORDER BY `create` DESC
|
|
LIMIT 1";
|
|
|
|
$result = $db->query($sql);
|
|
if ($result && $result->num_rows > 0) {
|
|
return new self($result->fetch_assoc());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|