<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\Bundle\CmsBundle\Form\CmsMediaLibraryType;

/**
 *
 * @author Employee Id: 5412
 *
 * Description : Controller class for media library
 *
 * @Route("/{_locale}")
 */
class ArtLibraryController extends Controller
{
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function __construct()
    {
        $this->cmsMediaLibraryManager = null;
        $this->responseArray = null;
        $this->request = null;
        $this->translator = null;
        $this->projectSetting = null;
        $this->user_id = null;
        
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to unset member variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function __destruct()
    {
        unset($this->cmsMediaLibraryManager);
        unset($this->responseArray);
        unset($this->request);
        unset($this->translator);
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to initialize the variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->cmsMediaLibraryManager = $this->get('office_brain_cms_media_library_manager');
        $this->responseArray = array();
        $this->translator = $this->get('translator');
        $this->projectSetting = $this->request->get('project_setting');
        $this->user_id = $this->projectSetting['user_id'];
        $this->em = $this->container->get('doctrine')->getEntityManager();
        
        
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to list uploaded media
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/my-account/art-library", name="office_brain_custombundle_cmsbundle_cmsmedialibrary_list")
     * @Template("OfficeBrainCustomBundleCmsBundle:ArtLibrary:list.html.twig")
     */
    public function listAction()
    {
        $this->initAction();
        //list($this->responseArray['media_types'], $this->responseArray['pagination']) = $this->cmsMediaLibraryManager->getMedia();
        $this->responseArray['library'] = $this->cmsMediaLibraryManager->getMediaById($this->user_id);
        $this->responseArray['library_count']= $this->cmsMediaLibraryManager->CountMedia($this->user_id);
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used download a file
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/download/{name}", name="office_brain_cmsbundle_cmsmedialibrary_download")
     * @Template()
     */
    public function downloadAction()
    {
    	
        $this->initAction();
        
        $name = $this->request->get('name');
        $this->cmsMediaLibraryManager->downloadMedia($name);
        unset($name);
        return $this->redirect($this->generateUrl('office_brain_cmsbundle_cmsmedialibrary_list'));
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used delete a file permanantly
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/my-account/art-delete", name="office_brain_custombundle_cmsbundle_cmsmedialibrary_delete")
     * @Template()
     * options={"expose"=true}
     */
    public function deleteAction()
    { 
    	$this->initAction();
     	$id = $this->request->get('id');
        $response = $this->cmsMediaLibraryManager->deleteMediaById($id);
        unset($id);
       
        
        if ($response) {
    
            $this->responseArray['status'] = 'success';
            $this->responseArray['success_message'] = $this->translator->trans('cms.media_library.list.art_delete_success');
         //   $this->responseArray['page_url'] = $this->generateUrl('office_brain_cusombundle_cmsbundle_cmsmedialibrary_list');
            
        }
        return new JsonResponse($this->responseArray);
    }
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used upload a file 
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/my-account/art-upload", name="office_brain_custombundle_cmsbundle_cmsmedialibrary_save")
     * @Template()
     */
    public function uploadAction()
    {
     $this->initAction();
        if (isset($_FILES['file']) && !empty($_FILES['file'])) {
        	//if (in_array($_FILES['file']['type'], array('image/jpeg', 'image/gif', 'image/png', 'application/pdf','application/force-download','application/x-download'))) {
            	$this->responseArray = $this->cmsMediaLibraryManager->uploadMediaFile($_FILES);
        	//}else{
        		//$errorMessage = $this->translator->trans('cms.media_library.upload.file_upload_error.file_type');
        		//return new JsonResponse(array('status' => 'error', 'error_message' => $errorMessage));
        	//}
        }
        if (isset($this->responseArray[0]['error'])) {
            $errorMessage = '';
            if ($this->responseArray[0]['error'] == 'MEDIA_FILE_TYPE_NOT_ALLOWED') {
                $errorMessage = $this->translator->trans('cms.media_library.upload.file_upload_error.file_type');
            }
            
            if ($this->responseArray[0]['error'] == 'MEDIA_FILE_TOO_LARGE') {
                $errorMessage = $this->translator->trans('cms.media_library.upload.file_upload_error.file_size');
            }
            
            return new JsonResponse(array('status' => 'error', 'error_message' => $errorMessage));
        } else {
            return new JsonResponse(array('status' => 'success', 'success_message' => $this->translator->trans('cms.media_library.upload.file_upload_success'),'page_url'=> $this->generateUrl('office_brain_custombundle_cmsbundle_cmsmedialibrary_list')));
        }
    }
     
}
