Skip to content

Commit

Permalink
Merge branch 'release/v1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxes3 committed Oct 8, 2024
2 parents 6325ff4 + 2c3786e commit 826feaa
Show file tree
Hide file tree
Showing 25 changed files with 707 additions and 303 deletions.
52 changes: 41 additions & 11 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,56 @@
name: tableBundle
name: PHP Symfony CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main, develop ]

env:
DATABASE_URL: mysql://root:[email protected]:3306/table_bundle
DATABASE_DRIVER: pdo_mysql

jobs:
phpunit:
build:
runs-on: ubuntu-latest

strategy:
matrix:
php: [8.1, 8.2, 8.3]
symfony: [6.4.*, 7.0.*, 7.1.*]
exclude:
- php: 8.1
symfony: 7.1.*
- php: 8.1
symfony: 7.0.*

services:
mysql:
image: mysql:latest
env:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping --silent"
--health-interval 10s
--health-timeout 5s
--health-retries 3
steps:
- uses: shivammathur/setup-php@2cb9b829437ee246e9b3cac53555a39208ca6d28
- uses: actions/checkout@v4
- name: Setup PHP ${{ matrix.php }}
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
- uses: actions/checkout@v2
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests (Unit and Feature tests) via PHPUnit
php-version: ${{ matrix.php }}
tools: flex
- name: Download dependencies
env:
DATABASE_URL: sqlite:///%kernel.project_dir%/var/app.db
SYMFONY_REQUIRE: ${{ matrix.symfony }}
uses: ramsey/composer-install@v2
- name: Run test suite on PHP ${{ matrix.php }} and Symfony ${{ matrix.symfony }}
run: vendor/bin/simple-phpunit
- name: Check Code Styles
- name: Run ECS
run: vendor/bin/ecs
- name: Check PHP Stan
- name: Run PHPStan
run: vendor/bin/phpstan analyse src tests
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## v1.2.0
- Removed symfony ^5.4 support
- Added Table option `OPT_SUB_TABLE_COLLAPSED`. This will collapse the sub table by default, you can also pass a callable to determine if the sub table should be collapsed or not
- Added footer columns to the table. This can be used to display totals or other information
- Fixed a bug where the table count would be of when using group by in the default query builder
- Date filters now use the `datetime_controller.js` provided by the core-bundle
- UX improvements

## v1.0.7
- More documentation and better styling of the documentation
- Added a new optional parameter to the `FilterTypeInterface::getValueField()` method to allow for more complex value fields.
Expand Down
26 changes: 13 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
],
"require": {
"php": ">=8.1",
"symfony/framework-bundle": "^5.4|^6.4|^7.0",
"symfony/http-kernel": "^5.4|^6.4|^7.0",
"symfony/validator": "^5.4|^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/validator": "^6.4|^7.0",
"araise/core-bundle": "^1.1",
"araise/search-bundle": "^3.1",
"phpoffice/phpspreadsheet": "^1.22|^2.0",
"symfony/stimulus-bundle": "^2.16"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.4|^6.4|^7.0",
"symfony/config": "^5.4|^6.4|^7.0",
"symfony/dependency-injection": "^5.4|^6.4|^7.0",
"symfony/yaml": "^5.4|^6.4|^7.0",
"symfony/phpunit-bridge": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0",
"doctrine/doctrine-bundle": "^2.5.5",
"doctrine/orm": "^2.11|^3.1",
"doctrine/orm": "^2.11",
"whatwedo/php-coding-standard": "^1.0",
"zenstruck/foundry": "^1.16",
"zenstruck/foundry": "^v2.0.7",
"zenstruck/console-test": "^v1.1.0",
"symfony/translation": "^5.4|^6.4|^7.0",
"symfony/twig-bundle": "^5.4|^6.4|^7.0",
"symfony/translation": "^6.4|^7.0",
"symfony/twig-bundle": "^6.4|^7.0",
"gedmo/doctrine-extensions": "^3.15",
"symfony/webpack-encore-bundle": "^1.14|^2.1",
"symfony/security-core": "^5.4|^6.4|^7.0",
"symfony/security-bundle": "^5.4|^6.4|^7.0",
"symfony/security-core": "^6.4|^7.0",
"symfony/security-bundle": "^6.4|^7.0",
"phpstan/phpstan": "^1.5"
},
"autoload": {
Expand Down
14 changes: 13 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,19 @@
<!-- Source: https://github.com/Leward/mermaid-docsify -->
<script type="module">
import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs";
mermaid.initialize({ startOnLoad: true });
mermaid.initialize({
startOnLoad: true,
theme: 'base',
themeVariables: {
darkMode: true,
primaryColor: '#5b47d1',
primaryTextColor: '#ffffff',
primaryBorderColor: '#1b1447',
lineColor: '#fc00ff',
secondaryColor: '#fc00ff',
tertiaryColor: '#fc00ff',
},
});
window.mermaid = mermaid;
</script>
<script src="https://unpkg.com/[email protected]/dist/docsify-mermaid.js"></script>
Expand Down
53 changes: 53 additions & 0 deletions docs/table-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ All Options are as constants in `Table` class.
- - `content_show_header`: Boolean, default: `true`
- - `content_show_entry_dropdown`: Boolean, default: `true`
- - `content_show_pagination_if_page_total_less_than_limit`: Boolean, default: `true`
- `sub_table_collapsed`: Boolean or callable, default: `true`


### Collapse Sub-Tables
By default, sub-tables will be rendered collapsed.
This can be changed by setting the `sub_table_collapsed` option to either `false` or pass a callable that returns a boolean.

```php
$table->setOption(Table::OPT_SUB_TABLE_COLLAPSED, false);
```

Note that if you pass a function, you will get the current row as an argument.
You can use this to only expand certain rows:

```php
// Expand only users with an email
$table->setOption(Table::OPT_SUB_TABLE_COLLAPSED, function(array|object $row) {
if(!$row instanceof User) {
return true;
}
if($row->getEmail() !== null) {
return false;
}
return true;
});
```


## Column Options
Expand All @@ -48,6 +74,33 @@ All Options are as constants in `Column` class.
- `formatter`: [Formatter](formatter.md)
- `sort_expression`: String, example: `'trainerGroup.name'`

## Footer Columns
In this example we would like to add a count of the content at the end of our table.
We can to this by adding a footer column like so:

```php
$data= [
[
'id' => 1,
],
[
'id' => 2,
]
];

$table
->setFooterData(['count' => count($data)])
->addFooterColumn('totalLabel', null, [
Column::OPT_CALLABLE => fn (array $content) => 'Total',
])
->addFooterColumn('count', null, [
Column::OPT_CALLABLE => fn(array $content) => $content['count'],
])
;
```

Note that this is only a simple example. The data could easily be replaced with an array of entities for example.

## Action Columns

Action Columns are here to link to other pages (f.ex. link to edit or view).
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
<env name="DATABASE_URL" value="mysql://root:[email protected]:3306/table_bundle" />
<env name="DATABASE_DRIVER" value="pdo_mysql" />

</php>
<testsuites>
Expand Down
22 changes: 19 additions & 3 deletions src/DataLoader/DoctrineDataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use araise\TableBundle\Extension\PaginationExtension;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
Expand Down Expand Up @@ -43,9 +44,24 @@ public function getResults(): iterable
/** @var QueryBuilder $qb */
$qb = (clone $this->options[self::OPT_QUERY_BUILDER]);
$qb->select('COUNT('.$qb->getRootAliases()[0].')');
$qb->resetDQLPart('groupBy');
$qb->resetDQLPart('orderBy');
$this->paginationExtension->setTotalResults((int) $qb->getQuery()->getSingleScalarResult());

if (count($qb->getDQLPart('groupBy')) > 0) {
$sql = $qb->getQuery()->getSQL();
$params = array_values(
array_map(
static fn (Parameter $parameter) => $parameter->getValue(),
$qb->getParameters()->toArray()
)
);

$sql = sprintf('SELECT COUNT(*) as row_count FROM (%s) AS sub;', $sql);
$rsm = new ResultSetMapping();
$rsm->addScalarResult('row_count', 'row_count', 'integer');
$count = $this->entityManager->createNativeQuery($sql, $rsm)->setParameters($params)->getSingleScalarResult();
$this->paginationExtension->setTotalResults((int) $count);
} else {
$this->paginationExtension->setTotalResults($qb->getQuery()->getSingleScalarResult());
}

if ($this->getOption(self::OPT_SAVE_LAST_QUERY) && $this->requestStack->getCurrentRequest()?->hasSession()) {
/** @var QueryBuilder $qbSave */
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/araiseTableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class araiseTableExtension extends Extension implements PrependExtensionInterface
{
Expand Down
25 changes: 22 additions & 3 deletions src/Filter/Type/DateFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,36 @@
namespace araise\TableBundle\Filter\Type;

use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;

class DateFilterType extends DatetimeFilterType
{
protected $locale;

public function __construct(
?string $column = null,
array $joins = [],
protected ?RequestStack $requestStack = null
) {
parent::__construct($column, $joins);
$this->locale = $requestStack->getMainRequest()?->getLocale() ?? 'en';
}

public function getValueField(?string $value = null, ?string $operator = null): string
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());

$stimulusAttrs = (new StimulusHelper(null))->createStimulusAttributes();
$stimulusAttrs
->addController('araise/core-bundle/datetime', [
'lang' => $this->locale,
])
;
return sprintf(
'<input type="date" name="{name}" value="%s">',
$operator !== static::CRITERIA_IS_EMPTY ? $value : ''
'<input type="date" name="{name}" value="%s" %s>',
$operator !== static::CRITERIA_IS_EMPTY ? $value : '',
$stimulusAttrs
);
}

Expand Down
23 changes: 20 additions & 3 deletions src/Filter/Type/DatetimeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace araise\TableBundle\Filter\Type;

use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;

class DatetimeFilterType extends FilterType
{
Expand All @@ -20,6 +22,14 @@ class DatetimeFilterType extends FilterType

public const CRITERIA_IS_EMPTY = 'is_empty';

public function __construct(
?string $column = null,
array $joins = [],
protected ?RequestStack $requestStack = null
) {
parent::__construct($column, $joins);
}

public function getOperators(): array
{
return [
Expand All @@ -38,10 +48,17 @@ public function getValueField(?string $value = null, ?string $operator = null):
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());

$locale = $this->requestStack->getMainRequest()?->getLocale();
$stimulusAttrs = (new StimulusHelper(null))->createStimulusAttributes();
$stimulusAttrs
->addController('araise/core-bundle/datetime', [
'lang' => $locale ?? 'en',
])
;
return sprintf(
'<input type="datetime-local" name="{name}" value="%s">',
$operator !== static::CRITERIA_IS_EMPTY ? $value : ''
'<input type="datetime-local" name="{name}" value="%s" %s>',
$operator !== static::CRITERIA_IS_EMPTY ? $value : '',
$stimulusAttrs
);
}

Expand Down
Loading

0 comments on commit 826feaa

Please sign in to comment.