<?php
namespace Rawafed\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Rawafed\CommonsBundle\Model\UserModelInterface;
use Rawafed\CommonsBundle\Model\UserProfileModelInterface;
use Rawafed\CommonsBundle\Model\HWIUserExtraFieldsModelInterface;
use Rawafed\CommonsBundle\Entity\Traits\HasPublicIdTrait as HasPublicId;
use Rawafed\CommonsBundle\Entity\UserProfile;
use Rawafed\EnhancedUploaderBundle\Mapping\Annotation as Uploader;
use Rawafed\SecurityBundle\Entity\Role;
/**
*
* @ORM\Table(name="cms_users",
uniqueConstraints={@ORM\UniqueConstraint(name="inx_public_id", columns={"public_id"})},
indexes={@ORM\Index(name="idx_created_at", columns={"created_at"})}
)
*
* @ORM\Entity(repositoryClass="Rawafed\UserBundle\Entity\Repository\UserRepository")
* @ORM\EntityListeners({"Rawafed\UserBundle\Listener\UserListener"})
* @UniqueEntity(fields="email", groups={"UserManagement","Registration","Profile"})
* @UniqueEntity(fields="username", groups={"UserManagement","Registration","Profile"})
* @ORM\HasLifecycleCallbacks()
*
* @Uploader\Uploadable
*
*/
class User extends BaseUser implements UserModelInterface, InterfaceUserReportingAware
{
use HasPublicId;
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var UserProfileModelInterface userProfile
*
* @Assert\Valid()
*/
protected $userProfile;
/**
* @ORM\OneToMany(targetEntity="\Rawafed\CommonsBundle\Model\HWIUserExtraFieldsModelInterface", mappedBy="user" ,cascade={"persist"} )
* @ORM\OrderBy({"id" = "DESC"})
* @var ArrayCollection UserProfileModelInterface HWIUserExtraFields
*
* @Assert\Valid()
*/
protected $userHWIUserExtraFields;
/**
* @ORM\Column(name="real_name", type="string", length=255, nullable=true)
* @Assert\NotBlank(groups={"Registration","Profile","UserManagement"})
*/
protected $realName;
/**
* @ORM\ManyToMany(targetEntity="Rawafed\UserBundle\Entity\Group", inversedBy="users", cascade={"persist"})
* @ORM\JoinTable(name="cms_users_groups",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="group_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*
* @ORM\OrderBy({"title" = "ASC"})
* @var ArrayCollection groups
*
*/
protected $groups;
// @ ORM\Column(name="email", type="string", length=255, unique=false) */
//protected $email;
/**
* @ORM\Column(name="updated_at", type="datetime", length=255, unique=false, nullable=true)
* @var \DateTime
*/
protected $updatedAt;
/**
* @ORM\Column(name="created_at", type="datetime", length=255, unique=false, nullable=true)
* @var \DateTime
*/
protected $createdAt;
/**
* @Uploader\UploadableField(mapping="rawafed_upload", fileNameProperty="imageName", uploadsDirName="users")
* @Assert\File(
* maxSize="1M",
* mimeTypes={"image/png", "image/jpeg", "image/gif"},
* groups={"Registration","Profile","UserManagement"}
* )
*
* @var File $imageFile
*/
protected $imageFile;
/**
* @ORM\Column(type="string", length=255, name="image_name", nullable=true)
*
* @var string $imageName
*/
protected $imageName;
/**
* @var integer
*
* @ORM\Column(name="reporting_reference_id", type="integer", nullable=true)
*/
private $reportingReferenceId;
/**
* @var integer
*
* @ORM\Column(name="reporting_banned", type="boolean", options={"default" = false})
*/
private $reportingBanned = false;
/**
* @var integer
*
* @ORM\Column(name="reporting_immuned", type="boolean", options={"default" = false})
*/
private $reportingImmune = false;
/**
* @var bool
*
* @ORM\Column(type="boolean", options={"default" = false})
*/
private $is_system = false;
public function __construct()
{
$this->groups = new ArrayCollection();
$this->userHWIUserExtraFields = new ArrayCollection();
parent::__construct();
}
/**
* @return string $username
*/
public function __toString()
{
return $this->getUsername();
}
/**
* @param string $default OPTIONAL default value to be returned in case that no real name defined.
* @return string $realName
*/
public function getRealName($default = null)
{
return !empty($this->realName) ? $this->realName : $default;
}
/**
* @param string $realName
* @return User $this
*/
public function setRealName($realName)
{
$this->realName = $realName;
return $this;
}
/**
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $imageFile
* @return \Rawafed\UserBundle\Entity\User
*
*/
public function setImageFile(File $imageFile = null)
{
$this->imageFile = $imageFile;
if ($imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
if($this->getId()) $this->updatedAt = new \DateTime('now');
else $this->createdAt = new \DateTime('now');
}
return $this;
}
/**
*
* @return \Symfony\Component\HttpFoundation\File\File
*/
public function getImageFile()
{
return $this->imageFile; //new File($this->image, false);
}
/**
*
* @return string
*/
public function getImageName()
{
return $this->imageName;
}
/**
*
* @param string $imageName
* @return \Rawafed\UserBundle\Entity\User
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
return $this;
}
/**
*
* overridden to be able to get array of all user tree nested roles
* to use it in isGranted action which will show or hide view actions (add, edit, .....)
*
* Returns the user roles
*
* @return array The roles
*/
public function getRoles()
{
$roles = $this->roles;
foreach($this->getGroups() as $group) {
foreach( $group->getCustomRoles() as $role ){
$roles[] = $role->getRole();
$roles = array_merge( $roles, $this->getRoleChilds($role) );
}
}
$roles[] = static::ROLE_DEFAULT;
return array_unique($roles);
}
/**
*
* @param string|Role $role
* @return string[]|Role[]
*/
private function getRoleChilds($role)
{
$roles = array();
foreach($role->getChildren() as $childRole) {
$roles[] = $childRole->getRole();
if($role->getChildren()) {
$roles = array_merge($roles, $this->getRoleChilds($childRole));
}
}
return $roles;
}
/**
* @return UserProfile|UserProfileModelInterface
*/
public function getUserProfile()
{
return $this->userProfile;
}
/**
* @param UserProfileModelInterface $userProfile
* @return $this
*/
public function setUserProfile(UserProfileModelInterface $userProfile)
{
$this->userProfile = $userProfile;
$userProfile->setUser($this);
return $this;
}
/**
* @return ArrayCollection
*/
public function getUserHWIUserExtraFields()
{
return $this->userHWIUserExtraFields ;
}
/**
* @param ArrayCollection $userHWIUserExtraFields
* @return $this
*/
public function setUserHWIUserExtraFields(ArrayCollection $userHWIUserExtraFields)
{
$this->userHWIUserExtraFields = $userHWIUserExtraFields;
return $this;
}
/**
* @param HWIUserExtraFieldsModelInterface $userHWIUserExtraField
* @return $this
*/
public function addUserHWIUserExtraField(HWIUserExtraFieldsModelInterface $userHWIUserExtraField)
{
$userHWIUserExtraField->setUser($this);
$this->userHWIUserExtraFields->add($userHWIUserExtraField);
return $this;
}
/**
* @param HWIUserExtraFieldsModelInterface $userHWIUserExtraField
* @return $this
*/
public function removeUserHWIUserExtraField(HWIUserExtraFieldsModelInterface $userHWIUserExtraField)
{
//$this->getUserHWIUserExtraFields()->matching($criteria);
if($this->getUserHWIUserExtraFields()->contains($userHWIUserExtraField))
$this->userHWIUserExtraFields->removeElement($userHWIUserExtraField);
return $this;
}
/**
* @param array $fields
* @return null|HWIUserExtraFieldsModelInterface
*/
public function findUserHWIUserExtraFieldsBy($fields = array())
{
$item = $this->getUserHWIUserExtraFields()->filter(
function($entry) use ($fields) {
$match = false;
foreach ($fields as $key => $value){
$fieldGetter = "get".ucfirst($key);
if($entry->$fieldGetter() == $value){
$match = true;
}
}
if($match) return $entry;
})->first();
return ($item instanceof HWIUserExtraFieldsModelInterface) ? $item : NULL;
}
/**
*
* @param \DateTime $createdAt
* @return User
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
*
* @param \DateTime $updatedAt
* @return User
*/
public function setUpdatedAt(\DateTime $updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/** @ORM\PrePersist() */
public function prePersist()
{
if(is_null($this->getCreatedAt()))
$this->setCreatedAt(new \DateTime());
}
/** @ORM\PreUpdate() */
public function preUpdate()
{
$this->setUpdatedAt(new \DateTime());
}
public function getReportedContent()
{
return $this->getUsername()." (".$this->getRealName().")";
}
public function getReportingBanned()
{
return $this->reportingBanned;
}
public function getReportingImmuned()
{
return $this->reportingImmune;
}
public function getReportingReferenceId()
{
return $this->reportingReferenceId;
}
public function getReportingTypeName()
{
return 'user.titles.user';
}
public function setReportingBanned($reportingBanned)
{
$this->reportingBanned = $reportingBanned;
return $this;
}
public function setReportingImmuned($reportingImmune)
{
$this->reportingImmune = $reportingImmune;
return $this;
}
public function setReportingReferenceId($reportingReferenceId)
{
$this->reportingReferenceId = $reportingReferenceId;
return $this;
}
/**
* @return bool
*/
public function isSystem()
{
return $this->is_system;
}
/**
* @param bool $is_system
* @return User
*/
public function setIsSystem($is_system)
{
$this->is_system = $is_system;
return $this;
}
}