<?php

namespace OfficeBrain\Bundle\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: 4464
 *
 * Description : Controller class for media library
 *
 * @Route("/admin/{_locale}/media-library")
 */
class CmsMediaLibraryAdminController extends Controller
{
    /**
     * @author Employee Id: 4464
     *
     * 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;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * 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: 4464
     *
     * 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');
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to upload image, pdf and doc files
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/upload", name="office_brain_cmsbundle_cmsmedialibrary_upload")
     * @method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:upload.html.twig")
     */
    public function uploadAction()
    {
        $this->initAction();

        //$form = $this->createForm(new CmsMediaLibraryType());
        //$this->responseArray['form'] = $form->createView();

        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to process uploaded image, pdf and doc files
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/upload", name="office_brain_cmsbundle_cmsmedialibrary_uploadprocess")
     * @method("POST")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:upload.html.twig")
     */
    public function uploadProcessAction()
    {
        $this->initAction();

        if (isset($_FILES['upload']) && !empty($_FILES['upload'])) {
            $this->responseArray = $this->cmsMediaLibraryManager->uploadMediaFile($_FILES);
        }
        
        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')));
        }
    }

    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to list uploaded media
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/list/{page}", name="office_brain_cmsbundle_cmsmedialibrary_list",
     * requirements={"page"="\d+"},
     * defaults={"page"=1})
     * @method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:list.html.twig")
     */
    public function listAction()
    {
        $this->initAction();
        
        //list($this->responseArray['media_types'], $this->responseArray['pagination']) = $this->cmsMediaLibraryManager->getMedia();
        $this->responseArray['pagination'] = $this->cmsMediaLibraryManager->getMedia();
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * 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: 4464
     *
     * Description : This function is used delete a file permanantly
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/delete", name="office_brain_cmsbundle_cmsmedialibrary_delete")
     * @Template()
     */
    public function deleteAction()
    {
        $this->initAction();
        
        $id = $this->request->get('id');
        $response = $this->cmsMediaLibraryManager->deleteMediaById($id);
        unset($id);
        
        /* if ($response) {
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('cms.media_library.list.media_delete_success'));
        } else {
            $this->get('session')->getFlashBag()->add('error_message', $this->translator->trans('cms.media_library.list.media_delete_error'));
        }
        unset($response);
        
        if ($this->request->isXmlHttpRequest()) {
            return new JsonResponse(array('success' => 'success', 'page_url' => $this->generateUrl('office_brain_cmsbundle_cmsmedialibrary_list', array())));
        } else {
            return $this->redirect($this->generateUrl('office_brain_cmsbundle_cmsmedialibrary_list'));
        } */
        
        if ($response) {
            $this->responseArray['status'] = 'success';
            $this->responseArray['success_message'] = $this->translator->trans('cms.media_library.list.media_delete_success');
        }
        return new JsonResponse($this->responseArray);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to view image
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/view/{name}", name="office_brain_cmsbundle_cmsmedialibrary_view")
     * @method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:view.html.twig")
     */
    public function viewAction()
    {
        $this->initAction();
        
        list($this->responseArray['image_path'], $this->responseArray['image_name']) = $this->cmsMediaLibraryManager->getImage();
        
        /* if ($this->request->isXmlHttpRequest()) {
            return new JsonResponse(array('success' => 'success', 'image_path' => $this->responseArray['image_path'], 'image_name' => $this->responseArray['image_name']));
        } else {
            return $this->redirect($this->generateUrl('office_brain_cmsbundle_cmsmedialibrary_list'));
        } */
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 5058
     *
     * Description : This function is used to view Media library popup
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/media_library_popup", name="office_brain_cmsbundle_cmsmedialibrary_medialibrarypopupajax",options={"expose"=true})
     * @method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:mediaLibraryPopupAjax.html.twig")
     */
    public function mediaLibraryPopupAjaxAction()
    {
        $this->initAction();
        
    
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 5058
     *
     * Description : This function is used to process uploaded image
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/media_library_popup", name="office_brain_cmsbundle_cmsmedialibrary_medialibrarypopupuploadprocessajax")
     * @method("POST")
     * @Template()
     */
    public function mediaLibraryPopupProcessAjaxAction()
    {
        $this->initAction();
    
        if (isset($_FILES['upload']) && !empty($_FILES['upload'])) {
            $this->responseArray = $this->cmsMediaLibraryManager->uploadMediaFile($_FILES);
        }
    
        if (isset($this->responseArray['error'])) {
            return new JsonResponse(array('error' => $this->responseArray['error']));
        } else {
            return new JsonResponse(array('success' => 'success'));
        }
    }
    
    /**
     * @author Employee Id: 5058
     *
     * Description : This function is used to view Media library upload
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/media_library_upload", name="office_brain_cmsbundle_cmsmedialibrary_medialibraryuploadajax",options={"expose"=true})
     * @method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:uploadAjax.html.twig")
     */
    public function mediaLibraryUploadAjaxAction()
    {
        $this->initAction();
         
    
        return $this->responseArray;
    }
    /**
     * @author Employee Id: 5058
     *
     * Description : This function is used to list uploaded media to popup
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/listmediaajax/{page}", name="office_brain_cmsbundle_cmsmedialibrary_listmediaajax",
     * requirements={"page"="\d+"},
     * defaults={"page"=1})
     * @method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMediaLibraryAdmin:listMediaAjax.html.twig")
     */
    public function listMediaAjaxAction()
    {
        $this->initAction();
    
        //list($this->responseArray['media_types'], $this->responseArray['pagination']) = $this->cmsMediaLibraryManager->getMedia();
        $this->responseArray['pagination'] = $this->cmsMediaLibraryManager->getMedia();
        return $this->responseArray;
    }
}
