src/Entity/Provider.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Business\User;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\Common\Collections\Criteria;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. /**
  11.  * @ORM\Entity
  12.  * @ORM\Entity(repositoryClass="App\Repository\ProviderRepository")
  13.  * @ORM\Table(name="provider")
  14.  */
  15. class Provider
  16. {
  17.     const MAIN_PROVIDER_ID 1;
  18.     use TimestampableEntity;
  19.     use BlameableEntity;
  20.     /**
  21.      * @var int|null
  22.      * @ORM\Id
  23.      * @ORM\Column(type="integer")
  24.      * @ORM\GeneratedValue
  25.      */
  26.     private $id;
  27.     /**
  28.      * @var string
  29.      * @ORM\Column(name="title", type="string")
  30.      */
  31.     private $title;
  32.     /**
  33.      * @Gedmo\Translatable
  34.      * @Gedmo\Slug(fields={"title"}, updatable=false)
  35.      * @ORM\Column(name="slug", type="string", length=255)
  36.      */
  37.     private $slug;
  38.     /**
  39.      * @var string
  40.      * @ORM\Column(type="text", nullable=true)
  41.      */
  42.     private $content;
  43.     /**
  44.      * @var string
  45.      * @ORM\Column(type="text", nullable=true)
  46.      */
  47.     private $rfpDescription;
  48.     /**
  49.      * @var ArrayCollection
  50.      * @ORM\OneToMany(targetEntity="App\Entity\RequestForProposal", mappedBy="provider", cascade={"all"}, orphanRemoval=true)
  51.      * @ORM\OrderBy({"deadline" = "ASC"})
  52.      *
  53.      */
  54.     private $rfps;
  55.     public function __construct()
  56.     {
  57.         $this->rfps = new ArrayCollection();
  58.     }
  59.     /**
  60.      * @return string
  61.      */
  62.     public function __toString(): string
  63.     {
  64.         return (string)$this->title;
  65.     }
  66.     public function toString(): string
  67.     {
  68.         return "{id=" $this->id ",title=" $this->title "}";
  69.     }
  70.     /**
  71.      * @return int|null
  72.      */
  73.     public function getId(): ?int
  74.     {
  75.         return $this->id;
  76.     }
  77.     /**
  78.      * @return string
  79.      */
  80.     public function getTitle(): ?string
  81.     {
  82.         return $this->title;
  83.     }
  84.     /**
  85.      * @param string|null $title
  86.      *
  87.      * @return self
  88.      */
  89.     public function setTitle(?string $title): self
  90.     {
  91.         $this->title $title;
  92.         return $this;
  93.     }
  94.     /**
  95.      * @return string
  96.      */
  97.     public function getSlug()
  98.     {
  99.         return $this->slug;
  100.     }
  101.     /**
  102.      * @param string $slug
  103.      */
  104.     public function setSlug($slug)
  105.     {
  106.         $this->slug $slug;
  107.     }
  108.     /**
  109.      * @return string
  110.      */
  111.     public function getContent(): ?string
  112.     {
  113.         return $this->content;
  114.     }
  115.     /**
  116.      * @param string|null $content
  117.      *
  118.      * @return self
  119.      */
  120.     public function setContent(?string $content): self
  121.     {
  122.         $this->content $content;
  123.         return $this;
  124.     }
  125.     /**
  126.      * @return string
  127.      */
  128.     public function getRfpDescription(): ?string
  129.     {
  130.         return $this->rfpDescription;
  131.     }
  132.     /**
  133.      * @param string|null $rfpDescription
  134.      */
  135.     public function setRfpDescription(?string $rfpDescription): self
  136.     {
  137.         $this->rfpDescription $rfpDescription;
  138.         return $this;
  139.     }
  140.     /**
  141.      * Remove rfps
  142.      *
  143.      * @param RequestForProposal $rfp
  144.      */
  145.     public function removeRfp(RequestForProposal $rfp)
  146.     {
  147.         $this->rfps->removeElement($rfp);
  148.     }
  149.     /**
  150.      * Get rfps
  151.      *
  152.      * @return Collection
  153.      */
  154.     public function getRfps()
  155.     {
  156.         return $this->rfps;
  157.     }
  158.     /**
  159.      * Set rfp
  160.      *
  161.      * @param Collection
  162.      * @return $this
  163.      */
  164.     public function setRfps($rfps)
  165.     {
  166.         if (!empty($rfps) && count($rfps ?? []) > 0) {
  167.             foreach ($rfps as $rfp) {
  168.                 $this->addRfp($rfp);
  169.             }
  170.         }
  171.         return $this;
  172.     }
  173.     /**
  174.      * Add rfp
  175.      *
  176.      * @param RequestForProposal $rfp
  177.      *
  178.      * @return Provider
  179.      */
  180.     public function addRfp(RequestForProposal $rfp)
  181.     {
  182.         $rfp->setProvider($this);
  183.         $this->rfps[] = $rfp;
  184.         return $this;
  185.     }
  186.     private function cleanRfps()
  187.     {
  188.         if (!empty($this->getRfps())) {
  189.             foreach ($this->getRfps() as $rfp) {
  190.                 $this->removeRfp($rfp);
  191.             }
  192.         }
  193.     }
  194.     private function getRfpsByGroups(?array $userGroups)
  195.     {
  196.         $rfpsToShow = [];
  197.         if (!empty($userGroups)) {
  198.             if (!empty($this->getRfps())) {
  199.                 foreach ($this->getRfps() as $rfp) {
  200.                     foreach ($rfp->getGroupRights() as $group) {
  201.                         if (in_array($group->getUid(), $userGroups)) {
  202.                             $rfpsToShow[] = $rfp;
  203.                             continue;
  204.                         }
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.         return $rfpsToShow;
  210.     }
  211.     private function getRfpsByCompanyUid(?string $companyUid)
  212.     {
  213.         $rfpsToShow = [];
  214.         if (!empty($companyUid)) {
  215.             if (!empty($this->getRfps())) {
  216.                 foreach ($this->getRfps() as $rfp) {
  217.                     foreach ($rfp->getCompanyRights() as $rfpCompany) {
  218.                         if ($companyUid === $rfpCompany->getUid()) {
  219.                             $rfpsToShow[] = $rfp;
  220.                         }
  221.                     }
  222.                 }
  223.             }
  224.         }
  225.         return $rfpsToShow;
  226.     }
  227.     private function hidePastRfps()
  228.     {
  229.         $now = new \DateTime(date('Y-m-d'));
  230.         $rfpsToShow null;
  231.         if (!empty($this->getRfps())) {
  232.             foreach ($this->getRfps() as $rfp) {
  233.                 if ($rfp->getDeadline() >= $now) {
  234.                     $rfpsToShow[] = $rfp;
  235.                 }
  236.             }
  237.         }
  238.         $this->cleanRfps();
  239.         $this->setRfps($rfpsToShow);
  240.         return $this;
  241.     }
  242.     public function getRfpsByRights(UserInterface $user)
  243.     {
  244.         $userCompanyUid null;
  245.         if (!empty($user) ) {
  246.             $userCompanyUid $user->getCompanyUid();
  247.         }
  248.         $this->hidePastRfps();
  249.         $rfpsByCompany $this->getRfpsByCompanyUid($userCompanyUid);
  250.         $rfpsToShow array_unique($rfpsByCompany);
  251.         usort($rfpsToShow, function($a$b) {
  252.             if ($a->getDeadline() == $b->getDeadline()) {
  253.                 return 0;
  254.             }
  255.             return $a->getDeadline() < $b->getDeadline() ? -1;
  256.         });
  257.         return $rfpsToShow;
  258.     }
  259. }