2019-03-18 12:47:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Models\VisualConsole;
|
|
|
|
use Models\Model;
|
2019-04-08 12:37:25 +02:00
|
|
|
// Enterprise item. It may not exist.
|
|
|
|
use Enterprise\Models\VisualConsole\Items\Service;
|
2019-03-18 12:47:39 +01:00
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Model of a Visual Console.
|
|
|
|
*/
|
2019-03-18 12:47:39 +01:00
|
|
|
final class Container extends Model
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Validate the received data structure to ensure if we can extract the
|
|
|
|
* values required to build the model.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @param array $data Input data.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return void
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @throws \InvalidArgumentException If any input value is considered
|
|
|
|
* invalid.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @overrides Model::validateData.
|
2019-03-21 17:47:10 +01:00
|
|
|
*/
|
2019-03-18 13:35:30 +01:00
|
|
|
protected function validateData(array $data): void
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
|
|
|
if (isset($data['id']) === false
|
|
|
|
|| \is_numeric($data['id']) === false
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the Id property is required and should be integer'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data['name']) === false
|
|
|
|
|| \is_string($data['name']) === false
|
|
|
|
|| empty($data['name']) === true
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the name property is required and should be string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data['width']) === false
|
|
|
|
|| \is_numeric($data['width']) === false
|
|
|
|
|| $data['width'] <= 0
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the width property is required and should greater than 0'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($data['height']) === false
|
|
|
|
|| \is_numeric($data['height']) === false
|
|
|
|
|| $data['height'] <= 0
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the height property is required and should greater than 0'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extractGroupId($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-21 17:47:10 +01:00
|
|
|
/**
|
2019-03-25 15:19:13 +01:00
|
|
|
* Returns a valid representation of the model.
|
|
|
|
*
|
|
|
|
* @param array $data Input data.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @return array Data structure representing the model.
|
2019-03-21 17:47:10 +01:00
|
|
|
*
|
2019-03-25 15:19:13 +01:00
|
|
|
* @overrides Model::decode.
|
2019-03-21 17:47:10 +01:00
|
|
|
*/
|
2019-03-18 13:35:30 +01:00
|
|
|
protected function decode(array $data): array
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => (int) $data['id'],
|
|
|
|
'name' => $data['name'],
|
|
|
|
'groupId' => $this->extractGroupId($data),
|
2019-04-12 09:14:44 +02:00
|
|
|
'backgroundImage' => $this->extractBackgroundImage($data),
|
2019-03-18 12:47:39 +01:00
|
|
|
'backgroundColor' => $this->extractBackgroundColor($data),
|
2019-03-19 12:03:02 +01:00
|
|
|
'isFavorite' => $this->extractFavorite($data),
|
2019-03-18 12:47:39 +01:00
|
|
|
'width' => (int) $data['width'],
|
|
|
|
'height' => (int) $data['height'],
|
2019-04-12 09:14:44 +02:00
|
|
|
'backgroundURL' => $this->extractBackgroundUrl($data),
|
2019-03-18 12:47:39 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Extract a group Id value.
|
|
|
|
*
|
|
|
|
* @param array $data Unknown input data structure.
|
|
|
|
*
|
|
|
|
* @return integer Valid identifier of a group.
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When a valid group Id can't be found.
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractGroupId(array $data): int
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
2019-03-25 15:19:13 +01:00
|
|
|
$groupId = static::parseIntOr(
|
|
|
|
static::issetInArray($data, ['id_group', 'groupId']),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($groupId === null || $groupId < 0) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'the group Id property is required and should be integer'
|
|
|
|
);
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
return $groupId;
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-12 09:14:44 +02:00
|
|
|
/**
|
|
|
|
* Extract a image name value.
|
|
|
|
*
|
|
|
|
* @param array $data Unknown input data structure.
|
|
|
|
*
|
|
|
|
* @return mixed String representing the image name (not empty) or null.
|
|
|
|
*/
|
|
|
|
private function extractBackgroundImage(array $data)
|
|
|
|
{
|
|
|
|
$backgroundImage = static::notEmptyStringOr(
|
|
|
|
static::issetInArray($data, ['background', 'backgroundURL']),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
return ($backgroundImage === 'None.png') ? null : $backgroundImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Extract a image url value.
|
|
|
|
*
|
|
|
|
* @param array $data Unknown input data structure.
|
|
|
|
*
|
|
|
|
* @return mixed String representing the image url (not empty) or null.
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractBackgroundUrl(array $data)
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
2019-04-12 09:51:26 +02:00
|
|
|
return static::notEmptyStringOr(
|
2019-04-12 09:14:44 +02:00
|
|
|
static::issetInArray($data, ['backgroundURL']),
|
2019-03-19 12:03:02 +01:00
|
|
|
null
|
|
|
|
);
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Extract a background color value.
|
|
|
|
*
|
|
|
|
* @param array $data Unknown input data structure.
|
|
|
|
*
|
|
|
|
* @return mixed String representing the color (not empty) or null.
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractBackgroundColor(array $data)
|
2019-03-18 12:47:39 +01:00
|
|
|
{
|
2019-03-25 15:19:13 +01:00
|
|
|
return static::notEmptyStringOr(
|
2019-03-25 16:36:38 +01:00
|
|
|
static::issetInArray(
|
|
|
|
$data,
|
|
|
|
[
|
|
|
|
'backgroundColor',
|
|
|
|
'background_color',
|
|
|
|
]
|
|
|
|
),
|
2019-03-19 12:03:02 +01:00
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
2019-03-18 12:47:39 +01:00
|
|
|
|
|
|
|
|
2019-03-25 15:19:13 +01:00
|
|
|
/**
|
|
|
|
* Extract the "is favorite" switch value.
|
|
|
|
*
|
|
|
|
* @param array $data Unknown input data structure.
|
|
|
|
*
|
|
|
|
* @return boolean If the item is favorite or not.
|
|
|
|
*/
|
2019-03-19 12:03:02 +01:00
|
|
|
private function extractFavorite(array $data): bool
|
|
|
|
{
|
2019-03-25 15:19:13 +01:00
|
|
|
return static::parseBool(
|
2019-03-25 16:36:38 +01:00
|
|
|
static::issetInArray($data, ['is_favourite', 'isFavorite'])
|
2019-03-19 12:03:02 +01:00
|
|
|
);
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-25 12:32:06 +01:00
|
|
|
/**
|
|
|
|
* Obtain a container data structure from the database using a filter.
|
|
|
|
*
|
|
|
|
* @param array $filter Filter of the Visual Console.
|
|
|
|
*
|
|
|
|
* @return self A Visual Console Container instance.
|
|
|
|
* @throws \Exception When the data cannot be retrieved from the DB.
|
|
|
|
*
|
|
|
|
* @override Model::fetchDataFromDB.
|
|
|
|
*/
|
2019-03-26 18:46:48 +01:00
|
|
|
protected static function fetchDataFromDB(array $filter)
|
2019-03-25 12:32:06 +01:00
|
|
|
{
|
|
|
|
// Due to this DB call, this function cannot be unit tested without
|
|
|
|
// a proper mock.
|
|
|
|
$row = \db_get_row_filter('tlayout', $filter);
|
|
|
|
|
|
|
|
if ($row === false) {
|
|
|
|
throw new \Exception('error fetching the data from the DB');
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:51:26 +02:00
|
|
|
$backgroundUrl = static::extractBackgroundUrl($row);
|
|
|
|
$backgroundImage = static::extractBackgroundImage($row);
|
|
|
|
|
|
|
|
if ($backgroundUrl === null && $backgroundImage !== null) {
|
|
|
|
$backgroundUrl = ui_get_full_url(
|
|
|
|
'images/console/background/'.$backgroundImage
|
|
|
|
);
|
|
|
|
|
|
|
|
$width = (int) $row['width'];
|
|
|
|
$height = (int) $row['height'];
|
|
|
|
|
|
|
|
if ($width > 0 && $height > 0) {
|
|
|
|
$q = [
|
|
|
|
'getFile' => 1,
|
|
|
|
'thumb' => 1,
|
|
|
|
'thumb_size' => $width.'x'.$height,
|
|
|
|
'file' => $backgroundUrl,
|
|
|
|
];
|
|
|
|
$row['backgroundURL'] = ui_get_full_url(
|
|
|
|
'include/Image/image_functions.php?'.http_build_query($q)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$row['backgroundURL'] = $backgroundUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:46:48 +01:00
|
|
|
return $row;
|
2019-03-25 12:32:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-03 10:08:01 +02:00
|
|
|
/**
|
2019-04-08 10:30:45 +02:00
|
|
|
* Obtain a item's class.
|
2019-04-03 10:08:01 +02:00
|
|
|
*
|
2019-04-08 10:30:45 +02:00
|
|
|
* @param integer $type Type of the item of the Visual Console.
|
2019-04-03 10:08:01 +02:00
|
|
|
*
|
2019-04-08 10:30:45 +02:00
|
|
|
* @return mixed A reference to the item's class.
|
2019-04-03 10:08:01 +02:00
|
|
|
*/
|
2019-04-08 10:30:45 +02:00
|
|
|
public static function getItemClass(int $type)
|
2019-04-03 10:08:01 +02:00
|
|
|
{
|
2019-04-08 10:30:45 +02:00
|
|
|
switch ($type) {
|
|
|
|
case STATIC_GRAPH:
|
|
|
|
return Items\StaticGraph::class;
|
|
|
|
|
|
|
|
case MODULE_GRAPH:
|
|
|
|
return Items\ModuleGraph::class;
|
|
|
|
|
|
|
|
case SIMPLE_VALUE:
|
|
|
|
case SIMPLE_VALUE_MAX:
|
|
|
|
case SIMPLE_VALUE_MIN:
|
|
|
|
case SIMPLE_VALUE_AVG:
|
|
|
|
return Items\SimpleValue::class;
|
|
|
|
|
|
|
|
case PERCENTILE_BAR:
|
|
|
|
case PERCENTILE_BUBBLE:
|
|
|
|
case CIRCULAR_PROGRESS_BAR:
|
|
|
|
case CIRCULAR_INTERIOR_PROGRESS_BAR:
|
|
|
|
return Items\Percentile::class;
|
|
|
|
|
|
|
|
case LABEL:
|
|
|
|
return Items\Label::class;
|
|
|
|
|
|
|
|
case ICON:
|
|
|
|
return Items\Icon::class;
|
|
|
|
|
2019-04-08 12:37:25 +02:00
|
|
|
// Enterprise item.
|
2019-04-08 17:16:05 +02:00
|
|
|
case SERVICE:
|
|
|
|
return \class_exists('Service') ? Service::class : Item::class;
|
2019-04-08 10:30:45 +02:00
|
|
|
|
|
|
|
case GROUP_ITEM:
|
2019-04-08 17:16:05 +02:00
|
|
|
return Items\Group::class;
|
2019-04-08 10:30:45 +02:00
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case BOX_ITEM:
|
2019-04-08 10:30:45 +02:00
|
|
|
return Items\Box::class;
|
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case LINE_ITEM:
|
2019-04-08 10:30:45 +02:00
|
|
|
return Items\Line::class;
|
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case AUTO_SLA_GRAPH:
|
2019-04-08 10:30:45 +02:00
|
|
|
return Items\EventsHistory::class;
|
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case DONUT_GRAPH:
|
2019-04-08 16:21:26 +02:00
|
|
|
return Items\DonutGraph::class;
|
2019-04-08 10:30:45 +02:00
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case BARS_GRAPH:
|
|
|
|
return Items\BarsGraph::class;
|
2019-04-08 10:30:45 +02:00
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case CLOCK:
|
2019-04-08 10:30:45 +02:00
|
|
|
return Items\Clock::class;
|
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
case COLOR_CLOUD:
|
2019-04-08 10:30:45 +02:00
|
|
|
return Items\ColorCloud::class;
|
|
|
|
|
2019-04-08 17:16:05 +02:00
|
|
|
default:
|
2019-04-08 10:30:45 +02:00
|
|
|
return Item::class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a list of items which belong to the Visual Console.
|
|
|
|
*
|
|
|
|
* @param integer $layoutId Identifier of the Visual Console.
|
|
|
|
*
|
|
|
|
* @return array A list of items.
|
|
|
|
* @throws \Exception When the data cannot be retrieved from the DB.
|
|
|
|
*/
|
|
|
|
public static function getItemsFromDB(int $layoutId): array
|
|
|
|
{
|
|
|
|
$filter = ['id_layout' => $layoutId];
|
|
|
|
$fields = [
|
|
|
|
'id',
|
|
|
|
'type',
|
|
|
|
];
|
|
|
|
$rows = \db_get_all_rows_filter('tlayout_data', $filter, $fields);
|
|
|
|
|
|
|
|
if ($rows === false) {
|
|
|
|
$rows = [];
|
2019-04-08 17:16:05 +02:00
|
|
|
// TODO: throw new \Exception('error fetching the data from the DB');.
|
2019-04-08 10:30:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($rows as $data) {
|
|
|
|
$itemId = (int) $data['id'];
|
|
|
|
$itemType = (int) $data['type'];
|
|
|
|
$class = static::getItemClass($itemType);
|
2019-04-09 16:35:58 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
\array_push($items, $class::fromDBWithId($itemId));
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// TODO: Log this?
|
|
|
|
}
|
2019-04-03 10:08:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 10:30:45 +02:00
|
|
|
return $items;
|
2019-04-03 10:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-18 12:47:39 +01:00
|
|
|
}
|