Skip to content

Commit

Permalink
Add custom decimal separator support for atk4_money type (#1763)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Voříšek <[email protected]>
  • Loading branch information
mkrecek234 and mvorisek authored Jun 12, 2022
1 parent c6a30b9 commit 48aac3c
Show file tree
Hide file tree
Showing 41 changed files with 98 additions and 194 deletions.
4 changes: 2 additions & 2 deletions demos/collection/card-deck.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
Header::addTo($app, ['Card Deck', 'size' => 1, 'subHeader' => 'Card can be display in a deck, also using model action.']);

$countries = new Country($app->db);
$countries->addCalculatedField('Cost', ['expr' => function (Country $country) {
return '$ ' . number_format(random_int(500, 1500));
$countries->addCalculatedField('Cost', ['type' => 'atk4_money', 'expr' => function (Country $country) {
return random_int(500, 1500);
}]);

$action = $countries->addUserAction('book', [
Expand Down
2 changes: 1 addition & 1 deletion docs/multiline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ In this case we display a message when any of the control value for 'qty' and 'b
$box = $cols['box'] ?? 0;
$total += $qty * $box;
}
return new JsToast('The new Total is '.number_format($total, 2));
return new JsToast('The new Total is ' . $app->ui_persistence->typecastSaveField(new Field(['type' => 'atk4_money']), $total));
}, ['qty', 'box']);


Expand Down
2 changes: 1 addition & 1 deletion docs/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ The tag will override model value. Here is example usage of :php:meth:`Table\\Co


class ExpiredColumn extends \Atk4\Ui\Table\Column
public function getDataCellHtml()
public function getDataCellHtml(): string
{
return '{$_expired}';
}
Expand Down
78 changes: 0 additions & 78 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,6 @@ parameters:
-
path: 'src/Text.php'
message: '~^Property Atk4\\Ui\\Text::\$defaultTemplate \(string\) does not accept default value of type false\.$~'
-
path: 'src/UserAction/JsCallbackExecutor.php'
message: '~^Property Atk4\\Ui\\UserAction\\JsCallbackExecutor::\$jsSuccess \(Atk4\\Ui\\JsExpressionable\) does not accept array\|Closure\.$~'
-
path: 'src/View.php'
message: '~^Parameter #1 \$object \(Atk4\\Ui\\View\) of method Atk4\\Ui\\View::add\(\) should be contravariant with parameter \$object \(Atk4\\Ui\\AbstractView\) of method Atk4\\Ui\\AbstractView::add\(\)$~'
Expand Down Expand Up @@ -575,9 +572,6 @@ parameters:
-
path: 'src/Form/Control/Lookup.php'
message: '~^Left side of && is always true\.$~'
-
path: 'src/Form/Control/Money.php'
message: '~^Ternary operator condition is always true\.$~'
-
path: 'src/Form/Control/Multiline.php'
message: '~^Negated boolean expression is always false\.$~'
Expand Down Expand Up @@ -665,18 +659,9 @@ parameters:
-
path: 'src/UserAction/BasicExecutor.php'
message: '~^Negated boolean expression is always false\.$~'
-
path: 'src/UserAction/BasicExecutor.php'
message: '~^Instanceof between Atk4\\Ui\\JsExpressionable and Closure will always evaluate to false\.$~'
-
path: 'src/UserAction/ConfirmationExecutor.php'
message: '~^Instanceof between Atk4\\Ui\\JsExpressionable and Closure will always evaluate to false\.$~'
-
path: 'src/UserAction/FormExecutor.php'
message: '~^Negated boolean expression is always false\.$~'
-
path: 'src/UserAction/JsCallbackExecutor.php'
message: '~^Instanceof between Atk4\\Ui\\JsExpressionable and Closure will always evaluate to false\.$~'
-
path: 'src/View.php'
message: '~^Negated boolean expression is always false\.$~'
Expand Down Expand Up @@ -1085,12 +1070,6 @@ parameters:
-
path: 'src/Columns.php'
message: '~^Method Atk4\\Ui\\Columns::addRow\(\) has no return type specified\.$~'
-
path: 'src/Component/InlineEdit.php'
message: '~^Instanceof between Closure and Closure will always evaluate to true\.$~'
-
path: 'src/Component/InlineEdit.php'
message: '~^Method Atk4\\Ui\\Component\\InlineEdit::onChange\(\) has no return type specified\.$~'
-
path: 'src/Console.php'
message: '~^Property Atk4\\Ui\\Console::\$last_exit_code has no type specified\.$~'
Expand Down Expand Up @@ -1121,18 +1100,6 @@ parameters:
-
path: 'src/Crud.php'
message: '~^Method Atk4\\Ui\\Crud::setItemsAction\(\) has no return type specified\.$~'
-
path: 'src/Crud.php'
message: '~^Method Atk4\\Ui\\Crud::onFormEdit\(\) has no return type specified\.$~'
-
path: 'src/Crud.php'
message: '~^Method Atk4\\Ui\\Crud::onFormAdd\(\) has no return type specified\.$~'
-
path: 'src/Crud.php'
message: '~^Method Atk4\\Ui\\Crud::onFormAddEdit\(\) has no return type specified\.$~'
-
path: 'src/Dropdown.php'
message: '~^Method Atk4\\Ui\\Dropdown::onChange\(\) has no return type specified\.$~'
-
path: 'src/Form.php'
message: '~^Method Atk4\\Ui\\Form::initLayout\(\) has no return type specified\.$~'
Expand All @@ -1148,9 +1115,6 @@ parameters:
-
path: 'src/Form/Control.php'
message: '~^Property Atk4\\Ui\\Form\\Control::\$width has no type specified\.$~'
-
path: 'src/Form/Control.php'
message: '~^Method Atk4\\Ui\\Form\\Control::onChange\(\) has no return type specified\.$~'
-
path: 'src/Form/Control.php'
message: '~^Method Atk4\\Ui\\Form\\Control::jsInput\(\) has parameter \$action with no type specified\.$~'
Expand All @@ -1172,9 +1136,6 @@ parameters:
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::setOption\(\) has parameter \$value with no type specified\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::onChange\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/Checkbox.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Checkbox::jsChecked\(\) has parameter \$action with no type specified\.$~'
Expand Down Expand Up @@ -1265,9 +1226,6 @@ parameters:
-
path: 'src/Form/Control/Password.php'
message: '~^Property Atk4\\Ui\\Form\\Control\\Password::\$inputType has no type specified\.$~'
-
path: 'src/Form/Control/Radio.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Radio::onChange\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/ScopeBuilder.php'
message: '~^Property Atk4\\Ui\\Form\\Control\\ScopeBuilder::\$atkLookupOptions has no type specified\.$~'
Expand Down Expand Up @@ -1319,12 +1277,6 @@ parameters:
-
path: 'src/Form/Control/Upload.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Upload::addJsAction\(\) has parameter \$action with no type specified\.$~'
-
path: 'src/Form/Control/Upload.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Upload::onUpload\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/Upload.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Upload::onDelete\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/UploadImage.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\UploadImage::setThumbnailSrc\(\) has no return type specified\.$~'
Expand Down Expand Up @@ -1424,9 +1376,6 @@ parameters:
-
path: 'src/ItemsPerPageSelector.php'
message: '~^Method Atk4\\Ui\\ItemsPerPageSelector::jsSetLabel\(\) has parameter \$ipp with no type specified\.$~'
-
path: 'src/ItemsPerPageSelector.php'
message: '~^Method Atk4\\Ui\\ItemsPerPageSelector::onPageLengthSelect\(\) has no return type specified\.$~'
-
path: 'src/JsCallback.php'
message: '~^Method Atk4\\Ui\\JsCallback::setConfirm\(\) has no return type specified\.$~'
Expand Down Expand Up @@ -1496,9 +1445,6 @@ parameters:
-
path: 'src/JsNotify.php'
message: '~^Method Atk4\\Ui\\JsNotify::attachTo\(\) has parameter \$to with no type specified\.$~'
-
path: 'src/JsPaginator.php'
message: '~^Method Atk4\\Ui\\JsPaginator::onScroll\(\) has no return type specified\.$~'
-
path: 'src/JsReload.php'
message: '~^Property Atk4\\Ui\\JsReload::\$includeStorage has no type specified\.$~'
Expand Down Expand Up @@ -1529,9 +1475,6 @@ parameters:
-
path: 'src/JsSearch.php'
message: '~^Property Atk4\\Ui\\JsSearch::\$btnStyle has no type specified\.$~'
-
path: 'src/JsSortable.php'
message: '~^Method Atk4\\Ui\\JsSortable::onReorder\(\) has no return type specified\.$~'
-
path: 'src/JsSse.php'
message: '~^Method Atk4\\Ui\\JsSse::send\(\) has no return type specified\.$~'
Expand Down Expand Up @@ -1721,9 +1664,6 @@ parameters:
-
path: 'src/Panel/Right.php'
message: '~^Method Atk4\\Ui\\Panel\\Right::addConfirmation\(\) has no return type specified\.$~'
-
path: 'src/Panel/Right.php'
message: '~^Method Atk4\\Ui\\Panel\\Right::onOpen\(\) has no return type specified\.$~'
-
path: 'src/Popup.php'
message: '~^Method Atk4\\Ui\\Popup::__construct\(\) has parameter \$triggerBy with no type specified\.$~'
Expand Down Expand Up @@ -1784,9 +1724,6 @@ parameters:
-
path: 'src/Table/Column.php'
message: '~^Method Atk4\\Ui\\Table\\Column::setHeaderDropdown\(\) has parameter \$items with no type specified\.$~'
-
path: 'src/Table/Column.php'
message: '~^Method Atk4\\Ui\\Table\\Column::getTagAttributes\(\) has parameter \$position with no type specified\.$~'
-
path: 'src/Table/Column/ActionButtons.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\ActionButtons::addButton\(\) has parameter \$isDisabled with no type specified\.$~'
Expand Down Expand Up @@ -1817,21 +1754,12 @@ parameters:
-
path: 'src/Table/Column/ColorRating.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\ColorRating::createGradientSingle\(\) has parameter \$steps with no type specified\.$~'
-
path: 'src/Table/Column/ColorRating.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\ColorRating::getTagAttributes\(\) has parameter \$position with no type specified\.$~'
-
path: 'src/Table/Column/ColorRating.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\ColorRating::getColorFromValue\(\) has no return type specified\.$~'
-
path: 'src/Table/Column/DragHandler.php'
message: '~^Property Atk4\\Ui\\Table\\Column\\DragHandler::\$class has no type specified\.$~'
-
path: 'src/Table/Column/DragHandler.php'
message: '~^Property Atk4\\Ui\\Table\\Column\\DragHandler::\$tag has no type specified\.$~'
-
path: 'src/Table/Column/DragHandler.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\DragHandler::onReorder\(\) has no return type specified\.$~'
-
path: 'src/Table/Column/FilterModel.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\FilterModel::afterInit\(\) has no return type specified\.$~'
Expand All @@ -1853,15 +1781,9 @@ parameters:
-
path: 'src/Table/Column/FilterPopup.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\FilterPopup::setFilterCondition\(\) has parameter \$tableModel with no type specified\.$~'
-
path: 'src/Table/Column/JsHeader.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\JsHeader::onSelectItem\(\) has no return type specified\.$~'
-
path: 'src/Table/Column/KeyValue.php'
message: '~^Property Atk4\\Ui\\Table\\Column\\KeyValue::\$values has no type specified\.$~'
-
path: 'src/Table/Column/Money.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\Money::getTagAttributes\(\) has parameter \$position with no type specified\.$~'
-
path: 'src/Table/Column/Multiformat.php'
message: '~^Method Atk4\\Ui\\Table\\Column\\Multiformat::__construct\(\) has parameter \$callback with no type specified\.$~'
Expand Down
32 changes: 1 addition & 31 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,43 +880,13 @@ public function jsRedirect($page, bool $newWindow = false): JsExpression
* ]);
* --> <a href="hello"><b class="red"><i class="blue">welcome</i></b></a>'
*
* @param string|array $tag
* @param string|array $attr
* @param string|array $value
*/
public function getTag($tag = null, $attr = null, $value = null): string
public function getTag(string $tag = null, $attr = null, $value = null): string
{
if ($tag === null) {
$tag = 'div';
} elseif (is_array($tag)) {
$tmp = $tag;

if (isset($tmp[0])) {
$tag = $tmp[0];

if (is_array($tag)) {
// OH a bunch of tags
$output = '';
foreach ($tmp as $subtag) {
$output .= $this->getTag($subtag);
}

return $output;
}

unset($tmp[0]);
} else {
$tag = 'div';
}

if (isset($tmp[1])) {
$value = $tmp[1];
unset($tmp[1]);
} else {
$value = null;
}

$attr = $tmp;
}

$tag = strtolower($tag);
Expand Down
8 changes: 3 additions & 5 deletions src/Component/InlineEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function init(): void
$this->cb = \Atk4\Ui\JsCallback::addTo($this);

// Set default validation error handler.
if (!$this->formatErrorMsg || !($this->formatErrorMsg instanceof \Closure)) {
if (!$this->formatErrorMsg) {
$this->formatErrorMsg = function ($e, $value) {
$caption = $this->model->getField($this->fieldName)->getCaption();

Expand Down Expand Up @@ -117,7 +117,7 @@ public function setModel(Model $model): void
* The function will receive one param:
* value: the new input value.
*/
public function onChange(\Closure $fx)
public function onChange(\Closure $fx): void
{
if (!$this->autoSave) {
$value = $this->getApp()->ui_persistence->typecastLoadField($this->model->getField($this->fieldName), $_POST['value'] ?? null);
Expand All @@ -130,11 +130,9 @@ public function onChange(\Closure $fx)
/**
* On success notifier.
*
* @param string $message
*
* @return \Atk4\Ui\JsToast
*/
public function jsSuccess($message)
public function jsSuccess(string $message)
{
return new JsToast([
'title' => 'Success',
Expand Down
6 changes: 3 additions & 3 deletions src/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private function _getModelActions(string $appliesTo): array
* Set callback for edit action in Crud.
* Callback function will receive the Edit Form and Executor as param.
*/
public function onFormEdit(\Closure $fx)
public function onFormEdit(\Closure $fx): void
{
$this->setOnActions('edit', $fx);
}
Expand All @@ -310,7 +310,7 @@ public function onFormEdit(\Closure $fx)
* Set callback for add action in Crud.
* Callback function will receive the Add Form and Executor as param.
*/
public function onFormAdd(\Closure $fx)
public function onFormAdd(\Closure $fx): void
{
$this->setOnActions('add', $fx);
}
Expand All @@ -319,7 +319,7 @@ public function onFormAdd(\Closure $fx)
* Set callback for both edit and add action form.
* Callback function will receive Forms and Executor as param.
*/
public function onFormAddEdit(\Closure $fx)
public function onFormAddEdit(\Closure $fx): void
{
$this->onFormEdit($fx);
$this->onFormAdd($fx);
Expand Down
2 changes: 1 addition & 1 deletion src/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function init(): void
*
* @param \Closure $fx handler where new selected Item value is passed too
*/
public function onChange(\Closure $fx)
public function onChange(\Closure $fx): void
{
// setting dropdown option for using callback url.
$this->dropdownOptions['onChange'] = new JsFunction(['value', 'name', 't'], [
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ protected function renderTemplateToHtml(string $region = null): string
* @param string|\Atk4\Ui\JsExpression|array|\Closure $expr
* @param array|bool $default
*/
public function onChange($expr, $default = [])
public function onChange($expr, $default = []): void
{
if (is_string($expr)) {
$expr = new \Atk4\Ui\JsExpression($expr);
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function renderView(): void
* @param string|JsExpression|array $expr
* @param array|bool $default
*/
public function onChange($expr, $default = [])
public function onChange($expr, $default = []): void
{
if (is_string($expr)) {
$expr = new \Atk4\Ui\JsExpression($expr);
Expand Down
9 changes: 3 additions & 6 deletions src/Form/Control/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@ class Money extends Input
{
public function getValue()
{
$v = $this->entityField ? $this->entityField->get() : ($this->content ?: null);
$res = parent::getValue() ?? null;
$res = str_replace("\u{00a0}" /* Unicode NBSP */, ' ', $res);

if ($v === null) {
return;
}

return number_format($v, $this->getApp()->ui_persistence->currency_decimals);
return trim(str_replace($this->getApp()->ui_persistence->currency, '', $res));
}

protected function renderView(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* }
* }
*
* return $form->js(true, null, 'input[name="grand_total"]')->val(number_format($grand_total, 2));
* return $form->js(true, null, 'input[name="grand_total"]')->val($app->ui_persistence->typecastSaveField(new Field(['type' => 'atk4_money']), $grand_total));
* }, ['qty', 'price']);
*
* Finally, it's also possible to use Multiline for quickly adding records to a
Expand Down
2 changes: 1 addition & 1 deletion src/Form/Control/Radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function renderView(): void
* @param string|\Atk4\Ui\JsExpression|array|\Closure $expr
* @param array|bool $default
*/
public function onChange($expr, $default = [])
public function onChange($expr, $default = []): void
{
if (is_string($expr)) {
$expr = new \Atk4\Ui\JsExpression($expr);
Expand Down
Loading

0 comments on commit 48aac3c

Please sign in to comment.