src/Entity/Kernel/KernelFileVersion.php line 385

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Kernel;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\HttpFoundation\File\UploadedFile;
  9. /**
  10.  * @ORM\Entity
  11.  */
  12. class KernelFileVersion {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\Column(type="string", length=255)
  21.      */
  22.     private $version;
  23.     /**
  24.      * @ORM\ManyToOne(targetEntity=KernelFile::class, inversedBy="kernelfileversions")
  25.      * @ORM\JoinColumn(nullable=false)
  26.      */
  27.     private $kernelfile;
  28.     /**
  29.      * @ORM\Column(type="boolean", nullable=true)
  30.      */
  31.     private $technical;
  32.     /**
  33.      * @ORM\ManyToOne(targetEntity=KernelFileVersion::class)
  34.      */
  35.     private $thumb;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity=KernelFileVersion::class)
  38.      */
  39.     private $squarethumb;
  40.     /**
  41.      * @ORM\ManyToOne(targetEntity=KernelFileVersion::class)
  42.      */
  43.     private $printthumb;
  44.     /**
  45.      * @Assert\File(maxSize="6000000")
  46.      */
  47.     private $file;
  48.     /**
  49.      * @ORM\Column(type="integer", nullable=true)
  50.      */
  51.     public $size;
  52.     /**
  53.      * @ORM\Column(type="integer", nullable=true)
  54.      */
  55.     public $width;
  56.     /**
  57.      * @ORM\Column(type="integer", nullable=true)
  58.      */
  59.     public $height;
  60.     /**
  61.      * @ORM\Column(type="string", length=255, nullable=true)
  62.      */
  63.     public $extention;
  64.     /**
  65.      * @ORM\Column(type="string", length=510, nullable=true)
  66.      */
  67.     public $mime;
  68.     /**
  69.      * @ORM\ManyToOne(targetEntity=Organization::class)
  70.      */
  71.     private $organization;
  72.     /**
  73.      * @ORM\Column(type="integer")
  74.      */
  75.     private $state;
  76.     /**
  77.      * @var datetime $created
  78.      *
  79.      * @Gedmo\Timestampable(on="create")
  80.      * @ORM\Column(type="datetime")
  81.      */
  82.     private $created;
  83.     /**
  84.      * @var string $createdBy
  85.      *
  86.      * @Gedmo\Blameable(on="create")
  87.      * @ORM\ManyToOne(targetEntity="App\Entity\Kernel\User")
  88.      * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
  89.      */
  90.     private $createdBy;
  91.     /**
  92.      * @var datetime $updated
  93.      *
  94.      * @ORM\Column(type="datetime")
  95.      * @Gedmo\Timestampable(on="update")
  96.      */
  97.     private $updated;
  98.     /**
  99.      * @var string $updatedBy
  100.      *
  101.      * @Gedmo\Blameable(on="update")
  102.      * @ORM\ManyToOne(targetEntity="App\Entity\Kernel\User")
  103.      * @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
  104.      */
  105.     private $updatedBy;
  106.     public function __construct() {
  107.         $this->actionprofiles = new ArrayCollection();
  108.     }
  109.     public function __toString() {
  110.         return "" . $this->id;
  111.     }
  112.     public function getId(): ?int {
  113.         return $this->id;
  114.     }
  115.     public function getName(): ?string {
  116.         return $this->name;
  117.     }
  118.     public function setName(string $name): self {
  119.         $this->name = $name;
  120.         return $this;
  121.     }
  122.     public function getState(): ?int {
  123.         return $this->state;
  124.     }
  125.     public function setState(int $state): self {
  126.         $this->state = $state;
  127.         return $this;
  128.     }
  129.     public function getCreated(): ?\DateTimeInterface {
  130.         return $this->created;
  131.     }
  132.     public function setCreated(\DateTimeInterface $created): self {
  133.         $this->created = $created;
  134.         return $this;
  135.     }
  136.     public function getUpdated(): ?\DateTimeInterface {
  137.         return $this->updated;
  138.     }
  139.     public function setUpdated(\DateTimeInterface $updated): self {
  140.         $this->updated = $updated;
  141.         return $this;
  142.     }
  143.     /**
  144.      * @return Collection|ActionProfile[]
  145.      */
  146.     public function getActionprofiles(): Collection {
  147.         return $this->actionprofiles;
  148.     }
  149.     public function addActionprofile(ActionProfile $actionprofile): self {
  150.         if (!$this->actionprofiles->contains($actionprofile)) {
  151.             $this->actionprofiles[] = $actionprofile;
  152.         }
  153.         return $this;
  154.     }
  155.     public function removeActionprofile(ActionProfile $actionprofile): self {
  156.         $this->actionprofiles->removeElement($actionprofile);
  157.         return $this;
  158.     }
  159.     public function getAuthorizationtype(): ?ListItem {
  160.         return $this->authorizationtype;
  161.     }
  162.     public function setAuthorizationtype(?ListItem $authorizationtype): self {
  163.         $this->authorizationtype = $authorizationtype;
  164.         return $this;
  165.     }
  166.     public function getOrganization(): ?Organization {
  167.         return $this->organization;
  168.     }
  169.     public function setOrganization(?Organization $organization): self {
  170.         $this->organization = $organization;
  171.         return $this;
  172.     }
  173.     public function getCreatedBy(): ?User {
  174.         return $this->createdBy;
  175.     }
  176.     public function setCreatedBy(?User $createdBy): self {
  177.         $this->createdBy = $createdBy;
  178.         return $this;
  179.     }
  180.     public function getUpdatedBy(): ?User {
  181.         return $this->updatedBy;
  182.     }
  183.     public function setUpdatedBy(?User $updatedBy): self {
  184.         $this->updatedBy = $updatedBy;
  185.         return $this;
  186.     }
  187.     public function getVersion(): ?string {
  188.         return $this->version;
  189.     }
  190.     public function setVersion(string $version): self {
  191.         $this->version = $version;
  192.         return $this;
  193.     }
  194.     public function getTechnical(): ?bool {
  195.         return $this->technical;
  196.     }
  197.     public function setTechnical(?bool $technical): self {
  198.         $this->technical = $technical;
  199.         return $this;
  200.     }
  201.     public function getSize(): ?int {
  202.         return $this->size;
  203.     }
  204.     public function setSize(?int $size): self {
  205.         $this->size = $size;
  206.         return $this;
  207.     }
  208.     public function getWidth(): ?int {
  209.         return $this->width;
  210.     }
  211.     public function setWidth(?int $width): self {
  212.         $this->width = $width;
  213.         return $this;
  214.     }
  215.     public function getHeight(): ?int {
  216.         return $this->height;
  217.     }
  218.     public function setHeight(?int $height): self {
  219.         $this->height = $height;
  220.         return $this;
  221.     }
  222.     public function getExtention(): ?string {
  223.         return $this->extention;
  224.     }
  225.     public function setExtention(?string $extention): self {
  226.         $this->extention = $extention;
  227.         return $this;
  228.     }
  229.     public function getMime(): ?string {
  230.         return $this->mime;
  231.     }
  232.     public function setMime(?string $mime): self {
  233.         $this->mime = $mime;
  234.         return $this;
  235.     }
  236.     public function getKernelfile(): ?KernelFile {
  237.         return $this->kernelfile;
  238.     }
  239.     public function setKernelfile(?KernelFile $kernelfile): self {
  240.         $this->kernelfile = $kernelfile;
  241.         return $this;
  242.     }
  243.     public function getThumb(): ?self {
  244.         return $this->thumb;
  245.     }
  246.     public function setThumb(?self $thumb): self {
  247.         $this->thumb = $thumb;
  248.         return $this;
  249.     }
  250.     public function getSquarethumb(): ?self {
  251.         return $this->squarethumb;
  252.     }
  253.     public function setSquarethumb(?self $squarethumb): self {
  254.         $this->squarethumb = $squarethumb;
  255.         return $this;
  256.     }
  257.     public function getPrintthumb(): ?self {
  258.         return $this->printthumb;
  259.     }
  260.     public function setPrintthumb(?self $printthumb): self {
  261.         $this->printthumb = $printthumb;
  262.         return $this;
  263.     }
  264.     public function getWebPath() {
  265.         return $this->getUploadDir() . '/';
  266.     }
  267.     
  268.         /**
  269.      * Get file.
  270.      *
  271.      * @return UploadedFile
  272.      */
  273.     public function getFile()
  274.     {
  275.         return $this->file;
  276.     }
  277.     public function getUploadRootDir() {
  278.         // the absolute directory path where uploaded
  279.         // documents should be saved
  280.         return __DIR__ . '/../../../' . $this->getUploadDir();
  281.     }
  282.     protected function getUploadDir() {
  283.         // get rid of the __DIR__ so it doesn't screw up
  284.         // when displaying uploaded doc/image in the view.
  285.         return 'data';
  286.     }
  287.     private $temp;
  288.     /**
  289.      * Sets file.
  290.      *
  291.      * @param UploadedFile $file
  292.      */
  293.     public function setFile(UploadedFile $file = null) {
  294.         $this->file = $file;
  295.         // check if we have an old image path
  296.         if (is_file($this->getAbsolutePath())) {
  297.             // store the old name to delete after the update
  298.             $this->temp = $this->getAbsolutePath();
  299.         } else {
  300.             //$this->path = 'initial';
  301.         }
  302.     }
  303.     /**
  304.      * @ORM\PrePersist()
  305.      * @ORM\PreUpdate()
  306.      */
  307.     public function preUpload() {
  308.         if (null !== $this->getFile()) {
  309.             $filename = $this->getFile()->getClientOriginalName();
  310.             $ext = pathinfo($filename, PATHINFO_EXTENSION);
  311.             $this->extention = $ext;
  312.             //$this->name = $filename;
  313.             try {
  314.                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
  315.                 $mime = finfo_file($finfo, $this->getFile());
  316.                 $this->mime = $mime;
  317.                 finfo_close($finfo);
  318.                 $this->name = $filename;
  319.             } catch (FileException$e) {
  320.                 //    $errorfile = true;
  321.             }
  322.         }
  323.     }
  324.     public function getFilename() {
  325.         return sprintf("%09d", $this->id);
  326.     }
  327.     public function getFileFolder() {
  328.         $filename = $this->getFilename();
  329.         return $this->getUploadRootDir() . "/" . substr($filename, 0, 3) . "/" . substr($filename, 3, 3);
  330.     }
  331.     /**
  332.      * @ORM\PostPersist()
  333.      * @ORM\PostUpdate()
  334.      */
  335.     public function upload() {
  336.         if (null === $this->getFile()) {
  337.             return;
  338.         }
  339.         // check if we have an old image
  340.         if (isset($this->temp)) {
  341.             // delete the old image
  342.             unlink($this->temp);
  343.             // clear the temp image path
  344.             $this->temp = null;
  345.         }
  346.         // you must throw an exception here if the file cannot be moved
  347.         // so that the entity is not persisted to the database
  348.         // which the UploadedFile move() method does
  349.         $filename = $this->getFile()->getClientOriginalName();
  350.         $ext = pathinfo($filename, PATHINFO_EXTENSION);
  351.         $finfo = finfo_open(FILEINFO_MIME_TYPE);
  352.         $mime = finfo_file($finfo, $this->getFile());
  353.         $this->mime = $mime;
  354.         finfo_close($finfo);
  355.         $this->extention = $ext;
  356.         $this->name = $filename;
  357.         $folder = $this->getFileFolder();
  358.         if (!is_dir($folder)) {
  359.             mkdir($folder, 0755, true);
  360.         }
  361.         $this->getFile()->move(
  362.                 $folder,
  363.                 $this->getFilename()
  364.         );
  365.         $size = filesize($folder . "/" . $this->getFilename());
  366.         $this->size = $size;
  367.         $this->setFile(null);
  368.     }
  369.     /**
  370.      * @ORM\PreRemove()
  371.      */
  372.     public function storeFilenameForRemove() {
  373.         $this->temp = $this->getFileFolder() . "/" . $this->getFilename();
  374.     }
  375.     /**
  376.      * @ORM\PostRemove()
  377.      */
  378.     public function removeUpload() {
  379.         if (isset($this->temp)) {
  380.             if (file_exists($this->temp)) {
  381.                 unlink($this->temp);
  382.             }
  383.         }
  384.     }
  385.     public function getAbsolutePath() {
  386.         //return null === $this->path
  387.         //    ? null
  388.         return $this->getUploadRootDir() . '/' . $this->id . '.' . $this->extention;
  389.     }
  390.     /**
  391.      * Get size
  392.      *
  393.      * @return integer 
  394.      */
  395.     public function getReadablesize() {
  396.         if ($this->size == 0) {
  397.             return $this->size;
  398.         }
  399.         $bytes = $this->size;
  400.         $precision = 2;
  401.         $units = array('B', 'KB', 'MB', 'GB', 'TB');
  402.         $bytes = max($bytes, 0);
  403.         $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  404.         $pow = min($pow, count($units) - 1);
  405.         // Uncomment one of the following alternatives
  406.         // $bytes /= pow(1024, $pow);
  407.         $bytes /= (1 << (10 * $pow));
  408.         return round($bytes, $precision) . ' ' . $units[$pow];
  409.     }
  410.     function getHumanFilesize() {
  411.         $bytes = $this->size;
  412.         $decimals = 2;
  413.         $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  414.         $factor = floor((strlen($bytes) - 1) / 3);
  415.         return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
  416.     }
  417. }