<?php
namespace App\Entity\Kernel;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @ORM\Entity
*/
class KernelFileVersion {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $version;
/**
* @ORM\ManyToOne(targetEntity=KernelFile::class, inversedBy="kernelfileversions")
* @ORM\JoinColumn(nullable=false)
*/
private $kernelfile;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $technical;
/**
* @ORM\ManyToOne(targetEntity=KernelFileVersion::class)
*/
private $thumb;
/**
* @ORM\ManyToOne(targetEntity=KernelFileVersion::class)
*/
private $squarethumb;
/**
* @ORM\ManyToOne(targetEntity=KernelFileVersion::class)
*/
private $printthumb;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* @ORM\Column(type="integer", nullable=true)
*/
public $size;
/**
* @ORM\Column(type="integer", nullable=true)
*/
public $width;
/**
* @ORM\Column(type="integer", nullable=true)
*/
public $height;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $extention;
/**
* @ORM\Column(type="string", length=510, nullable=true)
*/
public $mime;
/**
* @ORM\ManyToOne(targetEntity=Organization::class)
*/
private $organization;
/**
* @ORM\Column(type="integer")
*/
private $state;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var string $createdBy
*
* @Gedmo\Blameable(on="create")
* @ORM\ManyToOne(targetEntity="App\Entity\Kernel\User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
private $createdBy;
/**
* @var datetime $updated
*
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
*/
private $updated;
/**
* @var string $updatedBy
*
* @Gedmo\Blameable(on="update")
* @ORM\ManyToOne(targetEntity="App\Entity\Kernel\User")
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
*/
private $updatedBy;
public function __construct() {
$this->actionprofiles = new ArrayCollection();
}
public function __toString() {
return "" . $this->id;
}
public function getId(): ?int {
return $this->id;
}
public function getName(): ?string {
return $this->name;
}
public function setName(string $name): self {
$this->name = $name;
return $this;
}
public function getState(): ?int {
return $this->state;
}
public function setState(int $state): self {
$this->state = $state;
return $this;
}
public function getCreated(): ?\DateTimeInterface {
return $this->created;
}
public function setCreated(\DateTimeInterface $created): self {
$this->created = $created;
return $this;
}
public function getUpdated(): ?\DateTimeInterface {
return $this->updated;
}
public function setUpdated(\DateTimeInterface $updated): self {
$this->updated = $updated;
return $this;
}
/**
* @return Collection|ActionProfile[]
*/
public function getActionprofiles(): Collection {
return $this->actionprofiles;
}
public function addActionprofile(ActionProfile $actionprofile): self {
if (!$this->actionprofiles->contains($actionprofile)) {
$this->actionprofiles[] = $actionprofile;
}
return $this;
}
public function removeActionprofile(ActionProfile $actionprofile): self {
$this->actionprofiles->removeElement($actionprofile);
return $this;
}
public function getAuthorizationtype(): ?ListItem {
return $this->authorizationtype;
}
public function setAuthorizationtype(?ListItem $authorizationtype): self {
$this->authorizationtype = $authorizationtype;
return $this;
}
public function getOrganization(): ?Organization {
return $this->organization;
}
public function setOrganization(?Organization $organization): self {
$this->organization = $organization;
return $this;
}
public function getCreatedBy(): ?User {
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self {
$this->createdBy = $createdBy;
return $this;
}
public function getUpdatedBy(): ?User {
return $this->updatedBy;
}
public function setUpdatedBy(?User $updatedBy): self {
$this->updatedBy = $updatedBy;
return $this;
}
public function getVersion(): ?string {
return $this->version;
}
public function setVersion(string $version): self {
$this->version = $version;
return $this;
}
public function getTechnical(): ?bool {
return $this->technical;
}
public function setTechnical(?bool $technical): self {
$this->technical = $technical;
return $this;
}
public function getSize(): ?int {
return $this->size;
}
public function setSize(?int $size): self {
$this->size = $size;
return $this;
}
public function getWidth(): ?int {
return $this->width;
}
public function setWidth(?int $width): self {
$this->width = $width;
return $this;
}
public function getHeight(): ?int {
return $this->height;
}
public function setHeight(?int $height): self {
$this->height = $height;
return $this;
}
public function getExtention(): ?string {
return $this->extention;
}
public function setExtention(?string $extention): self {
$this->extention = $extention;
return $this;
}
public function getMime(): ?string {
return $this->mime;
}
public function setMime(?string $mime): self {
$this->mime = $mime;
return $this;
}
public function getKernelfile(): ?KernelFile {
return $this->kernelfile;
}
public function setKernelfile(?KernelFile $kernelfile): self {
$this->kernelfile = $kernelfile;
return $this;
}
public function getThumb(): ?self {
return $this->thumb;
}
public function setThumb(?self $thumb): self {
$this->thumb = $thumb;
return $this;
}
public function getSquarethumb(): ?self {
return $this->squarethumb;
}
public function setSquarethumb(?self $squarethumb): self {
$this->squarethumb = $squarethumb;
return $this;
}
public function getPrintthumb(): ?self {
return $this->printthumb;
}
public function setPrintthumb(?self $printthumb): self {
$this->printthumb = $printthumb;
return $this;
}
public function getWebPath() {
return $this->getUploadDir() . '/';
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getUploadRootDir() {
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../' . $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 'data';
}
private $temp;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null) {
$this->file = $file;
// check if we have an old image path
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
} else {
//$this->path = 'initial';
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload() {
if (null !== $this->getFile()) {
$filename = $this->getFile()->getClientOriginalName();
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$this->extention = $ext;
//$this->name = $filename;
try {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $this->getFile());
$this->mime = $mime;
finfo_close($finfo);
$this->name = $filename;
} catch (FileException$e) {
// $errorfile = true;
}
}
}
public function getFilename() {
return sprintf("%09d", $this->id);
}
public function getFileFolder() {
$filename = $this->getFilename();
return $this->getUploadRootDir() . "/" . substr($filename, 0, 3) . "/" . substr($filename, 3, 3);
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload() {
if (null === $this->getFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// 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->getFile()->getClientOriginalName();
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $this->getFile());
$this->mime = $mime;
finfo_close($finfo);
$this->extention = $ext;
$this->name = $filename;
$folder = $this->getFileFolder();
if (!is_dir($folder)) {
mkdir($folder, 0755, true);
}
$this->getFile()->move(
$folder,
$this->getFilename()
);
$size = filesize($folder . "/" . $this->getFilename());
$this->size = $size;
$this->setFile(null);
}
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove() {
$this->temp = $this->getFileFolder() . "/" . $this->getFilename();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload() {
if (isset($this->temp)) {
if (file_exists($this->temp)) {
unlink($this->temp);
}
}
}
public function getAbsolutePath() {
//return null === $this->path
// ? null
return $this->getUploadRootDir() . '/' . $this->id . '.' . $this->extention;
}
/**
* Get size
*
* @return integer
*/
public function getReadablesize() {
if ($this->size == 0) {
return $this->size;
}
$bytes = $this->size;
$precision = 2;
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
function getHumanFilesize() {
$bytes = $this->size;
$decimals = 2;
$size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
}