<?php

/**
 * co_plantilla_contrato actions.
 *
 * @package    contratacion
 * @subpackage co_plantilla_contrato
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
require_once 'HTMLPurifier.auto.php';
require 'HTMLPurifier.autoload.php';

class co_plantilla_contratoActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executePdf(sfWebRequest $request) {

        //Revisar que sea post
        //$this->forward404Unless($request->isMethod('post'));
        //Guardar los parametros del request en el log
        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("--------------------------------------------------* Ip: " . $_SERVER['REMOTE_ADDR']);
        foreach ($request->getGetParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("---END Logging request");


        try {

            $config = HTMLPurifier_Config::createDefault();
            $purifier = new HTMLPurifier($config);

            $pdf = new CDPDF('P', 'mm', 'letter');

            $pdf->SetFont('Times', 'B', 10);
            $pdf->SetFillColor(255);
            $pdf->SetTextColor(0);
            $pdf->setTipo(6);

            /*
             * Filtro
             */
            $id_plantilla_contrato = $request['id_plantilla_contrato'];
            $id_contratista = $request['id_contratista'];


            $consul = Doctrine_Manager::getInstance()->connection();

            //Sql
            $sql = <<<EOD
select 
id_plantilla_contrato,
descripcion,
version,
detalle
from co_plantilla_contrato
where 
id_plantilla_contrato = $id_plantilla_contrato
EOD;

            $st = $consul->execute($sql);
            $datos = $st->fetchAll();

            $detalle = $datos[0]['DETALLE'];
            //echo 'count::' . count($datos);

            $html = '<table border="0" width="100%">';
            $html .='<tr>';
            $html .='<td width="25%" align="center" valign="bottom"><img src="images/LogoEmpresa2.png" alt="" width="80px" height="83px" /></td>';
            $html .='<td width="40%"><br /><br /></td>';
            $html .='<td width="35%" align="center" valign="bottom"><br/><img src="images/prosperidad.png" alt="" width="130px" height="60px" /></td>';
            $html .='</tr>';
            $html .='</table>';

            //$html.='<br/>';
            //Limpiar html del registro
            $detalle = $this->htmlClear($purifier->purify($detalle));

            
            /**
             * Reemplazo de variables
             */
            //Sql
            $sql = <<<EOD
select 
nvl((select upper(cont.nombre_razon) as nombre_razon from co_contratista cont where cont.id_contratista = $id_contratista),'N/A') as nombre_contratista
from dual  
EOD;
            //Ejecutar SQL
            $record_dual = CN_TABLE::executeSQL($sql);

            foreach ($record_dual as $key_dual => $value_dual) {
                foreach ($value_dual as $key => $value) {
                    $this->logMessage("---$key---" . $value);
                    $detalle = str_replace('$' . $key, $value, $detalle);
                }
            }

            $html .= $detalle;

            //$html = '<p>CONTRATO DE PRESTACI&Oacute;N DE SERVICIOS PROFESIONALES &nbsp;No. ____________ DE 2017 SUSCRITO ENTRE LA AGENCIA NACIONAL DE MINERIA Y JOHANA LUCIA TORRES PARRA</p>';
            //echo $html;
            //die();

            /**
             * Reemplazo de variables
             */
            $pdf->setTitulo("PLANTILLA CONTRATOS");

            $pdf->AddPage();

            $pdf->SetFont('helvetica', '', 10);

            $pdf->writeHTML($html, true, false, true, false, '');

            $pdf->Output("co_plantilla_contrato.pdf", "D");
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    /**
     *
     * Metodo que reemplaza las etiquetas html para modificar el formato de tamaño y alineación de los textos
     */
    private function htmlClear($string) {




        $string = str_replace('<p>', '<p align="justify">', $string);
        $string = str_replace('<br>', '<br/>', $string);
        $string = str_replace('class=', '', $string);
        $string = str_replace('<img', '', $string);
        $string = str_replace('<td>X</td>', '<td align="center">X</td>', $string);
        $string = str_replace('<td rowspan="2">', '<td rowspan="2" align="justify">', $string);

        $string = str_replace('<font size="2">', '<font size="10">', $string);
        $string = str_replace('<font size="3">', '<font size="12">', $string);
        $string = str_replace('<font size="4">', '<font size="14">', $string);
        $string = str_replace('<font size="5">', '<font size="18">', $string);
        $string = str_replace('<font size="6">', '<font size="24">', $string);
        $string = str_replace('<font size="7">', '<font size="36">', $string);
        $string = str_replace('<table ', '<center><table ', $string);
        $string = str_replace('</table> ', '</table></center>', $string);
        $string = str_replace('<table ', '<table border="1"', $string);

        $string = str_replace('size="1"', 'size="10"', $string);
        $string = str_replace('size="2"', 'size="10"', $string);
        $string = str_replace('size="3"', 'size="12"', $string);
        $string = str_replace('size="4"', 'size="14"', $string);
        $string = str_replace('size="5"', 'size="18"', $string);
        $string = str_replace('size="6"', 'size="24"', $string);
        $string = str_replace('size="7"', 'size="36"', $string);

        $string = str_replace('border="0"', 'border="1"', $string);


        $string = str_replace('<div>', '<div align="justify">', $string);
        $string = str_replace('&amp;amp;amp;amp;amp;nbsp;', ' ', $string);
        $string = str_replace('&amp;amp;amp;amp;nbsp;', ' ', $string);
        $string = str_replace('&amp;amp;amp;nbsp;', ' ', $string);
        $string = str_replace('&amp;amp;nbsp;', '', $string);
        $string = str_replace('&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;nbsp;', '', $string);
        $string = str_replace('amp;', '', $string);

        $string = str_replace('<td colspan="2">', '<td colspan="2" align="justify">', $string);
        $string = str_replace('<td rowspan="3">', '<td rowspan="3" align="justify">', $string);
        $string = str_replace('<td colspan="3">', '<td colspan="3" align="justify">', $string);
        $string = str_replace('<td rowspan="4">', '<td rowspan="4" align="justify">', $string);
        $string = str_replace('<td colspan="4">', '<td colspan="4" align="justify">', $string);
        $string = str_replace('  ', ' ', $string);
        $string = str_replace('   ', ' ', $string);
        $string = str_replace('    ', ' ', $string);
        $string = str_replace('	    ', ' ', $string);
        $string = str_replace('		    ', ' ', $string);
        $string = str_replace('valign', 'align', $string);
        $string = str_replace('width=', '', $string);
        $string = str_replace('bordercolor=', '', $string);
        $string = str_replace('<td>', '<td align="justify">', $string);
        $string = str_replace('<li>', '<li align="justify">', $string);
        $string = str_replace('style=', '', $string);

        return trim($string);
    }

}
