<?php
namespace OfficeBrain\CustomBundle\CmsBundle\Manager\Extended;

use OfficeBrain\Bundle\CmsBundle\Manager\Extended\CmsMediaLibraryExtended as BaseCmsMediaLibraryExtended;

//use OfficeBrain\Bundle\CmsBundle\Entity\CmsPageMaster;
//use OfficeBrain\Bundle\CmsBundle\Entity\CmsPageLanguage;

class CmsMediaLibraryExtended extends BaseCmsMediaLibraryExtended
{
    
	/**
	 * @author Employee Id: 4464
	 *
	 * Description : This function is used to set member variables default value
	 *
	 * @param 1: $entityManager
	 * @param 2: $request
	 * @param 3: $knpPaginator
	 * @param 4: $mediaFileUrl
	 * @param 5: $mediaFilePath
	 *
	 * @return: NULL
	 *
	 * @throws Exception: Null
	 */
	public function __construct($entityManager, $request, $knpPaginator, $mediaFileUrl, $mediaFilePath)
	{
		parent::__construct($entityManager, $request, $knpPaginator, $mediaFileUrl, $mediaFilePath);
		$this->entityManager = $entityManager;
		
 		 $this->mediaFileUrl =$mediaFileUrl;
 		$this->mediaPath = $mediaFilePath;
 	
	}
	
	/**
	 * @author Employee Id: 4464
	 *
	 * Description : This function is used to unset member variables
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 */
	public function __destruct()
	{
		parent::__destruct();
	}
	
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to fetch the uploaded media from database
     *
     * @param : Null
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function getMediaById($UserId)
    {
        $mediaLibraryList = $this->entityManager->getRepository('OfficeBrainCustomBundleCmsBundle:CmsMediaLibrary')->findMediaById($UserId);
        /* $mediaTypes = $this->cmsMediaLibraryRepository->getMediaType();
        if (!empty($mediaTypes)) {
            foreach ($mediaTypes as $index) {
                foreach ($index as $key => $type) {
                    $temp[] = $type;
                }
            }
            $mediaTypes = $temp;
        } */
       $mediaLibraryListArray = $this->updateMediaLibraryFile($mediaLibraryList);
        if (!empty($mediaLibraryListArray)) {
            //return array($mediaTypes, $this->knpPaginator->paginate($mediaLibraryList, $this->request->get('page', '1'), $this->recordsPerPage, array(), count($mediaLibraryList)));
            return $mediaLibraryListArray; // $this->knpPaginator->paginate($mediaLibraryList, $this->request->get('page', '1'), $this->recordsPerPage, array(), count($mediaLibraryList));
        }
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to arrange uploaded file types
     *
     * @param : Null
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function updateMediaLibraryFile($mediaLibraryList)
    {
    	$previewImagesExt = array(".jpg",".png",".gif",".jpeg",".svg");
    	$MediaLibraryArray = array();
    	if(count($mediaLibraryList) > 0)
    	{
    		$i=0;
    		foreach ($mediaLibraryList as $index) {
    			$imageNameArray = explode('_',$index->getName());
				array_splice($imageNameArray, 0, 1);
				$imageNameStr = implode('',$imageNameArray);
				$ImageType = $index->getType();
				//Set Character Limit in Uploaded Media Library
				$fullimageName = $index->getName();
				if(strlen($imageNameStr)>15){
					$imageName= substr($imageNameStr, 0, 15);
					$imageName.='...';
				} 
				else{
					$imageName = $imageNameStr;
				}
				// Uploaded Media Library URL
    			if($ImageType =='Image'){
    				$ImageFullPath =  $index->getName();
    				if(file_exists($this->mediaPath.'/'.$ImageFullPath)) {
	    				$info = getimagesize($this->mediaPath.'/'.$ImageFullPath);
	    				$extension = image_type_to_extension($info[2]);
	    				if(in_array($extension,$previewImagesExt)){
	    					$imageDisplayPath =$this->mediaFileUrl.'/'.$ImageFullPath;
	    				}
	    				else{
	    					$imageDisplayPath =$this->mediaFileUrl.'/'.'no-image-available.png';
	    				}
    				}
    				else{
    					$imageDisplayPath =$this->mediaFileUrl.'/'.'no-image-available.png';
    				}
    			}
    			else if($ImageType == 'PDF'){
    				$imageDisplayPath =$this->mediaFileUrl.'/'.'pdf-img-2.png';
    				
    			}
    			else if($ImageType=='Document'){
    				$imageDisplayPath =$this->mediaFileUrl.'/'.'doc-icon.png';
    				
    			}
    			else{
    				$imageDisplayPath =$this->mediaFileUrl.'/'.'no-image-available.png';
    				
    			}
    			$MediaLibraryArray[$i]['id']= $index->getId();
				$MediaLibraryArray[$i]['type'] = $ImageType;
    			$MediaLibraryArray[$i]['imageName'] = $imageName;
    			$MediaLibraryArray[$i]['fullImageName'] = $fullimageName;
    			$MediaLibraryArray[$i]['imageDisplayPath'] = $imageDisplayPath;
   			 $i++; 
            }
    	}
    	return $MediaLibraryArray;
    	
    	/* $mediaTypes = $this->cmsMediaLibraryRepository->getMediaType();
    	 if (!empty($mediaTypes)) {
    	foreach ($mediaTypes as $index) {
    	foreach ($index as $key => $type) {
    	$temp[] = $type;
    	}
    	}
    	$mediaTypes = $temp;
    	} */
    	 
    	if (!empty($mediaLibraryList)) {
    		//return array($mediaTypes, $this->knpPaginator->paginate($mediaLibraryList, $this->request->get('page', '1'), $this->recordsPerPage, array(), count($mediaLibraryList)));
    		return $mediaLibraryList; // $this->knpPaginator->paginate($mediaLibraryList, $this->request->get('page', '1'), $this->recordsPerPage, array(), count($mediaLibraryList));
    	}
    }
    
    
   /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to count all user media 
     *
     * @param : Null
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function CountMedia($UserId)
    {
    	$CountLibraryList = $this->entityManager->getRepository('OfficeBrainCustomBundleCmsBundle:CmsMediaLibrary')->CountMedia($UserId);
    	 return $CountLibraryList;
    }
    /**
     * @author Employee Id: 5412
     *
     * To delete exist file
     *
     * @param string $filePath
     * @param string $fileName
     *
     * @throws Exception
     *
     * @return boolean
     */
    public function deleteFile($filePath, $fileName)
    {
    
    	//if(!$this->exists($filePath, $fileName))
    	//throw new Exception(sprint("Can't delete unexisting file \"%s\"", $fileName));
    
    	return true; // unlink($filePath .'/'. $fileName);
    }
    
    
 
}
