Allow to add/delete attachments via part edit page.

This commit is contained in:
Jan Böhmer 2019-08-26 23:30:35 +02:00
parent fcfab982a8
commit 3a11933a89
12 changed files with 370 additions and 50 deletions

View file

@ -10,7 +10,7 @@ parameters:
banner: '' # The info text shown in the homepage
use_gravatar: true # Set to false, if no Gravatar images should be used for user profiles.
default_currency: 'EUR' # The currency that should be used
media_directory: ''
media_directory: 'data/media/'
services:
# default configuration for services in *this* file

View file

@ -32,10 +32,12 @@ namespace App\Controller;
use App\Entity\Parts\Category;
use App\Entity\Parts\Part;
use App\Form\AttachmentFormType;
use App\Form\Part\PartBaseType;
use App\Services\AttachmentHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
@ -70,7 +72,8 @@ class PartController extends AbstractController
* @param EntityManagerInterface $em
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator)
public function edit(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator,
AttachmentHelper $attachmentHelper)
{
$this->denyAccessUnlessGranted('edit', $part);
@ -78,10 +81,17 @@ class PartController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//Upload passed files
$attachments = $form['attachments'];
foreach ($attachments as $attachment) {
/** @var $attachment FormInterface */
$attachmentHelper->upload( $attachment->getData(), $attachment['file']->getData());
}
$em->persist($part);
$em->flush();
$this->addFlash('info', $translator->trans('part.edited_flash'));
//Reload form, so the SIUnitType entries use the new part unit
$form = $this->createForm(PartBaseType::class, $part);
} elseif ($form->isSubmitted() && ! $form->isValid()) {

View file

@ -44,13 +44,13 @@ abstract class Attachment extends NamedDBElement
* @var bool
* @ORM\Column(type="boolean")
*/
protected $show_in_table;
protected $show_in_table = false;
/**
* @var string The filename using the %BASE% variable
* @ORM\Column(type="string", name="filename")
*/
protected $path;
protected $path = "";
/**
* ORM mapping is done in sub classes (like PartAttachment)
@ -63,7 +63,7 @@ abstract class Attachment extends NamedDBElement
* @ORM\JoinColumn(name="type_id", referencedColumnName="id")
* @Selectable()
*/
protected $attachement_type;
protected $attachment_type;
/***********************************************************
* Various function
@ -205,9 +205,9 @@ abstract class Attachment extends NamedDBElement
* @return AttachmentType the type of this attachement
*
*/
public function getType(): AttachmentType
public function getAttachmentType(): ?AttachmentType
{
return $this->attachement_type;
return $this->attachment_type;
}
/**
@ -237,6 +237,39 @@ abstract class Attachment extends NamedDBElement
return $this;
}
abstract public function setElement(AttachmentContainingDBElement $element) : Attachment;
/**
* @param string $path
* @return Attachment
*/
public function setPath(string $path): Attachment
{
$this->path = $path;
return $this;
}
/**
* @param AttachmentType $attachement_type
* @return Attachment
*/
public function setAttachmentType(AttachmentType $attachement_type): Attachment
{
$this->attachment_type = $attachement_type;
return $this;
}
public function setURL(?string $url) : Attachment
{
//Only set if the URL is not empty
if (!empty($url)) {
$this->path = $url;
}
return $this;
}
/*****************************************************************************************************
* Static functions
*****************************************************************************************************/

View file

@ -72,9 +72,6 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
*/
protected $attachments;
//TODO
protected $attachmentTypes;
public function __construct()
{
$this->attachments = new ArrayCollection();
@ -87,44 +84,35 @@ abstract class AttachmentContainingDBElement extends NamedDBElement
*********************************************************************************/
/**
* Get all different attachement types of the attachements of this element.
*
* @return AttachmentType[] the attachement types as a one-dimensional array of AttachementType objects,
* sorted by their names
*
* @throws Exception if there was an error
* Gets all attachments associated with this element.
* @return Attachment[]|Collection
*/
public function getAttachmentTypes(): ?array
public function getAttachments() : Collection
{
return $this->attachmentTypes;
return $this->attachments;
}
/**
* Get all attachements of this element / Get the element's attachements with a specific type.
*
* @param int $type_id * if NULL, all attachements of this element will be returned
* * if this is a number > 0, only attachements with this type ID will be returned
* @param bool $only_table_attachements if true, only attachements with "show_in_table == true"
*
* @return Collection|Attachment[] the attachements as a one-dimensional array of Attachement objects
*
* @throws Exception if there was an error
* Adds an attachment to this element
* @param Attachment $attachment Attachment
* @return $this
*/
public function getAttachments($type_id = null, bool $only_table_attachements = false) : Collection
public function addAttachment(Attachment $attachment) : self
{
if ($only_table_attachements || $type_id) {
$attachements = $this->attachments;
//Attachment must be associated with this element
$attachment->setElement($this);
$this->attachments->add($attachment);
return $this;
}
foreach ($attachements as $key => $attachement) {
if (($only_table_attachements && (!$attachement->getShowInTable()))
|| ($type_id && ($attachement->getType()->getID() !== $type_id))) {
unset($attachements[$key]);
}
}
return $attachements;
}
return $this->attachments;
/**
* Removes the given attachment from this element
* @param Attachment $attachment
* @return $this
*/
public function removeAttachment(Attachment $attachment) : self
{
$this->attachments->removeElement($attachment);
return $this;
}
}

View file

@ -31,6 +31,7 @@
namespace App\Entity\Attachments;
use App\Entity\Parts\Part;
use Doctrine\ORM\Mapping as ORM;
/**
@ -48,4 +49,14 @@ class PartAttachment extends Attachment
*/
protected $element;
public function setElement(AttachmentContainingDBElement $element): Attachment
{
if (!$element instanceof Part) {
throw new \InvalidArgumentException("The element associated with a PartAttachment must be a Part!");
}
$this->element = $element;
return $this;
}
}

View file

@ -84,7 +84,8 @@ class Part extends AttachmentContainingDBElement
public const INSTOCK_UNKNOWN = -2;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\PartAttachment", mappedBy="element")
* @ORM\OneToMany(targetEntity="App\Entity\Attachments\PartAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true)
* @Assert\Valid()
*/
protected $attachments;

View file

@ -0,0 +1,102 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 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 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\Form;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentType;
use App\Entity\Base\StructuralDBElement;
use App\Form\Type\StructuralEntityType;
use App\Services\AttachmentHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
class AttachmentFormType extends AbstractType
{
protected $attachment_helper;
public function __construct(AttachmentHelper $attachmentHelper)
{
$this->attachment_helper = $attachmentHelper;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', TextType::class,
[
'label' => 'attachment.edit.name'
])
->add('attachment_type', StructuralEntityType::class, [
'label' => 'attachment.edit.attachment_type',
'class' => AttachmentType::class,
'disable_not_selectable' => true,
]);
$builder->add('showInTable', CheckboxType::class, ['required' => false,
'label' => 'attachment.edit.show_in_table',
'attr' => ['class' => 'form-control-sm'],
'label_attr' => ['class' => 'checkbox-custom']]);
$builder->add('url', UrlType::class, [
'label' => 'attachment.edit.url',
'required' => false
]);
$builder->add('file', FileType::class, [
'label' => 'attachment.edit.file',
'mapped' => false,
'required' => false,
'attr' => ['class' => 'file', 'data-show-preview' => 'false', 'data-show-upload' => 'false'],
'constraints' => [
new File([
'maxSize' => $options['max_file_size']
])
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Attachment::class,
'max_file_size' => '16M'
]);
}
}

View file

@ -31,12 +31,15 @@
namespace App\Form\Part;
use App\Entity\Attachments\PartAttachment;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\MeasurementUnit;
use App\Entity\Parts\Part;
use App\Entity\Parts\Storelocation;
use App\Form\AttachmentFormType;
use App\Form\AttachmentType;
use App\Form\Type\SIUnitType;
use App\Form\Type\StructuralEntityType;
use Doctrine\DBAL\Types\FloatType;
@ -130,6 +133,17 @@ class PartBaseType extends AbstractType
'by_reference' => false
]);
//Attachment section
$builder->add('attachments', CollectionType::class, [
'entry_type' => AttachmentFormType::class,
'allow_add' => true, 'allow_delete' => true,
'label' => false,
'entry_options' => [
'data_class' => PartAttachment::class
],
'by_reference' => false
]);
$builder
//Buttons
->add('save', SubmitType::class, ['label' => 'part.edit.save'])

View file

@ -33,15 +33,18 @@ namespace App\Services;
use App\Entity\Attachments\Attachment;
use Doctrine\ORM\EntityManagerInterface;
use SebastianBergmann\CodeCoverage\Node\File;
use App\Entity\Attachments\PartAttachment;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\KernelInterface;
class AttachmentHelper
{
/**
* @var string The folder where the attachments are saved. By default this is data/media in the project root string
*/
protected $base_path;
public function __construct(ParameterBagInterface $params, KernelInterface $kernel)
@ -54,10 +57,42 @@ class AttachmentHelper
if ($fs->isAbsolutePath($tmp_base_path)) {
$this->base_path = $tmp_base_path;
} else {
$this->base_path = realpath($kernel->getProjectDir() . $tmp_base_path);
$this->base_path = realpath($kernel->getProjectDir() . DIRECTORY_SEPARATOR . $tmp_base_path);
}
}
/**
* Converts an relative placeholder filepath (with %MEDIA% or older %BASE%) to an absolute filepath on disk.
* @param string $placeholder_path The filepath with placeholder for which the real path should be determined.
* @return string The absolute real path of the file
*/
protected function placeholderToRealPath(string $placeholder_path) : string
{
//The new attachments use %MEDIA% as placeholders, which is the directory set in media_directory
$placeholder_path = str_replace("%MEDIA%", $this->base_path, $placeholder_path);
//Older path entries are given via %BASE% which was the project root
$placeholder_path = str_replace("%BASE%/data/media", $this->base_path, $placeholder_path);
return $placeholder_path;
}
/**
* Converts an real absolute filepath to a placeholder version.
* @param string $real_path The absolute path, for which the placeholder version should be generated.
* @param bool $old_version By default the %MEDIA% placeholder is used, which is directly replaced with the
* media directory. If set to true, the old version with %BASE% will be used, which is the project directory.
* @return string The placeholder version of the filepath
*/
protected function realPathToPlaceholder(string $real_path, bool $old_version = false) : string
{
if ($old_version) {
return str_replace($this->base_path, "%BASE%/data/media", $real_path);
}
return str_replace($this->base_path, "%MEDIA%", $real_path);
}
/**
* Returns the absolute filepath of the attachment. Null is returned, if the attachment is externally saved.
* @param Attachment $attachment The attachment for which the filepath should be determined
@ -70,7 +105,7 @@ class AttachmentHelper
}
$path = $attachment->getPath();
$path = str_replace("%BASE%", $this->base_path, $path);
$path = $this->placeholderToRealPath($path);
return realpath($path);
}
@ -127,4 +162,61 @@ class AttachmentHelper
return sprintf("%.{$decimals}f", $bytes / 1024 ** $factor) . @$sz[$factor];
}
/**
* Generate a path to a folder, where this attachment can save its file.
* @param Attachment $attachment The attachment for which the folder should be generated
* @return string The path to the folder (without trailing slash)
*/
public function generateFolderForAttachment(Attachment $attachment) : string
{
$mapping = [PartAttachment::class => 'part'];
$path = $this->base_path . DIRECTORY_SEPARATOR . $mapping[get_class($attachment)] . DIRECTORY_SEPARATOR . $attachment->getElement()->getID();
return $path;
}
/**
* Moves the given uploaded file to a permanent place and saves it into the attachment
* @param Attachment $attachment The attachment in which the file should be saved
* @param UploadedFile|null $file The file which was uploaded
* @return Attachment The attachment with the new filepath
*/
public function upload(Attachment $attachment, ?UploadedFile $file) : Attachment
{
//If file is null, do nothing (helpful, so we dont have to check if the file was reuploaded in controller)
if (!$file) {
return $attachment;
}
$folder = $this->generateFolderForAttachment($attachment);
//Sanatize filename
$originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename . '.' . $file->getClientOriginalExtension();
//If a file with this name is already existing add a number to the filename
if (file_exists($folder . DIRECTORY_SEPARATOR . $newFilename)) {
$bak = $newFilename;
$number = 1;
$newFilename = $folder . DIRECTORY_SEPARATOR . $safeFilename . '-' . $number . '.' . $file->getClientOriginalExtension();
while (file_exists($newFilename)) {
$number++;
$newFilename = $folder . DIRECTORY_SEPARATOR . $safeFilename . '-' . $number . '.' . $file->getClientOriginalExtension();
}
}
//Move our temporay attachment to its final location
$file_path = $file->move($folder, $newFilename)->getRealPath();
//Make our file path relative to %BASE%
$file_path = $this->realPathToPlaceholder($file_path);
//Save the path to the attachment
$attachment->setPath($file_path);
return $attachment;
}
}

View file

@ -0,0 +1,61 @@
{% set delete_btn %}
<button type="button" class="btn btn-danger lot_btn_delete" onclick="delete_attachment_entry(this);">
<i class="fas fa-trash-alt fa-fw"></i>
{% trans %}attachment.delete{% endtrans %}
</button>
{% endset %}
<table class="table table-striped" id="attachments_table" data-prototype="{{ form_widget(form.attachments.vars.prototype)|e('html_attr') }}">
<tbody>
{% for attachment in form.attachments %}
<tr>
<td>
{{ form_widget(attachment) }}
</td>
<td>
{{ delete_btn }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-success" onclick="create_attachment_entry(this)">
<i class="fas fa-plus-square fa-fw"></i>
{% trans %}attachment.create{% endtrans %}
</button>
<script>
function delete_attachment_entry(btn) {
window.bootbox.confirm('{% trans %}part_lot.edit.delete.confirm{% endtrans %}', function (result) {
if(result) {
$(btn).parents("tr").remove();
}
})
}
function create_attachment_entry(btn) {
//Determine the table, so we can determine, how many entries there are already.
$holder = $("#attachments_table");
var index = $holder.find(":input").length;
var newForm = $holder.data("prototype");
//Increase the index
newForm = newForm.replace(/__name__/g, index);
newForm = '<td>' + newForm + '</td>';
$newFormRow = $('<tr></tr>').html(newForm);
//Add delete button
$btn = '<td>' + '{{ delete_btn|e('js') }}' + '</td>';
$newFormRow.append($btn);
$holder.append($newFormRow);
//Reinit the selectpickers
$(".selectpicker").selectpicker();
$(".file").fileinput();
}
</script>

View file

@ -40,6 +40,12 @@
{% trans %}part.edit.tab.part_lots{% endtrans %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" role="tab" href="#attachments">
<i class="fas fa-paperclip fa-fw"></i>
{% trans %}part.edit.tab.attachments{% endtrans %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" role="tab" href="#comment">
<i class="fas fa-sticky-note fa-fw"></i>
@ -63,7 +69,9 @@
<div class="tab-pane fade p-2" id="part_lots" role="tabpanel">
{% include "Parts/edit/_lots.html.twig" %}
</div>
<div class="tab-pane fade p-2" id="attachments" role="tabpanel">
{% include "Parts/edit/_attachments.html.twig" %}
</div>
<div class="tab-pane fade p-2" id="comment" role="tabpanel">
{{ form_widget(form.comment)}}
</div>

View file

@ -21,7 +21,7 @@
{{ helper.attachment_icon(attachment, attachment_helper) }}
</td>
<td class="align-middle">{{ attachment.name }}</td>
<td class="align-middle">{{ attachment.type.fullPath }}</td>
<td class="align-middle">{{ attachment.attachmentType.fullPath }}</td>
<td class="align-middle">
{% if attachment.external %}
<a href="{{ attachment.uRL }}" target="_blank" class="link-external">{{ attachment.host }}</a>