Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement code for basic kicad import #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion edit_devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use PartDB\Database;
use PartDB\Device;
use PartDB\DevicePart;
use PartDB\HTML;
use PartDB\Log;
use PartDB\User;
Expand Down Expand Up @@ -111,14 +112,76 @@
switch ($action) {
case 'add':
try {
$parts = array();
if(isset($_FILES["fileToUpload"]))
{
//We have a xml file upload here
$xml = simplexml_load_file($_FILES["fileToUpload"]["tmp_name"]);
if($xml === false)
{
throw new Exception("XML Datei hat kein gültiges Format");
}
if(isset($xml->components))
{
foreach($xml->components[0] as $comp)
{
if(isset($comp->fields))
{
$hasID = false;
foreach($comp->fields[0] as $field)
{
if(strtolower($field["name"]) === "id")
{
$id = (string)$field;
if(isset($parts[$id]))
{
array_push($parts[$id], (string)$comp["ref"]);
}
else
{
$parts[$id] = array((string)$comp["ref"]);
}
$hasID = true;
break;
}
}
if(!$hasID)
{
throw new Exception("No ID for part " . $comp["ref"]);
}
}
else
{
throw new Exception("No ID for part " . $comp["ref"]);
}
}
}
else
{
throw new Exception("File appears not to be a valid Kicad XML file");
}
//Start reading the xmlfile an parsing the device ids
}
$new_device = Device::add($database, $current_user, $log, $new_name, $new_parent_id, $new_comment);

$html->setVariable('refresh_navigation_frame', true, 'boolean');

if (! $add_more) {
$selected_device = $new_device;
$selected_id = $selected_device->getID();
}

foreach($parts as $id => $refs)
{
$qty = 0;
$refstring = "";
foreach($refs as $ref)
{
$refstring .= $ref . " ";
$qty++;
}
$devicepart = DevicePart::add($database, $current_user, $log, $new_device->getID(), $id, $qty, $refstring, false);
}

} catch (Exception $e) {
$messages[] = array('text' => _('Die neue Baugruppe konnte nicht angelegt werden!'), 'strong' => true, 'color' => 'red');
$messages[] = array('text' => _('Fehlermeldung: ').nl2br($e->getMessage()), 'color' => 'red');
Expand Down
9 changes: 8 additions & 1 deletion templates/nextgen/edit_devices.php/smarty_edit_devices.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{t}Baugruppen{/t}
</div>
<div class="card-body">
<form action="" method="post" class="row no-progbar">
<form action="" method="post" class="row no-progbar" enctype="multipart/form-data">
<div class="col-md-4">

{if !isset($id) || $id == 0}
Expand Down Expand Up @@ -89,6 +89,13 @@
<p class="form-text text-muted">{t}Hinweis: Hier kann BBCode verwendet werden um den Text besonders auszuzeichnen (z.B. [b]Fett[/b]).{/t}</p>
</div>
</div>

<div class="form-group row">
<label class="col-form-label col-md-3">{t}XML File:{/t}</label>
<div class="col-md-9">
<p class="form-control-plaintext"><input type="file" name="fileToUpload" id="fileToUpload" accept=".xml" {if !$can_edit}disabled{/if}></p>
</div>
</div>
</div>

<div id="info" class="tab-pane fade">
Expand Down