Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.13 KB | None | 0 0
  1. <?php
  2.  
  3. namespace WIY\UserBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use FOS\UserBundle\Model\User as BaseUser;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. use Symfony\Component\Security\Core\Util\SecureRandom;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12.  
  13. /**
  14.  * User
  15.  *
  16.  * @ORM\Table(name="fos_user")
  17.  * @ORM\HasLifecycleCallbacks()
  18.  * @ORM\Entity(repositoryClass="WIY\UserBundle\Entity\UserRepository")
  19.  */
  20. class User extends BaseUser
  21. {
  22.     /**
  23.      * @var integer
  24.      *
  25.      * @ORM\Column(name="id", type="integer")
  26.      * @ORM\Id
  27.      * @ORM\GeneratedValue(strategy="AUTO")
  28.      */
  29.     protected $id;
  30.  
  31.     /**
  32.      * @Assert\File(maxSize="2048k")
  33.      * @Assert\Image(mimeTypesMessage="Please upload a valid image.")
  34.      */
  35.     protected $profilePictureFile;
  36.  
  37.     // for temporary storage
  38.     private $tempProfilePicturePath;
  39.  
  40.     /**
  41.      * @ORM\Column(type="string", length=255, nullable=true)
  42.      */
  43.     protected $profilePicturePath;
  44.  
  45.     /**
  46.     * @ORM\OneToMany(targetEntity="WIY\ProductsBundle\Entity\Avis", mappedBy="user", cascade={"persist","remove"})
  47.     */  
  48.     private $avis;
  49.  
  50.     /**
  51.      * @var string
  52.      *
  53.      * @ORM\Column(name="sexe", type="string", length=32)
  54.      * @Assert\NotBlank(message="Merci de remplir votre sexe.")
  55.      * groups={"Registration"}
  56.      */
  57.     private $sexe;
  58.  
  59.     /**
  60.      * @var string
  61.      *
  62.      * @ORM\Column(name="nom", type="string", length=64)*
  63.      * @Assert\Length(
  64.      *     min=2,
  65.      *     max=64,
  66.      *     minMessage="Vous devez rentrer un nom.",
  67.      *     maxMessage="Le nom rentré semble trop long (max: 64 cararctères)",
  68.      *     groups={"Registration"}
  69.      * )
  70.      * @Assert\Regex(
  71.      *      pattern="/[a-z|ï|é|è|ö|\-]+/i",
  72.      *      message="Le nom renseigné semble invalide"
  73.      *)
  74.      */
  75.     private $nom;
  76.  
  77.     /**
  78.      * @var string
  79.      *
  80.      * @ORM\Column(name="prenom", type="string", length=64)
  81.      * @Assert\Length(
  82.      *     min=3,
  83.      *     max=64,
  84.      *     minMessage="Vous devez rentrer un prénom.",
  85.      *     maxMessage="Le prénom rentré semble trop long (max: 64 caractères)",
  86.      *     groups={"Registration"}
  87.      * )
  88.      * @Assert\Regex(
  89.      *      pattern="/[a-z|ï|é|è|ö|\-]+/i",
  90.      *      message="Le prénom renseigné semble invalide"
  91.      *)
  92.      */
  93.     private $prenom;
  94.  
  95.     /**
  96.      * @var string
  97.      *
  98.      * @ORM\Column(name="naissance", type="string", length=4)
  99.      * @Assert\Regex("/^[0-9]{4}$/", message="Votre année de naissance doit être composé de 4 chiffres", groups={"Registration"})
  100.      */
  101.     private $naissance;
  102.  
  103.     /**
  104.      * @var string
  105.      *
  106.      * @ORM\Column(name="niveau", type="string", length=32)
  107.      * @Assert\Length(
  108.      *     min=5,
  109.      *     max=32,
  110.      *     minMessage="Vous devez renseigner votre niveau.",
  111.      *     maxMessage="Vous devez renseigner votre niveau",
  112.      * )
  113.      */
  114.     private $niveau;
  115.  
  116.     /**
  117.      * @var string
  118.      *
  119.      * @ORM\Column(name="frequence", type="string", length=32)
  120.      * @Assert\Length(
  121.      *     min=5,
  122.      *     max=32,
  123.      *     minMessage="Vous devez renseigner votre fréquence.",
  124.      *     maxMessage="Vous devez renseigner votre fréquence",
  125.      * )
  126.      */
  127.     private $frequence;
  128.  
  129.     /**
  130.      * @var string
  131.      *
  132.      * @ORM\Column(name="description", type="text", nullable=True)
  133.      */
  134.     private $description;
  135.  
  136.     /**
  137.      * @var string
  138.      *
  139.      * @ORM\Column(name="facebookId", type="string", length=64, nullable=True)
  140.      */
  141.     private $facebookId;
  142.  
  143.     /**
  144.      * @var string
  145.      *
  146.      * @ORM\Column(name="googleplusId", type="string", length=64, nullable=True)
  147.      */
  148.     private $googleplusId;
  149.  
  150.     /**
  151.      * @var datetime
  152.      *
  153.      * @ORM\Column(name="createdOn", type="datetime")
  154.      */
  155.     private $createdOn;
  156.  
  157.     /**
  158.      * @var datetime
  159.      *
  160.      * @ORM\Column(name="updatedOn", type="datetime", nullable=True)
  161.      */
  162.     private $updatedOn;
  163.  
  164.     /**
  165.      * Constructor
  166.      */
  167.     public function __construct()
  168.     {
  169.         parent::__construct();
  170.         $this->avis = new \Doctrine\Common\Collections\ArrayCollection();
  171.         $this->createdOn = new \DateTime();
  172.     }
  173.  
  174.     /**
  175.      * Sets the file used for profile picture uploads
  176.      *
  177.      * @param UploadedFile $file
  178.      * @return object
  179.      */
  180.     public function setProfilePictureFile(UploadedFile $file = null) {
  181.         // set the value of the holder
  182.         $this->profilePictureFile       =   $file;
  183.          // check if we have an old image path
  184.         if (isset($this->profilePicturePath)) {
  185.             // store the old name to delete after the update
  186.             $this->tempProfilePicturePath = $this->profilePicturePath;
  187.             $this->profilePicturePath = null;
  188.         } else {
  189.             $this->profilePicturePath = 'initial';
  190.         }
  191.  
  192.         return $this;
  193.     }
  194.  
  195.     /**
  196.      * Get the file used for profile picture uploads
  197.      *
  198.      * @return UploadedFile
  199.      */
  200.     public function getProfilePictureFile() {
  201.  
  202.         return $this->profilePictureFile;
  203.     }
  204.  
  205.     /**
  206.      * Set profilePicturePath
  207.      *
  208.      * @param string $profilePicturePath
  209.      * @return User
  210.      */
  211.     public function setProfilePicturePath($profilePicturePath)
  212.     {
  213.         $this->profilePicturePath = $profilePicturePath;
  214.  
  215.         return $this;
  216.     }
  217.  
  218.     /**
  219.      * Get profilePicturePath
  220.      *
  221.      * @return string
  222.      */
  223.     public function getProfilePicturePath()
  224.     {
  225.         return $this->profilePicturePath;
  226.     }
  227.  
  228.     /**
  229.      * Get the absolute path of the profilePicturePath
  230.      */
  231.     public function getProfilePictureAbsolutePath() {
  232.         return null === $this->profilePicturePath
  233.             ? null
  234.             : $this->getUploadRootDir().'/'.$this->profilePicturePath;
  235.     }
  236.  
  237.     /**
  238.      * Get root directory for file uploads
  239.      *
  240.      * @return string
  241.      */
  242.     protected function getUploadRootDir($type='profilePicture') {
  243.         // the absolute directory path where uploaded
  244.         // documents should be saved
  245.         return __DIR__.'/../../../../web/'.$this->getUploadDir($type);
  246.     }
  247.  
  248.     /**
  249.      * Specifies where in the /web directory profile pic uploads are stored
  250.      *
  251.      * @return string
  252.      */
  253.     protected function getUploadDir($type='profilePicture') {
  254.         // the type param is to change these methods at a later date for more file uploads
  255.         // get rid of the __DIR__ so it doesn't screw up
  256.         // when displaying uploaded doc/image in the view.
  257.         return 'images/uploads';
  258.     }
  259.  
  260.     /**
  261.      * Get the web path for the user
  262.      *
  263.      * @return string
  264.      */
  265.     public function getWebProfilePicturePath() {
  266.  
  267.         return '/'.$this->getUploadDir().'/'.$this->getProfilePicturePath();
  268.     }
  269.  
  270.     /**
  271.      * @ORM\PrePersist()
  272.      * @ORM\PreUpdate()
  273.      */
  274.     public function preUploadProfilePicture() {
  275.         if (null !== $this->getProfilePictureFile()) {
  276.             // a file was uploaded
  277.             // generate a unique filename
  278.             $filename = $this->generateRandomProfilePictureFilename();
  279.             $this->setProfilePicturePath($filename.'.'.$this->getProfilePictureFile()->guessExtension());
  280.         }
  281.     }
  282.  
  283.     /**
  284.      * Generates a 32 char long random filename
  285.      *
  286.      * @return string
  287.      */
  288.     public function generateRandomProfilePictureFilename() {
  289.         $count                  =   0;
  290.         do {
  291.             $generator = new SecureRandom();
  292.             $random = $generator->nextBytes(16);
  293.             $randomString = bin2hex($random);
  294.             $count++;
  295.         }
  296.         while(file_exists($this->getUploadRootDir().'/'.$randomString.'.'.$this->getProfilePictureFile()->guessExtension()) && $count < 50);
  297.  
  298.         return $randomString;
  299.     }
  300.  
  301.     /**
  302.      * @ORM\PostPersist()
  303.      * @ORM\PostUpdate()
  304.      *
  305.      * Upload the profile picture
  306.      *
  307.      * @return mixed
  308.      */
  309.     public function uploadProfilePicture() {
  310.         // check there is a profile pic to upload
  311.         if ($this->getProfilePictureFile() === null) {
  312.             return;
  313.         }
  314.         // if there is an error when moving the file, an exception will
  315.         // be automatically thrown by move(). This will properly prevent
  316.         // the entity from being persisted to the database on error
  317.         $this->getProfilePictureFile()->move($this->getUploadRootDir(), $this->getProfilePicturePath());
  318.  
  319.         // check if we have an old image
  320.         if (isset($this->tempProfilePicturePath) && file_exists($this->getUploadRootDir().'/'.$this->tempProfilePicturePath)) {
  321.             // delete the old image
  322.             unlink($this->getUploadRootDir().'/'.$this->tempProfilePicturePath);
  323.             // clear the temp image path
  324.             $this->tempProfilePicturePath = null;
  325.         }
  326.         $this->profilePictureFile = null;
  327.     }
  328.  
  329.      /**
  330.      * @ORM\PostRemove()
  331.      */
  332.     public function removeProfilePictureFile()
  333.     {
  334.         if ($file = $this->getProfilePictureAbsolutePath() && file_exists($this->getProfilePictureAbsolutePath())) {
  335.             unlink($file);
  336.         }
  337.     }
  338.  
  339.  
  340.  
  341.  
  342.     /**
  343.      * Get id
  344.      *
  345.      * @return integer
  346.      */
  347.     public function getId()
  348.     {
  349.         return $this->id;
  350.     }
  351.  
  352.     /**
  353.      * Set sexe
  354.      *
  355.      * @param string $sexe
  356.      * @return User
  357.      */
  358.     public function setSexe($sexe)
  359.     {
  360.         $this->sexe = $sexe;
  361.  
  362.         return $this;
  363.     }
  364.  
  365.     /**
  366.      * Get sexe
  367.      *
  368.      * @return string
  369.      */
  370.     public function getSexe()
  371.     {
  372.         return $this->sexe;
  373.     }
  374.  
  375.     /**
  376.      * Set nom
  377.      *
  378.      * @param string $nom
  379.      * @return User
  380.      */
  381.     public function setNom($nom)
  382.     {
  383.         $this->nom = $nom;
  384.  
  385.         return $this;
  386.     }
  387.  
  388.     /**
  389.      * Get nom
  390.      *
  391.      * @return string
  392.      */
  393.     public function getNom()
  394.     {
  395.         return $this->nom;
  396.     }
  397.  
  398.     /**
  399.      * Set prenom
  400.      *
  401.      * @param string $prenom
  402.      * @return User
  403.      */
  404.     public function setPrenom($prenom)
  405.     {
  406.         $this->prenom = $prenom;
  407.  
  408.         return $this;
  409.     }
  410.  
  411.     /**
  412.      * Get prenom
  413.      *
  414.      * @return string
  415.      */
  416.     public function getPrenom()
  417.     {
  418.         return $this->prenom;
  419.     }
  420.  
  421.     /**
  422.      * Set naissance
  423.      *
  424.      * @param string $naissance
  425.      * @return User
  426.      */
  427.     public function setNaissance($naissance)
  428.     {
  429.         $this->naissance = $naissance;
  430.  
  431.         return $this;
  432.     }
  433.  
  434.     /**
  435.      * Get naissance
  436.      *
  437.      * @return string
  438.      */
  439.     public function getNaissance()
  440.     {
  441.         return $this->naissance;
  442.     }
  443.  
  444.     /**
  445.      * Set niveau
  446.      *
  447.      * @param string $niveau
  448.      * @return User
  449.      */
  450.     public function setNiveau($niveau)
  451.     {
  452.         $this->niveau = $niveau;
  453.  
  454.         return $this;
  455.     }
  456.  
  457.     /**
  458.      * Get niveau
  459.      *
  460.      * @return string
  461.      */
  462.     public function getNiveau()
  463.     {
  464.         return $this->niveau;
  465.     }
  466.  
  467.     /**
  468.      * Set frequence
  469.      *
  470.      * @param string $frequence
  471.      * @return User
  472.      */
  473.     public function setFrequence($frequence)
  474.     {
  475.         $this->frequence = $frequence;
  476.  
  477.         return $this;
  478.     }
  479.  
  480.     /**
  481.      * Get frequence
  482.      *
  483.      * @return string
  484.      */
  485.     public function getFrequence()
  486.     {
  487.         return $this->frequence;
  488.     }
  489.  
  490.     /**
  491.      * Set description
  492.      *
  493.      * @param string $description
  494.      * @return User
  495.      */
  496.     public function setDescription($description)
  497.     {
  498.         $this->description = $description;
  499.  
  500.         return $this;
  501.     }
  502.  
  503.     /**
  504.      * Get description
  505.      *
  506.      * @return string
  507.      */
  508.     public function getDescription()
  509.     {
  510.         return $this->description;
  511.     }
  512.  
  513.     /**
  514.      * Set facebookId
  515.      *
  516.      * @param string $facebookId
  517.      * @return User
  518.      */
  519.     public function setFacebookId($facebookId)
  520.     {
  521.         $this->facebookId = $facebookId;
  522.  
  523.         return $this;
  524.     }
  525.  
  526.     /**
  527.      * Get facebookId
  528.      *
  529.      * @return string
  530.      */
  531.     public function getFacebookId()
  532.     {
  533.         return $this->facebookId;
  534.     }
  535.  
  536.     /**
  537.      * Set googleplusId
  538.      *
  539.      * @param string $googleplusId
  540.      * @return User
  541.      */
  542.     public function setGoogleplusId($googleplusId)
  543.     {
  544.         $this->googleplusId = $googleplusId;
  545.  
  546.         return $this;
  547.     }
  548.  
  549.     /**
  550.      * Get googleplusId
  551.      *
  552.      * @return string
  553.      */
  554.     public function getGoogleplusId()
  555.     {
  556.         return $this->googleplusId;
  557.     }
  558.  
  559.     /**
  560.      * Set createdOn
  561.      *
  562.      * @param \DateTime $createdOn
  563.      * @return User
  564.      */
  565.     public function setCreatedOn($createdOn)
  566.     {
  567.         $this->createdOn = $createdOn;
  568.  
  569.         return $this;
  570.     }
  571.  
  572.     /**
  573.      * Get createdOn
  574.      *
  575.      * @return \DateTime
  576.      */
  577.     public function getCreatedOn()
  578.     {
  579.         return $this->createdOn;
  580.     }
  581.  
  582.     /**
  583.      * Set updatedOn
  584.      *
  585.      * @param \DateTime $updatedOn
  586.      * @return User
  587.      */
  588.     public function setUpdatedOn($updatedOn)
  589.     {
  590.         $this->updatedOn = $updatedOn;
  591.  
  592.         return $this;
  593.     }
  594.  
  595.     /**
  596.      * Get updatedOn
  597.      *
  598.      * @return \DateTime
  599.      */
  600.     public function getUpdatedOn()
  601.     {
  602.         return $this->updatedOn;
  603.     }
  604.  
  605.     /**
  606.      * Add avis
  607.      *
  608.      * @param \WIY\ProductsBundle\Entity\Avis $avis
  609.      * @return User
  610.      */
  611.     public function addAvi(\WIY\ProductsBundle\Entity\Avis $avis)
  612.     {
  613.         $this->avis[] = $avis;
  614.  
  615.         return $this;
  616.     }
  617.  
  618.     /**
  619.      * Remove avis
  620.      *
  621.      * @param \WIY\ProductsBundle\Entity\Avis $avis
  622.      */
  623.     public function removeAvi(\WIY\ProductsBundle\Entity\Avis $avis)
  624.     {
  625.         $this->avis->removeElement($avis);
  626.     }
  627.  
  628.     /**
  629.      * Get avis
  630.      *
  631.      * @return \Doctrine\Common\Collections\Collection
  632.      */
  633.     public function getAvis()
  634.     {
  635.         return $this->avis;
  636.     }
  637. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement