<?php

/**
 * file actions.
 *
 * @package    regalias
 * @subpackage file
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fileActions extends sfActions {

    /**
     * Executes index action
     * Para crear directorio y archivo
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
            if ($value == 'null') {
                unset($request[$key]);
            }
        }
        $this->logMessage("--------------------------------------------------*  Ip: " . $_SERVER['REMOTE_ADDR']);
        foreach ($request->getGetParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }


        $file_name = '';
        if (isset($request['file_name'])) {
            $file_name = $request['file_name'];
        }


        $funcionalidad = $request['funcionalidad'];

        $this->logMessage("---END Logging request");



        try {

            foreach ($request->getFiles() as $fileName) {
                $this->logMessage(">>>> Found files");
                $fileSize = $fileName['size'];
                $fileType = $fileName['type'];
                $theFileName = $fileName['name'];
                $uploadDir = sfConfig::get("sf_upload_dir");
                $this->logMessage($uploadDir);
                $Contacts_uploads = $uploadDir . '/' . $funcionalidad;
                //Creación del directorio si no existe
                if (!is_dir($Contacts_uploads))
                    mkdir($Contacts_uploads, 0777);
                if ($file_name != '') {
                    move_uploaded_file($fileName['tmp_name'], $Contacts_uploads . '/' . $file_name);
                } else {
                    move_uploaded_file($fileName['tmp_name'], $Contacts_uploads . '/' . $theFileName);
                }
            }

//            foreach ($_FILES as $key->$file) {
//                $this->logMessage(">>>> Found file: " . $key);
//            }

            $this->logMessage(">>>> Testing File: " . $_FILES['uploadFormElement']['name']);
            $this->status = 0;
        } catch (Exception $e) {
            $this->status = 1;
            $this->mensajeError = "ERROR!!!";
            $this->logMessage("ERROR!!!");
            $this->logMessage($e->getMessage());
        }
    }

    /**
     * Carga el archivo adjunto
     * 
     * @param sfWebRequest $request
     */
    public function executeList(sfWebRequest $request) {

        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
            if ($value == 'null') {
                unset($request[$key]);
            }
        }
        $this->logMessage("--------------------------------------------------*  Ip: " . $_SERVER['REMOTE_ADDR']);
        foreach ($request->getGetParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }


        $file_name = 'libaux ejemplo 1.pdf';
        if (isset($request['file_name'])) {
            $file_name = $request['file_name'];
        }


        $funcionalidad = $request['funcionalidad'];
        //$funcionalidad = 'documentos';

        $this->logMessage("---END Logging request");


        try {
            $this->logMessage(">>>> Found files");
            $uploadDir = sfConfig::get("sf_upload_dir");
            $this->logMessage($uploadDir);
            $Contacts_uploads = $uploadDir . '/' . $funcionalidad;

            $nombre_fichero = $Contacts_uploads . '/' . $file_name;

            /**
             * Verificar que el archivo existe - 20190219
             */
            if (file_exists($nombre_fichero)) {
                $this->logMessage("El fichero $nombre_fichero existe");

                if (fopen($nombre_fichero, "r")) {

                    //header('Content-type: application/pdf');
                    // It will be called downloaded.pdf
                    header('Content-Disposition: attachment; filename="' . $file_name . '"');

                    // The PDF source is in original.pdf
                    readfile($nombre_fichero);
                } else {
                    echo 'El archivo no esta disponible.';
                }
            } else {
                $this->logMessage("El fichero $nombre_fichero no existe, favor verificar.");
                echo "El fichero $file_name no existe, favor verificar.";
                die();
            }

            $this->logMessage(">>>> Testing File: " . $_FILES['uploadFormElement']['name']);
            $this->status = 0;
        } catch (Exception $e) {
            $this->status = 1;
            $this->mensajeError = "ERROR!!!";
            $this->logMessage("ERROR!!!");
            $this->logMessage($e->getMessage());
        }
    }

}
