db = $this->getDbConnection(); $this->table = "object"; $this->cols = array( "guid", "perms", "owner", "parent", "name", "timeCreated", "timeUpdated", "type" ); $this->childTable = $this->db->esc($childTable); $this->childCols = array(); if (is_array($childCols)) { foreach ($childCols as $col) $this->childCols[] = $this->db->esc($col); } } /* * Populate this object with data from the DB with a given GUID */ function loadObj($guid = null) { if (is_null($guid)) return; if (!$this->isGUID($guid)) return; $escdGuid = $this->db->esc($guid); /* Base fields */ $query = "SELECT * FROM `" . $this->table . "` WHERE `guid` = '" . $escdGuid . "'"; $result = $this->db->query($query)[0]; foreach ($this->cols as $col) { if (isset($result[$col])) $this->$col = $result[$col]; } /* Child Table fields */ $query = "SELECT * FROM `" . $this->childTable . "` WHERE `guid` = '" . $escdGuid . "'"; $result = $this->db->query($query)[0]; foreach ($this->childCols as $col) { if (isset($result[$col])) $this->$col = $result[$col]; } } /* * Write this object to the database */ function saveObj() { if (isset($this->guid)) { /* Update Base */ $updateStr = ""; foreach ($this->cols as $col) { if (!isset($this->$col)) continue; $updateStr .= "`" . $col . "` = '" . $this->db->esc($this->$col) . "', "; } if (strlen($updateStr) > 0) { $updateStr = substr($updateStr, 0, -2); // remove ", " from the end $query = "UPDATE `" . $this->table . "` SET " . $updateStr . " WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; $this->db->query($query); } /* Update Child */ $updateStr = ""; foreach ($this->childCols as $col) { if (!isset($this->$col)) continue; $updateStr .= "`" . $col . "` = '" . $this->db->esc($this->$col) . "', "; } if (strlen($updateStr) > 0) { $updateStr = substr($updateStr, 0, -2); // remove ", " from the end $query = "UPDATE `" . $this->childTable . "` SET " . $updateStr . " WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; $this->db->query($query); } } else { $this->guid = $this->getNewGUID(); /* Insert Base */ $colsStr = ""; $valsStr = ""; foreach ($this->cols as $col) { if (!isset($this->$col)) continue; $colsStr .= "`" . $col . "`, "; $valsStr .= "'" . $this->db->esc($this->$col) . "', "; } if (strlen($colsStr) > 0) { $colsStr = substr($colsStr, 0, -2); // remove ", " $valsStr = substr($valsStr, 0, -2); $query = "INSERT INTO `" . $this->table . "` (" . $colsStr . ") VALUES (" . $valsStr . ")"; $this->db->query($query); } /* Insert Child */ $colsStr = ""; $valsStr = ""; foreach ($this->childCols as $col) { if (!isset($this->$col)) continue; $colsStr .= "`" . $col . "`, "; $valsStr .= "'" . $this->db->esc($this->$col) . "', "; } if (strlen($colsStr) > 0) { $colsStr = substr($colsStr, 0, -2); // remove ", " $valsStr = substr($valsStr, 0, -2); $query = "INSERT INTO `" . $this->childTable . "` (" . $colsStr . ") VALUES (" . $valsStr . ")"; $this->db->query($query); } } } /* * Remove this object from the database */ function delObj() { if (!isset($this->guid)) return; /* Delete Base */ $query = "DELETE FROM `" . $this->table . "` WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; $this->db->query($query); /* Delete Child */ $query = "DELETE FROM `" . $this->childTable . "` WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; $this->db->query($query); } /* * Check whether given GUID exists */ function isGUID($guid) { $query = "SELECT `guid` FROM `object` WHERE `guid` = '" . $this->db->esc($guid) . "'"; $result = $this->db->query($query); if (count($result) > 0) return true; return false; } /* * Get a new, unique GUID for a new system object */ function getNewGUID() { do { $sha = hash("sha256", rand()); $guid = substr($sha, 0, 8); } while ($this->isGUID($guid)); return $guid; } } /* * Concrete Database Object which can be used in a polymorphic way */ class DBObject extends Object { /* * Constructor */ function __construct($guid = null) { parent::__construct(); $this->loadObj($guid); } } ?>