From cc2cb11cf386e9a12cd555a2fa6d24b3f7c10e27 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 20 Jun 2017 21:33:43 +0200 Subject: [PATCH] ObjectsTable: new generic table --- library/Director/Web/Table/ObjectsTable.php | 169 ++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 library/Director/Web/Table/ObjectsTable.php diff --git a/library/Director/Web/Table/ObjectsTable.php b/library/Director/Web/Table/ObjectsTable.php new file mode 100644 index 00000000..303a036f --- /dev/null +++ b/library/Director/Web/Table/ObjectsTable.php @@ -0,0 +1,169 @@ + 'o.object_name', + 'id' => 'o.id', + ]; + + protected $searchColumns = ['o.object_name']; + + protected $showColumns = ['object_name' => 'Name']; + + protected $type; + + private $auth; + + /** + * @param $type + * @param Db $db + * @return static + */ + public static function create($type, Db $db) + { + $class = __NAMESPACE__ . '\\ObjectsTable' . ucfirst($type); + if (! class_exists($class)) { + $class = __CLASS__; + } + + /** @var static $table */ + $table = new $class($db); + $table->type = $type; + return $table; + } + + public function getType() + { + return $this->type; + } + + public function getAuth() + { + return $this->auth; + } + + public function setAuth(Auth $auth) + { + $this->auth = $auth; + return $this; + } + + public function addObjectRestriction(ObjectRestriction $restriction) + { + $this->objectRestrictions[$restriction->getName()] = $restriction; + return $this; + } + + public function getColumns() + { + return $this->columns; + } + + public function getColumnsToBeRendered() + { + return $this->showColumns; + } + + protected function getMainLinkLabel($row) + { + return $row->object_name; + } + + protected function renderObjectNameColumn($row) + { + $type = $this->getType(); + $url = Url::fromPath("director/${type}", [ + 'name' => $row->object_name + ]); + + return static::td(Link::create($this->getMainLinkLabel($row), $url)); + } + + protected function renderExtraColumns($row) + { + $columns = $this->getColumnsToBeRendered(); + unset($columns['object_name']); + $cols = []; + foreach ($columns as $key => & $label) { + $cols[] = static::td($row->$key); + } + + return $cols; + } + + public function renderRow($row) + { + $tr = static::tr([ + $this->renderObjectNameColumn($row), + $this->renderExtraColumns($row) + ]); + + $classes = $this->getRowClasses($row); + if (! empty($classes)) { + $tr->attributes()->add('class', $classes); + } + + return $tr; + } + + protected function applyObjectTypeFilter(ZfSelect $query) + { + return $query->where("o.object_type = 'object'"); + } + + protected function applyRestrictions(ZfSelect $query) + { + foreach ($this->getRestrictions() as $restriction) { + $restriction->applyToQuery($query); + } + + return $query; + } + + protected function getRestrictions() + { + if ($this->objectRestrictions === null) { + $this->objectRestrictions = $this->loadRestrictions(); + } + + return $this->objectRestrictions; + } + + protected function loadRestrictions() + { + return [ + new HostgroupRestriction($this->connection(), $this->getAuth()) + ]; + } + + protected function prepareQuery() + { + $type = $this->getType(); + $object = IcingaObject::createByType($type); + $table = $object->getTableName(); + $query = $this->applyRestrictions($this->db()->select() + ->from( + ['o' => $table], + $this->getColumns() + ) + ->order('o.object_name') + ); + + return $this->applyObjectTypeFilter($query); + } +}