<?php require_once 'includes/modal/new_device_group.inc.php'; require_once 'includes/modal/delete_device_group.inc.php'; $no_refresh = TRUE; echo '<div class="row"><div class="col-sm-12"><span id="message"></span></div></div>'; echo '<div class="table-responsive">'; echo '<table class="table table-condensed table-hover"><thead><tr>'; echo '<th>Name</th><th>Description</th><th>Pattern</th><th>Actions</th>'; echo '</tr></thead><tbody>'; foreach (GetDeviceGroups() as $group) { echo '<tr id="row_' . $group['id'] . '">'; echo '<td>' . $group['name'] . '</td>'; echo '<td>' . $group['desc'] . '</td>'; echo '<td>' . $group['pattern'] . '</td>'; echo '<td>'; echo "<button type='button' class='btn btn-primary btn-sm' aria-label='Edit' data-toggle='modal' data-target='#create-group' data-group_id='" . $group['id'] . "' name='edit-device-group'><span class='glyphicon glyphicon-pencil' aria-hidden='true'></span></button> "; echo "<button type='button' class='btn btn-danger btn-sm' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-group_id='" . $group['id'] . "' name='delete-device-group'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></button>"; echo '</td>'; echo '</tr>'; } echo '</tbody></table></div>'; echo "<button type='button' class='btn btn-primary btn-sm' aria-label='Add' data-toggle='modal' data-target='#create-group' data-group_id='' name='create-device-group'>Create new Group</button> ";
function get_device_groups() { $app = \Slim\Slim::getInstance(); $router = $app->router()->getCurrentRoute()->getParams(); $status = 'error'; $code = 404; $hostname = $router['hostname']; // use hostname as device_id if it's all digits $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); if (is_numeric($device_id)) { $groups = GetGroupsFromDevice($device_id, 1); } else { $groups = GetDeviceGroups(); } if (empty($groups)) { $message = 'No device groups found'; } else { $status = 'ok'; $code = 200; $message = 'Found ' . count($groups) . ' device groups'; } $output = array('status' => $status, 'message' => $message, 'count' => count($groups), 'groups' => $groups); $app->response->setStatus($code); $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); }
/** * Get all groups of Device * @param integer $device Device-ID * @return array */ function GetGroupsFromDevice($device) { $ret = array(); foreach (GetDeviceGroups() as $group) { if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?') . ' LIMIT 1', array($device)) == $device) { $ret[] = $group['id']; } } return $ret; }
/** * Run the group queries again to get fresh list of groups for this device * @param integer $device_id Device-ID * @param int $extra Return extra info about the groups (name, desc, pattern) * @return array */ function QueryGroupsFromDevice($device_id, $extra = 0) { $ret = array(); foreach (GetDeviceGroups() as $group) { if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?', $extra) . ' LIMIT 1', array($device_id)) == $device_id) { if ($extra === 0) { $ret[] = $group['id']; } else { $ret[] = $group; } } } return $ret; }
$common_output[] = '<option value=""' . ($current_state == '' ? ' selected' : '') . '>any state</option>'; foreach ($alert_states as $name => $val) { $common_output[] = "<option value=\"{$val}\"" . ($current_state == $name || is_numeric($current_state) && $current_state == $val ? ' selected' : '') . ">{$name}</option>"; } $common_output[] = ' </select> </div> </div> <div class="form-group row"> <div class="col-sm-4"> <label for="group" class="control-label">Device Group:</label> </div> <div class="col-sm-8"> <select class="form-control" name="group">'; $common_output[] = '<option value=""' . ($current_group == '' ? ' selected' : '') . '>any group</option>'; $device_groups = GetDeviceGroups(); $common_output[] = "<!-- " . print_r($device_groups, true) . " -->"; foreach ($device_groups as $group) { $group_id = $group['id']; $common_output[] = "<option value=\"{$group_id}\"" . (is_numeric($current_group) && $current_group == $group_id ? ' selected' : '') . ">" . $group['name'] . " - " . $group['description'] . "</option>"; } $common_output[] = ' </select> </div> </div> <div class="form-group"> <div class="col-sm-12"> <button type="submit" class="btn btn-default">Set</button> </div> </div>
/** * Run the group queries again to get fresh list of groups for this device * @param integer $device_id Device-ID * @param int $extra Return extra info about the groups (name, desc, pattern) * @return array */ function QueryGroupsFromDevice($device_id, $extra = 0) { $ret = array(); foreach (GetDeviceGroups() as $group) { $params = (array) json_decode($group['params']); array_unshift($params, $device_id); if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?', $extra) . ' LIMIT 1', $params) == $device_id) { if ($extra === 0) { $ret[] = $group['id']; } else { $ret[] = $group; } } } return $ret; }