From 19eec4188d6d871d980deee2255507ac7d265b6c Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Mon, 8 Aug 2016 14:30:18 +0200 Subject: subnet select. --- modules-available/roomplanner/clientscript.js | 64 ++++++++-------- modules-available/roomplanner/page.inc.php | 91 ++++++++++++++--------- modules-available/roomplanner/templates/page.html | 3 + 3 files changed, 91 insertions(+), 67 deletions(-) (limited to 'modules-available/roomplanner') diff --git a/modules-available/roomplanner/clientscript.js b/modules-available/roomplanner/clientscript.js index b7bd5d55..c19e63ec 100644 --- a/modules-available/roomplanner/clientscript.js +++ b/modules-available/roomplanner/clientscript.js @@ -48,37 +48,6 @@ function loadMachines(query, callback) { } -var searchSettings = { - plugins : ["remove_button"], - valueField: 'machineuuid', - searchField: "combined", - //labelField: "combined", - openOnFocus: false, - create: false, - render : { option : renderMachineEntry, item: renderMachineEntry}, - load: loadMachines, - maxItems: 1, - sortField: 'clientip', - sortDirection: 'asc', - onChange: clearSubnetBox - -} - -var subnetSettings = { - plugins : ["remove_button"], - valueField: 'machineuuid', - searchField: "combined", - //labelField: "combined", - openOnFocus: false, - create: false, - render : { option : renderMachineEntry, item: renderMachineEntry}, - load: loadMachines, - maxItems: 1, - sortField: 'clientip', - sortDirection: 'asc', - onChange: clearSearchBox - -} function clearSearchBox() { $selectizeSearch[0].selectize.setValue([], true); @@ -92,8 +61,37 @@ function initSelectize() { console.log("initializing selectize"); /* init modal */ $modal = $('#selectMachineModal'); - $selectizeSearch = $('#machineSearchBox').selectize(searchSettings); - $selectizeSubnet = $('#subnetBox').selectize(subnetSettings); + + /* for the search */ + $selectizeSearch = $('#machineSearchBox').selectize({ + plugins : ["remove_button"], + valueField: 'machineuuid', + searchField: "combined", + openOnFocus: false, + create: false, + render : { option : renderMachineEntry, item: renderMachineEntry}, + load: loadMachines, + maxItems: 1, + sortField: 'clientip', + sortDirection: 'asc', + onChange: clearSubnetBox + }); + + + /* for the subnet box */ + $selectizeSubnet = $('#subnetBox').selectize({ + options: subnetMachines, + plugins : ["remove_button"], + valueField: 'machineuuid', + searchField: "combined", + openOnFocus: true, + create: false, + render : { option : renderMachineEntry, item: renderMachineEntry}, + maxItems: 1, + sortField: 'clientip', + sortDirection: 'asc', + onChange: clearSearchBox + }); $('#selectMachineButton').on('click', onBtnSelect); diff --git a/modules-available/roomplanner/page.inc.php b/modules-available/roomplanner/page.inc.php index b533cf2f..222192c1 100644 --- a/modules-available/roomplanner/page.inc.php +++ b/modules-available/roomplanner/page.inc.php @@ -2,30 +2,38 @@ class Page_Roomplanner extends Page { - protected function doPreprocess() - { - User::load(); + protected function doPreprocess() + { + User::load(); - if (!User::hasPermission('superadmin')) { - Message::addError('main.no-permission'); - Util::redirect('?do=Main'); - } + if (!User::hasPermission('superadmin')) { + Message::addError('main.no-permission'); + Util::redirect('?do=Main'); + } + } - } + protected function doRender() + { + $locationid = Request::get('locationid', null, 'integer'); - protected function doRender() - { - Render::addTemplate('page', []); - } + if ($locationid === null) { + die('please specify locationid'); + } - protected function doAjax() - { - $action = Request::get('action', null, 'string'); + $subnetMachines = $this->getPotentialMachines($locationid); - if ($action === 'getmachines') { - $query = Request::get('query', null, 'string'); + Render::addTemplate('page', ['subnetMachines' => json_encode($subnetMachines), + ]); + } - /* the query could be anything: UUID, IP or macaddr */ + protected function doAjax() + { + $action = Request::get('action', null, 'string'); + + if ($action === 'getmachines') { + $query = Request::get('query', null, 'string'); + + /* the query could be anything: UUID, IP or macaddr */ // $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' // . ', MATCH (machineuuid, macaddr, clientip, hostname) AGAINST (:query) AS relevance ' // . 'FROM machine ' @@ -34,20 +42,35 @@ class Page_Roomplanner extends Page // . 'LIMIT 5' // , ['query' => $query]); // - $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' - . 'FROM machine ' - . "WHERE machineuuid LIKE :query " - . " OR macaddr LIKE :query " - . " OR clientip LIKE :query " - . " OR hostname LIKE :query ", ['query' => "%$query%"]); - - $returnObject = ['machines' => []]; - - while ($row = $result->fetch(PDO::FETCH_ASSOC)) { - $returnObject['machines'][] = $row; - } - echo json_encode($returnObject, JSON_PRETTY_PRINT); - - } - } + $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' + .'FROM machine ' + .'WHERE machineuuid LIKE :query ' + .' OR macaddr LIKE :query ' + .' OR clientip LIKE :query ' + .' OR hostname LIKE :query ', ['query' => "%$query%"]); + + $returnObject = ['machines' => []]; + + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + $returnObject['machines'][] = $row; + } + echo json_encode($returnObject, JSON_PRETTY_PRINT); + } + } + + protected function getPotentialMachines($locationid) + { + $result = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, hostname ' + .'FROM machine INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) ' + .'WHERE subnet.locationid = :locationid', ['locationid' => $locationid]); + + $machines = []; + + while ($row = $result->fetch(PDO::FETCH_ASSOC)) { + $row['combined'] = implode(' ', array_values($row)); + $machines[] = $row; + } + + return $machines; + } } diff --git a/modules-available/roomplanner/templates/page.html b/modules-available/roomplanner/templates/page.html index a8294dc5..f4d97a6e 100644 --- a/modules-available/roomplanner/templates/page.html +++ b/modules-available/roomplanner/templates/page.html @@ -333,6 +333,8 @@