Add functionality to services in backend.

- Add functionallity to categorize a one or more services under one
  service group. Partial fix for #7 and #90. (Frontend code to be done)
- Add description field to service to be displayed as a help text
  on front page. Partial fix for #51 (Frontend code to be done)
This commit is contained in:
thnilsen 2020-09-27 14:01:54 +02:00
parent 48b9cbbc82
commit 93d1491aac
9 changed files with 515 additions and 42 deletions

View File

@ -110,6 +110,16 @@ else{
require_once("new-user.php");
break;
case 'new-service':
case 'edit-service':
require_once('service.php');
break;
case 'new-service-group':
case 'edit-service-group':
require_once('service-group.php');
break;
case 'options':
require_once("options.php");
break;

99
admin/service-group.php Normal file
View File

@ -0,0 +1,99 @@
<?php
if (isset($_GET['new']))
{
ServiceGroup::add();
}
if (isset($_GET['edit']))
{
ServiceGroup::edit();
}
if (isset($_GET['delete']))
{
ServiceGroup::delete();
}
$boolEdit = false;
$group_value = isset($_POST['group']) ? $_POST['group'] : '';
$description_value = isset($_POST['description']) ? $_POST['description'] : '';
$visibility_id_value = isset($_POST['visibility_id']) ? $_POST['visibility_id'] : '';
if ( isset($_GET['id']) && !isset($_POST['id']) ) {
$group_id = (int) $_GET['id'];
$boolEdit = true;
$stmt = $mysqli->prepare("SELECT * FROM services_groups WHERE id LIKE ?");
$stmt->bind_param("i", $group_id);
$stmt->execute();
$query = $stmt->get_result();
$data = $query->fetch_assoc();
$group_value = $data['name'];
$description_value = $data['description'];
$visibility_id_value = $data['visibility'];
}
if (!$boolEdit) {
Template::render_header(_("New service group"), true); ?>
<div class="text-center">
<h2><?php echo _("Add new service group");?></h2>
</div>
<?php
$form_url = WEB_URL .'/admin/?do=new-service-group&amp;new=group';
} else {
Template::render_header(_("Edit service group"), true); ?>
<div class="text-center">
<h2><?php echo _("Edit service group");?></h2>
</div>
<?php
$form_url = WEB_URL .'/admin/?do=edit-service-group&amp;edit&amp;id='.$group_id;
}
?>
<form action="<?php echo $form_url;?>" method="POST" class="form-horizontal">
<?php if (isset($message))
{?>
<p class="alert alert-danger"><?php echo $message?></p>
<?php
} ?>
<div class="form-group">
<div class="col-sm-6"><label for="group"><?php echo _("Service Group Name");?>: </label><input type="text" maxlength="50" name="group" value="<?php echo ((isset($_POST['group']))?htmlspecialchars($_POST['group'],ENT_QUOTES):$group_value);?>" id="group" placeholder="<?php echo _("service group name");?>" class="form-control" required></div>
<div class="col-sm-6"><label for="description"><?php echo _("Description");?>: </label><input type="text" maxlength="100" name="description" value="<?php echo ((isset($_POST['description']))?htmlspecialchars($description_value,ENT_QUOTES):$description_value);?>" id="description" placeholder="<?php echo _("Description");?>" class="form-control"></div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label for="visibility_id"><?php echo _("Visibility");?>: </label>
<select name="visibility_id" id="visibility_id" class="form-control">
<?php
if (!empty($visibility_id_value))
{
$visibility_id = $visibility_id_value;
}
else
{
$visibility_id = null;
}
//$visibilitys = Service::get_groups();
foreach ($visibility as $key => $value) {
if ($visibility_id == $key)
{
echo '<option value="'.$key.'" selected>'.$value.'</option>';
}
else{
echo '<option value="'.$key.'">'.$value.'</option>';
}
}
?>
</select>
</div>
</div>
<?php
if ( $boolEdit ) {
echo '<input type="hidden" id="id" name="id" value="'.$group_id.'">';
}
?>
<button type="submit" class="btn btn-primary pull-right"><?php echo _("Submit");?></button>
</form>

97
admin/service.php Normal file
View File

@ -0,0 +1,97 @@
<?php
if (isset($_GET['new']))
{
Service::add();
}
if (isset($_GET['edit']))
{
Service::edit();
}
/*if (isset($_GET['delete']))
{
Service::delete();
}*/
$boolEdit = false;
$service_value = isset($_POST['service']) ? $_POST['service'] : '';
$description_value = isset($_POST['description']) ? $_POST['description'] : '';
$group_id_value = isset($_POST['group_id']) ? $_POST['group_id'] : '';
if ( isset($_GET['id']) && !isset($_POST['id']) ) {
$service_id = (int) $_GET['id'];
$boolEdit = true;
$stmt = $mysqli->prepare("SELECT * FROM services WHERE id LIKE ?");
$stmt->bind_param("i", $service_id);
$stmt->execute();
$query = $stmt->get_result();
$data = $query->fetch_assoc();
//print_r($data);
$service_value = $data['name'];
$description_value = $data['description'];
$group_id_value = $data['group_id'];
}
if (!$boolEdit) {
Template::render_header(_("New service"), true); ?>
<div class="text-center">
<h2><?php echo _("Add new service");?></h2>
</div>
<?php
$form_url = WEB_URL . '/admin/?do=new-service&amp;new=service';
} else {
Template::render_header(_("New service"), true); ?>
<div class="text-center">
<h2><?php echo _("Add new service");?></h2>
</div>
<?php
$form_url = WEB_URL . '/admin/?do=edit-service&amp;edit&amp;id='.$service_id;
}
?>
<form action="<?php echo $form_url;?>" method="POST" class="form-horizontal">
<?php if (isset($message))
{?>
<p class="alert alert-danger"><?php echo $message?></p>
<?php
} ?>
<div class="form-group">
<div class="col-sm-6"><label for="service"><?php echo _("Service");?>: </label><input type="text" maxlength="50" name="service" value="<?php echo ((isset($_POST['service']))?htmlspecialchars($_POST['service'],ENT_QUOTES):$service_value);?>" id="service" placeholder="<?php echo _("service");?>" class="form-control" required></div>
<div class="col-sm-6"><label for="description"><?php echo _("Description");?>: </label><input type="text" maxlength="200" name="description" value="<?php echo ((isset($_POST['description']))?htmlspecialchars($_POST['description'],ENT_QUOTES):$description_value);?>" id="description" placeholder="<?php echo _("Description");?>" class="form-control"></div>
</div>
<div class="form-group">
<div class="col-sm-6">
<label for="group_id"><?php echo _("Service Group");?>: </label>
<select name="group_id" id="group_id" class="form-control">
<?php
if (!empty($group_id_value))
{
$group_id = $group_id_value;
}
else
{
$group_id = null;
}
$groups = ServiceGroup::get_groups();
foreach ($groups as $key => $value) {
if ($group_id == $key)
{
echo '<option value="'.$key.'" selected>'.$value.'</option>';
}
else{
echo '<option value="'.$key.'">'.$value.'</option>';
}
}
?>
</select>
</div>
</div>
<?php
if ( $boolEdit ) {
echo '<input type="hidden" id="id" name="id" value="'.$service_id.'">';
}
?>
<button type="submit" class="btn btn-primary pull-right"><?php echo _("Submit");?></button>
</form>

View File

@ -1,13 +1,13 @@
<?php
if (isset($_GET['new']))
{
Service::add();
}
if (isset($_GET['delete']))
if (isset($_GET['delete']) && isset($_GET['type']))
{
if ( $_GET['type'] == 'service') {
Service::delete();
}
elseif ( $_GET['type'] == 'groups') {
ServiceGroup::delete();
}
}
Template::render_header(_("Settings"), true);
?>
@ -24,10 +24,7 @@ if (isset($message)){
<?php if ($user->get_rank() <= 1){?>
<form action="?do=settings&new=service" method="post">
<div class="input-group pull-right new-service">
<input class="form-control" name="service" placeholder="Name" type="text" value="<?php echo ((isset($_POST['service']))?htmlspecialchars($_POST['service']):''); ?>" maxlength="50" required>
<span class="input-group-btn">
<button type="submit" class="btn btn-success pull-right"><?php echo _("Add service");?></button>
</span>
<a href="<?php echo WEB_URL;?>/admin/?do=new-service" class="btn btn-success pull-right"><?php echo _("Add new service");?></a>
</div>
</form>
<?php }?>
@ -35,8 +32,10 @@ if (isset($message)){
<table class="table">
<thead><tr>
<th scope="col"><?php echo _("ID");?></th>
<!--<th scope="col"><?php echo _("ID");?></th>-->
<th scope="col"><?php echo _("Name");?></th>
<th scope="col"><?php echo _("Description");?></th>
<th scope="col"><?php echo _("Group");?></th>
<?php if ($user->get_rank()<=1)
{?>
<th scope="col"><?php echo _("Delete");?></th>
@ -45,15 +44,65 @@ if (isset($message)){
</thead>
<tbody>
<?php
$query = $mysqli->query("SELECT * FROM services");
$query = $mysqli->query("SELECT services.*, services_groups.name AS group_name FROM `services` LEFT JOIN services_groups ON services.group_id = services_groups.id ORDER BY services.name ASC");
while($result = $query->fetch_assoc())
{
echo "<tr>";
echo "<td>".$result['id']."</td>";
echo "<td>".$result['name']."</td>";
//echo "<td>".$result['id']."</td>";
echo '<td><a href="'.WEB_URL.'/admin?do=edit-service&id='.$result['id'].'">'.$result['name'].'</a></th>';
echo "<td>".$result['description']."</td>";
echo "<td>".$result['group_name']."</td>";
if ($user->get_rank()<=1)
{
echo '<td><a href="'.WEB_URL.'/admin/?do=settings&delete='.$result['id'].'" class="pull-right delete-service"><i class="fa fa-trash"></i></a></td>';
echo '<td><a href="'.WEB_URL.'/admin/?do=settings&type=service&delete='.$result['id'].'" class="pull-right delete-service"><i class="fa fa-trash"></i></a></td>';
}
echo "</tr>";
}?>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="pull-left"><?php echo _("Services Groups");?></h3>
<?php if ($user->get_rank() <= 1){?>
<form action="?do=settings&new=service-group" method="post">
<div class="input-group pull-right new-service">
<a href="<?php echo WEB_URL;?>/admin/?do=new-service-group" class="btn btn-success pull-right"><?php echo _("Add new service group");?></a>
</div>
</form>
<?php }?>
<div class="table-responsive">
<table class="table">
<thead><tr>
<!--<th scope="col"><?php echo _("ID");?></th>-->
<th scope="col"><?php echo _("Group Name");?></th>
<th scope="col"><?php echo _("In use by");?></th>
<th scope="col"><?php echo _("Description");?></th>
<th scope="col"><?php echo _("Visibility");?></th>
<?php if ($user->get_rank()<=1)
{?>
<th scope="col"><?php echo _("Delete");?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
$query = $mysqli->query("SELECT sg.* , (SELECT COUNT(*) FROM services WHERE services.group_id = sg.id) AS counter FROM services_groups AS sg ORDER BY sg.id ASC");
while($result = $query->fetch_assoc())
{
echo "<tr>";
//echo "<td>".$result['id']."</td>";
echo '<td><a href="'.WEB_URL.'/admin?do=edit-service-group&id='.$result['id'].'">'.$result['name'].'</a></th>';
echo '<td> <span class="badge badge-danger ml-2">'.$result['counter'].'</span>';
echo "<td>".$result['description']."</td>";
echo "<td>".$visibility[$result['visibility']]."</td>";
if ($user->get_rank()<=1)
{
echo '<td><a href="'.WEB_URL.'/admin/?do=settings&type=groups&delete='.$result['id'].'" class="pull-right delete-service"><i class="fa fa-trash"></i></a></td>';
}
echo "</tr>";
}?>

View File

@ -2,6 +2,7 @@
//DIR Because of include problems
require_once(__DIR__ . "/incident.php");
require_once(__DIR__ . "/service.php");
require_once(__DIR__ . "/service-group.php");
require_once(__DIR__ . "/user.php");
require_once(__DIR__ . "/token.php");
/**

167
classes/service-group.php Normal file
View File

@ -0,0 +1,167 @@
<?php
/**
* Class for managing services
*/
class ServiceGroup
{
private $id;
private $name;
private $description;
private $visibility_id;
/**
* Constructs servicegroup from its data.
* @param int $id service ID
* @param String $name service name
* @param String $description tooltip text
* @param int $visibility_id how to display group items
*/
function __construct($id, $name, $description, $visibility_id)
{
//TODO: Maybe get data from ID?
$this->id = $id;
$this->name = $name;
$this->description = $description;
$this->visibility_id = $visibility_id;
$this->status = $status;
}
/**
* Returns id of this servicegroup
* @return int id
*/
public function get_id()
{
return $this->id;
}
/**
* Returns name of this servicegroup
* @return String name
*/
public function get_name()
{
return $this->name;
}
/**
* Returns description of this servicegroup
* @return String description
*/
public function get_description()
{
return $this->description;
}
/**
* Processes submitted form and adds service unless problem is encountered,
* calling this is possible only for admin or higher rank. Also checks requirements
* for char limits.
* @return void
*/
public static function add()
{
global $user, $message;
if (strlen($_POST['group'])>50)
{
$message = _("Service group name is too long! Character limit is 50");
return;
}else if (strlen(trim($_POST['group']))==0){
$message = _("Please enter name!");
return;
}
if ($user->get_rank()<=1)
{
global $mysqli;
$name = $_POST["group"];
$description = $_POST["description"];
$visibility_id = $_POST["visibility_id"];
$stmt = $mysqli->prepare("INSERT INTO services_groups VALUES(NULL,?,?,?)");
$stmt->bind_param("ssi", $name, $description, $visibility_id);
$stmt->execute();
$stmt->get_result();
header("Location: ".WEB_URL."/admin/?do=settings");
}else
{
$message = _("You don't have the permission to do that!");
}
}
public static function edit()
{
global $user, $message;
if (strlen($_POST['group'])>50)
{
$message = _("Service group name is too long! Character limit is 50");
return;
}else if (strlen(trim($_POST['group']))==0){
$message = _("Please enter name!");
return;
}
if ($user->get_rank()<=1)
{
global $mysqli;
$name = $_POST["group"];
$description = $_POST["description"];
$visibility_id = $_POST["visibility_id"];
$group_id = $_POST["id"];
$stmt = $mysqli->prepare("UPDATE services_groups SET name=?, description=?,visibility=? WHERE id LIKE ?");
$stmt->bind_param("ssii", $name, $description, $visibility_id, $group_id);
$stmt->execute();
$stmt->get_result();
header("Location: ".WEB_URL."/admin/?do=settings");
}else
{
$message = _("You don't have the permission to do that!");
}
}
/**
* Deletes this service - first checks if user has permission to do that.
* @return void
*/
public static function delete()
{
global $user, $message;
if ($user->get_rank()<=1)
{
global $mysqli;
$id = $_GET['delete'];
$stmt = $mysqli->prepare("DELETE FROM services_groups WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$query = $stmt->get_result();
$stmt = $mysqli->prepare("UPDATE services SET group_id = NULL WHERE group_id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$query = $stmt->get_result();
header("Location: ".WEB_URL."/admin/?do=settings");
}
else
{
$message = _("You don't have the permission to do that!");
}
}
/**
* Get list of services groups.
* @return array $groups
*/
public function get_groups() {
global $mysqli;
$stmt = $mysqli->query("SELECT id, name FROM services_groups ORDER by name ASC");
$groups = array();
$groups[0] = '';
while ($res = $stmt->fetch_assoc()) {
$groups[$res['id']] = $res['name'];
}
return $groups;
}
}

View File

@ -70,9 +70,46 @@ class Service implements JsonSerializable
if ($user->get_rank()<=1)
{
global $mysqli;
$name = $_POST['service'];
$stmt = $mysqli->prepare("INSERT INTO services ( name ) VALUES ( ? )");
$stmt->bind_param("s", $name);
$name = htmlspecialchars($_POST['service']);
$description = htmlspecialchars($_POST['description']);
$group_id = $_POST['group_id'];
$stmt = $mysqli->prepare("INSERT INTO services ( name, description, group_id ) VALUES ( ?, ?, ? )");
$stmt->bind_param("ssi", $name, $description, $group_id);
$stmt->execute();
$stmt->get_result();
header("Location: ".WEB_URL."/admin/?do=settings");
}else
{
$message = _("You don't have the permission to do that!");
}
}
/**
* Processes submitted form and adds service unless problem is encountered,
* calling this is possible only for admin or higher rank. Also checks requirements
* for char limits.
* @return void
*/
public static function edit()
{
global $user, $message;
if (strlen($_POST['service'])>50)
{
$message = _("Service name is too long! Character limit is 50");
return;
}else if (strlen(trim($_POST['service']))==0){
$message = _("Please enter name!");
return;
}
if ($user->get_rank()<=1)
{
global $mysqli;
$service_id = $_POST["id"];
$name = htmlspecialchars($_POST['service']);
$description = htmlspecialchars($_POST["description"]);
$group_id = $_POST["group_id"];
$stmt = $mysqli->prepare("UPDATE services SET name=?, description=?, group_id=? WHERE id = ?");
$stmt->bind_param("ssii", $name, $description, $group_id, $service_id);
$stmt->execute();
$stmt->get_result();
header("Location: ".WEB_URL."/admin/?do=settings");

View File

@ -57,6 +57,14 @@ CREATE TABLE queue_notify (
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;
CREATE TABLE services_groups (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) DEFAULT NULL,
visibility tinyint(4) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE queue_task (
id int(11) NOT NULL AUTO_INCREMENT,
type_id int(11) NOT NULL,
@ -128,3 +136,7 @@ ALTER TABLE `subscribers` ADD COLUMN update_time int(11) DEFAULT NULL;
ALTER TABLE `subscribers` DROP INDEX telegramID; # was UNIQUE (telegramID)
ALTER TABLE `subscribers` ADD UNIQUE userID (userID);
COMMIT;
ALTER TABLE services ADD COLUMN description varchar(200) COLLATE utf8_czech_ci NOT NULL;
ALTER TABLE services ADD COLUMN group_id int(11) DEFAULT NULL;
COMMIT;

View File

@ -7,6 +7,7 @@ $icons = array("fa fa-times", "fa fa-exclamation", "fa fa-info", "fa fa-check" )
$some = array(_("Some systems are experiencing major outages"), _("Some systems are experiencing minor outages"), _("Some systems are under maintenance"));
$all = array(_("Our systems are experiencing major outages."), _("Our systems are experiencing minor outages"), _("Our systems are under maintenance"), _("All systems operational"));
$permissions = array(_("Super admin"), _("Admin"), _("Editor"));
$visibility = array(_("Collapsed"), _("Expanded"), _("Expand on events"));
/**
* Class that encapsulates methods to render header and footer