2019-09-07 15:23:03 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2019-11-01 13:40:30 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony)
|
2019-09-07 15:23:03 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-09-07 15:23:03 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\DataTables\Column;
|
|
|
|
|
|
|
|
|
2019-09-16 13:27:53 +02:00
|
|
|
use App\Entity\Base\DBElement;
|
2019-09-07 15:23:03 +02:00
|
|
|
use App\Entity\Base\NamedDBElement;
|
2019-09-08 16:56:05 +02:00
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Services\EntityURLGenerator;
|
2019-09-07 15:23:03 +02:00
|
|
|
use Omines\DataTablesBundle\Column\AbstractColumn;
|
|
|
|
use Symfony\Component\OptionsResolver\Options;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
2019-09-08 16:56:05 +02:00
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
2019-09-07 15:23:03 +02:00
|
|
|
|
|
|
|
class EntityColumn extends AbstractColumn
|
|
|
|
{
|
|
|
|
|
2019-09-08 16:56:05 +02:00
|
|
|
protected $urlGenerator;
|
|
|
|
protected $accessor;
|
|
|
|
|
|
|
|
public function __construct(EntityURLGenerator $URLGenerator, PropertyAccessorInterface $accessor)
|
|
|
|
{
|
|
|
|
$this->urlGenerator = $URLGenerator;
|
|
|
|
$this->accessor = $accessor;
|
|
|
|
}
|
|
|
|
|
2019-09-07 15:23:03 +02:00
|
|
|
/**
|
|
|
|
* The normalize function is responsible for converting parsed and processed data to a datatables-appropriate type.
|
|
|
|
*
|
|
|
|
* @param mixed $value The single value of the column
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function normalize($value)
|
|
|
|
{
|
|
|
|
/** @var NamedDBElement $value */
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function configureOptions(OptionsResolver $resolver)
|
|
|
|
{
|
|
|
|
parent::configureOptions($resolver);
|
|
|
|
|
|
|
|
$resolver->setRequired('property');
|
|
|
|
|
|
|
|
$resolver->setDefault('field', function (Options $option) {
|
|
|
|
return $option['property'] . '.name';
|
|
|
|
});
|
2019-09-08 16:56:05 +02:00
|
|
|
|
|
|
|
$resolver->setDefault('render', function (Options $options) {
|
|
|
|
return function ($value, Part $context) use ($options) {
|
2019-09-16 13:27:53 +02:00
|
|
|
/** @var DBElement $entity */
|
2019-09-08 16:56:05 +02:00
|
|
|
$entity = $this->accessor->getValue($context, $options['property']);
|
|
|
|
|
|
|
|
if ($entity) {
|
2019-09-16 13:27:53 +02:00
|
|
|
if ($entity->getID() !== null) {
|
|
|
|
return sprintf(
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
$this->urlGenerator->listPartsURL($entity),
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return sprintf('<i>%s</i>', $value);
|
|
|
|
}
|
2019-09-08 16:56:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2019-09-07 15:23:03 +02:00
|
|
|
}
|
|
|
|
}
|