Added entities and properties for some future features.

This commit is contained in:
Jan Böhmer 2019-08-12 15:47:57 +02:00
parent bcdba8b3e0
commit 7826e3d2ad
49 changed files with 1477 additions and 362 deletions

266
src/Entity/Base/Company.php Normal file
View file

@ -0,0 +1,266 @@
<?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
*
*/
declare(strict_types=1);
/**
* Part-DB Version 0.4+ "nextgen"
* 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\Entity\Base;
use App\Entity\Base\StructuralDBElement;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This abstract class is used for companies like suppliers or manufacturers.
*
* @ORM\MappedSuperclass()
*/
abstract class Company extends StructuralDBElement
{
/**
* @var string The address of the company
* @ORM\Column(type="string")
*/
protected $address = "";
/**
* @var string The phone number of the company
* @ORM\Column(type="string")
*/
protected $phone_number = "";
/**
* @var string The fax number of the company
* @ORM\Column(type="string")
*/
protected $fax_number = "";
/**
* @var string The email address of the company
* @ORM\Column(type="string")
* @Assert\Email()
*/
protected $email_address = "";
/**
* @var string The website of the company
* @ORM\Column(type="string")
* @Assert\Url()
*/
protected $website = "";
/**
* @var string
* @ORM\Column(type="string")
*/
protected $auto_product_url = "";
/********************************************************************************
*
* Getters
*
*********************************************************************************/
/**
* Get the address.
*
* @return string the address of the company (with "\n" as line break)
*/
public function getAddress(): string
{
return $this->address;
}
/**
* Get the phone number.
*
* @return string the phone number of the company
*/
public function getPhoneNumber(): string
{
return $this->phone_number;
}
/**
* Get the fax number.
*
* @return string the fax number of the company
*/
public function getFaxNumber(): string
{
return $this->fax_number;
}
/**
* Get the e-mail address.
*
* @return string the e-mail address of the company
*/
public function getEmailAddress(): string
{
return $this->email_address;
}
/**
* Get the website.
*
* @return string the website of the company
*/
public function getWebsite(): string
{
return $this->website;
}
/**
* Get the link to the website of an article.
*
* @param string $partnr * NULL for returning the URL with a placeholder for the part number
* * or the part number for returning the direct URL to the article
*
* @return string the link to the article
*/
public function getAutoProductUrl($partnr = null): string
{
if (\is_string($partnr)) {
return str_replace('%PARTNUMBER%', $partnr, $this->auto_product_url);
}
return $this->auto_product_url;
}
/********************************************************************************
*
* Setters
*
*********************************************************************************/
/**
* Set the addres.
*
* @param string $new_address the new address (with "\n" as line break)
*
* @return self
*/
public function setAddress(string $new_address): self
{
$this->address = $new_address;
return $this;
}
/**
* Set the phone number.
*
* @param string $new_phone_number the new phone number
*
* @return self
*/
public function setPhoneNumber(string $new_phone_number): self
{
$this->phone_number = $new_phone_number;
return $this;
}
/**
* Set the fax number.
*
* @param string $new_fax_number the new fax number
*
* @return self
*/
public function setFaxNumber(string $new_fax_number): self
{
$this->fax_number = $new_fax_number;
return $this;
}
/**
* Set the e-mail address.
*
* @param string $new_email_address the new e-mail address
*
* @return self
*/
public function setEmailAddress(string $new_email_address): self
{
$this->email_address = $new_email_address;
return $this;
}
/**
* Set the website.
*
* @param string $new_website the new website
*
* @return self
*/
public function setWebsite(string $new_website): self
{
$this->website = $new_website;
return $this;
}
/**
* Set the link to the website of an article.
*
* @param string $new_url the new URL with the placeholder %PARTNUMBER% for the part number
*
* @return self
*/
public function setAutoProductUrl(string $new_url): self
{
$this->auto_product_url = $new_url;
return $this;
}
}

View file

@ -0,0 +1,125 @@
<?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
*
*/
declare(strict_types=1);
/**
* Part-DB Version 0.4+ "nextgen"
* 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\Entity\Base;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* This class is for managing all database objects.
*
* You should use this class for ALL classes which manages database records!
* (except special tables like "internal"...)
* Every database table which are managed with this class (or a subclass of it)
* must have the table row "id"!! The ID is the unique key to identify the elements.
*
* @ORM\MappedSuperclass()
*
* @ORM\EntityListeners({"App\Security\EntityListeners\ElementPermissionListener"})
*
* @DiscriminatorMap(typeProperty="type", mapping={
* "attachment_type" = "App\Entity\AttachmentType",
* "attachment" = "App\Entity\Attachment",
* "category" = "App\Entity\Attachment",
* "device" = "App\Entity\Device",
* "device_part" = "App\Entity\DevicePart",
* "footprint" = "App\Entity\Footprint",
* "group" = "App\Entity\Group",
* "manufacturer" = "App\Entity\Manufacturer",
* "orderdetail" = "App\Entity\Orderdetail",
* "part" = "App\Entity\Part",
* "pricedetail" = "App\Entity\Pricedetail",
* "storelocation" = "App\Entity\Storelocation",
* "supplier" = "App\Entity\Supplier",
* "user" = "App\Entity\User"
* })
*/
abstract class DBElement
{
/** @var int The Identification number for this part. This value is unique for the element in this table.
* @ORM\Column(type="integer")
* @ORM\Id()
* @ORM\GeneratedValue()
* @Groups({"full"})
*/
protected $id;
/**
* Get the ID. The ID can be zero, or even negative (for virtual elements). If an elemenent is virtual, can be
* checked with isVirtualElement().
*
* Returns null, if the element is not saved to the DB yet.
*
* @return int|null the ID of this element
*/
final public function getID(): ?int
{
return $this->id;
}
/**
* Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14.
*
* @return string The ID as a string;
*
*/
abstract public function getIDString(): string;
public function __clone()
{
//Set ID to null, so that an new entry is created
$this->id = null;
}
}

View file

@ -0,0 +1,178 @@
<?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
*
*/
declare(strict_types=1);
/**
* Part-DB Version 0.4+ "nextgen"
* 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\Entity\Base;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* All subclasses of this class have an attribute "name".
*
* @ORM\MappedSuperclass()
* @ORM\HasLifecycleCallbacks()
*/
abstract class NamedDBElement extends DBElement
{
/**
* @var string The name of this element.
* @ORM\Column(type="string")
* @Assert\NotBlank()
* @Groups({"simple", "extended", "full"})
*/
protected $name = '';
/**
* @var \DateTime The date when this element was modified the last time.
* @ORM\Column(type="datetimetz", name="last_modified")
* @Groups({"extended", "full"})
*/
protected $lastModified;
/**
* @var \DateTime The date when this element was created.
* @ORM\Column(type="datetimetz", name="datetime_added")
* @Groups({"extended", "full"})
*/
protected $addedDate;
/********************************************************************************
*
* Getters
*
*********************************************************************************/
/**
* Get the name.
*
* @return string the name of this element
*/
public function getName(): string
{
/*
//Strip HTML from Name, so no XSS injection is possible.
return strip_tags($this->name); */
return $this->name;
}
/**
* Returns the last time when the element was modified.
* Returns null if the element was not yet saved to DB yet.
*
* @return \DateTime|null The time of the last edit.
*/
public function getLastModified(): ?\DateTime
{
return $this->lastModified;
}
/**
* Returns the date/time when the element was created.
* Returns null if the element was not yet saved to DB yet.
*
* @return \DateTime|null The creation time of the part.
*/
public function getAddedDate(): ?\DateTime
{
return $this->addedDate;
}
/********************************************************************************
*
* Setters
*
*********************************************************************************/
/**
* Change the name of this element.
*
* Spaces at the begin and at the end of the string will be removed
* automatically in NamedDBElement::check_values_validity().
* So you don't have to do this yourself.
*
* @param string $new_name the new name
*
* @return self
*/
public function setName(string $new_name): self
{
$this->name = $new_name;
return $this;
}
/******************************************************************************
*
* Helpers
*
******************************************************************************/
/**
* Helper for updating the timestamp. It is automatically called by doctrine before persisting.
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
$this->lastModified = new \DateTime('now');
if (null === $this->addedDate) {
$this->addedDate = new \DateTime('now');
}
}
public function __toString()
{
return $this->getName();
}
}

View file

@ -0,0 +1,65 @@
<?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
*
*/
declare(strict_types=1);
/**
* Part-DB Version 0.4+ "nextgen"
* 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\Entity\Base;
use Doctrine\ORM\Mapping as ORM;
/**
* Class PartsContainingDBElement.
*
* @ORM\MappedSuperclass()
*/
abstract class PartsContainingDBElement extends StructuralDBElement
{
protected $parts;
}

View file

@ -0,0 +1,326 @@
<?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
*
*/
declare(strict_types=1);
/**
* Part-DB Version 0.4+ "nextgen"
* 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\Entity\Base;
use App\Entity\Attachments\AttachmentContainingDBElement;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\PersistentCollection;
use App\Validator\Constraints\NoneOfItsChildren;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\DiscriminatorMap;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* All elements with the fields "id", "name" and "parent_id" (at least).
*
* This class is for managing all database objects with a structural design.
* All these sub-objects must have the table columns 'id', 'name' and 'parent_id' (at least)!
* The root node has always the ID '0'.
* It's allowed to have instances of root elements, but if you try to change
* an attribute of a root element, you will get an exception!
*
* @ORM\MappedSuperclass(repositoryClass="App\Repository\StructuralDBElementRepository")
*
* @UniqueEntity(fields={"name", "parent"}, ignoreNull=false, message="structural.entity.unique_name")
*/
abstract class StructuralDBElement extends AttachmentContainingDBElement
{
public const ID_ROOT_ELEMENT = 0;
//This is a not standard character, so build a const, so a dev can easily use it
public const PATH_DELIMITER_ARROW = ' → ';
// We can not define the mapping here or we will get an exception. Unfortunatly we have to do the mapping in the
// subclasses
/**
* @var StructuralDBElement[]
* @Groups({"include_children"})
*/
protected $children;
/**
* @var StructuralDBElement
* @NoneOfItsChildren()
* @Groups({"include_parents"})
*/
protected $parent;
/**
* @var string The comment info for this element
* @ORM\Column(type="text")
* @Groups({"simple", "extended", "full"})
*/
protected $comment = "";
/**
* @var int
* @ORM\Column(type="integer", nullable=true)
*/
protected $parent_id;
/**
* @var bool If this property is set, this element can not be selected for part properties.
* Useful if this element should be used only for grouping, sorting.
* @ORM\Column(type="boolean")
*/
protected $not_selectable = false;
/**
* @var int
*/
protected $level = 0;
/** @var string[] all names of all parent elements as a array of strings,
* the last array element is the name of the element itself
*
*/
private $full_path_strings;
public function __construct()
{
$this->children = new ArrayCollection();
}
/******************************************************************************
* StructuralDBElement constructor.
*****************************************************************************/
/**
* Check if this element is a child of another element (recursive).
*
* @param StructuralDBElement $another_element the object to compare
* IMPORTANT: both objects to compare must be from the same class (for example two "Device" objects)!
*
* @return bool True, if this element is child of $another_element.
*
* @throws \InvalidArgumentException if there was an error
*/
public function isChildOf(StructuralDBElement $another_element)
{
$class_name = \get_class($this);
//Check if both elements compared, are from the same type:
if ($class_name != \get_class($another_element)) {
throw new \InvalidArgumentException('isChildOf() only works for objects of the same type!');
}
if (null == $this->getParent()) { // this is the root node
return false;
}
//If this' parents element, is $another_element, then we are finished
return ($this->parent->getID() == $another_element->getID())
|| $this->parent->isChildOf($another_element); //Otherwise, check recursivley
}
/******************************************************************************
*
* Getters
*
******************************************************************************/
/**
* Get the parent-ID
*
* @return integer * the ID of the parent element
* * NULL means, the parent is the root node
* * the parent ID of the root node is -1
*/
protected function getParentID(): int
{
return $this->parent_id ?? self::ID_ROOT_ELEMENT; //Null means root element
}
/**
* Get the parent of this element.
*
* @return StructuralDBElement|null The parent element. Null if this element, does not have a parent.
*/
public function getParent(): ?self
{
return $this->parent;
}
/**
* Get the comment of the element.
* @return string the comment
*/
public function getComment(): ?string
{
return $this->comment;
}
/**
* Get the level.
*
* The level of the root node is -1.
*
* @return int the level of this element (zero means a most top element
* [a subelement of the root node])
*
*/
public function getLevel(): int
{
/**
* Only check for nodes that have a parent. In the other cases zero is correct.
*/
if (0 === $this->level && $this->parent !== null) {
$element = $this->parent;
while ($element !== null) {
/** @var StructuralDBElement $element */
$element = $element->parent;
++$this->level;
}
}
return $this->level;
}
/**
* Get the full path.
*
* @param string $delimeter the delimeter of the returned string
*
* @return string the full path (incl. the name of this element), delimeted by $delimeter
*
*/
public function getFullPath(string $delimeter = self::PATH_DELIMITER_ARROW): string
{
if (!\is_array($this->full_path_strings)) {
$this->full_path_strings = array();
$this->full_path_strings[] = $this->getName();
$element = $this;
$overflow = 20; //We only allow 20 levels depth
while (null != $element->parent && $overflow >= 0) {
$element = $element->parent;
$this->full_path_strings[] = $element->getName();
//Decrement to prevent mem overflow.
$overflow--;
}
$this->full_path_strings = array_reverse($this->full_path_strings);
}
return implode($delimeter, $this->full_path_strings);
}
/**
* Get all subelements of this element.
*
* @param bool $recursive if true, the search is recursive
*
* @return static[] all subelements as an array of objects (sorted by their full path)
*/
public function getSubelements(): iterable
{
return $this->children;
}
public function getChildren(): iterable
{
return $this->children;
}
/******************************************************************************
*
* Setters
*
******************************************************************************/
/**
* Sets the new parent object
* @param self $new_parent The new parent object
* @return StructuralDBElement
*/
public function setParent(?self $new_parent) : self
{
/*
if ($new_parent->isChildOf($this)) {
throw new \InvalidArgumentException('You can not use one of the element childs as parent!');
} */
$this->parent = $new_parent;
return $this;
}
/**
* Set the comment.
* @param string $new_comment the new comment
* @return StructuralDBElement
*/
public function setComment(?string $new_comment): self
{
$this->comment = $new_comment;
return $this;
}
public function setChildren(array $element) : self
{
$this->children = $element;
return $this;
}
public function clearChildren() : self
{
$this->children = new ArrayCollection();
return $this;
}
}