Skip to content

Commit

Permalink
Merge pull request #28 from Fohn-Group/update-atk-data
Browse files Browse the repository at this point in the history
Table Filtering / Sizing
  • Loading branch information
ibelar authored Jun 20, 2024
2 parents 997ca38 + dda05b9 commit c2c0b37
Show file tree
Hide file tree
Showing 95 changed files with 3,898 additions and 1,517 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'build',
'vendor',
'js',
'app-test/_app-data',
]);

return (new PhpCsFixer\Config())
Expand Down
2,459 changes: 2,013 additions & 446 deletions app-test/_app-data/create-sqlite-db.php

Large diffs are not rendered by default.

Binary file modified app-test/_app-data/db.sqlite
Binary file not shown.
4 changes: 2 additions & 2 deletions app-test/_includes/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static function getNavigationGroup(string $baseUrl = '/app-test/'): arra
'items' => [
new Item(['name' => 'Table', 'url' => $baseUrl . 'collection/table.php']),
new Item(['name' => 'Table w. Atk Model', 'url' => $baseUrl . 'collection/table-as-crud.php']),
new Item(['name' => 'Table Filters', 'url' => $baseUrl . 'collection/table-filter.php']),
new Item(['name' => 'Table Filters', 'url' => $baseUrl . 'collection/employee.php']),
],
]),
new Group([
Expand Down Expand Up @@ -127,7 +127,7 @@ private static function getNavigationGroup(string $baseUrl = '/app-test/'): arra
/**
* Create button suitable to use in a table action column.
*/
public static function tableBtnFactory(string $iconName, string $color = 'info'): View\Button
public static function tableBtnFactory(string $iconName, string $color = 'info'): Button
{
$btn = new Button(['iconName' => $iconName, 'color' => $color, 'shape' => 'circle', 'size' => 'small', 'type' => 'text']);
$btn->removeTailwind('mx-2');
Expand Down
2 changes: 1 addition & 1 deletion app-test/_includes/Model/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function init(): void
$this->addField('numcode', ['caption' => 'ISO Numeric Code', 'type' => 'integer', 'required' => true]);
$this->addField('phonecode', ['caption' => 'Phone Prefix', 'type' => 'integer', 'required' => true]);

$this->onHook(Model::HOOK_BEFORE_SAVE, function (Model $model) {
$this->onHook(Model::HOOK_BEFORE_SAVE, static function (Model $model) {
if (!$model->get('sys_name')) {
$model->set('sys_name', mb_strtoupper($model->get('name')));
}
Expand Down
61 changes: 61 additions & 0 deletions app-test/_includes/Model/Employees.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);
/**
* Employees.
*/

namespace Fohn\Ui\AppTest\Model;

use Atk4\Data\Model;
use Fohn\Ui\Component\Table\Column\Currency;
use Fohn\Ui\Component\Table\Column\Date;
use Fohn\Ui\Component\Table\Column\Generic;
use Fohn\Ui\Component\Table\Filter\Number;
use Fohn\Ui\Component\Table\Filter\Text;
use Fohn\Ui\Service\Ui;

class Employees extends Model
{
public $table = 'employees';

// public ?string $titleField = 'first_name';

protected function init(): void
{
parent::init();
$this->addExpression('name', ['expr' => 'concat(last_name, ", ", first_name)']);
$this->addField('first_name', ['caption' => 'First Name', 'type' => 'string']);
$this->addField('last_name', ['caption' => 'Last Name', 'type' => 'string']);
$this->addField('email', ['caption' => 'Email', 'type' => 'string']);
$this->addField('title', ['caption' => 'Title', 'type' => 'string']);
$this->addField('city', ['caption' => 'City', 'type' => 'string']);
$this->addField('state', ['caption' => 'State', 'type' => 'string']);
$this->addField('country', ['caption' => 'Country', 'type' => 'string']);
$this->addField('birth_date', ['caption' => 'Birthdate', 'type' => 'date']);
$this->addField('salary', ['caption' => 'Gender', 'type' => 'atk4_money']);
}

public function getTableColumns(): array
{
return [
'name' => Generic::factory(['isSortable' => true]),
'title' => Generic::factory(['isSortable' => true]),
'city' => Generic::factory(['isSortable' => true]),
'state' => Generic::factory(['isSortable' => true]),
'birth_date' => Date::factory(['isSortable' => true, 'caption' => 'Birthdate', 'format' => Ui::getDisplayFormat('date')]),
'salary' => Currency::factory(['isSortable' => true]),
];
}

public function getTableFilters(): array
{
return [
new Text('name'),
new Text('first_name', 'First Name'),
new Text('last_name', 'Last Name'),
new \Fohn\Ui\Component\Table\Filter\Date('birth_date', Ui::getDisplayFormat('date'), 'Birthdate'),
new Number('salary', 'Salary'),
];
}
}
6 changes: 4 additions & 2 deletions app-test/_unit-test/table-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

declare(strict_types=1);

use Fohn\Ui\Component\Table\Filter;
use Fohn\Ui\Component\Table\Filter\Date;
use Fohn\Ui\Component\Table\Filter\DateTime;
use Fohn\Ui\Component\Table\Filter\Integer;
use Fohn\Ui\Component\Table\Filter\Text;
use Fohn\Ui\Component\Table\Filter\Time;
use Fohn\Ui\Component\Utils;
use Fohn\Ui\Service\Ui;

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

if ($locale = Ui::service()->getQueryParamValue('locale')) {
\Fohn\Ui\Component\Utils::requireFLatPickrLocale(\Fohn\Ui\Service\Ui::page(), $locale);
Utils::requireFLatPickrLocale(Ui::page(), $locale);
}

$filter = \Fohn\Ui\Component\Table\Filter::addTo(Ui::layout());
$filter = Filter::addTo(Ui::layout());

$filter->addColumnFilter(new Integer('id'));
$filter->addColumnFilter(new Text('name'));
Expand Down
2 changes: 1 addition & 1 deletion app-test/basic/breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require_once __DIR__ . '/../init-ui.php';

$pages = ['Home', 'Product', 'Electronic', 'Computer'];
$addBreadcrumbItems = function (Breadcrumb $breadcrumb, array $items, string $separator = '/') {
$addBreadcrumbItems = static function (Breadcrumb $breadcrumb, array $items, string $separator = '/') {
foreach ($items as $k => $item) {
$isLast = $k === count($items) - 1;
if (!$isLast) {
Expand Down
4 changes: 2 additions & 2 deletions app-test/basic/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
];

// Utility function in order to create data.
$factoryPeople = function (int $number): array {
$factoryPeople = static function (int $number): array {
$faker = Factory::create();
$items = [];
for ($i = 0; $i < $number; ++$i) {
Expand Down Expand Up @@ -70,7 +70,7 @@
$list = View\HtmlList::addTo(Ui::layout());
$list->setItems($factoryPeople(10));

$list->onItemRender('Items', function (HtmlTemplate $template, array $tags) {
$list->onItemRender('Items', static function (HtmlTemplate $template, array $tags) {
$newT = new HtmlTemplate('<li> <span class="{$classAttr}">{$gender}</span> <span>{$name}</span>');
if ($tags['gender'] === 'Ms.' || $tags['gender'] === 'Mrs.' || $tags['gender'] === 'Mss') {
$newT->set('classAttr', Tw::textColor('secondary'));
Expand Down
40 changes: 40 additions & 0 deletions app-test/collection/employee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Fohn\Ui\AppTest;

use Fohn\Ui\AppTest\Model\Employees;
use Fohn\Ui\Component\Table;
use Fohn\Ui\Js\JsStatements;
use Fohn\Ui\Js\JsToast;
use Fohn\Ui\Service\Data;
use Fohn\Ui\Service\Ui;
use Fohn\Ui\Tailwind\Tw;
use Fohn\Ui\View\Button;

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

$modelCtrl = Data::tableModelCtrl(new Employees(Data::db()));
$modelCtrl->setSearchFields(['firt_name', 'last_name']);

$table = Table::addTo(Ui::layout(), ['keepSelectionAcrossPage' => true]);
$table->setCaption(AppTest::tableCaptionFactory('Employees'));
$table->getTableTw()->merge([Tw::textSize('sm')]);

// Multiple process action
$actionProcess = (new Table\Action(['keepSelection' => true]))->setTrigger(Button::factory(['label' => 'Process', 'color' => 'neutral']));
$table->addRowsAction($actionProcess)->onTrigger(static function ($ids) {
$count = count($ids);

return JsStatements::with([JsToast::success("Process Action on {$count} employee(s)")]);
});

// @phpstan-ignore-next-line
$table->addColumns($modelCtrl->getModel()->getTableColumns());
// @phpstan-ignore-next-line
$table->filterColumns($modelCtrl->getModel()->getTableFilters());

$table->onDataRequest(static function (Table\Payload $payload, Table\Result\Set $result) use ($modelCtrl): void {
$modelCtrl->setTableResultSet($payload, $result);
});
14 changes: 7 additions & 7 deletions app-test/collection/table-as-crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
$addForm = $addDialog->addForm(Ui::factory(Form::class));
$addForm->addControls($formCtrl->factoryFormControls(null));
// Response to form submit request.
$addForm->onSubmit(function (Form $f, ?string $id) use ($formCtrl, $addDialog, $table): JsRenderInterface {
$addForm->onSubmit(static function (Form $f, ?string $id) use ($formCtrl, $addDialog, $table): JsRenderInterface {
if ($errors = $formCtrl->saveModelUsingForm($id, $f->getControls())) {
$f->addValidationErrors($errors);
}
Expand All @@ -80,7 +80,7 @@
$actionMsg->multiple = 'This action will delete {#} countries. Are you sure?';

$actionDelete->addConfirmationDialog('Delete countries:', $actionMsg);
$table->addRowsAction($actionDelete)->onTrigger(function ($ids, $dialog) use ($formCtrl) {
$table->addRowsAction($actionDelete)->onTrigger(static function ($ids, $dialog) use ($formCtrl) {
// performs deletes on ids
foreach ($ids as $id) {
$formCtrl->getModel()->delete($id);
Expand All @@ -91,7 +91,7 @@

// Multiple process action
$actionProcess = (new Table\Action(['keepSelection' => true]))->setTrigger(Button::factory(['label' => 'Process', 'color' => 'neutral']));
$table->addRowsAction($actionProcess)->onTrigger(function ($ids) {
$table->addRowsAction($actionProcess)->onTrigger(static function ($ids) {
sleep(2);

return JsStatements::with([JsToast::success('Process Action! ' . implode(' / ', $ids))]);
Expand All @@ -106,12 +106,12 @@
$editForm->addControls($formCtrl->factoryFormControls(null));

// Response to form request value callback using $ctrl.
$editForm->onControlsValueRequest(function ($id, Form\Response\Value $response) use ($formCtrl) {
$editForm->onControlsValueRequest(static function ($id, Form\Response\Value $response) use ($formCtrl) {
$response->mergeValues($formCtrl->getFormInputValue((string) $id));
});

// Response to form submit request.
$editForm->onSubmit(function (Form $f, ?string $id) use ($formCtrl, $editDialog, $table): JsRenderInterface {
$editForm->onSubmit(static function (Form $f, ?string $id) use ($formCtrl, $editDialog, $table): JsRenderInterface {
if ($errors = $formCtrl->saveModelUsingForm($id, $f->getControls())) {
$f->addValidationErrors($errors);
}
Expand Down Expand Up @@ -142,7 +142,7 @@

// Add callback event to Dialog when user confirm the action.
$deleteDialog->addCallbackEvent('confirm', new Button(['label' => 'Delete', 'color' => 'info']));
$deleteDialog->onCallbackEvent('confirm', function ($payload) use ($deleteDialog, $table, $formCtrl) {
$deleteDialog->onCallbackEvent('confirm', static function ($payload) use ($deleteDialog, $table, $formCtrl) {
// Delete record in db. Record id is set in $payload['id']
$formCtrl->getModel()->delete($payload['id']);

Expand All @@ -155,7 +155,7 @@

// Response to an onDataRequest event from Table.
// Fill in Table\Result\Set $dataSet depending on $payload value.
$table->onDataRequest(function (Table\Payload $payload, Table\Result\Set $result) use ($tableCtrl): void {
$table->onDataRequest(static function (Table\Payload $payload, Table\Result\Set $result) use ($tableCtrl): void {
$tableCtrl->setTableResultSet($payload, $result);
});

Expand Down
2 changes: 1 addition & 1 deletion app-test/collection/table-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

// Response to an onDataRequest event from Table.
// Fill in Table\Result\Set $dataSet depending on $payload value.
$table->onDataRequest(function (Table\Payload $payload, Table\Result\Set $result) use ($ctrl): void {
$table->onDataRequest(static function (Table\Payload $payload, Table\Result\Set $result) use ($ctrl): void {
$ctrl->setTableResultSet($payload, $result);
});

Expand Down
12 changes: 6 additions & 6 deletions app-test/collection/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
$table->addColumn('is_director', Table\Column\Boolean::factory(['caption' => 'Director']));
$table->addColumn('tag', Table\Column\Html::factory(['caption' => 'I']));

$table->applyCssRow(function (string $id, object $row) {
$table->applyCssRow(static function (string $id, object $row) {
$tws = Tw::from([]);
if ($row->sales > 250000) {
$tws->merge(['bg-' . TwConstant::COLORS['accent-light']]);
Expand All @@ -69,14 +69,14 @@
});

// Apply specific formatter based on cell value. Formatter function must return a string.
$table->getTableColumn('name_email')->formatValue(function ($col, $value) {
$table->getTableColumn('name_email')->formatValue(static function ($col, $value) {
$textColor = Tw::textColor('info-light');

return "<a href=mailto:'{$value['email']}' class='{$textColor} underline'>{$value['name']}</a>";
});

// Apply specific formatter based on cell value. Formatter function must return a string.
$table->getTableColumn('date_publish')->formatValue(function ($column, $value) use ($locale) {
$table->getTableColumn('date_publish')->formatValue(static function ($column, $value) use ($locale) {
$fmt = \IntlDateFormatter::create(
$locale,
\IntlDateFormatter::LONG,
Expand All @@ -86,7 +86,7 @@
return $fmt->format($value);
})->alignText('center');

$table->getTableColumn('tag')->formatValue(function ($column, $value) {
$table->getTableColumn('tag')->formatValue(static function ($column, $value) {
$tag = (new View\Tag(['textSize' => 'x-small', 'shape' => 'rounded', 'width' => '8']));
$tag->setTextContent((string) $value);
$tag->removeTailwind('mx-2')->removeTailwind('my-1');
Expand All @@ -97,7 +97,7 @@
})->alignText('center');

// Apply specific Tw css utility on sales column based on cell value.
$table->getTableColumn('sales')->applyCssCell(function ($v) {
$table->getTableColumn('sales')->applyCssCell(static function ($v) {
$tw = Tw::from([]);
if ($v < 0) {
$tw->merge(['text-' . TwConstant::COLORS['error-light']]);
Expand All @@ -107,7 +107,7 @@
});

// Load fake data into table.
$table->onDataRequest(function (Table\Payload $payload, Table\Result\Set $result): void {
$table->onDataRequest(static function (Table\Payload $payload, Table\Result\Set $result): void {
$faker = Factory::create();
$data = [];
for ($i = 0; $i < 15; ++$i) {
Expand Down
3 changes: 2 additions & 1 deletion app-test/config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

declare(strict_types=1);
use Fohn\Ui\Service\Ui;

return [
'env' => \Fohn\Ui\Service\Ui::PROD_ENV,
'env' => Ui::PROD_ENV,
'timezone' => 'America/Toronto',
'locale' => 'en_CA',
'format' => [
Expand Down
6 changes: 3 additions & 3 deletions app-test/form/form-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* Return a Javascript function that will update text inside a view when executed.
* Will render as: (newValue) => { $('#VIEW_ID')->text(newValue); }.
*/
$changeTextFn = function (View $view): JsFunction {
$changeTextFn = static function (View $view): JsFunction {
return JsFunction::arrow([Js::var('newValue')])->execute(Jquery::withView($view)->text(Js::var('newValue')));
};

Expand Down Expand Up @@ -62,7 +62,7 @@

/** @var Form\Control\Select $countrySelect */
$countrySelect = $form->getControl('country');
$countrySelect->onItemsRequest(function (Form\Response\Items $response) {
$countrySelect->onItemsRequest(static function (Form\Response\Items $response) {
$response->setItems(FormControlFactory::getSelectItems(new Country(Data::db())));
});

Expand All @@ -77,7 +77,7 @@
$chip->appendTailwind('mx-auto');
$range->onChange($changeTextFn($chip->content), 500);

$form->onSubmit(function (Form $f) {
$form->onSubmit(static function (Form $f) {
return JsToast::success('Submit!');
});

Expand Down
4 changes: 2 additions & 2 deletions app-test/form/form-custom-input.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

$form = Form::addTo(Ui::layout());

$form->onHook(Form::HOOK_BEFORE_CONTROL_ADD, function ($form, Form\Control $control, $layoutName) {
$form->onHook(Form::HOOK_BEFORE_CONTROL_ADD, static function ($form, Form\Control $control, $layoutName) {
if (get_class($control) === Form\Control\Input::class) {
$control->setTemplate(Ui::templateFromFile(__DIR__ . '/template/custom-input.html'));
}
});

$form->addControls($modelCtrl->factoryFormControls($id));
$form->onSubmit(function (Form $f) use ($modelCtrl, $id) {
$form->onSubmit(static function (Form $f) use ($modelCtrl, $id) {
if ($errors = $modelCtrl->saveModelUsingForm($id, $f->getControls())) {
$f->addValidationErrors($errors);
}
Expand Down
2 changes: 1 addition & 1 deletion app-test/form/form-left.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$form = Form::addTo(Ui::layout());
$form->addControls($modelCtrl->factoryFormControls($id));

$form->onSubmit(function (Form $f) use ($modelCtrl, $id) {
$form->onSubmit(static function (Form $f) use ($modelCtrl, $id) {
if ($errors = $modelCtrl->saveModelUsingForm($id, $f->getControls())) {
$f->addValidationErrors($errors);
}
Expand Down
Loading

0 comments on commit c2c0b37

Please sign in to comment.