Skip to content

Instantly share code, notes, and snippets.

@alxfv
Created November 16, 2011 20:14
Show Gist options
  • Save alxfv/1371217 to your computer and use it in GitHub Desktop.
Save alxfv/1371217 to your computer and use it in GitHub Desktop.
<?php
namespace Application\Bundle\DefaultBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;
/**
* @ORM\Entity(repositoryClass="Application\Bundle\DefaultBundle\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks
* @DoctrineAssert\UniqueEntity(fields = "username")
*/
class User implements UserInterface, \Serializable
{
const ROLE_SPECIALIST = 'ROLE_SPECIALIST';
const ROLE_EMPLOYER = 'ROLE_EMPLOYER';
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Assert\NotBlank
* @Assert\Email()
* @Assert\MinLength(limit = 4)
* @ORM\Column(type="string", unique="true")
*/
protected $username;
/**
* @Assert\NotBlank
* @Assert\MinLength(limit = 6)
* @ORM\Column(type="string")
*/
protected $password;
/**
* @ORM\Column(type="string")
*/
protected $salt;
/**
* @Assert\NotBlank(message="Нужно выбрать один из типов аккаунта")
* @ORM\Column(type="array", nullable="true")
*/
protected $roles;
/**
*
* @var type @ORM\Column(name="confirmation_token", type="string", nullable="true")
*/
protected $confirmationToken;
/**
*
* @ORM\Column(type="boolean")
*/
protected $enabled = false;
/**
* Type of user: 0 - occupy, 1 - free
* @ORM\Column(type="smallint")
*/
protected $status = 1;
/**
* @Assert\NotBlank
* @ORM\Column(type="string", nullable="true")
*/
protected $firstname;
/**
* @Assert\NotBlank
* @ORM\Column(type="string", nullable="true")
*/
protected $lastname;
/**
* Type of user: 0 - employee, 1 - employer, 2 - employee and employer
* @ORM\Column(type="smallint")
*/
protected $type = 0;
/**
* Text "About user"
*
* @Assert\NotBlank
* @Assert\MaxLength(300)
* @ORM\Column(type="text", nullable="true")
*/
protected $info;
/**
* Specialist work history
*
* @Assert\NotBlank
* @Assert\MaxLength(3000)
* @ORM\Column(type="text", nullable="true")
*/
protected $experience;
/**
* Specialist skills in detail
*
* @Assert\NotBlank
* @Assert\MaxLength(3000)
* @ORM\Column(type="text", nullable="true")
*/
protected $skills;
/**
* @Assert\Date(message="Указана не существующая дата")
* @ORM\Column(type="date", nullable="true")
*/
protected $dob;
/**
* @ORM\Column(type="string", length="1", nullable="true")
*/
protected $sex;
/**
* @ORM\Column(type="integer")
*/
protected $rating = 0;
/**
* @ORM\Column(type="string", nullable="true", length="64")
*/
protected $phone;
/**
* @Recaptcha\True
*/
public $recaptcha;
/**
*
* @ORM\OneToMany(targetEntity="Comment", mappedBy="user")
*/
protected $comments;
/**
*
* @ORM\OneToMany(targetEntity="Image", mappedBy="user")
*/
protected $images;
/**
*
* @ORM\OneToMany(targetEntity="Opinion", mappedBy="user")
*/
protected $opinions;
/**
*
* @ORM\OneToMany(targetEntity="Job", mappedBy="user")
*/
protected $jobs;
/**
* @ORM\ManyToMany(targetEntity="Metro", inversedBy="users")
* @ORM\JoinTable(name="users_metros")
*/
protected $metros;
/**
* @ORM\ManyToOne(targetEntity="City", inversedBy="users")
* @ORM\JoinColumn(name="city_id", referencedColumnName="id")
*/
protected $city;
/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="users")
* @ORM\JoinTable(name="users_categories")
*/
protected $categories;
/**
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @ORM\Column(type="datetime", nullable="true")
*/
protected $updated;
/**
* @ORM\Column(type="boolean")
*/
protected $free = true;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
/**
* @var int $age
*/
protected $age;
/**
* Serializes the user.
*
* The serialized data have to contain the fields used by the equals method and the username.
*
* @return string
*/
public function serialize()
{
return serialize(array(
$this->password,
$this->username,
));
}
public function isSpecialist()
{
return in_array(self::ROLE_SPECIALIST, $this->roles);
}
public function isEmployer()
{
return in_array(self::ROLE_EMPLOYER, $this->roles);
}
/**
* Unserializes the user.
*
* @param string $serialized
*/
public function unserialize($serialized)
{
list(
$this->password,
$this->username,
) = unserialize($serialized);
}
public function __construct()
{
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
$this->opinions = new \Doctrine\Common\Collections\ArrayCollection();
$this->metros = new \Doctrine\Common\Collections\ArrayCollection();
}
public function __toString()
{
return $this->lastname.' '.$this->firstname;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) {
// remove file if is exists
$this->removeUpload();
// do whatever you want to generate a unique name
$this->setPath(uniqid().'.'.$this->file->guessExtension());
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) {
return;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does automatically
$this->file->move($this->getUploadRootDir(), $this->path);
unset($this->file);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
unlink($file);
}
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path ? null : '/'.$this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded documents should be saved
return $_SERVER['DOCUMENT_ROOT'].'/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads/images';
}
/**
* Return age
*
* @return integer
*/
public function getAge()
{
if (!$this->getDob()) return null;
return $this->getDob()->diff(new \DateTime())->y;
}
/**
* @return string
*/
public function getOnSite()
{
$diff = $this->getCreated()->diff(new \DateTime());
return $diff->format('a');
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
public function getEmail()
{
return $this->username;
}
/**
* Set username
*
* @param string $username
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get username
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set password
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set salt
*
* @param string $salt
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set roles
*
* @param array $roles
*/
public function setRoles($roles)
{
$this->roles = $roles;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
public function getHumanSex()
{
$sex = array('m' => 'муж.', 'f' => 'жен.');
if ($this->sex) return $sex[$this->sex];
return null;
}
/**
* Set firstname
*
* @param string $firstname
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set dob
*
* @param date $dob
*/
public function setDob($dob)
{
$this->dob = $dob;
}
/**
* Get dob
*
* @return date
*/
public function getDob()
{
return $this->dob;
}
/**
* Set sex
*
* @param string $sex
*/
public function setSex($sex)
{
$this->sex = $sex;
}
/**
* Get sex
*
* @return string
*/
public function getSex()
{
return $this->sex;
}
/**
* Set phone
*
* @param string $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Add comments
*
* @param Application\Bundle\DefaultBundle\Entity\Comment $comments
*/
public function addComment(\Application\Bundle\DefaultBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
}
/**
* Get comments
*
* @return Doctrine\Common\Collections\Collection
*/
public function getComments()
{
return $this->comments;
}
/**
* Add jobs
*
* @param Application\Bundle\DefaultBundle\Entity\Job $jobs
*/
public function addJob(\Application\Bundle\DefaultBundle\Entity\Job $jobs)
{
$this->jobs[] = $jobs;
}
/**
* Get jobs
*
* @return Doctrine\Common\Collections\Collection
*/
public function getJobs()
{
return $this->jobs;
}
/**
* Add categories
*
* @param Application\Bundle\DefaultBundle\Entity\Category $categories
*/
public function addCategory(\Application\Bundle\DefaultBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
}
/**
* Get categories
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
/**
* Set created
*
* @param datetime $created
*/
public function setCreated($created)
{
$this->created = $created;
}
/**
* Get created
*
* @return datetime
*/
public function getCreated()
{
return $this->created;
}
/**
* Set updated
*
* @param datetime $updated
*/
public function setUpdated($updated)
{
$this->updated = $updated;
}
/**
* Get updated
*
* @return datetime
*/
public function getUpdated()
{
return $this->updated;
}
/**
* Set rating
*
* @param integer $rating
*/
public function setRating($rating)
{
$this->rating = $rating;
}
/**
* Get rating
*
* @return integer
*/
public function getRating()
{
return $this->rating;
}
/**
* Removes sensitive data from the user.
*
* @return void
*/
function eraseCredentials()
{
}
/**
* The equality comparison should neither be done by referential equality
* nor by comparing identities (i.e. getId() === getId()).
*
* However, you do not need to compare every attribute, but only those that
* are relevant for assessing whether re-authentication is required.
*
* @param UserInterface $user
* @return Boolean
*/
function equals(UserInterface $user)
{
return true;
}
/**
* Add opinions
*
* @param Application\Bundle\DefaultBundle\Entity\Opinion $opinions
*/
public function addOpinion(\Application\Bundle\DefaultBundle\Entity\Opinion $opinions)
{
$this->opinions[] = $opinions;
}
/**
* Get opinions
*
* @return Doctrine\Common\Collections\Collection
*/
public function getOpinions()
{
return $this->opinions;
}
/**
* Add metros
*
* @param Application\Bundle\DefaultBundle\Entity\Metro $metros
*/
public function addMetro(\Application\Bundle\DefaultBundle\Entity\Metro $metros)
{
$this->metros[] = $metros;
}
/**
* Get metros
*
* @return Doctrine\Common\Collections\Collection
*/
public function getMetros()
{
return $this->metros;
}
/**
* Set city
*
* @param Application\Bundle\DefaultBundle\Entity\City $city
*/
public function setCity(\Application\Bundle\DefaultBundle\Entity\City $city)
{
$this->city = $city;
}
/**
* Get city
*
* @return Application\Bundle\DefaultBundle\Entity\City
*/
public function getCity()
{
return $this->city;
}
/**
* Set type
*
* @param integer $type
*/
public function setType($type)
{
$this->type = $type;
}
/**
* Get type
*
* @return integer
*/
public function getType()
{
return $this->type;
}
/**
* Set enabled
*
* @param boolean $enabled
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;
}
/**
* Get enabled
*
* @return boolean
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* Set info
*
* @param text $info
*/
public function setInfo($info)
{
$this->info = $info;
}
/**
* Get info
*
* @return text
*/
public function getInfo()
{
return $this->info;
}
/**
* Add images
*
* @param Application\Bundle\DefaultBundle\Entity\Image $images
*/
public function addImage(\Application\Bundle\DefaultBundle\Entity\Image $images)
{
$this->images[] = $images;
}
/**
* Get images
*
* @return Doctrine\Common\Collections\Collection
*/
public function getImages()
{
return $this->images;
}
/**
* Set confirmationToken
*
* @param string $confirmationToken
*/
public function setConfirmationToken($confirmationToken)
{
$this->confirmationToken = $confirmationToken;
}
/**
* Get confirmationToken
*
* @return string
*/
public function getConfirmationToken()
{
return $this->confirmationToken;
}
/**
* Generates the confirmation token if it is not set.
*/
public function generateConfirmationToken()
{
if (null === $this->confirmationToken) {
$this->confirmationToken = $this->generateToken();
}
}
/**
* Generates a token.
*
* @return string
*/
protected function generateToken()
{
$bytes = hash('sha256', uniqid(mt_rand(), true), true);
return base_convert(bin2hex($bytes), 16, 36);
}
/**
* Set free
*
* @param boolean $free
*/
public function setFree($free)
{
$this->free = $free;
}
/**
* Get free
*
* @return boolean
*/
public function getFree()
{
return $this->free;
}
/**
* Set experience
*
* @param text $experience
*/
public function setExperience($experience)
{
$this->experience = $experience;
}
/**
* Get experience
*
* @return text
*/
public function getExperience()
{
return $this->experience;
}
/**
* Set skills
*
* @param text $skills
*/
public function setSkills($skills)
{
$this->skills = $skills;
}
/**
* Get skills
*
* @return text
*/
public function getSkills()
{
return $this->skills;
}
/**
* Set status
*
* @param smallint $status
*/
public function setStatus($status)
{
$this->status = $status;
}
/**
* Get status
*
* @return smallint
*/
public function getStatus()
{
return $this->status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment