<?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;
use Symfony\Component\Translation\Translator;
use OfficeBrain\CustomBundle\CmsBundle\Entity\UploadArtworkCenter;
use OfficeBrain\CustomBundle\CmsBundle\Form\UploadArtworkCenterType;
use Symfony\Component\HttpFoundation\Request;

/**
 *
 * @author Employee Id: 5412
 *
 * Description : Controller class for media library
 *
 * @Route("/{_locale}")
 */
class UploadArtworkCenterController 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->cmsUploadArtworkManager = 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);
        unset($this->commonLibraryManager);
    }
    
    /**
     * @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->imageLibraryManager = $this->get('office_brain_cms_media_library_manager');
        $this->commonLibraryManager = $this->get('office_brain_common_library');
        
        $this->mediaFilePath = $this->container->getParameter('cms.media_file_path');
        $this->cmsUploadArtworkManager = $this->get('office_brain_cms_upload_artwork_center_manager_custom');
        $this->uploadArtworkCenterFileUrl = $this->container->getParameter('upload_artwork_center.file_url');
        $this->uploadArtworkCenterFilePath = $this->container->getParameter('upload_artwork_center.file_path');
        
        $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();
        $this->userId = $this->projectSetting['user_id'];
        $this->orderManager = $this->get('office_brain_order_manager');
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to list uploaded media
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/upload-artwork-center", name="office_brain_custombundle_cmsbundle_upload_artwork_center_add")
     * @Template("OfficeBrainCustomBundleCmsBundle:UploadArtworkCenter:add.html.twig")
     */
    public function addAction()
    {// will transfer this in manager tomorrow
        $this->initAction();
        $orderId=$this->request->get('orderid');
        $orderBillingInfo=$this->orderManager->getBillingDetailsBYOrderId($orderId);
    	$uploadArtworkCenterEntity = new UploadArtworkCenter();
    	$uploadArtworkCenterForm = $this->createForm(new UploadArtworkCenterType(), $uploadArtworkCenterEntity);
    	
    	return array(
    			'artworkForm' => $uploadArtworkCenterForm->createView(),'billing_info'=>$orderBillingInfo,
    	);
    }
    
    /**
     * @author Employee Id: 5412
     *
     * Description : This function is used to insert a new artwork file
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     *
     * @Route("/upload-artwork-center/new", name="office_brain_custombundle_cmsbundle_upload_artwork_center_new")
     * @Template("OfficeBrainCustomBundleCmsBundle:UploadArtworkCenter:list.html.twig")
     */
    public function newAction(Request $request)
    {// will transfer this in manager tomorrow
        $this->initAction();
        
        
    	    if ($this->request->isXmlHttpRequest())
    		{
    			return $this->cmsUploadArtworkManager->uploadArtworkFile($request);
    		}
    		else
    		{
    			return $this->redirect($this->generateUrl('office_brain_page_homepage_view'));
    		}  			
    }
    
    /**
     * @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("/artwork-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.media_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("/artwork-upload", name="office_brain_custombundle_cmsbundle_cmsmedialibrary_upload")
     * @Template()
     */
    public function uploadAction()
    {
     $this->initAction();
        if (isset($_FILES['file']) && !empty($_FILES['file'])) {
            $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'),'page_url'=> $this->generateUrl('office_brain_custombundle_cmsbundle_upload_artwork_center_add')));
        }
    }
     
}
