<?php
namespace App\Entity\Techcard;
use App\Entity\Agro\Operation;
use App\Entity\Client;
use App\Entity\Company;
use App\Entity\Culture;
use App\Entity\Field;
use App\Entity\Monitoring\Monitoring;
use App\Entity\Monitoring\MonitoringParameterValue;
use App\Entity\Season;
use App\Entity\Task\MonitoringObjective;
use App\Entity\Task\Objective;
use App\Entity\User;
use App\Model\CacheRemover;
use App\Repository\Techcard\TechcardRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TechcardRepository::class)
*/
class Techcard
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Season::class, inversedBy="techcards")
*/
private $season;
/**
* @ORM\ManyToOne(targetEntity=Culture::class, inversedBy="techcards")
*/
private $culture;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="techcards")
*/
private $company;
/**
* @ORM\ManyToMany(targetEntity=Operation::class, mappedBy="techcard")
*/
private $operations;
/**
* @ORM\OneToMany(targetEntity=ParametersValue::class, mappedBy="techcard")
*/
private $parametersValues;
/**
* @ORM\OneToMany(targetEntity=Field::class, mappedBy="techcard")
*/
private $fields;
/**
* @ORM\ManyToOne(targetEntity=Client::class, inversedBy="techcards")
*/
private $client;
/**
* @ORM\OneToMany(targetEntity=MonitoringParameterValue::class, mappedBy="techcard")
*/
private $monitoringParameterValues;
/**
* @ORM\OneToMany(targetEntity=Monitoring::class, mappedBy="techcard")
*/
private $monitorings;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default":false})
*/
private $removed;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $hash;
/**
* @ORM\OneToMany(targetEntity=Objective::class, mappedBy="techcard")
*/
private $objectives;
/**
* @ORM\ManyToMany(targetEntity=Field::class, mappedBy="techcards")
*/
private $fieldsAll;
/**
* @ORM\OneToMany(targetEntity=MonitoringObjective::class, mappedBy="techcard")
*/
private $monitoringObjectives;
/**
* @ORM\Column(type="string", length=255, nullable=true, options={"default":"field"})
*/
private $objectType;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private $owner;
public function __construct()
{
if($this->id === null){
$this->name = "Новая техкарта";
$this->removed = false;
$this->setHash();
}
$this->operations = new ArrayCollection();
$this->parametersValues = new ArrayCollection();
$this->fields = new ArrayCollection();
$this->monitoringParameterValues = new ArrayCollection();
$this->monitorings = new ArrayCollection();
$this->objectives = new ArrayCollection();
$this->fieldsAll = new ArrayCollection();
$this->monitoringObjectives = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSeason(): ?Season
{
return $this->season;
}
public function setSeason(?Season $season): self
{
$this->season = $season;
return $this;
}
public function getCulture(): ?Culture
{
return $this->culture;
}
public function setCulture(?Culture $culture): self
{
$this->culture = $culture;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function __toString()
{
return $this->name;
}
/**
* @return Collection|Operation[]
*/
public function getOperations(): Collection
{
return $this->operations;
}
/**
* @return Collection|Operation[]
*/
public function getEnabledOperations(): Collection
{
$enabled = $this->operations->filter(function ($operation){
if(!$operation->isRemoved()) {
return $operation;
}
});
$sort = [];
foreach ($enabled as $item) {
$sort[$item->getPosition()][] = $item;
}
krsort($sort);
$collection = new ArrayCollection();
foreach ($sort as $key => $items){
foreach ($items as $item) {
$collection->add($item);
}
}
return $collection;
}
public function addOperation(Operation $operation): self
{
if (!$this->operations->contains($operation)) {
$this->operations[] = $operation;
$operation->addTechcard($this);
}
return $this;
}
public function removeOperation(Operation $operation): self
{
if ($this->operations->removeElement($operation)) {
$operation->removeTechcard($this);
}
return $this;
}
/**
* @return Collection|ParametersValue[]
*/
public function getParametersValues(): Collection
{
return $this->parametersValues;
}
public function addParametersValue(ParametersValue $parametersValue): self
{
if (!$this->parametersValues->contains($parametersValue)) {
$this->parametersValues[] = $parametersValue;
$parametersValue->setTechcard($this);
}
return $this;
}
public function removeParametersValue(ParametersValue $parametersValue): self
{
if ($this->parametersValues->removeElement($parametersValue)) {
// set the owning side to null (unless already changed)
if ($parametersValue->getTechcard() === $this) {
$parametersValue->setTechcard(null);
}
}
return $this;
}
public function getFieldsSize()
{
$size = 0;
if($this->getFieldsAll()->count() > 0){
foreach ($this->getFieldsAll() as $field) {
if ($field->isRemoved() or $field->isArchive()){
continue;
}
$size += (float)str_replace(' ','',$field->getSize());
}
}
return number_format($size,2,'.','');
}
/**
* @return Collection|Field[]
*/
public function getFields(): Collection
{
return $this->fields;
}
public function addField(Field $field): self
{
if (!$this->fields->contains($field)) {
$this->fields[] = $field;
$field->setTechcard($this);
}
return $this;
}
public function removeField(Field $field): self
{
if ($this->fields->removeElement($field)) {
// set the owning side to null (unless already changed)
if ($field->getTechcard() === $this) {
$field->setTechcard(null);
}
}
return $this;
}
public function removeAllField(): self
{
foreach ($this->fields as $field){
$this->removeField($field);
$field->removeTechcard($this);
}
return $this;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
/**
* @return Collection|MonitoringParameterValue[]
*/
public function getMonitoringParameterValues(): Collection
{
return $this->monitoringParameterValues;
}
public function addMonitoringParameterValue(MonitoringParameterValue $monitoringParameterValue): self
{
if (!$this->monitoringParameterValues->contains($monitoringParameterValue)) {
$this->monitoringParameterValues[] = $monitoringParameterValue;
$monitoringParameterValue->setTechcard($this);
}
return $this;
}
public function removeMonitoringParameterValue(MonitoringParameterValue $monitoringParameterValue): self
{
if ($this->monitoringParameterValues->removeElement($monitoringParameterValue)) {
// set the owning side to null (unless already changed)
if ($monitoringParameterValue->getTechcard() === $this) {
$monitoringParameterValue->setTechcard(null);
}
}
return $this;
}
/**
* @return Collection|Monitoring[]
*/
public function getMonitorings(): Collection
{
return $this->monitorings;
}
/**
* @return Collection|Monitoring[]
*/
public function getEnabledMonitorings(): Collection
{
$enabled = $this->monitorings->filter(function ($monitoring){
if(!$monitoring->isRemoved()) {
return $monitoring;
}
});
$sort = [];
foreach ($enabled as $item) {
$sort[$item->getPosition()][] = $item;
}
krsort($sort);
$collection = new ArrayCollection();
foreach ($sort as $key => $items){
foreach ($items as $item) {
$collection->add($item);
}
}
return $collection;
}
public function addMonitoring(Monitoring $monitoring): self
{
if (!$this->monitorings->contains($monitoring)) {
$this->monitorings[] = $monitoring;
$monitoring->setTechcard($this);
}
return $this;
}
public function removeMonitoring(Monitoring $monitoring): self
{
if ($this->monitorings->removeElement($monitoring)) {
// set the owning side to null (unless already changed)
if ($monitoring->getTechcard() === $this) {
$monitoring->setTechcard(null);
}
}
return $this;
}
public function getRemoved(): ?bool
{
return $this->removed;
}
public function setRemoved(?bool $removed): self
{
$this->removed = $removed;
return $this;
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(): self
{
$this->hash = (string)bin2hex(random_bytes(15));
return $this;
}
public function __clone()
{
$this->id = null;
$this->operations = new ArrayCollection();
$this->parametersValues = new ArrayCollection();
$this->fields = new ArrayCollection();
$this->monitoringParameterValues = new ArrayCollection();
$this->monitorings = new ArrayCollection();
$this->setHash();
}
/**
* @return Collection<int, Objective>
*/
public function getObjectives(): Collection
{
return $this->objectives;
}
public function addObjective(Objective $objective): self
{
if (!$this->objectives->contains($objective)) {
$this->objectives[] = $objective;
$objective->setTechcard($this);
}
return $this;
}
/**
* @return Collection|Field[]
*/
public function getFieldsAll(): Collection
{
return $this->fieldsAll;
}
public function addFieldsAll(Field $fieldsAll): self
{
if (!$this->fieldsAll->contains($fieldsAll)) {
$this->fieldsAll[] = $fieldsAll;
$fieldsAll->addTechcard($this);
}
return $this;
}
public function removeObjective(Objective $objective): self
{
if ($this->objectives->removeElement($objective)) {
// set the owning side to null (unless already changed)
if ($objective->getTechcard() === $this) {
$objective->setTechcard(null);
}
}
return $this;
}
public function removeFieldsAll(Field $fieldsAll): self
{
if ($this->fieldsAll->removeElement($fieldsAll)) {
$fieldsAll->removeTechcard($this);
}
return $this;
}
/**
* @return Collection<int, MonitoringObjective>
*/
public function getMonitoringObjectives(): Collection
{
return $this->monitoringObjectives;
}
public function addMonitoringObjective(MonitoringObjective $monitoringObjective): self
{
if (!$this->monitoringObjectives->contains($monitoringObjective)) {
$this->monitoringObjectives[] = $monitoringObjective;
$monitoringObjective->setTechcard($this);
}
return $this;
}
public function removeMonitoringObjective(MonitoringObjective $monitoringObjective): self
{
if ($this->monitoringObjectives->removeElement($monitoringObjective)) {
// set the owning side to null (unless already changed)
if ($monitoringObjective->getTechcard() === $this) {
$monitoringObjective->setTechcard(null);
}
}
return $this;
}
public function getObjectType(): ?string
{
return $this->objectType;
}
public function setObjectType(?string $object_type): self
{
$this->objectType = $object_type;
return $this;
}
/**
* @ORM\PrePersist
*/
public function prePersist()
{
$this->hash = md5($this->id.date("Y-m-d H:i:s").rand(0,99999999999));
CacheRemover::removeCache($this);
}
/**
* @ORM\PreUpdate
*/
public function preUpdate()
{
$this->hash = md5($this->id.date("Y-m-d H:i:s").rand(0,99999999999));
CacheRemover::removeCache($this);
}
public function checkTechcardIsViewByUser(User $user): bool
{
if(!empty($user->getEmployee()) && in_array($user->getRoleGroup()->getId(),[4,5])) {
$employeeFieldIds = $user->getEmployee()->getFieldIds();
$techcardFields = $this->getFields();
if (!empty($techcardFields)) {
foreach ($techcardFields as $techcardField) {
if (in_array($techcardField->getId(), $employeeFieldIds)) {
return true;
}
}
}
$techcardFields = $this->getFieldsAll();
if (!empty($techcardFields)) {
foreach ($techcardFields as $techcardField) {
if (in_array($techcardField->getId(), $employeeFieldIds)) {
return true;
}
}
}
return false;
}
return true;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): self
{
$this->owner = $owner;
return $this;
}
}