<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
//use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping\OrderBy;
use JsonSerializable;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, JsonSerializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=255)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=255)
*/
private $lastname;
/**
* @ORM\Column(type="boolean")
*/
private $status;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $telephone;
/**
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $activity;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $section_id;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $position_id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Section", inversedBy="users")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $section;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Position", inversedBy="users")
* @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
*/
private $position;
/**
* @ORM\Column(type="string", length=255)
*/
private $password;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $username;
/**
* @ORM\Column(type="boolean")
*/
private $admin;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $hash;
/**
* @ORM\Column(type="array", nullable=true)
*/
private $access = [];
/**
* @ORM\OneToMany(targetEntity="App\Entity\Project", mappedBy="author")
*/
private $ownedProjects;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Project", mappedBy="assigned")
*/
private $assignedProjects;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Prognosis", mappedBy="author")
*/
private $prognoses;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Prognosis", mappedBy="assigned")
*/
private $assignedPrognoses;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $theme = 'default';
/**
* @ORM\OneToOne(targetEntity="App\Entity\UserTeam", mappedBy="leader", cascade={"persist", "remove"})
*/
private $leader;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\UserTeam", inversedBy="users")
*/
private $team;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $bc_token;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $bc_token_expire;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $bc_user_id;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\KnowledgeCategory", mappedBy="users")
* @OrderBy({"ordering" = "ASC"})
*/
private $knowledgeCategories;
/**
* @ORM\OneToMany(targetEntity="App\Entity\TimerEntry", mappedBy="user", orphanRemoval=true)
*/
private $timerEntries;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Project", mappedBy="users")
*/
private $projects;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $hourValue;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $daysOff;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private $supervisor;
/**
* @ORM\OneToMany(targetEntity="App\Entity\InvoiceRequest", mappedBy="author", orphanRemoval=true)
*/
private $invoiceRequests;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $avatar;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $slackUserId;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $color;
public function __construct()
{
$this->created = new \DateTime();
$this->ownedProjects = new ArrayCollection();
$this->assignedProjects = new ArrayCollection();
$this->prognoses = new ArrayCollection();
$this->assignedPrognoses = new ArrayCollection();
$this->knowledgeCategories = new ArrayCollection();
$this->invoiceRequests = new ArrayCollection();
$this->timerEntries = new ArrayCollection();
$this->projects = new ArrayCollection();
$this->daysOff = 0;
}
public function __toString()
{
return $this->lastname . " " . $this->firstname;
}
public function jsonSerialize()
{
return array(
'username'=> $this->username,
'firstname' => $this->firstname,
'lastname' => $this->lastname,
'position' => (string) $this->position,
'section' => (string) $this->section,
'isAdmin' => $this->isAdmin(),
'email' => $this->email,
'telephone' => $this->telephone,
'activity' => $this->activity
);
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function getStatusName(): ?string
{
return $this->status ? 'Aktywny' : 'Zablokowany';
}
public function setStatus(bool $status): self
{
$this->status = $status;
return $this;
}
public function getTelephone(): ?string
{
return $this->telephone;
}
public function setTelephone(?string $telephone): self
{
$this->telephone = $telephone;
return $this;
}
public function getCreated(): ?\DateTimeInterface
{
return $this->created;
}
public function setCreated(\DateTimeInterface $created): self
{
$this->created = $created;
return $this;
}
public function getActivity(): ?\DateTimeInterface
{
return $this->activity;
}
public function setActivity(\DateTimeInterface $activity): self
{
$this->activity = $activity;
return $this;
}
public function getSectionId(): ?int
{
return $this->section_id;
}
public function setSectionId(int $section_id): self
{
$this->section_id = $section_id;
return $this;
}
public function getPositionId(): ?int
{
return $this->position_id;
}
public function setPositionId(int $position_id): self
{
$this->position_id = $position_id;
return $this;
}
public function getSection(): ?Section
{
return $this->section;
}
public function setSection(?Section $section): self
{
$this->section = $section;
return $this;
}
public function getPosition(): ?Position
{
return $this->position;
}
public function setPosition(?Position $position): self
{
$this->position = $position;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
//SECURITY
public function isAdmin(){
return $this->admin;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getRoles()
{
return $this->isAdmin() ? array('ROLE_ADMIN','ROLE_USER') : ( $this->canImpersonate() ? ['ROLE_USER', 'ROLE_ALLOWED_TO_SWITCH'] : array('ROLE_USER'));
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, array('allowed_classes' => false));
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getAdmin(): ?bool
{
return $this->admin;
}
public function setAdmin(bool $admin): self
{
$this->admin = $admin;
return $this;
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(?string $hash): self
{
$this->hash = $hash;
return $this;
}
//USER AUTH ITEMS
public function isEnabled(){
return $this->status == 1;
}
public function isAccountNonExpired(){
return $this->status == 1;
}
public function isAccountNonLocked(){
return $this->status <= 1;
}
public function isCredentialsNonExpired(){
return $this->status == 1;
}
public function getAccess(): ?array
{
return $this->access;
}
public function setAccess(array $access): self
{
$this->access = $access;
return $this;
}
/**
* @return Collection|Project[]
*/
public function getOwnedProjects(): Collection
{
return $this->ownedProjects;
}
public function addOwnedProject(Project $project): self
{
if (!$this->ownedProjects->contains($project)) {
$this->ownedProjects[] = $project;
$project->setAuthor($this);
}
return $this;
}
public function removeOwnedProject(Project $project): self
{
if ($this->ownedProjects->contains($project)) {
$this->ownedProjects->removeElement($project);
// set the owning side to null (unless already changed)
if ($project->getAuthor() === $this) {
$project->setAuthor(null);
}
}
return $this;
}
/**
* @return Collection|Project[]
*/
public function getAssignedProjects(): Collection
{
return $this->assignedProjects;
}
public function addAssignedProject(Project $assignedProject): self
{
if (!$this->assignedProjects->contains($assignedProject)) {
$this->assignedProjects[] = $assignedProject;
$assignedProject->setAssigned($this);
}
return $this;
}
public function removeAssignedProject(Project $assignedProject): self
{
if ($this->assignedProjects->contains($assignedProject)) {
$this->assignedProjects->removeElement($assignedProject);
// set the owning side to null (unless already changed)
if ($assignedProject->getAssigned() === $this) {
$assignedProject->setAssigned(null);
}
}
return $this;
}
/**
* @return Collection|Prognosis[]
*/
public function getPrognoses(): Collection
{
return $this->prognoses;
}
public function addPrognosis(Prognosis $prognosis): self
{
if (!$this->prognoses->contains($prognosis)) {
$this->prognoses[] = $prognosis;
$prognosis->setAuthor($this);
}
return $this;
}
public function removePrognosis(Prognosis $prognosis): self
{
if ($this->prognoses->contains($prognosis)) {
$this->prognoses->removeElement($prognosis);
// set the owning side to null (unless already changed)
if ($prognosis->getAuthor() === $this) {
$prognosis->setAuthor(null);
}
}
return $this;
}
/**
* @return Collection|Prognosis[]
*/
public function getAssignedPrognoses(): Collection
{
return $this->assignedPrognoses;
}
public function addAssignedPrognosis(Prognosis $assignedPrognosis): self
{
if (!$this->assignedPrognoses->contains($assignedPrognosis)) {
$this->assignedPrognoses[] = $assignedPrognosis;
$assignedPrognosis->setAssigned($this);
}
return $this;
}
public function removeAssignedPrognosis(Prognosis $assignedPrognosis): self
{
if ($this->assignedPrognoses->contains($assignedPrognosis)) {
$this->assignedPrognoses->removeElement($assignedPrognosis);
// set the owning side to null (unless already changed)
if ($assignedPrognosis->getAssigned() === $this) {
$assignedPrognosis->setAssigned(null);
}
}
return $this;
}
public function getTheme(): ?string
{
return $this->theme;
}
public function setTheme(?string $theme): self
{
$this->theme = $theme;
return $this;
}
public function getLeader(): ?UserTeam
{
return $this->leader;
}
public function setLeader(UserTeam $leader): self
{
$this->leader = $leader;
// set the owning side of the relation if necessary
if ($this !== $leader->getLeader()) {
$leader->setLeader($this);
}
return $this;
}
public function getTeam(): ?UserTeam
{
return $this->team;
}
public function setTeam(?UserTeam $team): self
{
$this->team = $team;
return $this;
}
public function getBcToken(): ?string
{
return $this->bc_token;
}
public function setBcToken(?string $bc_token): self
{
$this->bc_token = $bc_token;
return $this;
}
public function getBcTokenExpire(): ?\DateTimeInterface
{
return $this->bc_token_expire;
}
public function setBcTokenExpire(?\DateTimeInterface $bc_token_expire): self
{
$this->bc_token_expire = $bc_token_expire;
return $this;
}
public function getBcUserId(): ?int
{
return $this->bc_user_id;
}
public function setBcUserId(?int $bc_user_id): self
{
$this->bc_user_id = $bc_user_id;
return $this;
}
/**
* @return Collection|KnowledgeCategory[]
*/
public function getKnowledgeCategories(): Collection
{
return $this->knowledgeCategories;
}
public function addKnowledgeCategory(KnowledgeCategory $knowledgeCategory): self
{
if (!$this->knowledgeCategories->contains($knowledgeCategory)) {
$this->knowledgeCategories[] = $knowledgeCategory;
$knowledgeCategory->addUser($this);
}
return $this;
}
public function removeKnowledgeCategory(KnowledgeCategory $knowledgeCategory): self
{
if ($this->knowledgeCategories->contains($knowledgeCategory)) {
$this->knowledgeCategories->removeElement($knowledgeCategory);
$knowledgeCategory->removeUser($this);
}
return $this;
}
/**
* @return Collection|InvoiceRequest[]
*/
public function getInvoiceRequests(): Collection
{
return $this->invoiceRequests;
}
public function addInvoiceRequest(InvoiceRequest $invoiceRequest): self
{
if (!$this->invoiceRequests->contains($invoiceRequest)) {
$this->invoiceRequests[] = $invoiceRequest;
$invoiceRequest->setAuthor($this);
}
return $this;
}
public function removeInvoiceRequest(InvoiceRequest $invoiceRequest): self
{
if ($this->invoiceRequests->contains($invoiceRequest)) {
$this->invoiceRequests->removeElement($invoiceRequest);
// set the owning side to null (unless already changed)
if ($invoiceRequest->getAuthor() === $this) {
$invoiceRequest->setAuthor(null);
}
}
return $this;
}
/**
* @return Collection|TimerEntry[]
*/
public function getTimerEntries(): Collection
{
return $this->timerEntries;
}
public function addTimerEntry(TimerEntry $timerEntry): self
{
if (!$this->timerEntries->contains($timerEntry)) {
$this->timerEntries[] = $timerEntry;
$timerEntry->setUser($this);
}
return $this;
}
public function removeTimerEntry(TimerEntry $timerEntry): self
{
if ($this->timerEntries->contains($timerEntry)) {
$this->timerEntries->removeElement($timerEntry);
// set the owning side to null (unless already changed)
if ($timerEntry->getUser() === $this) {
$timerEntry->setUser(null);
}
}
return $this;
}
/**
* @return Collection|Project[]
*/
public function getProjects(): Collection
{
return $this->projects;
}
public function addProject(Project $project): self
{
if (!$this->projects->contains($project)) {
$this->projects[] = $project;
$project->addUser($this);
}
return $this;
}
public function removeProject(Project $project): self
{
if ($this->projects->contains($project)) {
$this->projects->removeElement($project);
$project->removeUser($this);
}
return $this;
}
public function getHourValue(): ?int
{
return $this->hourValue;
}
public function setHourValue(?int $hourValue): self
{
$this->hourValue = $hourValue;
return $this;
}
public function canTimer(){
// return false;
return $this->hourValue && $this->hourValue > 0;
}
public function canImpersonate(): ?string{
return $this->getUsername() === 'm.marczyk@hypercrew.pl' ? 'it@hypercrew.pl' : null;//wkrotce inaczej...
}
public function getDaysOff(): ?int
{
return $this->daysOff !== null ? $this->daysOff : 0;
}
public function setDaysOff(?int $daysOff): self
{
$this->daysOff = $daysOff;
return $this;
}
public function getSupervisor(): ?self
{
return $this->supervisor;
}
public function setSupervisor(?self $supervisor): self
{
$this->supervisor = $supervisor;
return $this;
}
public function getAvatar(): ?string
{
// return empty($this->avatar) ? 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->email))) : $this->avatar;
return empty($this->avatar) ? '/images/avatar.png' : $this->avatar;
}
public function hasAvatar(){
return !empty($this->avatar);
}
public function setAvatar(?string $avatar): self
{
$this->avatar = $avatar;
return $this;
}
public function getSlackUserId(): ?string
{
return $this->slackUserId;
}
public function setSlackUserId(?string $slackUserId): self
{
$this->slackUserId = $slackUserId;
return $this;
}
public function getColor(): ?string
{
return $this->color ?: '#ec5992';
}
public function setColor(?string $color): self
{
$this->color = $color;
return $this;
}
}