From 21febddad213f8b23ce58e1aac0d883a7f2d7c48 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 17 Jun 2014 14:00:38 +0000 Subject: [PATCH] Widget\Filter*: rudimentary filter widgets Initial implementation, more to come --- library/Icinga/Web/Widget/FilterEditor.php | 193 ++++++++++++++++++ library/Icinga/Web/Widget/FilterWidget.php | 107 ++++++++++ .../controllers/ListController.php | 9 + .../views/scripts/list/services.phtml | 1 + 4 files changed, 310 insertions(+) create mode 100644 library/Icinga/Web/Widget/FilterEditor.php create mode 100644 library/Icinga/Web/Widget/FilterWidget.php diff --git a/library/Icinga/Web/Widget/FilterEditor.php b/library/Icinga/Web/Widget/FilterEditor.php new file mode 100644 index 000000000..7f9edefc0 --- /dev/null +++ b/library/Icinga/Web/Widget/FilterEditor.php @@ -0,0 +1,193 @@ +filter = $props['filter']; + if (array_key_exists('query', $props)) { + $this->query = $props['query']; + } + } + + protected function select($name, $list, $selected) + { + $view = $this->view(); + $html = ''; + return $html; + } + + public function markIndex($idx) + { + $this->selectedIdx = $idx; + return $this; + } + + public function removeIndex($idx) + { + $this->selectedIdx = $idx; + return $this; + } + + protected function renderFilter($filter, $level = 0) + { + $html = ''; + $url = Url::fromRequest(); + + $view = $this->view(); + $idx = $filter->getId(); + $markUrl = clone($url); + $markUrl->setParam('fIdx', $idx); + + $removeUrl = clone($url); + $removeUrl->setParam('removeId', $idx); + $removeLink = ' ' . $view->icon('remove.png') . ''; + + $addUrl = clone($url); + $addUrl->setParam('addToId', $idx); + $addLink = ' ' . $view->icon('create.png') . ''; + + + $selectedIndex = ($idx === $this->selectedIdx ? ' -<--' : ''); + $selectIndex = ' o'; + + if ($filter instanceof FilterChain) { + $parts = array(); + $i = 0; + + foreach ($filter->filters() as $f) { + $i++; + $parts[] = $this->renderFilter($f, $level + 1); + } + + if (empty($parts)) { + return $html; + } + $op = $this->select( + 'operator', + array( + 'OR' => 'OR', + 'AND' => 'AND', + 'NOT' => 'NOT' + ), + $filter->getOperatorName() + ) . $addLink . $removeLink; + $html .= ' '; + + if ($level === 0) { + $html .= $op + . ''; + } else { + $html .= $op . ''; + } + return $html; + + } elseif ($filter instanceof FilterExpression) { + $u = $url->without($filter->getColumn()); + } else { + throw new \Exception('WTF'); + } + $value = $filter->getExpression(); + if (is_array($value)) { + $value = implode('|', $value); + } + $html .= $this->selectColumn($filter) . ' = ' . $removeLink; + + return $html; + } + + protected function arrayForSelect($array) + { + $res = array(); + foreach ($array as $k => $v) { + if (is_int($k)) { + $res[$v] = $v; + } else { + $res[$k] = $v; + } + } + // sort($res); + return $res; + } + + protected function selectColumn($filter) + { + $name = 'column_' . $filter->getId(); + + if ($this->query === null) { + return sprintf( + '', + $name, + $filter->getColumn() + ); + } else { + return $this->select( + $name, + $this->arrayForSelect($this->query->getColumns()), + $filter->getColumn() + ); + } + } + + public function render() + { + return '
' + . $this->renderFilter($this->filter) + . '' + . '
'; + } +} diff --git a/library/Icinga/Web/Widget/FilterWidget.php b/library/Icinga/Web/Widget/FilterWidget.php new file mode 100644 index 000000000..37227a654 --- /dev/null +++ b/library/Icinga/Web/Widget/FilterWidget.php @@ -0,0 +1,107 @@ +filter = $filter; + } + + protected function renderFilter($filter, $level = 0) + { + $html = ''; + $url = Url::fromRequest(); + if ($filter instanceof FilterChain) { + if ($level === 0) { + $op = '
  • )' . $filter->getOperatorName() . ' ('; + } else { + $op = '
  • ) ' . $filter->getOperatorName() . ' ( '; + } + $parts = array(); + foreach ($filter->filters() as $f) { + $parts[] = $this->renderFilter($f, $level + 1); + } + if (empty($parts)) { + return $html; + } + if ($level === 0) { + $html .= ''; + } else { + $html .= ''; + } + return $html; + } elseif ($filter instanceof FilterExpression) { + $u = $url->without($filter->getColumn()); + } else { + $u = $url . '--'; + } + $html .= '' . $filter . ' '; + return $html; + } + + public function render() + { + $url = Url::fromRequest(); + $html = '

    '; + + + // $html .= $this->renderFilter($this->filter); + + $editorUrl = clone $url; + $editorUrl->setParam('modifyFilter', true); + if ($this->filter->isEmpty()) { + $txt = t('Filter'); + $title = t('Filter this list'); + $remove = ''; + } else { + $txt = t('Filtered'); + $title = t('Modify this filter'); + $remove = ' ' + . t('remove') + . ''; + } + $filter = $this->filter->isEmpty() ? '' : ': ' . $this->filter; + $html .= '' . $this->view()->escape($txt) . '' + . $filter . $remove; + + return $html; + } +} diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 0d17ae87f..0a7d29796 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -451,6 +451,15 @@ class Monitoring_ListController extends Controller $filter = Filter::fromQueryString((string) $params); $query->applyFilter($filter); + if ($modifyFilter) { + $this->view->filterWidget = Widget::create('filterEditor', array( + 'filter' => $filter, + 'query' => $query + )); + } else { + $this->view->filterWidget = Widget::create('filterWidget', $filter); + } + $this->view->filter = $filter; if ($sort) { $query->order($sort, $dir); } diff --git a/modules/monitoring/application/views/scripts/list/services.phtml b/modules/monitoring/application/views/scripts/list/services.phtml index 11795da5b..adc0cd749 100644 --- a/modules/monitoring/application/views/scripts/list/services.phtml +++ b/modules/monitoring/application/views/scripts/list/services.phtml @@ -6,6 +6,7 @@ if (!$this->compact): ?> tabs ?>
    translate('Sort by') ?> sortControl ?> +filterWidget ?>
    paginationControl($services, null, null, array('preserve' => $this->preserve)) ?>