added tags for timesheets (#604)

This commit is contained in:
Mathias
2019-05-12 01:40:04 +02:00
committed by Kevin Papst
parent f31118292c
commit e29e183e84
84 changed files with 2132 additions and 158 deletions

118
src/Entity/Tag.php Normal file
View File

@@ -0,0 +1,118 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="kimai2_tags",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"name"})
* }
* )
* @ORM\Entity(repositoryClass="App\Repository\TagRepository")
* @UniqueEntity("name")
*/
class Tag
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
* @Assert\NotBlank()
* @Assert\Length(min=2, max=255)
*/
private $name;
/**
* @var Timesheet[]|ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Timesheet", mappedBy="tags", fetch="EXTRA_LAZY")
*/
protected $timesheets;
public function __construct()
{
$this->timesheets = new ArrayCollection();
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param string $tagName
* @return Tag
*/
public function setName($tagName)
{
$this->name = $tagName;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param Timesheet $timesheet
*/
public function addTimesheet(Timesheet $timesheet)
{
if ($this->timesheets->contains($timesheet)) {
return;
}
$this->timesheets->add($timesheet);
$timesheet->addTag($this);
}
/**
* @param Timesheet $timesheet
*/
public function removeTimesheet(Timesheet $timesheet)
{
if (!$this->timesheets->contains($timesheet)) {
return;
}
$this->timesheets->removeElement($timesheet);
$timesheet->removeTag($this);
}
/**
* @return string
*/
public function __toString()
{
return $this->getName();
}
}

View File

@@ -9,6 +9,7 @@
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
@@ -122,6 +123,30 @@ class Timesheet
*/
private $exported = false;
/**
* @var \App\Entity\Tag[]
*
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="timesheets", cascade={"persist"})
* @ORM\JoinTable(
* name="kimai2_timesheet_tags",
* joinColumns={
* @ORM\JoinColumn(name="timesheet_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
* }
* )
*/
protected $tags;
/**
* Default constructor, initializes collections
*/
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* Get entry id
*
@@ -319,6 +344,54 @@ class Timesheet
return $this->rate;
}
/**
* @param Tag $tag
* @return Timesheet
*/
public function addTag(Tag $tag)
{
if ($this->tags->contains($tag)) {
return $this;
}
$this->tags->add($tag);
$tag->addTimesheet($this);
return $this;
}
/**
* @param Tag $tag
*/
public function removeTag(Tag $tag)
{
if (!$this->tags->contains($tag)) {
return;
}
$this->tags->removeElement($tag);
$tag->removeTimesheet($this);
}
/**
* @return Tag[]|ArrayCollection
*/
public function getTags()
{
return $this->tags;
}
/**
* @return string[]
*/
public function getTagsAsArray()
{
return array_map(
function (Tag $element) {
return $element->getName();
},
$this->getTags()->toArray()
);
}
/**
* @return bool
*/