2020-02-22 20:04:43 +01:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-03-15 13:56:31 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-02-22 20:04:43 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 20:04:43 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\DataTables\Column;
|
|
|
|
|
|
|
|
use Omines\DataTablesBundle\Column\AbstractColumn;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
|
|
|
|
class IconLinkColumn extends AbstractColumn
|
|
|
|
{
|
2022-08-14 19:09:07 +02:00
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-02-22 20:04:43 +01:00
|
|
|
public function normalize($value)
|
|
|
|
{
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
public function configureOptions(OptionsResolver $resolver): self
|
2020-02-22 20:04:43 +01:00
|
|
|
{
|
|
|
|
parent::configureOptions($resolver);
|
|
|
|
$resolver->setDefaults([
|
2020-03-15 13:56:31 +01:00
|
|
|
'icon' => 'fas fa-fw fa-edit',
|
|
|
|
'title' => null,
|
|
|
|
'href' => null,
|
|
|
|
'disabled' => false,
|
|
|
|
]);
|
2020-02-22 20:04:43 +01:00
|
|
|
|
|
|
|
$resolver->setAllowedTypes('title', ['null', 'string', 'callable']);
|
|
|
|
$resolver->setAllowedTypes('icon', ['null', 'string', 'callable']);
|
|
|
|
$resolver->setAllowedTypes('href', ['null', 'string', 'callable']);
|
2020-03-07 20:49:52 +01:00
|
|
|
$resolver->setAllowedTypes('disabled', ['bool', 'callable']);
|
2020-02-29 22:57:25 +01:00
|
|
|
|
|
|
|
return $this;
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
public function render($value, $context): string
|
2020-02-22 20:04:43 +01:00
|
|
|
{
|
|
|
|
$href = $this->getHref($value, $context);
|
|
|
|
$icon = $this->getIcon($value, $context);
|
|
|
|
$title = $this->getTitle($value, $context);
|
2020-03-07 20:49:52 +01:00
|
|
|
$disabled = $this->getDisabled($value, $context);
|
2020-02-22 20:04:43 +01:00
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null !== $href) {
|
2020-02-22 20:04:43 +01:00
|
|
|
return sprintf(
|
2020-03-07 20:49:52 +01:00
|
|
|
'<a class="btn btn-primary btn-sm %s" href="%s" title="%s"><i class="%s"></i></a>',
|
|
|
|
$disabled ? 'disabled' : '',
|
2020-02-22 20:04:43 +01:00
|
|
|
$href,
|
|
|
|
$title,
|
|
|
|
$icon
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
return '';
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:49:52 +01:00
|
|
|
protected function getDisabled($value, $context): bool
|
|
|
|
{
|
|
|
|
$provider = $this->options['disabled'];
|
|
|
|
if (is_bool($provider)) {
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
if (is_callable($provider)) {
|
2020-08-21 22:43:37 +02:00
|
|
|
return $provider($value, $context);
|
2020-03-07 20:49:52 +01:00
|
|
|
}
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-03-07 20:49:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-22 20:04:43 +01:00
|
|
|
protected function getHref($value, $context): ?string
|
|
|
|
{
|
|
|
|
$provider = $this->options['href'];
|
|
|
|
if (is_string($provider)) {
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
if (is_callable($provider)) {
|
2020-08-21 22:43:37 +02:00
|
|
|
return $provider($value, $context);
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getIcon($value, $context): ?string
|
|
|
|
{
|
|
|
|
$provider = $this->options['icon'];
|
|
|
|
if (is_string($provider)) {
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
if (is_callable($provider)) {
|
2020-08-21 22:43:37 +02:00
|
|
|
return $provider($value, $context);
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTitle($value, $context): ?string
|
|
|
|
{
|
|
|
|
$provider = $this->options['title'];
|
|
|
|
if (is_string($provider)) {
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
if (is_callable($provider)) {
|
2020-08-21 22:43:37 +02:00
|
|
|
return $provider($value, $context);
|
2020-02-22 20:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-15 13:56:31 +01:00
|
|
|
}
|