<?php
$loader = $this->registry->get('load');
$loader->controller('common/upload');

class ControllerCommonRecordManager extends ControllerCommonUpload
{


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

    $this->setPathFile($this->ftrgeralregistry->getPathRecord());
    $this->setPathValidate('common/filemanager');

    $this->setAllowed(array_merge($this->allowedImg, $this->allowedRecord));

    $this->setAllowedExt(array_merge($this->allowedImgExt, $this->allowedRecordExt));

    $this->setFolder('arquivo');

    $this->setLinkFile($this->urlloja->link('', '', 'SSL', false) . 'arquivo/data/');

  }


  public function list()
  {
    $this->load->dmodel('tool/files');
    $this->load->dmodel('tool/image');

    if (isset($this->request->get['file'])) {

      $file = pathinfo($this->request->get['file']);
      if(in_array('.'.$file['extension'], $this->allowedRecordExt)){
        $this->response->setOutput($this->dmodel_tool_files->getThumb(html_entity_decode($this->request->get['file'], ENT_QUOTES, 'UTF-8')));
      }else{
        $this->response->setOutput($this->dmodel_tool_files->resize(html_entity_decode($this->request->get['file'], ENT_QUOTES, 'UTF-8'), 100, 100));
      }

    }
  }


  public function upload()
  {
    $this->language->load('common/filemanager');

    $json = array();
    $size = 1000000;
    $error_size = $this->language->get('error_file_size');
    if (isset($this->request->post['size'])) {
      $size = $this->request->post['size'];
      $error_size = $this->language->get('error_file_size_bigger');
    }

    if (isset($this->request->post['directory'])) {
      if (isset($this->request->files['file']) && $this->request->files['file']['tmp_name']) {
        $filename = basename(html_entity_decode(preg_replace(['/[^A-Za-z0-9 _ .-]/', '/ /i'], ['', '_'], $this->request->files['file']['name']), ENT_QUOTES, 'UTF-8'));

        if ((strlen($filename) < 3) || (strlen($filename) > 255)) {
          $json['error'] = $this->language->get('error_filename');
        }

        $directory = rtrim($this->getPathFile() . 'data/' . str_replace('../', '', $this->request->post['directory']), '/');

        if (!is_dir($directory)) {
          $json['error'] = $this->language->get('error_directory');
        }

        if ($this->request->files['file']['size'] > $size) {
          $json['error'] = $error_size;
        }

        if (!in_array($this->request->files['file']['type'], $this->getAllowed())) {
          $json['error'] = $this->language->get('error_file_type');
        }

        if (!in_array(strtolower(strrchr($filename, '.')), $this->getAllowedExt())) {
          $json['error'] = $this->language->get('error_file_type');
        }

        if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
          $json['error'] = 'error_upload_' . $this->request->files['file']['error'];
        }
      } else {
        $json['error'] = $this->language->get('error_file');
      }
    } else {
      $json['error'] = $this->language->get('error_directory');
    }

    if (!$this->user->hasPermission('modify', $this->getPathValidate())) {
      $json['error'] = $this->language->get('error_permission');
    }

    if (!isset($json['error'])) {
      if (@move_uploaded_file($this->request->files['file']['tmp_name'], $directory . '/' . $filename)) {
        $json['success'] = $this->language->get('text_uploaded');
      } else {
        $json['error'] = $this->language->get('error_uploaded');
      }
    }

    $this->response->setOutput(json_encode($json));
  }


}
