<?php
namespace App\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Knp\DoctrineBehaviors\Model\Blameable\BlameableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Doctrine\ORM\Mapping\Entity
* @Doctrine\ORM\Mapping\Table(name="user_groups")
*/
class UserGroup implements TranslatableInterface, BlameableInterface, TimestampableInterface, SoftDeletableInterface {
use BlameableTrait;
use TimestampableTrait;
use TranslatableTrait;
use SoftDeletableTrait;
/**
* @Assert\Valid
*/
protected $translations;
/**
* @Doctrine\ORM\Mapping\Id
* @Doctrine\ORM\Mapping\Column(type="integer")
* @Doctrine\ORM\Mapping\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Doctrine\ORM\Mapping\OneToMany(targetEntity="UserGroupModule", mappedBy="group", cascade={"persist", "remove"})
*/
protected $groupsModules;
/**
* @Doctrine\ORM\Mapping\OneToMany(targetEntity="App\Entity\User", mappedBy="userGroup")
*/
protected $users;
public function __construct()
{
$this->groupsModules = new \Doctrine\Common\Collections\ArrayCollection();
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
public function countUsers() {
$c = 0;
/** @var $user User */
foreach ($this->getUsers() as $user) {
if ($user->getDeletedBy() === null and $user->isEnabled()) {
$c = $c + 1;
}
}
return $c;
}
/**
* Zwraca uprawnienia dla danego modułu
* @param string $moduleName Nazwa kodowa modułu
* @return boolean
*/
public function getGroupModuleByName($moduleName)
{
foreach($this->groupsModules as $groupModule)
{
if($groupModule->getModule()->getCodeName() === $moduleName)
{
return $groupModule;
}
}
return false;
}
/**
* Obsługa tłumaczeń
* @param $method
* @param $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function getName(){
return $this->translate()->getName();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add groupsModule
*
* @param \App\Entity\UserGroupModule $groupsModule
*
* @return UserGroup
*/
public function addGroupsModule(\App\Entity\UserGroupModule $groupsModule)
{
$groupsModule->setGroup($this);
$this->groupsModules[] = $groupsModule;
return $this;
}
/**
* Remove groupsModule
*
* @param \App\Entity\UserGroupModule $groupsModule
*/
public function removeGroupsModule(\App\Entity\UserGroupModule $groupsModule)
{
$this->groupsModules->removeElement($groupsModule);
}
/**
* Get groupsModules
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroupsModules()
{
return $this->groupsModules;
}
/**
* Add user
*
* @param \App\Entity\User $user
*
* @return UserGroup
*/
public function addUser(\App\Entity\User $user)
{
$this->users[] = $user;
return $this;
}
/**
* Remove user
*
* @param \App\Entity\User $user
*/
public function removeUser(\App\Entity\User $user)
{
$this->users->removeElement($user);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}