| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/backend-accounting.sk/app/app/classes/ |
Upload File : |
<?php
namespace app\classes;
class PDOElement {
protected $element = array();
protected $id;
protected $table;
protected $idColumnName;
/** @var PDODatabase */
protected $db;
protected $uniqueColumn = NULL;
protected $metadata = array();
protected $hasMetadata = false;
protected $keepLogsForDays = false;
private $dbUpdatesLoggingEnabled = true;
protected $archiveMode = true;
protected $selfUpdate = false;
/**
* @param string $id <p>unique value of row in database table</p>
* @param string $table <p>table name in database</p>
* @param array $bindArray <p>array of bind parameters in condition string</p>
* @param string $idColumnName <p>ID column name</p>
* @param string $condition <p>select condition (SELECT * FROM $table WHERE $idColumnName = $idColumnName AND $condition)</p>
* @param string $condition <p>custom selection (full select string)</p>
* @param bool $allow_deleted <p>if param is set to true and selected record has state DELETED exception will be thrown </p>
*/
public function __construct($id, $table, array $bindArray = array(), $idColumnName = "id", $condition = false, $customSelection = false, $allow_deleted = false) {
$this->id = $id;
$this->table = $table;
$this->idColumnName = $idColumnName;
$this->db = PDODatabase::getInstance();
if (is_array($id)) {
$this->element = $id;
$this->id = $this->element["id"];
} else {
$this->element = $this->db->getRow($table, $id, $idColumnName, $condition, $bindArray, $customSelection);
if (!is_array($this->element) || (isset($this->element["state"]) && $this->element["state"] === \app\constants\State::DELETED && !$allow_deleted)) {
throw new \Exception(\app\constants\HttpReponseCodes::getMessageForCode(\app\constants\ResponseCodes::ITEM_NOT_FOUND) . " " . $this->table, \app\constants\ResponseCodes::ITEM_NOT_FOUND);
}
}
if (key_exists("metadata", $this->element)) {
$rawMetadata = $this->element["metadata"];
if ($rawMetadata === null || $rawMetadata === '') {
$this->metadata = array();
} else {
$this->metadata = unserialize($rawMetadata);
if ($this->metadata === false) {
$this->metadata = array();
$this->update(array("metadata" => serialize(array())));
$this->_set("last_update", $this->db->getTime());
}
}
$this->hasMetadata = true;
} else {
$this->metadata = array();
}
}
/**
* Delete element from database. Not recommended to use this function!
* @return void <p>No value is returned.</p>
*/
public function dbDelete() {
return $this->db->deleteRecord($this->table, $this->id, $this->idColumnName);
}
/**
* Updates element in database.
* @param array $dataArray <p>array of variables to update (columnName => value)</p>
* @param string $exclude <p>array of variables to exclude in update given in $dataArray (columnName1, columnName2, ...)</p>
* @return void <p>No value is returned.</p>
*/
public function update(array $dataArray, array $exclude = array()) {
if (key_exists("last_update", $this->element)) {
if (isset($dataArray["last_update"])) {
if (strtotime($dataArray["last_update"]) !== strtotime($this->element["last_update"])) {
throw new \Exception("Update failed. The item had been updated before. Please reload the page for the up to date version.");
}
unset($dataArray["last_update"]);
}
}
foreach ($dataArray as $key => $val) {
if (!key_exists($key, $this->element)) {
unset($dataArray[$key]);
}
}
$this->db->updateRecord($this->table, $dataArray, $this->id, $this->idColumnName, $exclude);
if ($this->dbUpdatesLoggingEnabled) {
$this->logUpdate($dataArray, $exclude);
}
if ($this->selfUpdate) {
foreach ($dataArray as $key => $val) {
if (!in_array($key, $exclude)) {
$this->element[$key] = $val;
}
}
}
}
/**
* Delete element (sets state to DELETED). If there is unique column, set name of column to variable $uniqueColumn in constructor and timestamp will be add after deletion to prevent uniqeu collisions for new elements.
* @return void <p>No value is returned.</p>
*/
public function delete() {
if ($this->archiveMode) {
$con = $this->db->getConnection();
$con->beginTransaction();
try {
$this->dbDelete();
$con->rollBack();
} catch (\Exception $ex) {
$this->archive();
$con->commit();
return;
}
}
$this->db->sqlCommand("UPDATE " . $this->table . " SET state = '" . \app\constants\State::DELETED . "' WHERE $this->idColumnName = :$this->idColumnName", array(":$this->idColumnName" => $this->id));
if (is_array($this->uniqueColumn)) {
foreach ($this->uniqueColumn as $key) {
if (key_exists($key, $this->element)) {
$this->db->sqlCommand("UPDATE " . $this->table . " SET " . $key . " = :deleted_unique_column WHERE $this->idColumnName = :$this->idColumnName", array(":$this->idColumnName" => $this->id, ":deleted_unique_column" => $this->element[$key] . "_" . $this->db->getTime(true)));
}
}
} else {
if ($this->uniqueColumn !== NULL && key_exists($this->uniqueColumn, $this->element)) {
$this->db->sqlCommand("UPDATE " . $this->table . " SET " . $this->uniqueColumn . " = :deleted_unique_column WHERE $this->idColumnName = :$this->idColumnName", array(":$this->idColumnName" => $this->id, ":deleted_unique_column" => $this->element[$this->uniqueColumn] . "_" . $this->db->getTime(true)));
}
}
if (class_exists("\modules\dbUpdatesLogger\code\Logger")) {
$logger = new \modules\dbUpdatesLogger\code\Logger();
$logger->log($this->table, $this->toArray(), array("state" => \app\constants\State::DELETED), $this->idColumnName, $this->keepLogsForDays);
}
}
public function archive() {
$this->update(array("state" => \app\constants\State::ARCHIVED));
if (is_array($this->uniqueColumn)) {
foreach ($this->uniqueColumn as $key) {
if (key_exists($key, $this->element)) {
$this->db->sqlCommand("UPDATE " . $this->table . " SET " . $key . " = :deleted_unique_column WHERE $this->idColumnName = :$this->idColumnName", array(":$this->idColumnName" => $this->id, ":deleted_unique_column" => $this->element[$key] . "_" . $this->db->getTime(true)));
}
}
} else {
if ($this->uniqueColumn !== NULL && key_exists($this->uniqueColumn, $this->element)) {
$this->db->sqlCommand("UPDATE " . $this->table . " SET " . $this->uniqueColumn . " = :deleted_unique_column WHERE $this->idColumnName = :$this->idColumnName", array(":$this->idColumnName" => $this->id, ":deleted_unique_column" => $this->element[$this->uniqueColumn] . "_" . $this->db->getTime(true)));
}
}
}
/**
* Get value of given column name.
* @param string $columnName <p>column name</p>
* @return mixed <p>Value if column name is set, otherwise NULL.</p>
*/
public function _get($columnName) {
if (key_exists($columnName, $this->element)) {
return $this->element[$columnName];
}
// printError("<b>" . $columnName . "</b> is undefined.");
return NULL;
}
/**
* @return array
*/
public function _getSet($columnName): array {
return explode(\app\constants\Config::DB_ITEMS_SEPARATOR, $this->_get($columnName));
}
/**
* Set value of given column name (this change will be not affected in database).
* @param string $columnName <p>column name</p>
* @return bool <p>Returns true.</p>
*/
public function _set($columnName, $value) {
$this->element[$columnName] = $value;
return true;
}
public function _print($column_name) {
echo $this->_get($column_name);
}
/**
* Get columns values of element in array.
* @param array $exclude <p>columns names to be excluded</p>
* @return array <p>Returns columns values of element in array.</p>
*/
public function toArray($exclude = array()) {
array_push($exclude, "metadata");
$return = $this->element;
foreach ($exclude as $item) {
unset($return[$item]);
}
return $return;
}
/**
* Get database table name of this element.
* @return string <p>table name</p>
*/
public function getDbTable() {
return $this->table;
}
/**
* Get id value.
* @return string <p>id value</p>
*/
public function getId() {
return $this->_get($this->idColumnName);
}
/**
* Get metadata value by given key.
* @param string $key <p>metadata key</p>
* @return mixed <p>Returns metadata value by given key (string, array or bool). If key is FALSE, returns all metadata array. If key doesn't exist, returns FALSE.</p>
*/
public function getMetadata($key = false) {
if ($this->metadata === null) {
return false;
}
if ($key === false) {
return $this->metadata;
}
if (!key_exists($key, $this->metadata)) {
return false;
}
return $this->metadata[$key];
}
/**
* Set metadata value by given key. This change will be stored automatically in database.
* @param string $key <p>metadata key</p>
* @param mixed $val <p>metadata value to be set (string or array)</p>
* @return bool <p>Returns TRUE on success and FALSE on failure (if metadata variable doesn't exist).</p>
*/
public function setMetadata($key, $val, $temporary = false) {
if (!key_exists($key, $this->metadata) || !$this->hasMetadata) {
return false;
}
$this->metadata[$key] = $val;
if (!$temporary) {
$this->update(array("metadata" => serialize($this->metadata)));
}
return true;
}
/**
* Add new metadata variable.
* @param string $key <p>metadata key</p>
* @param mixed $val <p>metadata value to be set (string or array)</p>
* @return bool <p>Returns TRUE on success and FALSE on failure (if metadata variable already exists).</p>
*/
public function addMetadataVariable($key, $val = "") {
if (key_exists($key, $this->metadata) || !$this->hasMetadata) {
return false;
}
$this->metadata[$key] = $val;
$this->update(array("metadata" => serialize($this->metadata)));
return true;
}
/**
* Remove metadata variable.
* @param string $key <p>metadata key</p>
* @param mixed $val <p>if $val is set, variable will be deleted only if current value is equal to given paramater</p>
* @return bool <p>Returns TRUE on success and FALSE on failure (if metadata variable doesn't exist).</p>
*/
public function removeMetadataVariable($key, $val = "") {
if (!key_exists($key, $this->metadata) || !$this->hasMetadata) {
return false;
}
if ($val === "" || ($val === $this->metadata[$key])) {
unset($this->metadata[$key]);
$this->update(array("metadata" => serialize($this->metadata)));
}
return true;
}
public static function newElement($data, $requiredColumns, $columns = array()) {
$columnsToVerify = array_merge($columns, $requiredColumns);
foreach ($data as $key => $val) {
if (array_search($key, $columnsToVerify) === false) {
unset($data[$key]);
}
}
return $data;
}
protected function logUpdate($updateArray, $exclude) {
foreach ($exclude as $val) {
unset($updateArray[$val]);
}
if (class_exists("\modules\dbUpdatesLogger\code\Logger")) {
$logger = new \modules\dbUpdatesLogger\code\Logger();
$logger->log($this->table, $this->toArray(), $updateArray, $this->idColumnName, $this->keepLogsForDays);
}
}
/**
* FALSE for forever.
*/
public function setKeepLogsForDays($days) {
$this->keepLogsForDays = $days;
}
protected function findRelations(array $tableColumnPairs) {
foreach ($tableColumnPairs as $table => $column) {
$match = $this->db->getValue("SELECT " . $column . " FROM " . $table . " WHERE " . $column . "=:" . $column, array($column => $this->_get($this->idColumnName)));
if ($match !== null) {
return true;
}
}
return false;
}
public function disableDbUpdatesLogging() {
$this->dbUpdatesLoggingEnabled = false;
}
public function enableDbUpdatesLogging() {
$this->dbUpdatesLoggingEnabled = true;
}
public function getTable() {
return $this->table;
}
public function getIdColumnName() {
return $this->idColumnName;
}
public function enableSelfUpdate() {
$this->selfUpdate = true;
}
}