Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename "money" type to "atk4_money" type #1665

Merged
merged 5 commits into from
Oct 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ three steps involved:
3. If needed - Use [Agile API](https://github.com/atk4/api) to provide API access for your Mobile/React app or IoT devices.

Agile Data allows you to define models, fields, relations, formulas, aggregates, expressions, user action and
access control rules. Both Agile UI and Agile API will follow those rules.
access control rules. Both Agile UI and Agile API will follow those rules.

### Integrations and Apps using Agile UI

@@ -256,7 +256,6 @@ Add-ons:
- [Charts add-on](https://github.com/atk4/chart) - Modern looking and free charts with [chartJS](https://www.chartjs.org/)
- [Audit for Models](https://github.com/atk4/audit) - Record all DB operations with Undo/Redo support for Agile Data
- [Data for Reports](https://github.com/atk4/report) - Implement data aggregation and union models for Agile Data
- [Schema and Migration](https://github.com/atk4/schema) - Tools to migrate your database structure

Integrations:

25 changes: 16 additions & 9 deletions demos/_demo-data/create-sqlite-db.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
namespace Atk4\Ui\Demos;

use Atk4\Data\Model;
use Atk4\Data\Schema\Migration;

require_once __DIR__ . '/../init-autoloader.php';

@@ -52,7 +53,7 @@ public function import(array $rowsMulti)
$model->addField('name', ['type' => 'string']);
$model->addField('addresses', ['type' => 'text']);
$model->addField('accounts', ['type' => 'text']);
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
(new Migration($model))->dropIfExists()->create();
$model->import([
['id' => 1, 'name' => 'John', 'addresses' => null, 'accounts' => null],
['id' => 2, 'name' => 'Jane', 'addresses' => null, 'accounts' => null],
@@ -65,7 +66,7 @@ public function import(array $rowsMulti)
$model->addField('iso3', ['type' => 'string']); // should be CHAR(3) NOT NULL
$model->addField('numcode', ['type' => 'smallint']);
$model->addField('phonecode', ['type' => 'integer']);
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
(new Migration($model))->dropIfExists()->create();
$model->import([
['id' => 1, 'iso' => 'AF', 'name' => 'AFGHANISTAN', 'nicename' => 'Afghanistan', 'iso3' => 'AFG', 'numcode' => 4, 'phonecode' => 93],
['id' => 2, 'iso' => 'AL', 'name' => 'ALBANIA', 'nicename' => 'Albania', 'iso3' => 'ALB', 'numcode' => 8, 'phonecode' => 355],
@@ -327,7 +328,7 @@ public function import(array $rowsMulti)
$model->addField('is_folder', ['type' => 'boolean']);
$model->addField('parent_folder_id', ['type' => 'bigint']);
// KEY `fk_file_file_idx` (`parent_folder_id`)
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
(new Migration($model))->dropIfExists()->create();
$model->import([
['id' => 1, 'name' => 'phpunit.xml', 'type' => 'xml', 'is_folder' => 0, 'parent_folder_id' => null],
['id' => 2, 'name' => 'LICENSE', 'type' => '', 'is_folder' => 0, 'parent_folder_id' => null],
@@ -417,17 +418,23 @@ public function import(array $rowsMulti)
$model->addField('finish_time', ['type' => 'time']);
$model->addField('created', ['type' => 'datetime']);
$model->addField('updated', ['type' => 'datetime']);
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
$model->import([
(new Migration($model))->dropIfExists()->create();
$data = [
['id' => 1, 'project_name' => 'Agile DSQL', 'project_code' => 'at01', 'description' => 'DSQL is a composable SQL query builder. You can write multi-vendor queries in PHP profiting from better security, clean syntax and avoid human errors.', 'client_name' => 'Agile Toolkit', 'client_address' => 'Some Street,' . "\n" . 'Garden City' . "\n" . 'UK', 'client_country_iso' => 'GB', 'is_commercial' => 0, 'currency' => 'GBP', 'is_completed' => 1, 'project_budget' => 7000, 'project_invoiced' => 0, 'project_paid' => 0, 'project_hour_cost' => 0, 'project_hours_est' => 150, 'project_hours_reported' => 125, 'project_expenses_est' => 50, 'project_expenses' => 0, 'project_mgmt_cost_pct' => 0.1, 'project_qa_cost_pct' => 0.2, 'start_date' => '2016-01-26', 'finish_date' => '2016-06-23', 'finish_time' => '12:50:00', 'created' => '2017-04-06 10:34:34', 'updated' => '2017-04-06 10:35:04'],
['id' => 2, 'project_name' => 'Agile Core', 'project_code' => 'at02', 'description' => 'Collection of PHP Traits for designing object-oriented frameworks.', 'client_name' => 'Agile Toolkit', 'client_address' => 'Some Street,' . "\n" . 'Garden City' . "\n" . 'UK', 'client_country_iso' => 'GB', 'is_commercial' => 0, 'currency' => 'GBP', 'is_completed' => 1, 'project_budget' => 3000, 'project_invoiced' => 0, 'project_paid' => 0, 'project_hour_cost' => 0, 'project_hours_est' => 70, 'project_hours_reported' => 56, 'project_expenses_est' => 50, 'project_expenses' => 0, 'project_mgmt_cost_pct' => 0.1, 'project_qa_cost_pct' => 0.2, 'start_date' => '2016-04-27', 'finish_date' => '2016-05-21', 'finish_time' => '18:41:00', 'created' => '2017-04-06 10:21:50', 'updated' => '2017-04-06 10:35:04'],
['id' => 3, 'project_name' => 'Agile Data', 'project_code' => 'at03', 'description' => 'Agile Data implements an entirely new pattern for data abstraction, that is specifically designed for remote databases such as RDS, Cloud SQL, BigQuery and other distributed data storage architectures. It focuses on reducing number of requests your App have to send to the Database by using more sophisticated queries while also offering full Domain Model mapping and Database vendor abstraction.', 'client_name' => 'Agile Toolkit', 'client_address' => 'Some Street,' . "\n" . 'Garden City' . "\n" . 'UK', 'client_country_iso' => 'GB', 'is_commercial' => 0, 'currency' => 'GBP', 'is_completed' => 1, 'project_budget' => 12000, 'project_invoiced' => 0, 'project_paid' => 0, 'project_hour_cost' => 0, 'project_hours_est' => 300, 'project_hours_reported' => 394, 'project_expenses_est' => 600, 'project_expenses' => 430, 'project_mgmt_cost_pct' => 0.2, 'project_qa_cost_pct' => 0.3, 'start_date' => '2016-04-17', 'finish_date' => '2016-06-20', 'finish_time' => '03:04:00', 'created' => '2017-04-06 10:30:15', 'updated' => '2017-04-06 10:35:04'],
['id' => 4, 'project_name' => 'Agile UI', 'project_code' => 'at04', 'description' => 'Web UI Component library.', 'client_name' => 'Agile Toolkit', 'client_address' => 'Some Street,' . "\n" . 'Garden City' . "\n" . 'UK', 'client_country_iso' => 'GB', 'is_commercial' => 0, 'currency' => 'GBP', 'is_completed' => 0, 'project_budget' => 20000, 'project_invoiced' => 0, 'project_paid' => 0, 'project_hour_cost' => 0, 'project_hours_est' => 600, 'project_hours_reported' => 368, 'project_expenses_est' => 1200, 'project_expenses' => 0, 'project_mgmt_cost_pct' => 0.3, 'project_qa_cost_pct' => 0.4, 'start_date' => '2016-09-17', 'finish_date' => '', 'finish_time' => '', 'created' => '2017-04-06 10:30:15', 'updated' => '2017-04-06 10:35:04'],
]);
];
foreach ($data as $rowIndex => $row) {
foreach (['start_date', 'finish_date', 'finish_time', 'created', 'updated'] as $k) {
$data[$rowIndex][$k] = new \DateTime($row[$k] . ' GMT');
}
}
$model->import($data);

$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'product_category']);
$model->addField('name', ['type' => 'string']);
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
(new Migration($model))->dropIfExists()->create();
$model->import([
['id' => 1, 'name' => 'Condiments and Gravies'],
['id' => 2, 'name' => 'Beverages'],
@@ -437,7 +444,7 @@ public function import(array $rowsMulti)
$model = new ImportModelWithPrefixedFields($persistence, ['table' => 'product_sub_category']);
$model->addField('name', ['type' => 'string']);
$model->addField('product_category_id', ['type' => 'bigint']);
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
(new Migration($model))->dropIfExists()->create();
$model->import([
['id' => 1, 'name' => 'Gravie', 'product_category_id' => 1],
['id' => 2, 'name' => 'Spread', 'product_category_id' => 1],
@@ -455,7 +462,7 @@ public function import(array $rowsMulti)
$model->addField('brand', ['type' => 'string']);
$model->addField('product_category_id', ['type' => 'bigint']);
$model->addField('product_sub_category_id', ['type' => 'bigint']);
(new \Atk4\Schema\Migration($model))->dropIfExists()->create();
(new Migration($model))->dropIfExists()->create();
$model->import([
['id' => 1, 'name' => 'Mustard', 'brand' => 'Condiment Corp.', 'product_category_id' => 1, 'product_sub_category_id' => 2],
['id' => 2, 'name' => 'Ketchup', 'brand' => 'Condiment Corp.', 'product_category_id' => 1, 'product_sub_category_id' => 2],
2 changes: 1 addition & 1 deletion demos/_includes/SomeData.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ protected function init(): void
$model->addField('name');
$model->addField('surname', ['actual' => 'name']);
$model->addField('date', ['type' => 'date']);
$model->addField('salary', ['type' => 'money', 'actual' => 'randomNumber']);
$model->addField('salary', ['type' => 'atk4_money', 'actual' => 'randomNumber']);
$model->addField('logo_url');
}
}
4 changes: 2 additions & 2 deletions demos/collection/table2.php
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
];

$model = new \Atk4\Data\Model(new \Atk4\Data\Persistence\Static_($data));
$model->getField('amount')->type = 'money';
$model->getField('amount')->type = 'atk4_money';

\Atk4\Ui\Header::addTo($app, ['Table with various headers', 'subHeader' => 'Demonstrates how you can add subheaders, footnotes and other insertions into your data table', 'icon' => 'table']);

@@ -54,7 +54,7 @@
// copy of amount through a PHP callback
$model->addExpression('amount_copy', [function (\Atk4\Data\Model $model) {
return $model->get('amount');
}, 'type' => 'money']);
}, 'type' => 'atk4_money']);

// column with 2 decorators that stack. Money will use red ink and alignment, format will change text.
$table->addColumn('amount', [Table\Column\Money::class]);
12 changes: 6 additions & 6 deletions demos/init-db.php
Original file line number Diff line number Diff line change
@@ -216,16 +216,16 @@ protected function init(): void
$model->currency_symbol = $map[$model->currency] ?? '?';
});

$this->addField($this->fieldName()->project_budget, ['type' => 'money']);
$this->addField($this->fieldName()->project_invoiced, ['type' => 'money']);
$this->addField($this->fieldName()->project_paid, ['type' => 'money']);
$this->addField($this->fieldName()->project_hour_cost, ['type' => 'money']);
$this->addField($this->fieldName()->project_budget, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_invoiced, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_paid, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_hour_cost, ['type' => 'atk4_money']);

$this->addField($this->fieldName()->project_hours_est, ['type' => 'integer']);
$this->addField($this->fieldName()->project_hours_reported, ['type' => 'integer']);

$this->addField($this->fieldName()->project_expenses_est, ['type' => 'money']);
$this->addField($this->fieldName()->project_expenses, ['type' => 'money']);
$this->addField($this->fieldName()->project_expenses_est, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_expenses, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_mgmt_cost_pct, new Percent());
$this->addField($this->fieldName()->project_qa_cost_pct, new Percent());

2 changes: 1 addition & 1 deletion docs/overview.rst
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ clarifications::
$this->addField('domain_name');
$this->addFields(['contact_email', 'contact_phone']);
$this->addField('date', ['type' => 'date']);
$this->addField('offer', ['type' => 'money']);
$this->addField('offer', ['type' => 'atk4_money']);
$this->addField('is_accepted', ['type' => 'boolean']);
}
}
8 changes: 4 additions & 4 deletions docs/table.rst
Original file line number Diff line number Diff line change
@@ -102,12 +102,12 @@ field if it's not alrady defined in ``Order::init()``::
$table = Table::addTo($app);
$order = new Order($db);

$order->addExpression('total', '[price]*[amount]')->type = 'money';
$order->addExpression('total', '[price]*[amount]')->type = 'atk4_money';

$table->setModel($order, ['name', 'price', 'amount', 'total', 'status']);

The type of the Model Field determines the way how value is presented in the table. I've specified
value to be 'money' which makes column align values to the right, format it with 2 decimal signs
value to be 'atk4_money' which makes column align values to the right, format it with 2 decimal signs
and possibly add a currency sign.

To learn about value formatting, read documentation on :ref:`ui_persistence`.
@@ -121,7 +121,7 @@ specify the caption, you can use code like this::

$order->addExpression('total', [
'[price]*[amount]',
'type' => 'money',
'type' => 'atk4_money',
'caption' => 'Total Price'
]);

@@ -148,7 +148,7 @@ then to save memory Table will re-use the same objects for all generic fields.

Protected property that will contain "generic" column that will be used to format all
columns, unless a different column type is specified or the Field type will require a use
of a different class (e.g. 'money'). Value will be initialized after first call to
of a different class (e.g. 'atk4_money'). Value will be initialized after first call to
:php:meth:`Table::addColumn`

.. php:attr:: columns
4 changes: 2 additions & 2 deletions docs/tablecolumn.rst
Original file line number Diff line number Diff line change
@@ -36,9 +36,9 @@ Generic Column Decorator
Generic description of a column for :php:class:`Atk4\\Ui\\Table`

Table object relies on a separate class: `\\Atk4\\Ui\\Table\\Column` to present most of the values. The goals
of the column object is to format anything around the actual values. The type = 'money' will result in
of the column object is to format anything around the actual values. The type = 'atk4_money' will result in
a custom formatting of the value, but will also require column to be right-aligned. To simplify this,
type = 'money' will use a different column class - :php:class:`Table\\Column\\Money`. There are several others,
type = 'atk4_money' will use a different column class - :php:class:`Table\\Column\\Money`. There are several others,
but first we need to look at the generic column and understand it's base capabilities:

A class resposnible for cell formatting. This class defines 3 main methods that is used by the Table
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
includes:
- vendor/mahalux/atk4-hintable/phpstan-ext.neon
- vendor/mvorisek/atk4-hintable/phpstan-ext.neon

parameters:
level: 6
2 changes: 1 addition & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
@@ -553,7 +553,7 @@ public function controlFactory(\Atk4\Data\Field $field, $seed = [])
'datetime' => [Form\Control\Calendar::class, ['type' => 'datetime']],
'date' => [Form\Control\Calendar::class, ['type' => 'date']],
'time' => [Form\Control\Calendar::class, ['type' => 'time']],
'money' => [Form\Control\Money::class],
'atk4_money' => [Form\Control\Money::class],
];

/**
6 changes: 3 additions & 3 deletions src/Form/Control/Multiline.php
Original file line number Diff line number Diff line change
@@ -478,7 +478,7 @@ protected function getSuiTableCellProps(Field $field): array
{
$props = [];

if ($field->type === 'money' || $field->type === 'integer') {
if ($field->type === 'integer' || $field->type === 'atk4_money') {
$props['text-align'] = 'right';
}

@@ -492,7 +492,7 @@ protected function getSuiInputProps(Field $field): array
{
$props = $this->componentProps[self::INPUT] ?? [];

$props['type'] = ($field->type === 'integer' || $field->type === 'float' || $field->type === 'money') ? 'number' : 'text';
$props['type'] = ($field->type === 'integer' || $field->type === 'float' || $field->type === 'atk4_money') ? 'number' : 'text';

return array_merge($props, $field->ui['multiline'][self::INPUT] ?? []);
}
@@ -861,9 +861,9 @@ private function getDummyExpression(FieldSqlExpression $exprField, $model)
private function getValueForExpression(Field $exprField, string $fieldName, Model $model)
{
switch ($exprField->type) {
case 'money':
case 'integer':
case 'float':
case 'atk4_money':
// Value is 0 or the field value.
$value = (string) $model->get($fieldName) ?: 0;

2 changes: 1 addition & 1 deletion src/Form/Control/ScopeBuilder.php
Original file line number Diff line number Diff line change
@@ -308,7 +308,7 @@ class ScopeBuilder extends Control
],
'integer' => 'numeric',
'float' => 'numeric',
'money' => 'numeric',
'atk4_money' => 'numeric',
'checkbox' => 'boolean',
];

4 changes: 1 addition & 3 deletions src/Grid.php
Original file line number Diff line number Diff line change
@@ -207,7 +207,7 @@ public function addButton($text)
/**
* Set item per page value.
*
* if an array is passed, it will also add an ItemPerPageSelector to paginator.
* If an array is passed, it will also add an ItemPerPageSelector to paginator.
*
* @param int|array $ipp
* @param string $label
@@ -216,8 +216,6 @@ public function setIpp($ipp, $label = 'Items per page:')
{
if (is_array($ipp)) {
$this->addItemsPerPageSelector($ipp, $label);

$this->ipp = isset($_GET['ipp']) ? (int) $_GET['ipp'] : $ipp[0];
} else {
$this->ipp = $ipp;
}
6 changes: 3 additions & 3 deletions src/Persistence/Ui.php
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ public function _typecastSaveField(\Atk4\Data\Field $f, $value): string
$value = $value ? $this->yes : $this->no;

break;
case 'money':
case 'atk4_money':
$value = ($this->currency ? $this->currency . ' ' : '') . number_format($value, $this->currency_decimals);

break;
@@ -122,8 +122,8 @@ public function _typecastLoadField(\Atk4\Data\Field $f, $value)
$value = (bool) $value;

break;
case 'money':
$value = str_replace(',', '', $value);
case 'atk4_money':
$value = str_replace(',', '', $value); // we should use standard Field::normalize here

break;
case 'date':
2 changes: 1 addition & 1 deletion src/Table.php
Original file line number Diff line number Diff line change
@@ -364,7 +364,7 @@ public function decoratorFactory(\Atk4\Data\Field $field, $seed = [])
}

protected $typeToDecorator = [
'money' => [Table\Column\Money::class],
'atk4_money' => [Table\Column\Money::class],
'text' => [Table\Column\Text::class],
'boolean' => [Table\Column\Status::class, ['positive' => [true], 'negative' => [false]]],
];