<?php

/**
 * Prestataire actions.
 *
 * @package www.katao.fr
 * @subpackage Prestataire
 * @author Your name here
 * @version SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class PrestataireActions extends sfActions {
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex($request)
    {
        $this->initializeCatalyzVariables();
        $this->forward('default', 'module');
    }

    public function executeSubscribestep1($request)
    {
        $this->initializeCatalyzVariables();
    	$this->form = new PrestataireAdhesionForm;

    	$this->link_form_pdf = $this->generatePdfForm($this->form->getWidgetSchema() ,'formulairedetest.pdf' );

    	if($request->isMethod('post')){
    		$this->form->bind($request->getParameter('form_presta_subscription'));
    		if($this->form->isValid()){

    			// Génération du PDF
    			$pdf = new ScanReportPdf('Prestataire adhésion');
    			$pdf->setPrestaSubscriptionData($this->form->getValues());
    			$this->filename = 'prestataire_adhesion_'.md5(uniqid(rand(), true)).'.pdf';
    			$pdf->execute($this->filename);

    			$values = $this->form->getValues();
    			$mode  = $values['payment_mode'];

    			// Montant
    			$amount = $values['payment_amount'];
    			$hash_amount = array('0' => 100,
	    							'1' => 25,
	    							'2' => 25,
	    							'3' => 100,);

    			$hash_mode   = array('0' => 'Carte Bleue',
    								 '1' => 'Espèces',
    								 '2' => 'Chèque',
    								 '5' => 'Sols-Violette');

    			$amount = $hash_amount[$amount];

    			$this->getUser()->setAttribute('amount', $amount);

	//			var_dump($amount);
    			//die();

    			if($mode == KataoMemberDeposit::PAYMENT_MODE_OTHER){  // Si carte bleue choisie
    				$this->forward('Prestataire', 'paypalStep1');
    			} else {
    				$this->forward('Prestataire', 'thanks');
    			}
    		}
    	}
	}

	public function executeThanks($request){

	}

    /*public function thankyou($request)
    {
        $this->initializeCatalyzVariables();
    }*/

	public function executePaypalStep1($request){
		$this->initializeCatalyzVariables();

		$this->amount = $this->getUser()->getAttribute('amount');
		$this->label = "Adhesion Sol Violette";
	}

	public function executePaypalConfirmation($request){
        $this->initializeCatalyzVariables();

		//permet de traiter le retour ipn de paypal
/*		$email_account = "cbarra_1354609815_biz@waterproof.fr";
   $req = 'cmd=_notify-validate';

   foreach ($_REQUEST as $key => $value) {
   $value = urlencode(stripslashes($value));
   $req .= "&$key=$value";
   }

   $header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
   $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
   $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
   $fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
   $item_name = $_REQUEST['item_name'];
   $item_number = $_REQUEST['item_number'];
   $payment_status = $_REQUEST['payment_status'];
   $payment_amount = $_REQUEST['mc_gross'];
   $payment_currency = $_REQUEST['mc_currency'];
   $txn_id = $_POST['txn_id'];
   $receiver_email = $_REQUEST['receiver_email'];
   $payer_email = $_REQUEST['payer_email'];
   parse_str($_REQUEST['custom'],$custom);

   if (!$fp) {

   } else {
   fputs ($fp, $header . $req);
   while (!feof($fp)) {
   $res = fgets ($fp, 1024);
   if (strcmp ($res, "VERIFIED") == 0) {
   // vérifier que payment_status a la valeur Completed
   if ( $payment_status == "Completed") {
   if ( $email_account == $receiver_email) {
   /**
   * C'EST LA QUE TOUT SE PASSE
   * PS : tjrs penser à vérifier la somme !!
   * /
   file_put_contents(sfConfig::get('sf_web_dir') . '/uploads/paypal_' . time() . '.log', print_r($_REQUEST, true)); // Fichier log transaction

   /**
   * FIN CODE
   * /
   }
   }
   else {
   // Statut de paiement: Echec
   }
   exit();
   }
   else if (strcmp ($res, "INVALID") == 0) {
   // Transaction invalide
   }
   }
   fclose ($fp);
   }*/
	}

	public function executePaypalThanks($request){
        $this->initializeCatalyzVariables();
	}

	public function executePaypalCancel($request){
		$this->initializeCatalyzVariables();
	}

	public function generatePdfForm($schema, $filename){
		$pdf = new ScanReportPdf('Formulaire d\'adhésion prestataires');
		$pdf->generateForm($schema); // Test générateur de formulaires
		$pdf->execute($filename);

		return $filename;
	}

    protected function initializeCatalyzVariables()
    {
        $this->ContentTree = ContentTree::instance();
        $this->ContentTree->build();
        $this->ContentTreeNode = $this->ContentTree->getRoot();
        $this->node = $this->ContentTreeNode->getNode();
        $this->page = $this->ContentTreeNode->getPage();
    }
}