src/Entity/Techcard/Techcard.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Techcard;
  3. use App\Entity\Agro\Operation;
  4. use App\Entity\Client;
  5. use App\Entity\Company;
  6. use App\Entity\Culture;
  7. use App\Entity\Field;
  8. use App\Entity\Monitoring\Monitoring;
  9. use App\Entity\Monitoring\MonitoringParameterValue;
  10. use App\Entity\Season;
  11. use App\Entity\Task\MonitoringObjective;
  12. use App\Entity\Task\Objective;
  13. use App\Entity\User;
  14. use App\Model\CacheRemover;
  15. use App\Repository\Techcard\TechcardRepository;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Doctrine\Common\Collections\Collection;
  18. use Doctrine\ORM\Mapping as ORM;
  19. /**
  20.  * @ORM\Entity(repositoryClass=TechcardRepository::class)
  21.  */
  22. class Techcard
  23. {
  24.     /**
  25.      * @ORM\Id
  26.      * @ORM\GeneratedValue
  27.      * @ORM\Column(type="integer")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @ORM\Column(type="string", length=255)
  32.      */
  33.     private $name;
  34.     /**
  35.      * @ORM\ManyToOne(targetEntity=Season::class, inversedBy="techcards")
  36.      */
  37.     private $season;
  38.     /**
  39.      * @ORM\ManyToOne(targetEntity=Culture::class, inversedBy="techcards")
  40.      */
  41.     private $culture;
  42.     /**
  43.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="techcards")
  44.      */
  45.     private $company;
  46.     /**
  47.      * @ORM\ManyToMany(targetEntity=Operation::class, mappedBy="techcard")
  48.      */
  49.     private $operations;
  50.     /**
  51.      * @ORM\OneToMany(targetEntity=ParametersValue::class, mappedBy="techcard")
  52.      */
  53.     private $parametersValues;
  54.     /**
  55.      * @ORM\OneToMany(targetEntity=Field::class, mappedBy="techcard")
  56.      */
  57.     private $fields;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="techcards")
  60.      */
  61.     private $client;
  62.     /**
  63.      * @ORM\OneToMany(targetEntity=MonitoringParameterValue::class, mappedBy="techcard")
  64.      */
  65.     private $monitoringParameterValues;
  66.     /**
  67.      * @ORM\OneToMany(targetEntity=Monitoring::class, mappedBy="techcard")
  68.      */
  69.     private $monitorings;
  70.     /**
  71.      * @ORM\Column(type="boolean", nullable=true, options={"default":false})
  72.      */
  73.     private $removed;
  74.     /**
  75.      * @ORM\Column(type="string", length=255, nullable=true)
  76.      */
  77.     private $hash;
  78.     /**
  79.      * @ORM\OneToMany(targetEntity=Objective::class, mappedBy="techcard")
  80.      */
  81.     private $objectives;
  82.     /**
  83.      * @ORM\ManyToMany(targetEntity=Field::class, mappedBy="techcards")
  84.      */
  85.     private $fieldsAll;
  86.     /**
  87.      * @ORM\OneToMany(targetEntity=MonitoringObjective::class, mappedBy="techcard")
  88.      */
  89.     private $monitoringObjectives;
  90.     /**
  91.      * @ORM\Column(type="string", length=255, nullable=true, options={"default":"field"})
  92.      */
  93.     private $objectType;
  94.     /**
  95.      * @ORM\ManyToOne(targetEntity=User::class)
  96.      */
  97.     private $owner;
  98.     public function __construct()
  99.     {
  100.         if($this->id === null){
  101.             $this->name "Новая техкарта";
  102.             $this->removed false;
  103.             $this->setHash();
  104.         }
  105.         $this->operations = new ArrayCollection();
  106.         $this->parametersValues = new ArrayCollection();
  107.         $this->fields = new ArrayCollection();
  108.         $this->monitoringParameterValues = new ArrayCollection();
  109.         $this->monitorings = new ArrayCollection();
  110.         $this->objectives = new ArrayCollection();
  111.         $this->fieldsAll = new ArrayCollection();
  112.         $this->monitoringObjectives = new ArrayCollection();
  113.     }
  114.     public function getId(): ?int
  115.     {
  116.         return $this->id;
  117.     }
  118.     public function getName(): ?string
  119.     {
  120.         return $this->name;
  121.     }
  122.     public function setName(string $name): self
  123.     {
  124.         $this->name $name;
  125.         return $this;
  126.     }
  127.     public function getSeason(): ?Season
  128.     {
  129.         return $this->season;
  130.     }
  131.     public function setSeason(?Season $season): self
  132.     {
  133.         $this->season $season;
  134.         return $this;
  135.     }
  136.     public function getCulture(): ?Culture
  137.     {
  138.         return $this->culture;
  139.     }
  140.     public function setCulture(?Culture $culture): self
  141.     {
  142.         $this->culture $culture;
  143.         return $this;
  144.     }
  145.     public function getCompany(): ?Company
  146.     {
  147.         return $this->company;
  148.     }
  149.     public function setCompany(?Company $company): self
  150.     {
  151.         $this->company $company;
  152.         return $this;
  153.     }
  154.     public function __toString()
  155.     {
  156.         return $this->name;
  157.     }
  158.     /**
  159.      * @return Collection|Operation[]
  160.      */
  161.     public function getOperations(): Collection
  162.     {
  163.         return $this->operations;
  164.     }
  165.     /**
  166.      * @return Collection|Operation[]
  167.      */
  168.     public function getEnabledOperations(): Collection
  169.     {
  170.         $enabled =  $this->operations->filter(function ($operation){
  171.             if(!$operation->isRemoved()) {
  172.                 return $operation;
  173.             }
  174.         });
  175.         $sort = [];
  176.         foreach ($enabled as $item) {
  177.             $sort[$item->getPosition()][] = $item;
  178.         }
  179.         krsort($sort);
  180.         $collection = new ArrayCollection();
  181.         foreach ($sort as $key => $items){
  182.             foreach ($items as  $item) {
  183.                 $collection->add($item);
  184.             }
  185.         }
  186.         return $collection;
  187.     }
  188.     public function addOperation(Operation $operation): self
  189.     {
  190.         if (!$this->operations->contains($operation)) {
  191.             $this->operations[] = $operation;
  192.             $operation->addTechcard($this);
  193.         }
  194.         return $this;
  195.     }
  196.     public function removeOperation(Operation $operation): self
  197.     {
  198.         if ($this->operations->removeElement($operation)) {
  199.             $operation->removeTechcard($this);
  200.         }
  201.         return $this;
  202.     }
  203.     /**
  204.      * @return Collection|ParametersValue[]
  205.      */
  206.     public function getParametersValues(): Collection
  207.     {
  208.         return $this->parametersValues;
  209.     }
  210.     public function addParametersValue(ParametersValue $parametersValue): self
  211.     {
  212.         if (!$this->parametersValues->contains($parametersValue)) {
  213.             $this->parametersValues[] = $parametersValue;
  214.             $parametersValue->setTechcard($this);
  215.         }
  216.         return $this;
  217.     }
  218.     public function removeParametersValue(ParametersValue $parametersValue): self
  219.     {
  220.         if ($this->parametersValues->removeElement($parametersValue)) {
  221.             // set the owning side to null (unless already changed)
  222.             if ($parametersValue->getTechcard() === $this) {
  223.                 $parametersValue->setTechcard(null);
  224.             }
  225.         }
  226.         return $this;
  227.     }
  228.     public function getFieldsSize()
  229.     {
  230.         $size 0;
  231.         if($this->getFieldsAll()->count() > 0){
  232.             foreach ($this->getFieldsAll() as $field) {
  233.                 if ($field->isRemoved() or $field->isArchive()){
  234.                     continue;
  235.                 }
  236.                 $size += (float)str_replace(' ','',$field->getSize());
  237.             }
  238.         }
  239.         return number_format($size,2,'.','');
  240.     }
  241.     /**
  242.      * @return Collection|Field[]
  243.      */
  244.     public function getFields(): Collection
  245.     {
  246.         return $this->fields;
  247.     }
  248.     public function addField(Field $field): self
  249.     {
  250.         if (!$this->fields->contains($field)) {
  251.             $this->fields[] = $field;
  252.             $field->setTechcard($this);
  253.         }
  254.         return $this;
  255.     }
  256.     public function removeField(Field $field): self
  257.     {
  258.         if ($this->fields->removeElement($field)) {
  259.             // set the owning side to null (unless already changed)
  260.             if ($field->getTechcard() === $this) {
  261.                 $field->setTechcard(null);
  262.             }
  263.         }
  264.         return $this;
  265.     }
  266.     public function removeAllField(): self
  267.     {
  268.         foreach ($this->fields as $field){
  269.             $this->removeField($field);
  270.             $field->removeTechcard($this);
  271.         }
  272.         return $this;
  273.     }
  274.     public function getClient(): ?Client
  275.     {
  276.         return $this->client;
  277.     }
  278.     public function setClient(?Client $client): self
  279.     {
  280.         $this->client $client;
  281.         return $this;
  282.     }
  283.     /**
  284.      * @return Collection|MonitoringParameterValue[]
  285.      */
  286.     public function getMonitoringParameterValues(): Collection
  287.     {
  288.         return $this->monitoringParameterValues;
  289.     }
  290.     public function addMonitoringParameterValue(MonitoringParameterValue $monitoringParameterValue): self
  291.     {
  292.         if (!$this->monitoringParameterValues->contains($monitoringParameterValue)) {
  293.             $this->monitoringParameterValues[] = $monitoringParameterValue;
  294.             $monitoringParameterValue->setTechcard($this);
  295.         }
  296.         return $this;
  297.     }
  298.     public function removeMonitoringParameterValue(MonitoringParameterValue $monitoringParameterValue): self
  299.     {
  300.         if ($this->monitoringParameterValues->removeElement($monitoringParameterValue)) {
  301.             // set the owning side to null (unless already changed)
  302.             if ($monitoringParameterValue->getTechcard() === $this) {
  303.                 $monitoringParameterValue->setTechcard(null);
  304.             }
  305.         }
  306.         return $this;
  307.     }
  308.     /**
  309.      * @return Collection|Monitoring[]
  310.      */
  311.     public function getMonitorings(): Collection
  312.     {
  313.         return $this->monitorings;
  314.     }
  315.     /**
  316.      * @return Collection|Monitoring[]
  317.      */
  318.     public function getEnabledMonitorings(): Collection
  319.     {
  320.         $enabled =  $this->monitorings->filter(function ($monitoring){
  321.             if(!$monitoring->isRemoved()) {
  322.                 return $monitoring;
  323.             }
  324.         });
  325.         $sort = [];
  326.         foreach ($enabled as $item) {
  327.             $sort[$item->getPosition()][] = $item;
  328.         }
  329.         krsort($sort);
  330.         $collection = new ArrayCollection();
  331.         foreach ($sort as $key => $items){
  332.             foreach ($items as  $item) {
  333.                 $collection->add($item);
  334.             }
  335.         }
  336.         return $collection;
  337.     }
  338.     public function addMonitoring(Monitoring $monitoring): self
  339.     {
  340.         if (!$this->monitorings->contains($monitoring)) {
  341.             $this->monitorings[] = $monitoring;
  342.             $monitoring->setTechcard($this);
  343.         }
  344.         return $this;
  345.     }
  346.     public function removeMonitoring(Monitoring $monitoring): self
  347.     {
  348.         if ($this->monitorings->removeElement($monitoring)) {
  349.             // set the owning side to null (unless already changed)
  350.             if ($monitoring->getTechcard() === $this) {
  351.                 $monitoring->setTechcard(null);
  352.             }
  353.         }
  354.         return $this;
  355.     }
  356.     public function getRemoved(): ?bool
  357.     {
  358.         return $this->removed;
  359.     }
  360.     public function setRemoved(?bool $removed): self
  361.     {
  362.         $this->removed $removed;
  363.         return $this;
  364.     }
  365.     public function getHash(): ?string
  366.     {
  367.         return $this->hash;
  368.     }
  369.     public function setHash(): self
  370.     {
  371.         $this->hash = (string)bin2hex(random_bytes(15));
  372.         return $this;
  373.     }
  374.     public function __clone()
  375.     {
  376.         $this->id null;
  377.         $this->operations = new ArrayCollection();
  378.         $this->parametersValues = new ArrayCollection();
  379.         $this->fields = new ArrayCollection();
  380.         $this->monitoringParameterValues = new ArrayCollection();
  381.         $this->monitorings = new ArrayCollection();
  382.         $this->setHash();
  383.     }
  384.     /**
  385.      * @return Collection<int, Objective>
  386.      */
  387.     public function getObjectives(): Collection
  388.     {
  389.         return $this->objectives;
  390.     }
  391.     public function addObjective(Objective $objective): self
  392.     {
  393.         if (!$this->objectives->contains($objective)) {
  394.             $this->objectives[] = $objective;
  395.             $objective->setTechcard($this);
  396.         }
  397.         return $this;
  398.     }
  399.     /**
  400.      * @return Collection|Field[]
  401.      */
  402.     public function getFieldsAll(): Collection
  403.     {
  404.         return $this->fieldsAll;
  405.     }
  406.     public function addFieldsAll(Field $fieldsAll): self
  407.     {
  408.         if (!$this->fieldsAll->contains($fieldsAll)) {
  409.             $this->fieldsAll[] = $fieldsAll;
  410.             $fieldsAll->addTechcard($this);
  411.         }
  412.         return $this;
  413.     }
  414.     public function removeObjective(Objective $objective): self
  415.     {
  416.         if ($this->objectives->removeElement($objective)) {
  417.             // set the owning side to null (unless already changed)
  418.             if ($objective->getTechcard() === $this) {
  419.                 $objective->setTechcard(null);
  420.             }
  421.         }
  422.         return $this;
  423.     }
  424.     public function removeFieldsAll(Field $fieldsAll): self
  425.     {
  426.         if ($this->fieldsAll->removeElement($fieldsAll)) {
  427.             $fieldsAll->removeTechcard($this);
  428.         }
  429.         return $this;
  430.     }
  431.     /**
  432.      * @return Collection<int, MonitoringObjective>
  433.      */
  434.     public function getMonitoringObjectives(): Collection
  435.     {
  436.         return $this->monitoringObjectives;
  437.     }
  438.     public function addMonitoringObjective(MonitoringObjective $monitoringObjective): self
  439.     {
  440.         if (!$this->monitoringObjectives->contains($monitoringObjective)) {
  441.             $this->monitoringObjectives[] = $monitoringObjective;
  442.             $monitoringObjective->setTechcard($this);
  443.         }
  444.         return $this;
  445.     }
  446.     public function removeMonitoringObjective(MonitoringObjective $monitoringObjective): self
  447.     {
  448.         if ($this->monitoringObjectives->removeElement($monitoringObjective)) {
  449.             // set the owning side to null (unless already changed)
  450.             if ($monitoringObjective->getTechcard() === $this) {
  451.                 $monitoringObjective->setTechcard(null);
  452.             }
  453.         }
  454.         return $this;
  455.     }
  456.     public function getObjectType(): ?string
  457.     {
  458.         return $this->objectType;
  459.     }
  460.     public function setObjectType(?string $object_type): self
  461.     {
  462.         $this->objectType $object_type;
  463.         return $this;
  464.     }
  465.     /**
  466.      * @ORM\PrePersist
  467.      */
  468.     public function prePersist()
  469.     {
  470.         $this->hash md5($this->id.date("Y-m-d H:i:s").rand(0,99999999999));
  471.         CacheRemover::removeCache($this);
  472.     }
  473.     /**
  474.      * @ORM\PreUpdate
  475.      */
  476.     public function preUpdate()
  477.     {
  478.         $this->hash md5($this->id.date("Y-m-d H:i:s").rand(0,99999999999));
  479.         CacheRemover::removeCache($this);
  480.     }
  481.     public function checkTechcardIsViewByUser(User $user): bool
  482.     {
  483.         if(!empty($user->getEmployee()) && in_array($user->getRoleGroup()->getId(),[4,5])) {
  484.             $employeeFieldIds $user->getEmployee()->getFieldIds();
  485.             $techcardFields $this->getFields();
  486.             if (!empty($techcardFields)) {
  487.                 foreach ($techcardFields as $techcardField) {
  488.                     if (in_array($techcardField->getId(), $employeeFieldIds)) {
  489.                         return true;
  490.                     }
  491.                 }
  492.             }
  493.             $techcardFields $this->getFieldsAll();
  494.             if (!empty($techcardFields)) {
  495.                 foreach ($techcardFields as $techcardField) {
  496.                     if (in_array($techcardField->getId(), $employeeFieldIds)) {
  497.                         return true;
  498.                     }
  499.                 }
  500.             }
  501.             return false;
  502.         }
  503.         return true;
  504.     }
  505.     public function getOwner(): ?User
  506.     {
  507.         return $this->owner;
  508.     }
  509.     public function setOwner(?User $owner): self
  510.     {
  511.         $this->owner $owner;
  512.         return $this;
  513.     }
  514. }