icingaweb2-module-director/library/Director/Web/Table/DatafieldTable.php

119 lines
3.3 KiB
PHP
Raw Normal View History

2015-07-03 10:48:50 +02:00
<?php
namespace Icinga\Module\Director\Web\Table;
2015-07-03 10:48:50 +02:00
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use Zend_Db_Adapter_Abstract as ZfDbAdapter;
use Zend_Db_Select as ZfDbSelect;
2015-07-03 10:48:50 +02:00
class DatafieldTable extends ZfQueryBasedTable
2015-07-03 10:48:50 +02:00
{
2021-03-01 17:16:07 +01:00
protected $searchColumns = [
2017-07-14 13:45:31 +02:00
'df.varname',
'df.caption',
2021-03-01 17:16:07 +01:00
];
2015-07-03 10:48:50 +02:00
public function getColumns()
{
2021-03-01 17:16:07 +01:00
return [
'id' => 'df.id',
'varname' => 'df.varname',
'caption' => 'df.caption',
'description' => 'df.description',
'datatype' => 'df.datatype',
2021-03-01 17:12:57 +01:00
'category' => 'dfc.category_name',
'assigned_fields' => 'SUM(used_fields.cnt)',
'assigned_vars' => 'SUM(used_vars.cnt)',
2021-03-01 17:16:07 +01:00
];
2015-07-03 10:48:50 +02:00
}
public function renderRow($row)
2015-07-03 10:48:50 +02:00
{
return $this::tr([
$this::td(Link::create(
$row->caption,
'director/datafield/edit',
['id' => $row->id]
)),
$this::td($row->varname),
2021-03-01 17:12:57 +01:00
$this::td($row->category),
$this::td($row->assigned_fields),
$this::td($row->assigned_vars)
]);
2015-07-03 10:48:50 +02:00
}
public function getColumnsToBeRendered()
2015-07-03 10:48:50 +02:00
{
2021-03-01 17:16:07 +01:00
return [
$this->translate('Label'),
$this->translate('Field name'),
2021-03-01 17:12:57 +01:00
$this->translate('Category'),
$this->translate('# Used'),
$this->translate('# Vars'),
2021-03-01 17:16:07 +01:00
];
2015-07-03 10:48:50 +02:00
}
public function prepareQuery()
2015-07-03 10:48:50 +02:00
{
$db = $this->db();
2021-03-01 17:16:07 +01:00
$fieldTypes = ['command', 'host', 'notification', 'service', 'user'];
$varsTypes = ['command', 'host', 'notification', 'service', 'service_set', 'user'];
2021-03-01 17:16:07 +01:00
$fieldsQueries = [];
foreach ($fieldTypes as $type) {
$fieldsQueries[] = $this->makeDatafieldSub($type, $db);
}
2021-03-01 17:16:07 +01:00
$varsQueries = [];
foreach ($varsTypes as $type) {
$varsQueries[] = $this->makeVarSub($type, $db);
}
return $db->select()->from(
2021-03-01 17:16:07 +01:00
['df' => 'director_datafield'],
$this->getColumns()
2021-03-01 17:12:57 +01:00
)->joinLeft(
['dfc' => 'director_datafield_category'],
'df.category_id = dfc.id',
[]
)->joinLeft(
2021-03-01 17:16:07 +01:00
['used_fields' => $db->select()->union($fieldsQueries, ZfDbSelect::SQL_UNION_ALL)],
'used_fields.datafield_id = df.id',
2021-03-01 17:16:07 +01:00
[]
)->joinLeft(
2021-03-01 17:16:07 +01:00
['used_vars' => $db->select()->union($varsQueries, ZfDbSelect::SQL_UNION_ALL)],
'used_vars.varname = df.varname',
2021-03-01 17:16:07 +01:00
[]
)->group('df.id')->group('df.varname')->group('dfc.category_name')->order('caption ASC');
}
/**
* @param $type
* @param ZfDbAdapter $db
*
* @return ZfDbSelect
*/
protected function makeDatafieldSub($type, ZfDbAdapter $db)
{
2021-03-01 17:16:07 +01:00
return $db->select()->from("icinga_${type}_field", [
'cnt' => 'COUNT(*)',
'datafield_id'
])->group('datafield_id');
}
/**
* @param $type
* @param ZfDbAdapter $db
*
* @return ZfDbSelect
*/
protected function makeVarSub($type, ZfDbAdapter $db)
{
2021-03-01 17:16:07 +01:00
return $db->select()->from("icinga_${type}_var", [
'cnt' => 'COUNT(*)',
'varname'
])->group('varname');
2015-07-03 10:48:50 +02:00
}
}