This commit is contained in:
Daniel Maya 2019-06-18 17:28:36 +02:00
parent 589000fb6c
commit 649689b1a1
5 changed files with 198 additions and 21 deletions

View File

@ -55,11 +55,9 @@ if ($getVisualConsole === true) {
$item_data = [];
$item_data['id'] = $visualConsoleItemId;
$item_data['id_layout'] = $visualConsoleId;
$item_data['type'] = $data['type'];
$item = $class::fromDB($item_data);
$item->save($item->toArray(), $data);
$item->save($data);
}
exit;

View File

@ -68,7 +68,7 @@ abstract class Model
*
* @abstract
*/
abstract public function save(array $data=[], array $newdata=[]);
abstract public function save(array $data=[]);
/**
@ -86,6 +86,12 @@ abstract class Model
}
public function setData(array $data)
{
$this->data = $data;
}
/**
* Instance the class with the unknown input data.
*

View File

@ -117,7 +117,7 @@ final class Container extends Model
*
* @overrides Model::save.
*/
public function save(array $data=[], array $newdata=[]): bool
public function save(array $data=[]): bool
{
return true;
}

View File

@ -1627,33 +1627,38 @@ class Item extends CachedModel
*
* @overrides Model::save.
*/
public function save(array $data=[], array $newdata=[])
public function save(array $data=[]): bool
{
$save = self::encode($data);
$newSave = self::encode($newdata);
if (empty($data)) {
return false;
}
$save = \array_merge($save, $newSave);
$dataModelEncode = $this->encode($this->toArray());
$dataEncode = $this->encode($data);
hd(self::toArray(), true);
$save = \array_merge($dataModelEncode, $dataEncode);
if (!empty($save)) {
if (empty($save['id'])) {
// Insert.
$result = \db_process_sql_insert('tlayout_data', $save);
if ($result) {
$item = static::fromDB(['id' => $result]);
}
} else {
// Update.
$result = \db_process_sql_update('tlayout_data', $save, ['id' => $save['id']]);
if ($result) {
$item = static::fromDB(['id' => $save['id']]);
// Update the model.
if (!empty($item)) {
$this->setData($item->toArray());
}
}
}
}
// Update the model.
if ($result) {
if (empty($save['id'])) {
$item = static::fromDB(['id' => $result]);
} else {
$item = static::fromDB(['id' => $save['id']]);
}
}
return $item;
return (bool) $result;
}

View File

@ -218,10 +218,150 @@ final class Line extends Model
protected function encode(array $data): array
{
$result = [];
$id = static::getId($data);
if ($id) {
$result['id'] = $id;
}
$id_layout = static::getIdLayout($data);
if ($id_layout) {
$result['id_layout'] = $id_layout;
}
$pos_x = static::parseIntOr(
static::issetInArray($data, ['x', 'pos_x', 'posX']),
null
);
if ($pos_x !== null) {
$result['pos_x'] = $pos_x;
}
$pos_y = static::parseIntOr(
static::issetInArray($data, ['y', 'pos_y', 'posY']),
null
);
if ($pos_y !== null) {
$result['pos_y'] = $pos_y;
}
$height = static::getHeight($data);
if ($height !== null) {
$result['height'] = $height;
}
$width = static::getWidth($data);
if ($width !== null) {
$result['width'] = $width;
}
$type = static::parseIntOr(
static::issetInArray($data, ['type']),
null
);
if ($type !== null) {
$result['type'] = $type;
}
$border_width = static::getBorderWidth($data);
if ($border_width !== null) {
$result['border_width'] = $border_width;
}
$border_color = static::extractBorderColor($data);
if ($border_color !== null) {
$result['border_color'] = $border_color;
}
$show_on_top = static::issetInArray($data, ['isOnTop', 'show_on_top', 'showOnTop']);
if ($show_on_top !== null) {
$result['show_on_top'] = static::parseBool($show_on_top);
}
return $result;
}
/**
* Extract item id.
*
* @param array $data Unknown input data structure.
*
* @return integer Item id. 0 by default.
*/
private static function getId(array $data): int
{
return static::parseIntOr(
static::issetInArray($data, ['id', 'itemId']),
0
);
}
/**
* Extract layout id.
*
* @param array $data Unknown input data structure.
*
* @return integer Item id. 0 by default.
*/
private static function getIdLayout(array $data): int
{
return static::parseIntOr(
static::issetInArray($data, ['id_layout', 'idLayout', 'layoutId']),
0
);
}
/**
* Extract item width.
*
* @param array $data Unknown input data structure.
*
* @return integer Item width. 0 by default.
*/
private static function getWidth(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['width', 'endX']),
null
);
}
/**
* Extract item height.
*
* @param array $data Unknown input data structure.
*
* @return integer Item height. 0 by default.
*/
private static function getHeight(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['height', 'endY']),
null
);
}
/**
* Extract a border width value.
*
* @param array $data Unknown input data structure.
*
* @return integer Valid border width.
*/
private static function getBorderWidth(array $data)
{
return static::parseIntOr(
static::issetInArray($data, ['border_width', 'borderWidth']),
null
);
}
/**
* Insert or update an item in the database
*
@ -231,9 +371,37 @@ final class Line extends Model
*
* @overrides Model::save.
*/
public function save(array $data=[], array $newdata=[]): bool
public function save(array $data=[]): bool
{
return true;
$data_model = $this->encode($this->toArray());
$newData = $this->encode($data);
$save = \array_merge($data_model, $newData);
if (!empty($save)) {
if (empty($save['id'])) {
// Insert.
$result = \db_process_sql_insert('tlayout_data', $save);
} else {
// Update.
$result = \db_process_sql_update('tlayout_data', $save, ['id' => $save['id']]);
}
}
// Update the model.
if ($result) {
if (empty($save['id'])) {
$item = static::fromDB(['id' => $result]);
} else {
$item = static::fromDB(['id' => $save['id']]);
}
if (!empty($item)) {
$this->setData($item->toArray());
}
}
return (bool) $result;
}