<?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'];
        }

        $_FILES['uploadFormElement']['name'] = $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;

            //Verificar que archivo existe - 20161115
            if (file_exists($Contacts_uploads . '/' . $file_name)) {
                if (fopen($Contacts_uploads . '/' . $file_name, "r")) {

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

                    // The PDF source is in original.pdf
                    readfile($Contacts_uploads . '/' . $file_name);


                    $this->status = 0;
                    $this->list_data = array();
                    $this->logMessage(">>>> Testing File: " . $_FILES['uploadFormElement']['name']);
                }
            } else {

                $mensajeError = "El archivo no esta disponible, verificar nombre(" . $file_name . ") y ruta(" . $Contacts_uploads . ").";

                $this->status = 1;
                $this->list_data = array();
                $this->mensajeError = $mensajeError;
                $this->logMessage(">>>> Testing File: " . $_FILES['uploadFormElement']['name'] . $mensajeError);

                echo $mensajeError;
                die();
            }
        } catch (Exception $e) {
            $this->status = 1;
            $this->mensajeError = "ERROR!!!";
            $this->logMessage("ERROR!!!");
            $this->logMessage($e->getMessage());
        }
    }

}
