Skip to content

Commit

Permalink
Merge fb423e0 into cc32040
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel89fg committed Apr 24, 2024
2 parents cc32040 + fb423e0 commit 0f091e6
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Core/Lib/Widget/BaseWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ protected function assets()
protected function inputHtml($type = 'text', $extraClass = '')
{
$class = $this->combineClasses($this->css('form-control'), $this->class, $extraClass);
return '<input type="' . $type . '" name="' . $this->fieldname . '" value="' . $this->value
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
return '<input type="' . $type . '" ' . $name . ' value="' . $this->value
. '" class="' . $class . '"' . $this->inputHtmlExtraParams() . '/>';
}

Expand Down
5 changes: 3 additions & 2 deletions Core/Lib/Widget/WidgetAutocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ public function edit($model, $title = '', $description = '', $titleurl = '')
$descriptionHtml = empty($description) ? '' : '<small class="form-text text-muted">' . static::$i18n->trans($description) . '</small>';
$inputHtml = $this->inputHtml();
$labelHtml = '<label class="mb-0">' . $this->onclickHtml(static::$i18n->trans($title), $titleurl) . '</label>';
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';

if ('' === $this->value || null === $this->value) {
return '<input type="hidden" name="' . $this->fieldname . '" value="' . $this->value . '"/>'
return '<input type="hidden" ' . $name . ' value="' . $this->value . '"/>'
. '<div class="form-group mb-2">'
. $labelHtml
. '<div class="input-group">'
Expand All @@ -80,7 +81,7 @@ public function edit($model, $title = '', $description = '', $titleurl = '')
. '</div>';
}

return '<input type="hidden" name="' . $this->fieldname . '" value="' . $this->value . '"/>'
return '<input type="hidden" ' . $name . ' value="' . $this->value . '"/>'
. '<div class="form-group mb-2">'
. $labelHtml
. '<div class="input-group">'
Expand Down
3 changes: 2 additions & 1 deletion Core/Lib/Widget/WidgetCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public function edit($model, $title = '', $description = '', $titleurl = '')
$class = $this->combineClasses($this->css('form-check-input'), $this->class);
$readonly = $this->readonly() ? ' onclick="return false;"' : '';
$tabindex = $this->tabindex ? ' tabindex="' . $this->tabindex . '"' : '';
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';

$inputHtml = '<input type="checkbox" name="' . $this->fieldname . '" value="TRUE" id="' . $id
$inputHtml = '<input type="checkbox" ' . $name . ' value="TRUE" id="' . $id
. '" class="' . $class . '"' . $checked . $readonly . $tabindex . '/>';
$labelHtml = '<label for="' . $id . '">' . static::$i18n->trans($title) . '</label>';
$descriptionHtml = empty($description) ? '' :
Expand Down
3 changes: 2 additions & 1 deletion Core/Lib/Widget/WidgetDatalist.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ protected function inputHtml($type = 'datalist', $extraClass = '')
}

$list = $this->fieldname . '-list-' . $this->getUniqueId();
$html = '<input type="text" name="' . $this->fieldname . '" value="' . $this->value . '"'
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
$html = '<input type="text" ' . $name . ' value="' . $this->value . '"'
. ' class="' . $class . '"'
. ' list="' . $list . '"'
. $this->inputHtmlExtraParams()
Expand Down
3 changes: 2 additions & 1 deletion Core/Lib/Widget/WidgetDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ protected function inputHtml($type = 'date', $extraClass = '')
{
$class = $this->combineClasses($this->css('form-control'), $this->class, $extraClass);
$value = empty($this->value) ? '' : date('Y-m-d', strtotime($this->value));
return '<input type="' . $type . '" name="' . $this->fieldname . '" value="' . $value
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
return '<input type="' . $type . '" ' . $name . ' value="' . $value
. '" class="' . $class . '"' . $this->inputHtmlExtraParams() . '/>';
}

Expand Down
3 changes: 2 additions & 1 deletion Core/Lib/Widget/WidgetDatetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ protected function inputHtml($type = 'datetime-local', $extraClass = '')
{
$class = $this->combineClasses($this->css('form-control'), $this->class, $extraClass);
$value = empty($this->value) ? '' : date('Y-m-d H:i:s', strtotime($this->value));
return '<input type="' . $type . '" name="' . $this->fieldname . '" value="' . $value
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
return '<input type="' . $type . '" ' . $name . ' value="' . $value
. '" class="' . $class . '"' . $this->inputHtmlExtraParams() . '/>';
}

Expand Down
2 changes: 1 addition & 1 deletion Core/Lib/Widget/WidgetFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function edit($model, $title = '', $description = '', $titleurl = '')
$class = $this->combineClasses($this->css('form-control'), $this->class);
return '<div class="form-group mb-2">'
. '<label class="mb-0">' . $this->onclickHtml(static::$i18n->trans($title), $titleurl) . '</label>'
. '<input type="hidden" name="' . $this->fieldname . '" value="' . $this->value . '"/>'
. '<input type="hidden" value="' . $this->value . '"/>'
. '<input type="text" value="' . $this->show() . '" class="' . $class . '" readonly=""/>'
. '</div>';
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Lib/Widget/WidgetLibrary.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function edit($model, $title = '', $description = '', $titleurl = '')

if ($this->readonly()) {
return '<div class="form-group mb-2">'
. '<input type="hidden" id="' . $this->id . '" name="' . $this->fieldname . '" value="' . $this->value . '">'
. '<input type="hidden" id="' . $this->id . '" value="' . $this->value . '">'
. $labelHtml
. '<a href="' . $file->url() . '" class="btn btn-block btn-outline-secondary">'
. '<i class="' . $icon . ' fa-fw"></i> ' . ($file->filename ? $file->shortFileName() : Tools::lang()->trans('select'))
Expand Down
3 changes: 2 additions & 1 deletion Core/Lib/Widget/WidgetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function edit($model, $title = '', $description = '', $titleurl = ''): st
protected function inputHtml($type = 'password', $extraClass = '')
{
$class = $this->combineClasses($this->css('form-control'), $this->class, $extraClass);
return '<input type="text" name="' . $this->fieldname . '" value="' . $this->value
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
return '<input type="text" ' . $name . ' value="' . $this->value
. '" class="fs-psw ' . $class . '"' . $this->inputHtmlExtraParams() . '/>';
}

Expand Down
10 changes: 6 additions & 4 deletions Core/Lib/Widget/WidgetRadio.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ protected function inputHtml($type = 'radio', $extraClass = '')
$firstCss = $class != '' ? ' ml-3' : 'ml-3';
}

$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
$html .= '<div class="form-check ' . $class . $firstCss . '">'
. '<input type="radio" class="form-check-input" id="' . $this->fieldname . $cont . '" name="'
. $this->fieldname . '" value="' . $option['value'] . '" ' . $check . ' ' . $readOnly . '>'
. '<input type="radio" class="form-check-input" id="' . $this->fieldname . $cont . '" '
. $name . ' value="' . $option['value'] . '" ' . $check . ' ' . $readOnly . '>'
. '<label class="form-check-label" for="' . $this->fieldname . $cont . '">'
. $title
. '</label>'
Expand All @@ -289,9 +290,10 @@ protected function inputHtmlImages($type = 'radio', $extraClass = ''): string

$nameImg = str_replace(' ', '-', $title);
$url = FS_ROUTE . DIRECTORY_SEPARATOR . $this->imagesPath . DIRECTORY_SEPARATOR . $nameImg . '.png';
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
$html .= '<div class="widget-radio d-inline-block p-2">'
. '<input type="radio" id="' . $nameImg . '" class="d-none imgbgchk" name="' . $this->fieldname
. '" value="' . $option['value'] . '" ' . $check . ' ' . $readOnly . '>'
. '<input type="radio" id="' . $nameImg . '" class="d-none imgbgchk" '
. $name . ' value="' . $option['value'] . '" ' . $check . ' ' . $readOnly . '>'
. '<label class="mb-0" for="' . $nameImg . '">'
. '<img src="' . $url . '" />'
. '<div class="tick_container">'
Expand Down
2 changes: 1 addition & 1 deletion Core/Lib/Widget/WidgetSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected function inputHtml($type = 'text', $extraClass = '')
}

if ($this->readonly()) {
return '<input type="hidden" name="' . $this->fieldname . '" value="' . $this->value . '"/>'
return '<input type="hidden" value="' . $this->value . '"/>'
. '<input type="text" value="' . $this->show() . '" class="' . $class . '" readonly/>';
}

Expand Down
3 changes: 2 additions & 1 deletion Core/Lib/Widget/WidgetTextarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public function tableCell($model, $display = 'left')
protected function inputHtml($type = 'text', $extraClass = '')
{
$class = $this->combineClasses($this->css('form-control'), $this->class, $extraClass);
return '<textarea rows="' . $this->rows . '" name="' . $this->fieldname . '" class="' . $class . '"'
$name = $this->readonly() ? '' : 'name="' . $this->fieldname . '"';
return '<textarea rows="' . $this->rows . '" ' . $name . ' class="' . $class . '"'
. $this->inputHtmlExtraParams() . '>' . $this->value . '</textarea>';
}
}
2 changes: 1 addition & 1 deletion Core/Lib/Widget/WidgetVariante.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function edit($model, $title = '', $description = '', $titleurl = '')

if ($this->readonly()) {
return '<div class="form-group mb-2">'
. '<input type="hidden" id="' . $this->id . '" name="' . $this->fieldname . '" value="' . $this->value . '">'
. '<input type="hidden" id="' . $this->id . '" value="' . $this->value . '">'
. $labelHtml
. '<a href="' . $variante->url() . '" class="btn btn-block btn-outline-secondary">'
. '<i class="' . $icon . ' fa-fw"></i> ' . ($variante->referencia ?? Tools::lang()->trans('select'))
Expand Down
7 changes: 6 additions & 1 deletion Core/Model/Base/ModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace FacturaScripts\Core\Model\Base;

use Exception;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Cache;
use FacturaScripts\Core\Tools;
Expand Down Expand Up @@ -314,7 +315,11 @@ public function test(): bool
$return = true;
foreach ($fields as $key => $value) {
if ($key == static::primaryColumn()) {
$this->{$key} = empty($this->{$key}) ? null : $this->{$key};
try {
$this->{$key} = empty($this->{$key}) ? null : $this->{$key};
} catch (Exception $e) {
throw new Exception('Error test property' . $key . ' in model ' . $this->modelClassName(), 0, $e);
}
} elseif (null === $value['default'] && $value['is_nullable'] === 'NO' && $this->{$key} === null) {
Tools::log()->warning('field-can-not-be-null', ['%fieldName%' => $key, '%tableName%' => static::tableName()]);
$return = false;
Expand Down
56 changes: 54 additions & 2 deletions Core/Model/Base/ModelCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@

namespace FacturaScripts\Core\Model\Base;

use Exception;
use FacturaScripts\Core\Base\DataBase;
use FacturaScripts\Core\Base\DataBase\DataBaseTools;
use FacturaScripts\Core\Base\ToolBox;
use FacturaScripts\Core\Cache;
use FacturaScripts\Core\DbQuery;
use FacturaScripts\Core\KernelException;
use FacturaScripts\Core\Lib\Import\CSVImport;
use FacturaScripts\Core\Tools;

Expand Down Expand Up @@ -183,13 +185,63 @@ public function changePrimaryColumnValue($newValue): bool
*/
public function clear()
{
foreach (array_keys($this->getModelFields()) as $fieldName) {
$this->{$fieldName} = null;
foreach ($this->getModelFields() as $key => $value) {
try {
$this->{$key} = $this->getClearValue($value);
} catch (Exception $e) {
throw new Exception('Error clearing property' . $key . ' in model ' . $this->modelClassName(), 0, $e);
}
}

$this->pipe('clear');
}

public function getClearValue(array $value)
{
// si la columna es la clave primaria, la ponemos a null
if ($value['name'] === static::primaryColumn()) {
return null;
}

$type = strpos($value['type'], '(') === false ?
$value['type'] :
substr($value['type'], 0, strpos($value['type'], '('));

// si es nullable y no es booleano, devolvemos null
if ($value['is_nullable'] === 'YES' && false === in_array($type, ['tinyint', 'boolean'])) {
return null;
}

switch ($type) {
case 'tinyint':
case 'boolean':
return false;

case 'integer':
case 'int':
return 0;

case 'decimal':
case 'double':
case 'double precision':
case 'float':
return 0.0;

case 'date':
return date(self::DATE_STYLE);

case 'datetime':
case 'timestamp':
return date(self::DATETIME_STYLE);

case 'time':
return date(self::HOUR_STYLE);

default:
return '';
}
}

/**
* This function is called when creating the model table. Returns the SQL
* that will be executed after the creation of the table. Useful to insert values
Expand Down

0 comments on commit 0f091e6

Please sign in to comment.