<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CmsMediaLibraryRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CmsMediaLibraryRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $deletedUid;
    protected $deletedAt;
    
    
    public function getPaginatorService()
    {
    	return $this->paginatorService;
    }
    
    public function setPaginatorService($paginatorService)
    {
    	$this->paginatorService = $paginatorService;
    }
    

    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to set project settings
     *
     * @param 1: $projectSetting
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get project settings
     *
     * @param : None
     *
     * @return: project setting array
     *
     * @throws Exception: Null
     */
    public function getProjectSetting()
    {
        return $this->projetSetting;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function prepareProjectSettingData()
    {
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->deletedUid = $this->userId;
        $this->deletedAt = new \DateTime();
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to perform add and edit records in the database.
     *
     * @param 1 : entity object
     *
     * @return Null
     *
     * @throws Exception: Null
     *
     **/
    public function commit($entity)
    {
        $this->_em->persist($entity);
        $this->_em->flush();
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : Find single  based on ID
     *
     * @param $id
     *
     * @return : Feedback Master Object
     *
     * @throws Exception: Null
     */
    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('ml')
        ->select('ml')
        ->where('ml.id = :id')
        ->andWhere('ml.deletedAt IS NULL')
        ->setParameter(':id', $id)
        ->getQuery()
        ->getOneOrNullResult();
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : Find single  based on name
     *
     * @param $name
     *
     * @return : cms media library object
     *
     * @throws Exception: Null
     */
    public function findByName($name)
    {
        return $this->createQueryBuilder('ml')
        ->select('ml')
        ->where('ml.name = :name')
        ->andWhere('ml.deletedAt IS NULL')
        ->setParameter(':name', $name)
        ->getQuery()
        ->getOneOrNullResult();
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to store the uploaded media's data into database
     *
     * @param : $uploadedMediaInfo
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function setMediaLibrary($uploadedMediaInfo)
    {
        $cmsMediaLibrary = new CmsMediaLibrary();
        $cmsMediaLibrary->setName($uploadedMediaInfo['name']);
        //$cmsMediaLibrary->setPath($uploadedMediaInfo['path']);
        $cmsMediaLibrary->setType($uploadedMediaInfo['type']);
        $cmsMediaLibrary->setCreatedAt(new \DateTime());
        $cmsMediaLibrary->setCreatedUid($this->userId);
        $cmsMediaLibrary->setInstanceId($this->instanceId);
        $cmsMediaLibrary->setInstanceType($this->instanceType);
        
        if (isset($uploadedMediaInfo['is_artwork'])) {
            $cmsMediaLibrary->setIsArtwork($uploadedMediaInfo['is_artwork']);
        }
        
        if (isset($uploadedMediaInfo['image_name'])) {
            $cmsMediaLibrary->setImageName($uploadedMediaInfo['image_name']);
        }
        
        if (isset($uploadedMediaInfo['customer_name'])) {
            $cmsMediaLibrary->setCustomerName($uploadedMediaInfo['customer_name']);
        }
        
        $this->commit($cmsMediaLibrary);
        return $cmsMediaLibrary->getId();
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to fetch the uploaded media from database
     *
     * @param : Null
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function getMediaLibrary($mediaType, $keyword='')
    {
        $cmsMediaLibrary = $this->createQueryBuilder('ml')
        ->select('ml')
        ->where('ml.instanceId = :instanceId')
        ->setParameter(':instanceId', $this->instanceId);
        if (!empty($keyword)) {
            $cmsMediaLibrary->andWhere('ml.name LIKE :name')
                ->setParameter('name', '%'.$keyword.'%');
        }
        if (!empty($mediaType)) {
            $cmsMediaLibrary->andWhere('ml.type LIKE :type')
            ->setParameter('type', $mediaType);
        }
        $cmsMediaLibrary->andWhere('ml.deletedAt IS NULL');
        $cmsMediaLibrary->orderBy('ml.createdAt', 'DESC');
        $cmsMediaLibrary->getQuery()->getResult();
        if (empty($cmsMediaLibrary)) {
            $cmsMediaLibrary = null;
        }
        
        return $cmsMediaLibrary;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to fetch the uploaded media from database (for other modules)
     *
     * @param : $mediaType
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function getMediaLibraryRecords($mediaType)
    {
        $cmsMediaLibrary = $this->createQueryBuilder('ml')
        ->select('ml')
        ->where('ml.instanceId = :instanceId')
        ->setParameter(':instanceId', $this->instanceId);
        
        if (!empty($mediaType)) {
            $cmsMediaLibrary->andWhere('ml.type = :type')
                ->setParameter(':type', $mediaType);
        }
        
        $cmsMediaLibrary->andWhere('ml.createdUid = :createdUid')
            ->setParameter(':createdUid', $this->userId);
        
        $cmsMediaLibrary->andWhere('ml.deletedAt IS NULL');
        
        $cmsMediaLibrary->orderBy('ml.createdAt', 'DESC');
        
        $cmsMediaLibrary = $cmsMediaLibrary->getQuery()->getResult();
        
        if (empty($cmsMediaLibrary)) {
            $cmsMediaLibrary = null;
        }
        return $cmsMediaLibrary;
    }
    
    /**
     * @author Employee Id: 4471
     *
     * Description : This function is used to fetch the uploaded media from database with pagination(for other modules)
     *
     * @param : $mediaType
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function getMediaLibraryRecordsWithPagination($mediaType,$page,$recodParPage)
    {
    	$cmsMediaLibrary = $this->createQueryBuilder('ml')
    	->select('ml')
    	->where('ml.instanceId = :instanceId')
    	->setParameter(':instanceId', $this->instanceId);
    
    	if (!empty($mediaType)) {
    		$cmsMediaLibrary->andWhere('ml.type = :type')
    		->setParameter(':type', $mediaType);
    	}
    
    	$cmsMediaLibrary->andWhere('ml.createdUid = :createdUid')
    	->setParameter(':createdUid', $this->userId);
    
    	$cmsMediaLibrary->andWhere('ml.deletedAt IS NULL');
    
    	$cmsMediaLibrary->orderBy('ml.createdAt', 'DESC');
    	return $this->paginatorService->paginate($cmsMediaLibrary, $page, $recodParPage);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to fetch media types
     *
     * @param : Null
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function getMediaType()
    {
        $cmsMediaTypes = $this->createQueryBuilder('ml')
        ->select('ml.type')
        ->where('ml.instanceId = :instanceId')
        ->setParameter(':instanceId', $this->instanceId)
        ->groupBy('ml.type')
        ->getQuery()
        ->getResult();
        if (empty($cmsMediaTypes)) {
            $cmsMediaTypes = null;
        }
    
        return $cmsMediaTypes;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to delete media by name
     *
     * @param : $name
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function deleteMediaByName($name)
    {
        $fileInfo = $this->findByName($name);
        //$this->_em->remove($fileInfo);
        //$this->_em->flush();
        
        $fileInfo->setDeletedAt($this->deletedAt);
        $fileInfo->setDeletedUid($this->deletedUid);
        
        $this->commit($fileInfo);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to delete media by id
     *
     * @param : $id
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function deleteMediaById($id)
    {
        $fileInfo = $this->find($id);
        //$this->_em->remove($fileInfo);
        //$this->_em->flush();
    
        $fileInfo->setDeletedAt($this->deletedAt);
        $fileInfo->setDeletedUid($this->deletedUid);
    
        $this->commit($fileInfo);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to fetch media types
     *
     * @param : $name
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function getImage($name)
    {
        $imageInfo = $this->findByName($name);
        if (empty($imageInfo)) {
            //$imagePath = '';
            $imageName = '';
        } else {
            //$imagePath = $imageInfo->getPath();
            $imageName = $imageInfo->getName();
        }
        //return array($imagePath, $imageName);
        return $imageName;
    }
}
