<?php
/* *****************
 * Model
 ******************/
class ContactPage extends ContenidoInteriorWebBtMalaga
{
	static $db = array(
		'Mailto' => 'Varchar(100)', //Email address to send submissions to
		'SubmitText' => 'HTMLText' //Text presented after submitting message
	);
	
	//CMS fields
	function getCMSFields() {
		$fields = parent::getCMSFields();
	
		$fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));	
		$fields->addFieldToTab("Root.Content.OnSubmission", new HTMLEditorField('SubmitText', 'Text on Submission'));	
	
		return $fields;	
	}

}
/* *****************
 *  Controller
 ******************/
class ContactPage_Controller extends ContenidoInteriorWebBtMalaga_Controller
{
	function ContactForm() {
      	// Create fields
		$Params = Director::urlParams();
		  
	    $fields = new FieldSet(
		    new TextField('Name', 'Nombre*'),
			new EmailField('Email', 'Email*'),
			new TextareaField('Comments','Asunto*')
		);
	 	
	    // Create action
	    $actions = new FieldSet(
	    	new FormAction('SendContactForm', 'Enviar')
	    );
		// Create action
		$validator = new RequiredFields('Name', 'Email', 'Comments');
	
			
	    return new Form($this, 'ContactForm', $fields, $actions, $validator);
	}
 
	function SendContactForm($data) {
      
	 	//Set data
		$From = $data['Email'];
		$To = $this->Mailto;
		$Subject = "Contacto desde la web de Malaga Comun";  	  
		$email = new Email($From, $To, $Subject, $data['Comments']);
		//set template
		//$email->setTemplate('ContactEmail');
		//populate template
		$email->populateTemplate($data);
		//send mail
		$email->send();
	  	//return to submitted message
		Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");

	}

	public function Success()
	{
		return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
	}
}