<?php
namespace App\Entity;
use App\Business\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="App\Repository\ProviderRepository")
* @ORM\Table(name="provider")
*/
class Provider
{
const MAIN_PROVIDER_ID = 1;
use TimestampableEntity;
use BlameableEntity;
/**
* @var int|null
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string
* @ORM\Column(name="title", type="string")
*/
private $title;
/**
* @Gedmo\Translatable
* @Gedmo\Slug(fields={"title"}, updatable=false)
* @ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
/**
* @var string
* @ORM\Column(type="text", nullable=true)
*/
private $content;
/**
* @var string
* @ORM\Column(type="text", nullable=true)
*/
private $rfpDescription;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\RequestForProposal", mappedBy="provider", cascade={"all"}, orphanRemoval=true)
* @ORM\OrderBy({"deadline" = "ASC"})
*
*/
private $rfps;
public function __construct()
{
$this->rfps = new ArrayCollection();
}
/**
* @return string
*/
public function __toString(): string
{
return (string)$this->title;
}
public function toString(): string
{
return "{id=" . $this->id . ",title=" . $this->title . "}";
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return string
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string|null $title
*
* @return self
*/
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* @param string $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* @return string
*/
public function getContent(): ?string
{
return $this->content;
}
/**
* @param string|null $content
*
* @return self
*/
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
/**
* @return string
*/
public function getRfpDescription(): ?string
{
return $this->rfpDescription;
}
/**
* @param string|null $rfpDescription
*/
public function setRfpDescription(?string $rfpDescription): self
{
$this->rfpDescription = $rfpDescription;
return $this;
}
/**
* Remove rfps
*
* @param RequestForProposal $rfp
*/
public function removeRfp(RequestForProposal $rfp)
{
$this->rfps->removeElement($rfp);
}
/**
* Get rfps
*
* @return Collection
*/
public function getRfps()
{
return $this->rfps;
}
/**
* Set rfp
*
* @param Collection
* @return $this
*/
public function setRfps($rfps)
{
if (!empty($rfps) && count($rfps ?? []) > 0) {
foreach ($rfps as $rfp) {
$this->addRfp($rfp);
}
}
return $this;
}
/**
* Add rfp
*
* @param RequestForProposal $rfp
*
* @return Provider
*/
public function addRfp(RequestForProposal $rfp)
{
$rfp->setProvider($this);
$this->rfps[] = $rfp;
return $this;
}
private function cleanRfps()
{
if (!empty($this->getRfps())) {
foreach ($this->getRfps() as $rfp) {
$this->removeRfp($rfp);
}
}
}
private function getRfpsByGroups(?array $userGroups)
{
$rfpsToShow = [];
if (!empty($userGroups)) {
if (!empty($this->getRfps())) {
foreach ($this->getRfps() as $rfp) {
foreach ($rfp->getGroupRights() as $group) {
if (in_array($group->getUid(), $userGroups)) {
$rfpsToShow[] = $rfp;
continue;
}
}
}
}
}
return $rfpsToShow;
}
private function getRfpsByCompanyUid(?string $companyUid)
{
$rfpsToShow = [];
if (!empty($companyUid)) {
if (!empty($this->getRfps())) {
foreach ($this->getRfps() as $rfp) {
foreach ($rfp->getCompanyRights() as $rfpCompany) {
if ($companyUid === $rfpCompany->getUid()) {
$rfpsToShow[] = $rfp;
}
}
}
}
}
return $rfpsToShow;
}
private function hidePastRfps()
{
$now = new \DateTime(date('Y-m-d'));
$rfpsToShow = null;
if (!empty($this->getRfps())) {
foreach ($this->getRfps() as $rfp) {
if ($rfp->getDeadline() >= $now) {
$rfpsToShow[] = $rfp;
}
}
}
$this->cleanRfps();
$this->setRfps($rfpsToShow);
return $this;
}
public function getRfpsByRights(UserInterface $user)
{
$userCompanyUid = null;
if (!empty($user) ) {
$userCompanyUid = $user->getCompanyUid();
}
$this->hidePastRfps();
$rfpsByCompany = $this->getRfpsByCompanyUid($userCompanyUid);
$rfpsToShow = array_unique($rfpsByCompany);
usort($rfpsToShow, function($a, $b) {
if ($a->getDeadline() == $b->getDeadline()) {
return 0;
}
return $a->getDeadline() < $b->getDeadline() ? -1 : 1;
});
return $rfpsToShow;
}
}