<?php

namespace Neag\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;

class AdminFileadminController extends ControllerAbstract
{
    /**
     * @Route("/admin/fileadmin/{type}")
     * @Template("NeagMainBundle:AdminFileadmin:index.html.twig")
     */
    public function indexAction($type = 'standalone')
    {
        $parent = '::admin.html.twig';
        //$this->trace($type);
        $click_on_file_text = true;
        if ($type == 'popup') {
            $parent = '::admin_blank.html.twig';
            $click_on_file_text = false;
        }
        $fileadmin = $this->get('fileadmin');
        $fileadmin->setBasePath('filedata/fileadmin/');
        $fileadmin->setBaseWebPath($this->container->get('templating.helper.assets')->getUrl('filedata/fileadmin/'));
        if ($type == 'popup') {
            $fileadmin->setFileCallback(Array($this, 'getFileSelectAttributes'));
        } else {
            $fileadmin->setFileCallback(Array($this, 'getFileAttributes'));
        }
        $result = $fileadmin->build();
        if ($fileadmin->isRedirect()) { 
            $redirect = $fileadmin->getRedirect(); 
            return $this->redirect($redirect);
        } else {
            return Array('fileadmin' => $result, 'parent' => $parent, 'click_on_file_text' => true);
        }
    }
    
    /**
     * ?????????? ???????? ???? a ??? ????? ? ??????
     *
     * @access      public
     * @param       string
     * @param       object xbkFileadmin
     * @return      array
     */
    public function getFileAttributes($dir, $file, $FileAdmin) {
        $request = $this->get('request');
        $link = $FileAdmin->getBaseWebPath().$dir.rawurlencode($file);
        $absolute_link = $request->getScheme().'://'.$request->getHttpHost().$FileAdmin->getBaseWebPath().$dir.rawurlencode($file);
        $attr = Array();
        $attr['href'] = 'javascript:fileadmin_display_file_urls(\''.addslashes($file).'\', \''.addslashes($link).'\', \''.addslashes($absolute_link).'\')';
        return $attr;
    }
    
    /**
     * @Route("/admin/afisha/fileadmin/{type}")
     * @Template("NeagMainBundle:AdminFileadmin:index.html.twig")
     */
    public function afishaAction($type = 'standalone')
    {
        $parent = '::admin.html.twig';
        //$this->trace($type);
        $click_on_file_text = true;
        if ($type == 'popup') {
            $parent = '::admin_blank.html.twig';
            $click_on_file_text = false;
        }
        $fileadmin = $this->get('fileadmin');
        $fileadmin->setBasePath('filedata/afisha_upload/');
        $fileadmin->setBaseWebPath($this->container->get('templating.helper.assets')->getUrl('filedata/afisha_upload/'));
        if ($type == 'popup') {
            $fileadmin->setFileCallback(Array($this, 'getFileSelectAttributes'));
        } else {
            $fileadmin->setFileCallback(Array($this, 'getFileAttributes'));
        }
        $result = $fileadmin->build();
        if ($fileadmin->isRedirect()) { 
            $redirect = $fileadmin->getRedirect(); 
            return $this->redirect($redirect);
        } else {
            return Array('fileadmin' => $result, 'parent' => $parent, 'click_on_file_text' => $click_on_file_text);
        }    
    }
    
    /**
     * ?????????? ???????? ???? a ??? ????? ? ?????? popup-?????????
     *
     * @access      public
     * @param       string
     * @param       object xbkFileadmin
     * @return      array
     */
    public function getFileSelectAttributes($dir, $file, $FileAdmin) {
        $request = $this->get('request');
        $link = $FileAdmin->getBaseWebPath().$dir.rawurlencode($file); 
        $attr = Array();
        $attr['href'] = 'javascript:fileadmin_select_file(\''.addslashes($link).'\')';
        return $attr;
    }    

}
