<?php
@session_start();
class FaleConoscoController extends Pampa_Controller_Action_Site {


  public function init() {
    parent::init();
    $this->view->menu = 'fale-conosco';
  }

  public function indexAction() {
    $this->forward('contato');
  }
  public function contatoAction() {
    
    $this->view->headTitle("Fale Conosco");
    $this->view->classmain = 'interna';
    $this->view->message = '';

    if($_SERVER['REQUEST_METHOD']=='POST'){

      
      $obj = $_POST['page'];
      $campos = '';
      if(empty($obj['nome'])){
        $campos = 'Nome';
      }else if(empty($obj['email'])){
        $campos = 'Email';
      }else if(empty($obj['mensagem'])){
        $campos = 'Mensagem';
      }
      if($campos==''){
        $msg = '';
        foreach ($obj as $key => $value) {
          $msg .= '<p>'.strtoupper($key).': '.$value.'</p>';
        }
        
        
        $serverurl='http://londigital.com.br/mailsender/ceped_ead_sender.php';
        $content = http_build_query($obj);

            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'POST',                    
                    'header' => //"Connection: close\r\n".
                                "Content-type: application/x-www-form-urlencoded\r\n".
                                "Content-Length: ".strlen($content)."\r\n",
                    'content' => $content                               
                )
            ));
            // Realize comunicação com o servidor
            $contents = file_get_contents($serverurl, null, $context);   
          


//             $ch = curl_init($serverurl);
// # Form data string
// $postString = http_build_query($obj, '', '&');
// # Setting our options
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// # Get the response
// $contents = curl_exec($ch);
// curl_close($ch);



          $contact = array(
            'email'=>$obj['email'],
            'nome'=>$obj['nome'],
            'msg'=>$msg,
          );
            // $send = Plugin_Mailer::sendContact($contact);
          if($contents){
          // if(mail($to, $subject, $message, $headers)){
            $this->view->message = '<div class="alert alert-success" role="alert">Mensagem enviada com sucesso!</div>';
            
          }else{

            $this->view->message = '<div class="alert alert-danger" role="alert">Erro ao enviar mensagem!</div>';
          }
      }else{
        $this->view->message = '<div class="alert alert-danger" role="alert">Preencha o campo '.$campos.'</div>';
      }
      
    }
    
    
   
  }

  
 
}

