diff options
author | Simon Rettberg | 2016-07-13 16:59:21 +0200 |
---|---|---|
committer | Simon Rettberg | 2016-07-13 16:59:21 +0200 |
commit | a2cfd0c8cea18861ad20f7eb535a98394f9239ff (patch) | |
tree | 49dd985595fe339e8f2a2aba377ddfca10088b9b | |
parent | [statistics] Minor html cleanup/fixes (diff) | |
download | slx-admin-a2cfd0c8cea18861ad20f7eb535a98394f9239ff.tar.gz slx-admin-a2cfd0c8cea18861ad20f7eb535a98394f9239ff.tar.xz slx-admin-a2cfd0c8cea18861ad20f7eb535a98394f9239ff.zip |
[statistics] Change delimiter to something more unusual, add captions for filter options
-rw-r--r-- | modules-available/statistics/inc/filter.inc.php | 15 | ||||
-rw-r--r-- | modules-available/statistics/page.inc.php | 17 | ||||
-rw-r--r-- | modules-available/statistics/templates/clientlist.html | 74 |
3 files changed, 67 insertions, 39 deletions
diff --git a/modules-available/statistics/inc/filter.inc.php b/modules-available/statistics/inc/filter.inc.php index 0f1a0d20..a3f1cbb5 100644 --- a/modules-available/statistics/inc/filter.inc.php +++ b/modules-available/statistics/inc/filter.inc.php @@ -4,9 +4,15 @@ * WARNING: argument is escaped, but $column and $operator are passed unfiltered into SQL */ class Filter { + /** + * Delimiter for js_selectize filters + */ + const DELIMITER = '~,~'; + public $column; public $operator; public $argument; + public function __construct($column, $operator, $argument = null) { $this->column = trim($column); @@ -40,11 +46,11 @@ class Filter { $operators = ['<=', '>=', '!=', '!~', '=', '~', '<', '>']; $filters = []; - foreach (explode(',', $query) as $q) { + foreach (explode(self::DELIMITER, $query) as $q) { $q = trim($q); /* find position of first operator */ $pos = 10000; - $operator; + $operator = false; foreach ($operators as $op) { $newpos = strpos($q, $op); if ($newpos > -1 && ($newpos < $pos)) { @@ -75,7 +81,7 @@ class Filter if (array_key_exists($lhs, Page_Statistics::$columns) && Page_Statistics::$columns[$lhs]['column']) { $filters[] = new Filter($lhs, $operator, $rhs); } else { - Message::addError('invalid-filter'); + Message::addError('invalid-filter-key', $lhs); } } } @@ -165,8 +171,7 @@ class StateFilter extends Filter } elseif ($this->argument === 'occupied') { return " $neg (lastseen + 600 > UNIX_TIMESTAMP() AND logintime <> 0 ) "; } else { - Message::addError('invalid-filter'); - + Message::addError('invalid-filter-argument', 'state', $this->argument); return ' 1'; } } diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index d46468f9..31ff280f 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -53,11 +53,6 @@ class Page_Statistics extends Page 'type' => 'date', 'column' => true, ], - 'position' => [ - 'op' => Page_Statistics::$op_nominal, - 'type' => 'string', - 'column' => true, - ], 'realcores' => [ 'op' => Page_Statistics::$op_ordinal, 'type' => 'int', @@ -544,8 +539,16 @@ class Page_Statistics extends Page } $rows[] = $row; } - Render::addTemplate('clientlist', array('rows' => $rows, 'filter' => $filter, - 'query' => $query, 'sortDirection' => $sortDirection, 'sortColumn' => $sortColumn, 'argument' => $argument, 'columns' => json_encode(Page_Statistics::$columns),)); + Render::addTemplate('clientlist', array( + 'rows' => $rows, + 'filter' => $filter, + 'query' => $query, + 'delimiter' => Filter::DELIMITER, + 'sortDirection' => $sortDirection, + 'sortColumn' => $sortColumn, + 'argument' => $argument, + 'columns' => json_encode(Page_Statistics::$columns), + )); } private function ramColorClass($mb) diff --git a/modules-available/statistics/templates/clientlist.html b/modules-available/statistics/templates/clientlist.html index 1ec3a143..57ec7f64 100644 --- a/modules-available/statistics/templates/clientlist.html +++ b/modules-available/statistics/templates/clientlist.html @@ -16,7 +16,7 @@ <input name="argument" id="argumentInput" class="form-control col-4-xs"> </input> <select name="argument" id="argumentSelect" class="form-control col-4-xs"> </select> </div> - <button type="button" class="btn btn-primary" onclick="addFilter()" > + <button type="button" class="btn btn-primary" onclick="addFilterFromForm()" > <span class="glyphicon glyphicon-plus"></span> {{lang_add}}</button> </form> @@ -30,7 +30,6 @@ <div> <!-- use GET here, to avoid the "resend form?" confirmation, and anyway this is stateless, so GET makes more sense --> <form id="queryForm" method="GET" action="?do=Statistics&show=list" class="" role="form"> - <input type="hidden" name="token" value="{{token}}"/> <input type="hidden" name="do" value="Statistics"/> <input type="hidden" name="show" value="list"/> <label for="filterInput">{{lang_labelFilter}}</label> @@ -137,16 +136,28 @@ var filterSelectize; -document.addEventListener("DOMContentLoaded", function () { +var slxFilterNames = { + machineuuid: '{{lang_uuid}}', + macaddr: '{{lang_macAddr}}', + firstseen: '{{lang_firstSeen}}', + lastseen: '{{lang_lastSeen}}', + lastboot: '{{lang_lastBoot}}', + logintime: '{{lang_lastLogin}}', + realcores: '{{lang_cores}}', + systemmodel: '{{lang_model}}', + cpumodel: '{{lang_cpuModel}}', + hddgb: '{{lang_tmpGb}}', + gbram: '{{lang_gbRam}}', + kvmstate: '{{lang_kvmSupport}}', + badsectors: '{{lang_reallocatedSectors}}', + clientip: '{{lang_ip}}', + state: '{{lang_usageState}}' +}; + +var slxFilterDel = '{{delimiter}}'; - var initComplete = false; - var comp = ['=', '!=']; - var ordered = ['<', '<=', '=', '>=', '>']; - // var filter = { - // 'machine' : {'op' : comp, 'type' : 'string'}, - // 'ram' : {'op' : ordered, 'type' : 'string'}, - // 'lastSeen' : {'op' : ordered, 'type' : 'date'}, - // }; + +document.addEventListener("DOMContentLoaded", function () { /* some objects */ var $columnSelect = $('#columnSelect'); @@ -158,13 +169,13 @@ document.addEventListener("DOMContentLoaded", function () { /* add options to column select */ for (var key in columns) { $columnSelect.append($('<option>', { - value: key, text: key})); + value: key, text: (slxFilterNames[key] ? slxFilterNames[key] : key) })); }; /* initialize selectize */ filterSelectize = $('#filterInput').selectize({ - delimiter: ',', + delimiter: slxFilterDel, plugins: ['restore_on_backspace', 'remove_button'], create: function(input) { return {value: input, text: input} @@ -177,9 +188,15 @@ document.addEventListener("DOMContentLoaded", function () { })[0].selectize; /* add query */ var str = "{{{query}}}"; - str.split(",").forEach(function(v) { - filterSelectize.addOption({value: v, text: v}); - filterSelectize.addItem(v); + var eExp = /^(\w+)\s*([=><!]+)\s*(.*)$/; + str.split(slxFilterDel).forEach(function(v) { + var match = eExp.exec(v); + if (match && match.length === 4) { + addFilter(match[1], match[2], match[3]); + } else { + filterSelectize.addOption({value: v, text: v}); + filterSelectize.addItem(v); + } }); $('#columnSelect').on('change', function() { @@ -211,7 +228,6 @@ document.addEventListener("DOMContentLoaded", function () { }); initButtons(); - initComplete = true; }, false); @@ -258,21 +274,25 @@ function popupFilter(field) { $modal.modal('show'); } -function addFilter() { +function addFilterFromForm() { var argument1 = $('#argumentInput').val(); var argument2 = $('#argumentSelect').val(); var argument = argument1 == '' ? argument2 : argument1; + var col = $('#columnSelect').val(); + var op = $('#operatorSelect').val(); - var ft = $('#columnSelect').val() + ' ' + $('#operatorSelect').val() + ' ' + argument; - filterSelectize.addOption({value: ft, text: ft}); - filterSelectize.addItem(ft); - - //$modal.modal('hide'); - refresh(); + addFilter(col, op, argument); + refresh(); // TODO: AJAX +} - // $('#columnSelect').val(''); - // $('#operatorSelect option').remove(); - // $('#argumentInput').val(''); +function addFilter(col, op, argument) { + var filterValue = col + ' ' + op + ' ' + argument; + var filterText = filterValue; + if (slxFilterNames[col]) { + filterText = slxFilterNames[col] + ' ' + op + ' ' + argument; + } + filterSelectize.addOption({value: filterValue, text: filterText}); + filterSelectize.addItem(filterValue); } function toggleSort(field) { |