Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // src/Acme/EntityBundle/Entity/File.php
- namespace Acme\EntityBundle\Entity;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Validator\Constraints as Assert;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- /**
- * @ORM\Entity
- * @ORM\Table(name="files")
- * @ORM\HasLifecycleCallbacks
- */
- class File
- {
- /**
- * @ORM\Id
- * @ORM\Column(type="integer")
- * @ORM\GeneratedValue(strategy="AUTO")
- */
- public $id;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\NotBlank
- */
- public $title;
- /**
- * @ORM\ManyToMany(targetEntity="\Acme\EntityBundle\Entity\Text", mappedBy="files")
- odkazuje na text, ve kterem je pouzit
- *
- */
- private $text;
- /**
- * @ORM\Column(type="string", length=255, nullable=true)
- */
- public $path;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $content;
- /**
- * @ORM\Column(type="integer")
- */
- protected $author;
- /**
- * @ORM\Column(type="datetime")
- */
- protected $date;
- /**
- * @Assert\File(maxSize="6000000")
- */
- private $file;
- /**
- * @ORM\Column(type="text", nullable=true)
- */
- private $uploadDir;
- private $temp;
- // set default upload dir
- public function __construct() {
- $this->setUploadDir();
- }
- 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 __DIR__.'/../../../../web/'.$this->getUploadDir();
- }
- public function setUploadDir($uploadDir = 'uploads/files')
- {
- $this->uploadDir = $uploadDir;
- return $this;
- }
- public function getUploadDir()
- {
- // get rid of the __DIR__ so it doesn't screw up
- // when displaying uploaded doc/image in the view.
- return $this->uploadDir;
- }
- /**
- * Sets file.
- *
- * @param UploadedFile $file
- */
- public function setFile(UploadedFile $file = null)
- {
- $this->file = $file;
- // check if we have an old image path
- if (isset($this->path)) {
- // store the old name to delete after the update
- $this->temp = $this->path;
- $this->path = null;
- } else {
- $this->path = 'initial';
- }
- }
- /**
- * Get file.
- *
- * @return UploadedFile
- */
- public function getFile()
- {
- return $this->file;
- }
- /**
- * @ORM\PrePersist()
- * @ORM\PreUpdate()
- */
- public function preUpload()
- {
- if (null !== $this->getFile()) {
- // do whatever you want to generate a unique name
- $filename = sha1(uniqid(mt_rand(), true));
- $this->path = $filename.'.'.$this->getFile()->guessExtension();
- }
- }
- /**
- * @ORM\PostPersist()
- * @ORM\PostUpdate()
- */
- public function upload()
- {
- if (null === $this->getFile()) {
- return;
- }
- // if there is an error when moving the file, an exception will
- // be automatically thrown by move(). This will properly prevent
- // the entity from being persisted to the database on error
- $this->getFile()->move($this->getUploadRootDir(), $this->path);
- // check if we have an old image
- if (isset($this->temp)) {
- // delete the old image
- unlink($this->getUploadRootDir().'/'.$this->temp);
- // clear the temp image path
- $this->temp = null;
- }
- $this->file = null;
- }
- /**
- * @ORM\PostRemove()
- */
- public function removeUpload()
- {
- if ($file = $this->getAbsolutePath()) {
- unlink($file);
- }
- }
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set name
- *
- * @param string $title
- * @return File
- */
- public function setTitle($title)
- {
- $this->title = $title;
- return $this;
- }
- /**
- * Get title
- *
- * @return string
- */
- public function getTitle()
- {
- return $this->title;
- }
- /**
- * Set path
- *
- * @param string $path
- * @return File
- */
- public function setPath($path)
- {
- $this->path = $path;
- return $this;
- }
- /**
- * Get path
- *
- * @return string
- */
- public function getPath()
- {
- return $this->path;
- }
- /**
- * Set content
- *
- * @param string $content
- * @return File
- */
- public function setContent($content)
- {
- $this->content = $content;
- return $this;
- }
- /**
- * Get content
- *
- * @return string
- */
- public function getContent()
- {
- return $this->content;
- }
- /**
- * Set author
- *
- * @param integer $author
- * @return File
- */
- public function setAuthor($author)
- {
- $this->author = $author;
- return $this;
- }
- /**
- * Get author
- *
- * @return integer
- */
- public function getAuthor()
- {
- return $this->author;
- }
- /**
- * Set date
- *
- * @param \DateTime $date
- * @return File
- */
- public function setDate($date)
- {
- $this->date = $date;
- return $this;
- }
- /**
- * Get date
- *
- * @return \DateTime
- */
- public function getDate()
- {
- return $this->date;
- }
- /**
- * Add text
- *
- * @param \Acme\EntityBundle\Entity\Text $text
- * @return File
- */
- public function addText(\Acme\EntityBundle\Entity\Text $text)
- {
- $this->text[] = $text;
- return $this;
- }
- /**
- * Remove text
- *
- * @param \Acme\EntityBundle\Entity\Text $text
- */
- public function removeText(\Acme\EntityBundle\Entity\Text $text)
- {
- $this->text->removeElement($text);
- }
- /**
- * Get text
- *
- * @return \Doctrine\Common\Collections\Collection
- */
- public function getText()
- {
- return $this->text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement