mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-18 17:31:35 +00:00
Add admin editor for KiCad autocomplete lists
This commit is contained in:
parent
753ecee849
commit
2d37330155
8 changed files with 397 additions and 0 deletions
70
src/Controller/KicadListEditorController.php
Normal file
70
src/Controller/KicadListEditorController.php
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Form\Settings\KicadListEditorType;
|
||||
use App\Services\EDA\KicadListFileManager;
|
||||
use RuntimeException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
use function Symfony\Component\Translation\t;
|
||||
|
||||
final class KicadListEditorController extends AbstractController
|
||||
{
|
||||
#[Route('/settings/misc/kicad-lists', name: 'settings_kicad_lists')]
|
||||
public function __invoke(Request $request, KicadListFileManager $fileManager): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('@config.change_system_settings');
|
||||
|
||||
$form = $this->createForm(KicadListEditorType::class, [
|
||||
'footprints' => $fileManager->getFootprintsContent(),
|
||||
'symbols' => $fileManager->getSymbolsContent(),
|
||||
]);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$data = $form->getData();
|
||||
|
||||
try {
|
||||
$fileManager->save($data['footprints'], $data['symbols']);
|
||||
$this->addFlash('success', t('settings.flash.saved'));
|
||||
|
||||
return $this->redirectToRoute('settings_kicad_lists');
|
||||
} catch (RuntimeException $exception) {
|
||||
$this->addFlash('error', $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($form->isSubmitted() && !$form->isValid()) {
|
||||
$this->addFlash('error', t('settings.flash.invalid'));
|
||||
}
|
||||
|
||||
return $this->render('settings/kicad_list_editor.html.twig', [
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
}
|
||||
57
src/Form/Settings/KicadListEditorType.php
Normal file
57
src/Form/Settings/KicadListEditorType.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Form\Settings;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
final class KicadListEditorType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('footprints', TextareaType::class, [
|
||||
'label' => 'settings.misc.kicad_eda.editor.footprints',
|
||||
'help' => 'settings.misc.kicad_eda.editor.footprints.help',
|
||||
'attr' => [
|
||||
'rows' => 16,
|
||||
'spellcheck' => 'false',
|
||||
'class' => 'font-monospace',
|
||||
],
|
||||
])
|
||||
->add('symbols', TextareaType::class, [
|
||||
'label' => 'settings.misc.kicad_eda.editor.symbols',
|
||||
'help' => 'settings.misc.kicad_eda.editor.symbols.help',
|
||||
'attr' => [
|
||||
'rows' => 16,
|
||||
'spellcheck' => 'false',
|
||||
'class' => 'font-monospace',
|
||||
],
|
||||
])
|
||||
->add('save', SubmitType::class, [
|
||||
'label' => 'save',
|
||||
]);
|
||||
}
|
||||
}
|
||||
96
src/Services/EDA/KicadListFileManager.php
Normal file
96
src/Services/EDA/KicadListFileManager.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\EDA;
|
||||
|
||||
use RuntimeException;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
final class KicadListFileManager
|
||||
{
|
||||
private const FOOTPRINTS_PATH = '/public/kicad/footprints.txt';
|
||||
private const SYMBOLS_PATH = '/public/kicad/symbols.txt';
|
||||
|
||||
public function __construct(
|
||||
#[Autowire('%kernel.project_dir%')]
|
||||
private readonly string $projectDir,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFootprintsContent(): string
|
||||
{
|
||||
return $this->readFile(self::FOOTPRINTS_PATH);
|
||||
}
|
||||
|
||||
public function getSymbolsContent(): string
|
||||
{
|
||||
return $this->readFile(self::SYMBOLS_PATH);
|
||||
}
|
||||
|
||||
public function save(string $footprints, string $symbols): void
|
||||
{
|
||||
$this->writeFile(self::FOOTPRINTS_PATH, $this->normalizeContent($footprints));
|
||||
$this->writeFile(self::SYMBOLS_PATH, $this->normalizeContent($symbols));
|
||||
}
|
||||
|
||||
private function readFile(string $path): string
|
||||
{
|
||||
$fullPath = $this->projectDir . $path;
|
||||
|
||||
if (!is_file($fullPath)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$content = file_get_contents($fullPath);
|
||||
if ($content === false) {
|
||||
throw new RuntimeException(sprintf('Failed to read KiCad list file "%s".', $fullPath));
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function writeFile(string $path, string $content): void
|
||||
{
|
||||
$fullPath = $this->projectDir . $path;
|
||||
$tmpPath = $fullPath . '.tmp';
|
||||
|
||||
if (file_put_contents($tmpPath, $content, LOCK_EX) === false) {
|
||||
throw new RuntimeException(sprintf('Failed to write KiCad list file "%s".', $fullPath));
|
||||
}
|
||||
|
||||
if (!rename($tmpPath, $fullPath)) {
|
||||
@unlink($tmpPath);
|
||||
throw new RuntimeException(sprintf('Failed to replace KiCad list file "%s".', $fullPath));
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeContent(string $content): string
|
||||
{
|
||||
$normalized = str_replace(["\r\n", "\r"], "\n", $content);
|
||||
|
||||
if ($normalized !== '' && !str_ends_with($normalized, "\n")) {
|
||||
$normalized .= "\n";
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
}
|
||||
17
templates/settings/kicad_list_editor.html.twig
Normal file
17
templates/settings/kicad_list_editor.html.twig
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
{% extends "main_card.html.twig" %}
|
||||
|
||||
{% block title %}{% trans %}settings.misc.kicad_eda.editor.title{% endtrans %}{% endblock %}
|
||||
|
||||
{% block card_title %}<i class="fa-solid fa-pen-to-square fa-fw"></i> {% trans %}settings.misc.kicad_eda.editor.title{% endtrans %}{% endblock %}
|
||||
|
||||
{% block card_content %}
|
||||
<p class="text-muted">
|
||||
{% trans %}settings.misc.kicad_eda.editor.description{% endtrans %}
|
||||
</p>
|
||||
|
||||
{{ form_start(form) }}
|
||||
{{ form_row(form.footprints) }}
|
||||
{{ form_row(form.symbols) }}
|
||||
{{ form_row(form.save) }}
|
||||
{{ form_end(form) }}
|
||||
{% endblock %}
|
||||
|
|
@ -49,6 +49,15 @@
|
|||
</div>
|
||||
</div>
|
||||
{{ form_widget(section_widget) }}
|
||||
{% if section_widget.vars.name == 'kicadEDA' %}
|
||||
<div class="row">
|
||||
<div class="{{ offset_label }} col mt-2 ps-2">
|
||||
<a href="{{ path('settings_kicad_lists') }}" class="btn btn-outline-secondary btn-sm">
|
||||
<i class="fa-solid fa-pen-to-square fa-fw"></i> {% trans %}settings.misc.kicad_eda.editor.link{% endtrans %}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</fieldset>
|
||||
{% if not loop.last %}
|
||||
<hr class="mx-0 mb-2 mt-2">
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ final class ApplicationAvailabilityFunctionalTest extends WebTestCase
|
|||
//User related things
|
||||
yield ['/user/settings'];
|
||||
yield ['/user/info'];
|
||||
yield ['/settings/misc/kicad-lists'];
|
||||
|
||||
//Login/logout
|
||||
yield ['/login'];
|
||||
|
|
|
|||
105
tests/Controller/KicadListEditorControllerTest.php
Normal file
105
tests/Controller/KicadListEditorControllerTest.php
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Controller;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
#[Group('slow')]
|
||||
#[Group('DB')]
|
||||
final class KicadListEditorControllerTest extends WebTestCase
|
||||
{
|
||||
private string $footprintsPath;
|
||||
private string $symbolsPath;
|
||||
private string $originalFootprints;
|
||||
private string $originalSymbols;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$projectDir = dirname(__DIR__, 2);
|
||||
$this->footprintsPath = $projectDir . '/public/kicad/footprints.txt';
|
||||
$this->symbolsPath = $projectDir . '/public/kicad/symbols.txt';
|
||||
$this->originalFootprints = (string) file_get_contents($this->footprintsPath);
|
||||
$this->originalSymbols = (string) file_get_contents($this->symbolsPath);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
file_put_contents($this->footprintsPath, $this->originalFootprints);
|
||||
file_put_contents($this->symbolsPath, $this->originalSymbols);
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testEditorRequiresAuthentication(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$client->request('GET', '/en/settings/misc/kicad-lists');
|
||||
|
||||
$this->assertResponseStatusCodeSame(401);
|
||||
}
|
||||
|
||||
public function testEditorAccessibleByAdmin(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$this->loginAsUser($client, 'admin');
|
||||
|
||||
$client->request('GET', '/en/settings/misc/kicad-lists');
|
||||
|
||||
$this->assertResponseIsSuccessful();
|
||||
$this->assertSelectorExists('form[name="kicad_list_editor"]');
|
||||
}
|
||||
|
||||
public function testEditorSavesFiles(): void
|
||||
{
|
||||
$client = static::createClient();
|
||||
$this->loginAsUser($client, 'admin');
|
||||
|
||||
$crawler = $client->request('GET', '/en/settings/misc/kicad-lists');
|
||||
$form = $crawler->filter('form[name="kicad_list_editor"]')->form();
|
||||
$form['kicad_list_editor[footprints]'] = "Package_DIP:DIP-8_W7.62mm\n";
|
||||
$form['kicad_list_editor[symbols]'] = "Device:R\n";
|
||||
|
||||
$client->submit($form);
|
||||
|
||||
$this->assertResponseRedirects('/en/settings/misc/kicad-lists');
|
||||
$this->assertSame("Package_DIP:DIP-8_W7.62mm\n", (string) file_get_contents($this->footprintsPath));
|
||||
$this->assertSame("Device:R\n", (string) file_get_contents($this->symbolsPath));
|
||||
}
|
||||
|
||||
private function loginAsUser($client, string $username): void
|
||||
{
|
||||
$entityManager = $client->getContainer()->get('doctrine')->getManager();
|
||||
$userRepository = $entityManager->getRepository(User::class);
|
||||
$user = $userRepository->findOneBy(['name' => $username]);
|
||||
|
||||
if (!$user) {
|
||||
$this->markTestSkipped(sprintf('User "%s" not found in fixtures', $username));
|
||||
}
|
||||
|
||||
$client->loginUser($user);
|
||||
}
|
||||
}
|
||||
|
|
@ -10029,6 +10029,48 @@ Please note, that you can not impersonate a disabled user. If you try you will g
|
|||
<target>When enabled, the datasheet field in KiCad will link to the actual PDF file (if found). When disabled, it will link to the Part-DB page instead. The Part-DB page link is always available as a separate "Part-DB URL" field.</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="e2e7mR1" name="settings.misc.kicad_eda.editor.title">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.title</source>
|
||||
<target>KiCad autocomplete lists</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="qjv1VVx" name="settings.misc.kicad_eda.editor.link">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.link</source>
|
||||
<target>Edit autocomplete lists</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="f0qkcqg" name="settings.misc.kicad_eda.editor.description">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.description</source>
|
||||
<target>Edit the contents of public/kicad/footprints.txt and public/kicad/symbols.txt. These files are used by the KiCad autocomplete fields in the admin UI.</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="AS3yDlb" name="settings.misc.kicad_eda.editor.footprints">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.footprints</source>
|
||||
<target>Footprints list</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="Jj_YR7n" name="settings.misc.kicad_eda.editor.footprints.help">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.footprints.help</source>
|
||||
<target>One entry per line. Used as autocomplete suggestions for KiCad footprint fields.</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="ELd3KQK" name="settings.misc.kicad_eda.editor.symbols">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.symbols</source>
|
||||
<target>Symbols list</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="A9TOJgM" name="settings.misc.kicad_eda.editor.symbols.help">
|
||||
<segment state="translated">
|
||||
<source>settings.misc.kicad_eda.editor.symbols.help</source>
|
||||
<target>One entry per line. Used as autocomplete suggestions for KiCad symbol fields.</target>
|
||||
</segment>
|
||||
</unit>
|
||||
<unit id="VwvmcWE" name="settings.behavior.sidebar">
|
||||
<segment state="translated">
|
||||
<source>settings.behavior.sidebar</source>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue