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

@ -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;