sqlite = $helper->getDB(); if(!$this->sqlite) return; $table = self::cleanTableName($table); $this->table = $table; $this->ts = $ts; // load info about the schema itself if($ts) { $sql = "SELECT * FROM schemas WHERE tbl = ? AND ts <= ? ORDER BY ts DESC LIMIT 1"; $opt = array($table, $ts); } else { $sql = "SELECT * FROM schemas WHERE tbl = ? ORDER BY ts DESC LIMIT 1"; $opt = array($table); } $res = $this->sqlite->query($sql, $opt); if($this->sqlite->res2count($res)) { $schema = $this->sqlite->res2arr($res); $result = array_shift($schema); $this->id = $result['id']; $this->chksum = $result['chksum']; } $this->sqlite->res_close($res); if(!$this->id) return; // load existing columns $sql = "SELECT SC.*, T.* FROM schema_cols SC, types T WHERE SC.sid = ? AND SC.tid = T.id ORDER BY SC.sort"; $res = $this->sqlite->query($sql, $this->id); $rows = $this->sqlite->res2arr($res); $this->sqlite->res_close($res); foreach($rows as $row) { $class = 'plugin\\struct\\types\\' . $row['class']; if(!class_exists($class)) { // This usually never happens, except during development msg('Unknown type "'.hsc($row['class']).'" falling back to Text', -1); $class = 'plugin\\struct\\types\\Text'; } $config = json_decode($row['config'], true); /** @var AbstractBaseType $type */ $type = new $class($config, $row['label'], $row['ismulti'], $row['tid']); $column = new Column( $row['sort'], $type, $row['colref'], $row['enabled'], $table ); $type->setContext($column); $this->columns[$row['colref']] = $column; if($row['sort'] > $this->maxsort) $this->maxsort = $row['sort']; } } /** * Cleans any unwanted stuff from table names * * @param string $table * @return string */ static public function cleanTableName($table) { $table = strtolower($table); $table = preg_replace('/[^a-z0-9_]+/', '', $table); $table = preg_replace('/^[0-9_]+/', '', $table); $table = trim($table); return $table; } /** * Gets a list of all available schemas * * @return string[] */ static public function getAll() { /** @var \helper_plugin_struct_db $helper */ $helper = plugin_load('helper', 'struct_db'); $db = $helper->getDB(); if(!$db) return array(); $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl"); $tables = $db->res2arr($res); $db->res_close($res); $result = array(); foreach($tables as $row) { $result[] = $row['tbl']; } return $result; } /** * @return string */ public function getChksum() { return $this->chksum; } /** * @return int */ public function getId() { return $this->id; } /** * Returns a list of columns in this schema * * @param bool $withDisabled if false, disabled columns will not be returned * @return Column[] */ public function getColumns($withDisabled = true) { if(!$withDisabled) { return array_filter( $this->columns, function (Column $col) { return $col->isEnabled(); } ); } return $this->columns; } /** * Find a column in the schema by its label * * Only enabled columns are returned! * * @param $name * @return bool|Column */ public function findColumn($name) { foreach($this->columns as $col) { if($col->isEnabled() && utf8_strtolower($col->getLabel()) == utf8_strtolower($name)) { return $col; } } return false; } /** * @return string */ public function getTable() { return $this->table; } /** * @return int the highest sort number used in this schema */ public function getMaxsort() { return $this->maxsort; } }