<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Gedmo\Mapping\Annotation as Gedmo;


/**
 * Image
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ImageRepository")
 * @ORM\HasLifecycleCallbacks()
 *
 */
class Image
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Gedmo\Slug(fields={"filename","id"})
     * @ORM\Column(name="alt", type="string", length=255, unique=true)
     *    
     */
    private $alt;

    /**
     * @var string
     *
     * @ORM\Column(name="filename", type="string", length=255)
     */
    private $filename;
	
	/**
    * @var datetime
    *
    * @ORM\Column(name="updated", type="datetime", length=255, nullable=true)
    */
    private $updated;


    private $tempFilename;
	private $file;
	
	
	public function getFile(){
	  return $this->file;
	}
  
  
	public function setFile(UploadedFile $file = null){
	  $this->file = $file;
	}
	
	public function getUrl()
	{
		if(strpos($this->filename, 'http')===false)$imageUrl='images/'.$this->filename; 
		else $imageUrl=$this->filename;
		return $imageUrl; 
		
	}
	 
	protected function getUploadRootDir()
	{
		// On retourne le chemin relatif vers l'image pour notre code PHP
		return __DIR__.'/../../../web/images'; // SERVEUR\src\AppBundle\Entity/../../../web/images/img.jpg
    }
	
	/**
	 * Manages the copying of the file to the relevant place on the server
	 */
	public function upload()
	{
		// the file property can be empty if the field is not required
		if (null === $this->getFile()) {
			return;
		}
		
		// Si on avait un ancien fichier, on le supprime
		 $oldFile = $this->getUploadRootDir().'/'.$this->getFilename();
		 if (file_exists($oldFile)&&!is_dir($oldFile)) {
			unlink($oldFile);
		 }
	
		// move takes the target directory and target filename as params
		$this->getFile()->move(
			$this->getUploadRootDir(),
			$this->getFile()->getClientOriginalName()
		);
			
		// set the path property to the filename where you've saved the file
		$filename = $this->getFile()->getClientOriginalName();
		$this->setFilename($filename);
		
		// clean up the file property as you won't need it anymore
		$this->setFile(null);
	}
	/**
	* @ORM\PreRemove()
	*/
	public function preRemoveUpload()
	{
		// On sauvegarde temporairement le nom du fichier, car il dépend de l'id
		$this->tempFilename = $this->getUploadRootDir().'/'.$this->filename;
	}
	
	/**
	* @ORM\PostRemove()
	*/
	public function removeUpload()
	{
		// En PostRemove, on n'a pas accès à l'id, on utilise notre nom sauvegardé
		if (file_exists($this->tempFilename)) {
		  // On supprime le fichier
		  unlink($this->tempFilename);
		}
	}
	
	/**
   	* @ORM\PrePersist()
    * @ORM\PreUpdate()
   	*/
	public function lifecycleFileUpload()
	{
		$this->upload();
	}
	
	/**
	 * Updates the hash value to force the preUpdate and postUpdate events to fire
	 */
	public function refreshUpdated()
	{
		$this->setUpdated(new \DateTime());
	}
	
	
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set alt
     *
     * @param string $alt
     *
     * @return Image
     */
    public function setAlt($alt)
    {
        $this->alt = $alt;

        return $this;
    }

    /**
     * Get alt
     *
     * @return string
     */
    public function getAlt()
    {
        return $this->alt;
    }

    /**
     * Set filename
     *
     * @param string $filename
     *
     * @return Image
     */
    public function setFilename($filename)
    {
        $this->filename = $filename;

        return $this;
    }

    /**
     * Get filename
     *
     * @return string
     */
    public function getFilename()
    {
        return $this->filename;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     *
     * @return Image
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }
}
