Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Repo\RepoBundle\Entity;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- use Symfony\Component\Validator\Constraints as Assert;
- // DON'T forget this use statement!!!
- use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
- /**
- * RepoFile
- * @ORM\Entity(repositoryClass="Repo\RepoBundle\Entity\Repository\RepoFileRepository")
- * @ORM\Table(name="repo_files")
- * @UniqueEntity("slug")
- * @UniqueEntity("sha1")
- * @ORM\HasLifecycleCallbacks()
- */
- class RepoFile {
- // a property used temporarily while deleting
- private $filenameForRemove;
- /**
- * @Assert\File(maxSize="6000000", mimeTypes={"application/xml", "text/xml"})
- */
- public $file;
- /**
- * @var integer
- *
- * @ORM\Column(name="id", type="integer", nullable=false)
- * @ORM\Id
- * @ORM\GeneratedValue(strategy="IDENTITY")
- */
- private $id;
- /**
- * @var string
- *
- * @ORM\Column(name="title", type="string", length=255, nullable=false)
- */
- private $title;
- /**
- * @var string
- *
- * @ORM\Column(name="slug", type="string", length=255, nullable=false, unique = true)
- */
- private $slug;
- /**
- * @ORM\ManyToOne(targetEntity="User", inversedBy="files")
- * @ORM\JoinColumn(name="author", referencedColumnName="id")
- */
- private $user;
- /**
- * @var string
- *
- * @ORM\Column(name="sha1", type="string", length=50, nullable=false, unique = true)
- */
- private $sha1;
- /**
- * @var integer
- *
- * @ORM\Column(name="author", type="integer", nullable=false)
- */
- private $author;
- /**
- * @var integer
- *
- * @ORM\Column(name="downloads", type="integer", nullable=false, options={"default" = 0})
- */
- private $downloads;
- /**
- * @var integer
- *
- * @ORM\Column(name="views", type="integer", nullable=false, options={"default" = 0})
- */
- private $views;
- /**
- * @var integer
- *
- * @ORM\Column(name="created", type="integer", nullable=false, options={"default" = 0})
- */
- private $created;
- /**
- * @var integer
- *
- * @ORM\Column(name="updated", type="integer", nullable=false)
- */
- private $updated;
- /**
- * @var integer
- *
- * @ORM\Column(name="votes", type="integer", nullable=false, options={"default" = 0})
- */
- private $votes;
- /**
- * Get id
- *
- * @return integer
- */
- public function getId() {
- return $this->id;
- }
- /**
- * Set author
- *
- * @param integer $author
- * @return RepoFile
- */
- public function setAuthor($author) {
- $this->author = $author;
- return $this;
- }
- /**
- * Get author
- *
- * @return integer
- */
- public function getAuthor() {
- return $this->author;
- }
- /**
- * Set downloads
- *
- * @param integer $downloads
- * @return RepoFile
- */
- public function setDownloads($downloads) {
- $this->downloads = $downloads;
- return $this;
- }
- /**
- * Get downloads
- *
- * @return integer
- */
- public function getDownloads() {
- return $this->downloads;
- }
- /**
- * Set views
- *
- * @param integer $views
- * @return RepoFile
- */
- public function setViews($views) {
- $this->views = $views;
- return $this;
- }
- /**
- * Get views
- *
- * @return integer
- */
- public function getViews() {
- return $this->views;
- }
- /**
- * Set created
- *
- * @param integer $created
- * @return RepoFile
- */
- public function setCreated($created) {
- $this->created = $created;
- return $this;
- }
- /**
- * Get created
- *
- * @return integer
- */
- public function getCreated() {
- return $this->created;
- }
- /**
- * Set updated
- *
- * @param integer $updated
- * @return RepoFile
- */
- public function setUpdated($updated) {
- $this->updated = $updated;
- return $this;
- }
- /**
- * Get updated
- *
- * @return integer
- */
- public function getUpdated() {
- return $this->updated;
- }
- /**
- * Set votes
- *
- * @param integer $votes
- * @return RepoFile
- */
- public function setVotes($votes) {
- $this->votes = $votes;
- return $this;
- }
- /**
- * Get votes
- *
- * @return integer
- */
- public function getVotes() {
- return $this->votes;
- }
- /**
- * Set user
- *
- * @param \Repo\RepoBundle\Entity\User $user
- * @return RepoFile
- */
- public function setUser(\Repo\RepoBundle\Entity\User $user = null) {
- $this->user = $user;
- return $this;
- }
- /**
- * Get user
- *
- * @return \Repo\RepoBundle\Entity\User
- */
- public function getUser() {
- return $this->user;
- }
- ////////////////////////////
- protected function getUploadRootDir() {
- // the absolute directory path where uploaded
- // documents should be saved
- return __DIR__ . '/../../../../web/' . $this->getUploadDir();
- }
- protected function getUploadDir() {
- // get rid of the __DIR__ so it doesn't screw up
- // when displaying uploaded doc/image in the view.
- return $this->upload_dir;
- }
- public function setUploadDir($uploadDir) {
- $this->upload_dir = $uploadDir;
- return $this;
- }
- // public function __set($name, $value) {
- //
- // echo 'setting ' . $name . ' to ' . $value;
- // $this->$name = $value;
- // return;
- // }
- // public function __unset($name) {
- // echo 'unsetting ' . $name ;
- // unset($this->$name);
- // return;
- // }
- /**
- * @ORM\PrePersist()
- * @ORM\PreUpdate()
- */
- public function preUpload() {
- if (null !== $this->file) {
- $this->path = $this->file->guessExtension();
- $this->sha1 = sha1($this->path);
- }
- $this->updated = time();
- return;
- }
- /**
- * @ORM\PrePersist()
- */
- public function fixValues() {
- $this->created = time();
- $this->downloads = 0;
- $this->views = 0;
- $this->votes = 0;
- $this->setTitle('Some idipo2958932856(*#(*$**@(@%333- -123=4-2title');
- return;
- }
- /**
- * @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
- $filename = $this->id . '.' . $this->file->guessExtension();
- $this->file->move(
- $this->getUploadRootDir(), $filename
- );
- unset($this->file);
- return;
- }
- /**
- * @ORM\PreRemove()
- */
- public function storeFilenameForRemove() {
- $this->filenameForRemove = $this->getAbsolutePath();
- }
- /**
- * @ORM\PostRemove()
- */
- public function removeUpload() {
- if ($this->filenameForRemove) {
- unlink($this->filenameForRemove);
- }
- }
- public function getAbsolutePath() {
- return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->id . '.' . $this->path;
- }
- public function slugify($text) {
- // replace non letter or digits by -
- $text = preg_replace('#[^\\pL\d]+#u', '-', $text);
- // trim
- $text = trim($text, '-');
- // transliterate
- if (function_exists('iconv')) {
- $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
- }
- // lowercase
- $text = strtolower($text);
- // remove unwanted characters
- $text = preg_replace('#[^-\w]+#', '', $text);
- if (empty($text)) {
- return 'n-a';
- }
- return $text;
- }
- /**
- * Set title
- *
- * @param string $title
- * @return RepoFile
- */
- public function setTitle($title) {
- $this->title = $title;
- $this->slug = $this->slugify($title);
- return $this;
- }
- /**
- * Get title
- *
- * @return string
- */
- public function getTitle() {
- return $this->title;
- }
- /**
- * Set slug
- *
- * @param string $slug
- * @return RepoFile
- */
- public function setSlug($slug) {
- $this->slug = $slug;
- return $this;
- }
- /**
- * Get slug
- *
- * @return string
- */
- public function getSlug() {
- return $this->slug;
- }
- /**
- * Set sha1
- *
- * @param string $sha1
- * @return RepoFile
- */
- public function setSha1($sha1) {
- $this->sha1 = $sha1;
- return $this;
- }
- /**
- * Get sha1
- *
- * @return string
- */
- public function getSha1() {
- return $this->sha1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment