<?php

class ControllerCommonFileManager extends Controller
{

    private $files;
    private $typefile;

    public function __construct($registry)
    {
        parent::__construct($registry);

        if (isset($this->request->get['typefile']) && !is_null($this->request->get['typefile'])) {
            $this->typefile = $this->request->get['typefile'];
        } else {
            $this->typefile = Type_UploadFile::Tp_Image;
        }

        $this->load->controller('common/' . Type_UploadFile::getNickFromValue($this->typefile));
        switch ($this->typefile) {
            case Type_UploadFile::Tp_Record:
                $this->files = new ControllerCommonRecordManager($this->registry);
                break;
            default:
                $this->files = new ControllerCommonImageManager($this->registry);
        }
    }

    public function index()
    {
        $this->load->type('tipocomum');

        $language = $this->language->load('common/filemanager');
		$this->data = array_merge($this->data, $language);

        $this->data['languageForJs'] = array(
            'common/filemanager' => $language,
        );

        $this->data['title'] = $this->language->get('heading_title');

        $this->data['base'] = $this->url->link('', '', 'SSL', false);

        $this->data['Type_ImageUpload'] = Type_ImageUpload::getTypeForJs();

        $token = $this->session->data['token'];
        $this->data['token'] = $token;
        $this->data['size'] = (isset($this->request->get['size'])) ? $this->request->get['size'] : 0;

        $this->data['directory'] = $this->urlloja->link('', '', 'SSL', false) . $this->files->getLinkFile();

        $this->load->dmodel('tool/image');

        $this->data['no_image'] = $this->dmodel_tool_image->resize('no_image.jpg', 100, 100);

        if (isset($this->request->get['field'])) {
            $this->data['field'] = $this->request->get['field'];
        } else {
            $this->data['field'] = '';
        }

        if (isset($this->request->get['type'])) {
            $this->data['type'] = $this->request->get['type'];
        } else {
            $this->data['type'] = Type_ImageUpload::Tp_Default;
        }

        $this->data['url_link'] = [
            'list' => $this->url->link('common/filemanager/list', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'directory' => $this->url->link('common/filemanager/directory', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'files' => $this->url->link('common/filemanager/files', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'create' => $this->url->link('common/filemanager/create', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'delete' => $this->url->link('common/filemanager/delete', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'deleteFiles' => $this->url->link('common/filemanager/deleteFiles', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'folders' => $this->url->link('common/filemanager/folders', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'move' => $this->url->link('common/filemanager/move', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'copy' => $this->url->link('common/filemanager/copy', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'rename' => $this->url->link('common/filemanager/rename', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'upload' => $this->url->link('common/filemanager/upload', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'sendFiles' => $this->url->link('common/filemanager/sendFiles', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'loadFolders' => $this->url->link('common/filemanager/loadFolders', 'token=' . $token . '&typefile=' . $this->typefile, 'SSL'),
            'no_image' => $this->dmodel_tool_image->resize('no_image.jpg', 100, 100),
            'folder' => $this->files->getFolder(),
        ];


        $this->data['size'] = (isset($this->request->get['size'])) ? $this->request->get['size'] : 0;

        $this->template = 'common/filemanager.twig';

        $this->response->setOutput($this->render());
    }

    function list()
    {
        try {
            $this->files->list();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function directory()
    {
        try {
            $this->files->directory();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function files()
    {
        try {
            $this->files->files();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function create()
    {
        try {
            return $this->files->create();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function deleteFiles()
    {
        try {
            return $this->files->deleteFiles();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function sendFiles()
    {
        try {
            return $this->files->sendFiles();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function loadFolders()
    {
        try {
            return $this->files->loadFolders();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }


    public function delete()
    {
        try {
            return $this->files->delete();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function move()
    {
        try {
            return $this->files->move();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function copy()
    {
        try {
            return $this->files->copy();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function recursiveCopy($source, $destination)
    {
        $directory = opendir($source);

        @mkdir($destination);

        while (false !== ($file = readdir($directory))) {
            if (($file != '.') && ($file != '..')) {
                if (is_dir($source . '/' . $file)) {
                    $this->recursiveCopy($source . '/' . $file, $destination . '/' . $file);
                } else {
                    copy($source . '/' . $file, $destination . '/' . $file);
                }
            }
        }

        closedir($directory);
    }

    public function folders()
    {
        $this->response->setOutput($this->recursiveFolders(DIR_IMAGE . 'data/'));
    }

    protected function recursiveFolders($directory)
    {
        $output = '';

        $output .= '<option value="' . utf8_substr($directory, strlen(DIR_IMAGE . 'data/')) . '">' . utf8_substr($directory, strlen(DIR_IMAGE . 'data/')) . '</option>';

        $directories = glob(rtrim(str_replace('../', '', $directory), '/') . '/*', GLOB_ONLYDIR);

        foreach ($directories as $directory) {
            $output .= $this->recursiveFolders($directory);
        }

        return $output;
    }

    public function rename()
    {
        try {
            return $this->files->rename();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function upload()
    {
        try {
            return $this->files->upload();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
}
