php8 visualconsoles review, warning! class and object missunderstanding here...

This commit is contained in:
fbsanchez 2022-01-12 20:46:19 +01:00
parent 8e5a0917ba
commit a58c6862e1
5 changed files with 66 additions and 23 deletions

View File

@ -56,7 +56,7 @@ abstract class Model
* *
* @abstract * @abstract
*/ */
abstract protected function encode(array $data): array; abstract static protected function encode(array $data): array;
/** /**
@ -98,6 +98,13 @@ abstract class Model
} }
/**
* Set data.
*
* @param array $data Data.
*
* @return void
*/
public function setData(array $data) public function setData(array $data)
{ {
$this->data = $data; $this->data = $data;
@ -122,6 +129,8 @@ abstract class Model
* Obtain a data structure from the database using a filter. * Obtain a data structure from the database using a filter.
* *
* @param array $filter Filter to retrieve the modeled element. * @param array $filter Filter to retrieve the modeled element.
* @param float|null $ratio Ratio.
* @param float|null $widthRatio Width ratio.
* *
* @return array The modeled element data structure stored into the DB. * @return array The modeled element data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB. * @throws \Exception When the data cannot be retrieved from the DB.
@ -139,6 +148,8 @@ abstract class Model
* Obtain a model's instance from the database using a filter. * Obtain a model's instance from the database using a filter.
* *
* @param array $filter Filter to retrieve the modeled element. * @param array $filter Filter to retrieve the modeled element.
* @param float|null $ratio Ratio.
* @param float|null $widthRatio Width ratio.
* *
* @return self A modeled element's instance. * @return self A modeled element's instance.
*/ */
@ -152,7 +163,7 @@ abstract class Model
/** /**
* JSON representation of the model. * JSON representation of the model.
* *
* @return string * @return array
*/ */
public function toArray(): array public function toArray(): array
{ {

View File

@ -103,7 +103,7 @@ final class Container extends Model
* *
* @overrides Model::encode. * @overrides Model::encode.
*/ */
protected function encode(array $data): array protected static function encode(array $data): array
{ {
$result = []; $result = [];
return $result; return $result;

View File

@ -757,6 +757,7 @@ class Item extends CachedModel
* *
* @param array $filter Filter of the Visual Console Item. * @param array $filter Filter of the Visual Console Item.
* @param float $ratio Ratio resize view. * @param float $ratio Ratio resize view.
* @param float|null $widthRatio Unknown.
* *
* @return array The Visual Console Item data structure stored into the DB. * @return array The Visual Console Item data structure stored into the DB.
* @throws \Exception When the data cannot be retrieved from the DB. * @throws \Exception When the data cannot be retrieved from the DB.
@ -1382,7 +1383,7 @@ class Item extends CachedModel
* *
* @overrides Model::encode. * @overrides Model::encode.
*/ */
protected function encode(array $data): array protected static function encode(array $data): array
{ {
$result = []; $result = [];
@ -1801,7 +1802,27 @@ class Item extends CachedModel
/** /**
* Insert or update an item in the database * Inserts a new item in the database
*
* @param array $data Unknown input data structure.
*
* @return boolean The modeled element data structure stored into the DB.
*
* @overrides Model::save.
*/
public static function create(array $data=[]): int
{
// Insert.
$save = static::encode($data);
$result = \db_process_sql_insert('tlayout_data', $save);
return $result;
}
/**
* Update an item in the database
* *
* @param array $data Unknown input data structure. * @param array $data Unknown input data structure.
* *
@ -1811,13 +1832,15 @@ class Item extends CachedModel
*/ */
public function save(array $data=[]): int public function save(array $data=[]): int
{ {
$data ??= $this->toArray();
if (empty($data) === false) { if (empty($data) === false) {
if (empty($data['id']) === true) { if (empty($data['id']) === true) {
// Insert. // Insert.
$save = static::encode($data); $save = $this->encode($data);
$result = \db_process_sql_insert('tlayout_data', $save); $result = \db_process_sql_insert('tlayout_data', $save);
if ($result) { if ($result !== false) {
$item = static::fromDB(['id' => $result]); $item = static::fromDB(['id' => $result]);
$item->setData($item->toArray()); $item->setData($item->toArray());
} }

View File

@ -135,6 +135,7 @@ final class StaticGraph extends Item
* *
* @param array $filter Filter of the Visual Console Item. * @param array $filter Filter of the Visual Console Item.
* @param float|null $ratio Ratio. * @param float|null $ratio Ratio.
* @param float|null $widthRatio Width ratio.
* *
* @return array The Visual Console Item data structure stored into the DB. * @return array The Visual Console Item data structure stored into the DB.
* @throws \InvalidArgumentException When an agent Id cannot be found. * @throws \InvalidArgumentException When an agent Id cannot be found.
@ -160,7 +161,7 @@ final class StaticGraph extends Item
include_once $config['homedir'].'/include/functions_io.php'; include_once $config['homedir'].'/include/functions_io.php';
include_once $config['homedir'].'/include/functions_visual_map.php'; include_once $config['homedir'].'/include/functions_visual_map.php';
include_once $config['homedir'].'/include/functions_modules.php'; include_once $config['homedir'].'/include/functions_modules.php';
if (is_metaconsole()) { if (is_metaconsole() === true) {
\enterprise_include_once('include/functions_metaconsole.php'); \enterprise_include_once('include/functions_metaconsole.php');
} }
@ -231,7 +232,7 @@ final class StaticGraph extends Item
$width = (int) $data['width']; $width = (int) $data['width'];
$height = (int) $data['height']; $height = (int) $data['height'];
if ($width === 0 || $height === 0) { if ($width === 0 || $height === 0) {
if (isset($imagePath) && $imagePath !== false) { if (isset($imagePath) === true && $imagePath !== false) {
$sizeImage = getimagesize($config['homedir'].'/'.$imagePath); $sizeImage = getimagesize($config['homedir'].'/'.$imagePath);
if ($ratio != 0) { if ($ratio != 0) {
$data['width'] = ($sizeImage[0] * $ratio); $data['width'] = ($sizeImage[0] * $ratio);
@ -271,7 +272,9 @@ final class StaticGraph extends Item
|| ($isBooleanModule && $showLastValueTooltip !== 'default') || ($isBooleanModule && $showLastValueTooltip !== 'default')
) { ) {
if (\is_numeric($value)) { if (\is_numeric($value)) {
$imgTitle .= __('Last value: ').\remove_right_zeros(\number_format((float) $value, (int) $config['graph_precision'])); $imgTitle .= __('Last value: ').\remove_right_zeros(
\number_format((float) $value, (int) $config['graph_precision'])
);
} else { } else {
$imgTitle .= __('Last value: ').$value; $imgTitle .= __('Last value: ').$value;
} }
@ -300,7 +303,7 @@ final class StaticGraph extends Item
* *
* @return array Of inputs. * @return array Of inputs.
* *
* @throws Exception On error. * @throws \Exception On error.
*/ */
public static function getFormInputs(array $values): array public static function getFormInputs(array $values): array
{ {
@ -311,7 +314,7 @@ final class StaticGraph extends Item
$inputs = Item::getFormInputs($values); $inputs = Item::getFormInputs($values);
if (is_array($inputs) !== true) { if (is_array($inputs) !== true) {
throw new Exception( throw new \Exception(
'[StaticGraph]::getFormInputs parent class return is not an array' '[StaticGraph]::getFormInputs parent class return is not an array'
); );
} }

View File

@ -30,7 +30,7 @@
namespace Models\VisualConsole; namespace Models\VisualConsole;
use Models\VisualConsole\Container as VisualConsole; use Models\VisualConsole\Container as VisualConsole;
define('__DEBUG', 0); define('__DEBUG', 1);
global $config; global $config;
require_once $config['homedir'].'/include/class/HTML.class.php'; require_once $config['homedir'].'/include/class/HTML.class.php';
@ -262,6 +262,9 @@ class View extends \HTML
public function processForm() public function processForm()
{ {
global $config; global $config;
$result = null;
// Inserted data in new item. // Inserted data in new item.
$vCId = \get_parameter('vCId', 0); $vCId = \get_parameter('vCId', 0);
$type = \get_parameter('type', null); $type = \get_parameter('type', null);
@ -552,11 +555,12 @@ class View extends \HTML
try { try {
// Save the new item. // Save the new item.
$data['id_layout'] = $vCId; $data['id_layout'] = $vCId;
$itemId = $class::save($data); $itemId = $class::create($data);
} catch (\Exception $e) { } catch (\Exception $e) {
// Bad params. // Bad params.
echo $e->getMessage();
if (__DEBUG === 1) { if (__DEBUG === 1) {
error_log($e->getMessage()); echo '<pre>'.$e->getTraceAsString().'</pre>';
} }
http_response_code(400); http_response_code(400);
@ -569,8 +573,9 @@ class View extends \HTML
$result = $item->toArray(); $result = $item->toArray();
} catch (\Exception $e) { } catch (\Exception $e) {
// Bad params. // Bad params.
echo $e->getMessage();
if (__DEBUG === 1) { if (__DEBUG === 1) {
error_log($e->getMessage()); echo '<pre>'.$e->getTraceAsString().'</pre>';
} }
http_response_code(400); http_response_code(400);
@ -582,8 +587,9 @@ class View extends \HTML
$item = VisualConsole::getItemFromDB($itemId); $item = VisualConsole::getItemFromDB($itemId);
} catch (\Exception $e) { } catch (\Exception $e) {
// Bad params. // Bad params.
echo $e->getMessage();
if (__DEBUG === 1) { if (__DEBUG === 1) {
error_log($e->getMessage()); echo '<pre>'.$e->getTraceAsString().'</pre>';
} }
http_response_code(400); http_response_code(400);