2020-04-14 17:21:30 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\LabelSystem\PlaceholderProviders;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Services\SIFormatter;
|
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
|
|
|
class PartProvider implements PlaceholderProviderInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $siFormatter;
|
|
|
|
protected $translator;
|
|
|
|
|
|
|
|
public function __construct(SIFormatter $SIFormatter, TranslatorInterface $translator)
|
|
|
|
{
|
|
|
|
$this->siFormatter = $SIFormatter;
|
|
|
|
$this->translator = $translator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function replace(string $placeholder, object $part, array $options = []): ?string
|
|
|
|
{
|
|
|
|
if (!$part instanceof Part) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[CATEGORY]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getCategory() ? $part->getCategory()->getName() : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[CATEGORY_FULL]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getCategory() ? $part->getCategory()->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[MANUFACTURER]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getManufacturer() ? $part->getManufacturer()->getName() : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[MANUFACTURER_FULL]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getManufacturer() ? $part->getManufacturer()->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[FOOTPRINT]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getFootprint() ? $part->getFootprint()->getName() : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[FOOTPRINT_FULL]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getFootprint() ? $part->getFootprint()->getFullPath() : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[MASS]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getMass() ? $this->siFormatter->format($part->getMass(), 'g', 1) : '';
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[MPN]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getManufacturerProductNumber();
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[TAGS]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $part->getTags();
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[M_STATUS]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
if ($part->getManufacturingStatus() === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $this->translator->trans('m_status.' . $part->getManufacturingStatus());
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedown = new \Parsedown();
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[DESCRIPTION]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $parsedown->line($part->getDescription());
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[DESCRIPTION_T]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return strip_tags($parsedown->line($part->getDescription()));
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[COMMENT]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return $parsedown->line($part->getComment());
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:19:15 +02:00
|
|
|
if ($placeholder === '[[COMMENT_T]]') {
|
2020-04-14 17:21:30 +02:00
|
|
|
return strip_tags($parsedown->line($part->getComment()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|