2017-11-23 18:09:36 -05:00
< ? php
2021-03-18 13:26:03 -04:00
2017-11-23 18:09:36 -05:00
/**
2021-03-18 13:26:03 -04:00
* Class for managing services
*/
2018-04-13 15:31:03 -04:00
class Service implements JsonSerializable
2017-11-23 18:09:36 -05:00
{
private $id ;
private $name ;
2020-11-05 08:54:04 -05:00
private $description ;
private $group_name ;
2017-11-23 18:09:36 -05:00
private $status ;
2018-01-07 14:39:10 -05:00
/**
* Constructs service from its data .
* @ param int $id service ID
* @ param String $name service name
2020-11-05 08:54:04 -05:00
* @ param String $descriotion service description for tooltip
2018-01-07 14:39:10 -05:00
* @ param int $status current service status
*/
2021-03-18 13:26:03 -04:00
function __construct ( $id , $name , $description = null , $group_name = '' , $status = 3 )
2017-11-23 18:09:36 -05:00
{
2018-01-07 14:39:10 -05:00
//TODO: Maybe get data from ID?
2017-11-23 18:09:36 -05:00
$this -> id = $id ;
$this -> name = $name ;
2020-11-05 08:54:04 -05:00
$this -> description = $description ;
$this -> group_name = $group_name ;
2017-11-23 18:09:36 -05:00
$this -> status = $status ;
}
2018-01-07 14:39:10 -05:00
/**
* Returns status of this service
* @ return int status
*/
2017-11-23 18:09:36 -05:00
public function get_status ()
{
return $this -> status ;
}
2018-01-07 14:39:10 -05:00
/**
* Returns id of this service
* @ return int id
*/
2017-11-23 18:09:36 -05:00
public function get_id ()
{
return $this -> id ;
}
2018-01-07 14:39:10 -05:00
/**
* Returns name of this service
* @ return String name
*/
2017-11-23 18:09:36 -05:00
public function get_name ()
{
return $this -> name ;
}
2020-10-14 13:54:32 -04:00
/**
2020-11-05 08:54:04 -05:00
* Returns description of this service
* @ return String description
*/
public function get_description ()
{
return $this -> description ;
}
/**
* Processes submitted form and adds service unless problem is encountered ,
2018-01-18 16:59:34 -05:00
* calling this is possible only for admin or higher rank . Also checks requirements
2018-01-07 14:39:10 -05:00
* for char limits .
* @ return void
*/
2017-11-23 18:09:36 -05:00
public static function add ()
{
global $user , $message ;
2021-03-18 13:26:03 -04:00
if ( strlen ( $_POST [ 'service' ]) > 50 ) {
2017-12-30 18:41:58 -05:00
$message = _ ( " Service name is too long! Character limit is 50 " );
2017-11-23 18:09:36 -05:00
return ;
2021-03-18 13:26:03 -04:00
} else if ( strlen ( trim ( $_POST [ 'service' ])) == 0 ) {
2017-12-30 18:41:58 -05:00
$message = _ ( " Please enter name! " );
2017-11-23 18:09:36 -05:00
return ;
}
2021-03-18 13:26:03 -04:00
if ( $user -> get_rank () <= 1 ) {
2017-11-23 18:09:36 -05:00
global $mysqli ;
2020-11-05 08:54:04 -05:00
$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 );
2017-11-23 18:09:36 -05:00
$stmt -> execute ();
2018-04-20 10:40:12 -04:00
$stmt -> get_result ();
2021-03-18 13:26:03 -04:00
header ( " Location: " . WEB_URL . " /admin/?do=settings " );
} else {
2017-12-30 18:41:58 -05:00
$message = _ ( " You don't have the permission to do that! " );
2017-11-23 18:09:36 -05:00
}
}
2020-11-05 08:54:04 -05:00
/**
2021-03-18 13:26:03 -04:00
* 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 ;
}
2020-11-05 08:54:04 -05:00
2021-03-18 13:26:03 -04:00
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 " );
} else {
$message = _ ( " You don't have the permission to do that! " );
2020-11-05 08:54:04 -05:00
}
2021-03-18 13:26:03 -04:00
}
2017-11-23 18:09:36 -05:00
2018-01-07 14:39:10 -05:00
/**
* Deletes this service - first checks if user has permission to do that .
* @ return void
*/
2017-11-23 18:09:36 -05:00
public static function delete ()
{
2018-04-20 10:40:12 -04:00
global $user , $message ;
2021-03-18 13:26:03 -04:00
if ( $user -> get_rank () <= 1 ) {
2017-11-23 18:09:36 -05:00
global $mysqli ;
$id = $_GET [ 'delete' ];
2018-07-10 08:33:37 -04:00
$stmt = $mysqli -> prepare ( " SELECT status_id as status, (SELECT count(*) FROM services_status as s WHERE s.status_id=status) as count FROM services_status WHERE service_id = ? GROUP BY status " );
2017-11-23 18:09:36 -05:00
$stmt -> bind_param ( " i " , $id );
$stmt -> execute ();
$query = $stmt -> get_result ();
while ( $res = $query -> fetch_assoc ()) {
2021-03-18 13:26:03 -04:00
if ( $res [ 'count' ] == 1 ) {
2017-11-23 18:09:36 -05:00
Incident :: delete ( $res [ 'status' ]);
}
}
$stmt = $mysqli -> prepare ( " DELETE FROM services WHERE id = ? " );
$stmt -> bind_param ( " i " , $id );
$stmt -> execute ();
$query = $stmt -> get_result ();
$stmt = $mysqli -> prepare ( " DELETE FROM services_status WHERE service_id = ? " );
$stmt -> bind_param ( " i " , $id );
$stmt -> execute ();
$query = $stmt -> get_result ();
2021-03-18 13:26:03 -04:00
header ( " Location: " . WEB_URL . " /admin/?do=settings " );
} else {
2017-12-30 18:41:58 -05:00
$message = _ ( " You don't have the permission to do that! " );
2017-11-23 18:09:36 -05:00
}
}
2018-01-07 14:39:10 -05:00
/**
* Renders current status for services from passed array of services .
* @ param Service [] $array array of services
* @ return void
*/
2021-03-18 13:26:03 -04:00
public static function current_status ( $array )
{
2017-11-23 18:09:36 -05:00
global $all , $some , $classes ;
2021-03-18 13:26:03 -04:00
$statuses = array ( 0 , 0 , 0 , 0 );
2017-11-23 18:09:36 -05:00
$worst = 5 ;
2018-01-12 15:35:31 -05:00
2017-11-23 18:09:36 -05:00
foreach ( $array as $service ) {
2021-03-18 13:26:03 -04:00
if ( $service -> status < $worst ) {
2017-11-23 18:09:36 -05:00
$worst = $service -> get_status ();
}
2020-11-05 08:54:04 -05:00
$statuses [ $service -> get_status ()] ++ ;
2017-11-23 18:09:36 -05:00
}
2021-03-18 13:26:03 -04:00
echo '<div id="status-big" class="alert-' . $classes [ $worst ] . '">' ;
2020-11-05 08:54:04 -05:00
2021-03-18 13:26:03 -04:00
if ( $statuses [ $worst ] == count ( $array )) {
2020-11-05 08:54:04 -05:00
echo $all [ $worst ];
2021-03-18 13:26:03 -04:00
} else {
2020-11-05 08:54:04 -05:00
echo $some [ $worst ];
2017-11-23 18:09:36 -05:00
}
echo '</div>' ;
}
2018-01-07 14:39:10 -05:00
/**
* Renders this service .
2020-11-05 08:54:04 -05:00
* @ param $boolGroup set to true if the groups name is to be rendered
2018-01-07 14:39:10 -05:00
* @ return void
*/
2021-03-18 13:26:03 -04:00
public function render ()
{
2017-11-23 18:09:36 -05:00
global $statuses ;
global $classes ;
2020-11-05 08:54:04 -05:00
static $arrCompletedGroups = array ();
//static $boolClosed;
static $boolOpened ;
// Check if previous ul has been opened, and if a empty/new group is being
// render_header, close the UL first.
2021-03-18 13:26:03 -04:00
if ( $boolOpened ) {
if ( empty ( $this -> group_name ) || ! in_array ( $this -> group_name , $arrCompletedGroups )) {
2020-11-05 08:54:04 -05:00
echo '</ul>' ;
$boolOpened = false ;
}
}
// If no group exist or group is new, start a new UL
2021-03-18 13:26:03 -04:00
if ( ! empty ( $this -> group_name ) && ! in_array ( $this -> group_name , $arrCompletedGroups )) {
2020-11-05 08:54:04 -05:00
echo '<ul class="list-group components">' ;
//echo '<ul class="platforms list-group mb-2">';
// Render the group status if it exists
2021-03-18 13:26:03 -04:00
echo '<li class="list-group-item list-group-item-success group-name"><span><i class="fas fa-folder-open"></i></span> ' . $this -> group_name . '<div class="status ' . $classes [ $this -> status ] . '">' . _ ( $statuses [ $this -> status ]) . '</div></li>' ;
2020-11-05 08:54:04 -05:00
//echo '<li class="cist-group-item d-flex flex-row justify-content-between platform list-group-item-action py-0 expanded" role="button">' . $this->group_name .'<div class="status '. $classes[$this->status] .'"'. _($statuses[$this->status]).'</div></li>';
$arrCompletedGroups [] = $this -> group_name ;
$boolOpened = true ;
}
2021-03-18 13:26:03 -04:00
if ( empty ( $this -> group_name )) {
2020-11-05 08:54:04 -05:00
echo '<ul class="list-group components">' ;
2021-03-18 13:26:03 -04:00
// echo '<ul class="platforms list-group mb-2">';
2020-11-05 08:54:04 -05:00
$boolFinish = true ;
}
// Render the service status
2021-03-18 05:15:38 -04:00
echo '<div>' ;
2021-03-18 13:26:03 -04:00
echo '<li class="list-group-item sub-component"><strong>' . $this -> name . '</strong>' ;
2020-11-05 08:54:04 -05:00
//echo '<li class="list-group-item d-flex flex-columns justify-content-between><span>+</span><h3 class="py-2 my-0 flex-fill expanded">' . $this->name . '</h3>';
2021-03-18 13:26:03 -04:00
if ( ! empty ( $this -> description )) {
echo '<a class="desc-tool-tip" data-toggle="tooltip" data-placement="top" title="' . $this -> description . '"> <span><i class="fas fa-question"></i></span></a>' ;
2020-11-05 08:54:04 -05:00
}
2021-03-18 13:26:03 -04:00
if ( $this -> status != - 1 ) { ?> <div class="status <?php echo $classes[$this->status]; ?>"><?php echo _($statuses[$this->status]); ?></div>
< ? php
2020-11-05 08:54:04 -05:00
}
echo '</li>' ;
2021-03-18 13:26:03 -04:00
if ( isset ( $boolFinish ) && $boolFinish ) {
2020-11-05 08:54:04 -05:00
echo '</ul>' ;
}
2021-03-18 05:15:38 -04:00
echo '</div>' ;
2017-11-23 18:09:36 -05:00
}
2018-04-13 15:31:03 -04:00
2021-03-18 13:26:03 -04:00
public function jsonSerialize ()
{
2018-04-13 15:31:03 -04:00
global $statuses ;
return [
" id " => $this -> id ,
" name " => $this -> name ,
2020-11-05 08:54:04 -05:00
" description " => $this -> description ,
2018-04-13 15:31:03 -04:00
" status " => $this -> status ,
" status_string " => $statuses [ $this -> status ]
];
}
2018-11-05 17:57:54 -05:00
}