Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 23, 2023
1 parent c034c08 commit 4f1bf1f
Show file tree
Hide file tree
Showing 22 changed files with 1,917 additions and 1,536 deletions.
2 changes: 1 addition & 1 deletion src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class BaseListView extends Widget
private string $layoutGridTable = "{items}\n{summary}\n{pager}";
private string $pagination = '';
protected ?ReadableDataInterface $dataReader = null;
private array $sortLinkAttributes = [];
protected array $sortLinkAttributes = [];
private ?string $summary = null;
private array $summaryAttributes = [];
private string $toolbar = '';
Expand Down
4 changes: 2 additions & 2 deletions src/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class AbstractColumn
private array $filterAttributes = [];
private string $footer = '';
private array $footerAttributes = [];
private string $label = '';
private ?string $label = null;
private array $labelAttributes = [];
protected bool $visible = true;

Expand Down Expand Up @@ -296,7 +296,7 @@ protected function getEmptyCell(): string
return $this->emptyCell;
}

protected function getLabel(): string
protected function getLabel(): ?string
{
return $this->label;
}
Expand Down
241 changes: 73 additions & 168 deletions src/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,209 +5,121 @@
namespace Yiisoft\Yii\DataView\Column;

use Closure;
use InvalidArgumentException;
use Yiisoft\Html\Tag\A;
use Yiisoft\Html\Tag\Span;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Router\UrlGeneratorInterface;

use function is_array;

/**
* ActionColumn is a column for the {@see GridView} widget that displays buttons for viewing and manipulating the items.
* `ActionColumn` is a column for the {@see GridView} widget that displays buttons for viewing and manipulating
* the items.
*/
final class ActionColumn extends AbstractColumn
final class ActionColumn implements ColumnInterface
{
private array $buttons = [];
private string $primaryKey = 'id';
private string $template = '{view}{update}{delete}';
private array|null $urlArguments = null;

private Closure|null $urlCreator = null;
private CurrentRoute $currentRoute;
private UrlGeneratorInterface|null $urlGenerator = null;
private array $urlParamsConfig = [];
private string $urlName = '';
private array $urlQueryParameters = [];
private array $visibleButtons = [];

/**
* Return new instance with the buttons array.
*
* @param array $value button rendering callbacks. The array keys are the button names (without curly brackets),
* and the values are the corresponding button rendering callbacks. The callbacks should use the following
* signature:
*
* ```php
* [
* buttons => [
* 'action' => function (string $url, $data, int $key) {
* // return the button HTML code
* }
* ],
* ]
* ```
*
* where `$url` is the URL that the column creates for the button, `$data` is the data object being rendered
* for the current row, and `$key` is the key of the data in the data provider array.
*
* You can add further conditions to the button, for example only display it, when the data is editable (here
* assuming you have a status field that indicates that):
*
* ```php
* [
* buttons = [
* 'update' => function (string $url, $data, $key) {
* return $data->status === 'editable' ? Html::a('Update', $url) : '';
* },
* ],
* ],
* ```
* @psalm-param array<string,Closure> $buttons
*/
public function buttons(array $value): self
{
$new = clone $this;
$new->buttons = $value;

return $new;
public function __construct(
private string $primaryKey = 'id',
private string $template = "{view}\n{update}\n{delete}",
private ?string $routeName = null,
private array $urlParamsConfig = [],
private ?array $urlArguments = null,
private array $urlQueryParameters = [],
private ?string $header = null,
private array $buttons = [],
private array $visibleButtons = [],
private array $columnAttributes = [],
private array $bodyAttributes = [],
private bool $visible = true,
) {
}

/**
* Initializes the default button rendering callback for single button.
*/
public function createDefaultButtons(): self
public function getPrimaryKey(): string
{
/** @psalm-var array<string,Closure> */
$defaultButtons = [
'view' => static fn (string $url): string => A::tag()
->attributes(
[
'name' => 'view',
'role' => 'button',
'style' => 'text-decoration: none!important;',
'title' => 'View',
],
)
->content(Span::tag()->content('🔎'))
->href($url)
->render(),
'update' => static fn (string $url): string => A::tag()
->attributes(
[
'name' => 'update',
'role' => 'button',
'style' => 'text-decoration: none!important;',
'title' => 'Update',
],
)
->content(Span::tag()->content(''))
->href($url)
->render(),
'delete' => static fn (string $url): string => A::tag()
->attributes(
[
'name' => 'delete',
'role' => 'button',
'style' => 'text-decoration: none!important;',
'title' => 'Delete',
],
)
->content(Span::tag()->content(''))
->href($url)
->render(),
];

$new = clone $this;

foreach ($defaultButtons as $name => $button) {
$new->buttons[$name] = $button;
}

return $new;
return $this->primaryKey;
}

public function currentRoute(CurrentRoute $value): self
public function getTemplate(): string
{
$new = clone $this;
$new->currentRoute = $value;
return $this->template;
}

return $new;
public function getRouteName(): ?string
{
return $this->routeName;
}

public function getLabel(): string
public function getUrlParamsConfig(): array
{
$label = parent::getLabel();
return $this->urlParamsConfig;
}

return $label !== '' ? $label : 'Actions';
public function getUrlArguments(): ?array
{
return $this->urlArguments;
}

public function getUrlGenerator(): UrlGeneratorInterface
public function getUrlQueryParameters(): array
{
if ($this->urlGenerator === null) {
throw new InvalidArgumentException('Url generator is not set.');
}
return $this->urlQueryParameters;
}

return $this->urlGenerator;
public function getHeader(): ?string
{
return $this->header;
}

/**
* Return new instance specifying which is the primaryKey of the data to be used to generate the url automatically.
*
* @param string $value the primaryKey of the data to be used to generate the url automatically.
* @psalm-return array<string,Closure>
*/
public function primaryKey(string $value): self
public function getButtons(): array
{
$new = clone $this;
$new->primaryKey = $value;
return $this->buttons;
}

return $new;
public function getVisibleButtons(): array
{
return $this->visibleButtons;
}

/**
* Return new instance with the template set.
*
* @param string $value The template used for composing each cell in the action column. Tokens enclosed within curly
* brackets are treated as controller action IDs (also called *button names* in the context of action column).
*
* They will be replaced by the corresponding button rendering callbacks specified in {@see buttons}. For example,
* the token `{view}` will be replaced by the result of the callback `buttons['view']`. If a callback cannot be
* found, the token will be replaced with an empty string.
*
* As an example, to only have the view, and update button you can add the ActionColumn to your GridView columns as
* follows:
*
* ```php
* [
* 'class' => ActionColumn::class,
* 'template()' => ['{view} {update} {delete}'],
* ],
* ```
*
* {@see buttons}
*/
public function template(string $value): self
public function getColumnAttributes(): array
{
$new = clone $this;
return $this->columnAttributes;
}

$result = preg_match_all('/{([\w\-\/]+)}/', $value, $matches);
public function getBodyAttributes(): array
{
return $this->bodyAttributes;
}

if ($result > 0 && !empty($matches[1])) {
$new->buttons = array_intersect_key($new->buttons, array_flip($matches[1]));
}
public function isVisible(): bool
{
return $this->visible;
}

$new->template = $value;
public function getLabel(): string
{
// $label = parent::getLabel();
$label = '';

return $new;
return $label !== '' ? $label : 'Actions';

Check failure on line 111 in src/Column/ActionColumn.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

TypeDoesNotContainType

src/Column/ActionColumn.php:111:17: TypeDoesNotContainType: Type "" for $label is always string() (see https://psalm.dev/056)

Check failure on line 111 in src/Column/ActionColumn.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

RedundantCondition

src/Column/ActionColumn.php:111:42: RedundantCondition: Type "" for $label is always string() (see https://psalm.dev/122)

Check failure on line 111 in src/Column/ActionColumn.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

TypeDoesNotContainType

src/Column/ActionColumn.php:111:17: TypeDoesNotContainType: Type '' for $label is always !=string() (see https://psalm.dev/056)

Check failure on line 111 in src/Column/ActionColumn.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RedundantCondition

src/Column/ActionColumn.php:111:42: RedundantCondition: Type '' for $label is always =string() (see https://psalm.dev/122)
}

/**
* Return a new instance with arguments of the route.
* Return new instance specifying which is the primaryKey of the data to be used to generate the url automatically.
*
* @param array $value Arguments of the route.
* @param string $value the primaryKey of the data to be used to generate the url automatically.
*/
public function urlArguments(array $value): self
public function primaryKey(string $value): self
{
$new = clone $this;
$new->urlArguments = $value;
$new->primaryKey = $value;

return $new;
}
Expand Down Expand Up @@ -248,19 +160,6 @@ public function urlGenerator(UrlGeneratorInterface $value): self
return $new;
}

/**
* Returns a new instance with the name of the route.
*
* @param string $value The name of the route.
*/
public function urlName(string $value): self
{
$new = clone $this;
$new->urlName = $value;

return $new;
}

/**
* Return a new instance with query parameters of the route.
*
Expand Down Expand Up @@ -337,7 +236,8 @@ public function visibleButtons(array $value): self
protected function renderDataCellContent(array|object $data, mixed $key, int $index): string
{
if ($this->getContent() !== null) {

Check failure on line 238 in src/Column/ActionColumn.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.0-ubuntu-latest

UndefinedMethod

src/Column/ActionColumn.php:238:20: UndefinedMethod: Method Yiisoft\Yii\DataView\Column\ActionColumn::getContent does not exist (see https://psalm.dev/022)

Check failure on line 238 in src/Column/ActionColumn.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

src/Column/ActionColumn.php:238:20: UndefinedMethod: Method Yiisoft\Yii\DataView\Column\ActionColumn::getContent does not exist (see https://psalm.dev/022)
return parent::renderDataCellContent($data, $key, $index);
return '';
// return parent::renderDataCellContent($data, $key, $index);
}

if (empty($this->buttons)) {
Expand Down Expand Up @@ -429,4 +329,9 @@ private function isVisibleButton(string $name, array|object $data, mixed $key, i

return $visible;
}

public function getRenderer(): string
{
return ActionColumnRenderer::class;
}
}
Loading

0 comments on commit 4f1bf1f

Please sign in to comment.