<?php
/**
 *  twitter
 * 
 *  @author freshface
 */

// private register this widget for framework 
fEnv::addWidget('ffContact');

class ffContact extends frameworkWidget {
    /**
     *  Defining the widget options
     */

     protected $_widgetAdminTitle =       "Contact Form - Custom Widget";
     protected $_widgetAdminDescription = "Working Contact Form";
     protected $_widgetWrapperClasses =   "";
     protected $_widgetFormSize =         frameworkWidget::WIDGET_FORM_SIZE_THIN;



     protected $options = array(
        array(  'id' => 'title',
                'description' => 'Title',
                'type' => 'text',
                'default' => 'Contact Form Widget'),
                
     	array(  'description' => 'Translation',
     			'type' => 'header', ),
     		
     	array(  'id' => 'trans_name',
     			'description' => 'Name*',
				'type' => 'text',
     			'default' => 'Name*'),

		array(  'id' => 'trans_email',
     			'description' => 'Email*',
     			'type' => 'text',
     			'default' => 'Email*'),

		array(  'id' => 'trans_button_send',
     			'description' => 'Send button',
     			'type' => 'text',
     			'default' => 'Send E-Mail'),     		

		array(  'id' => 'trans_message_ok',
     			'description' => 'Message Sent',
     			'type' => 'text',
				'default' => 'Message has been sent'),

		array(  'id' => 'trans_message_wrong',
     			'description' => 'Message wasnt sent',
     			'type' => 'text',
				'default' => 'Message was not sent, please try again later'),     		
     		
		array(  'description' => 'Settings',
     			'type' => 'header', ),
     		
        array(  'id' => 'email',
                'description' => 'Your email where you wish to recive messages',
                'type' => 'text',
                'default' => ''),
                

        array(  'id' => 'subject',
                'description' => 'Subject of emails that you receive',
                'type' => 'text',
                'default' => ''),

     );

    /**
     * Printing widget, called by wordpress
     */
    function _widget($args, $instance) {

        extract( $args );
        echo $before_widget;
        if( !empty( $instance['title'] ) ) {
            $instance['title'] = apply_filters('widget_title', $instance['title'] );
        }
		echo $before_title . $instance['title'] . $after_title;
		
		if( isset( $_POST['fc_send_email'] ) ) {
			$this->processEmail( $instance );
		} else {
			$this->printForm( $instance );
		}

        echo $after_widget;        
    }
    
    private function printForm( $instance ) {
    	?>
		<form action="" id="widget_contact" method="post">
    		<p><input type="text" name="fc_name" id="fc_name" /><label for="fc_name"><?php echo $instance['trans_name']; ?></label></p>
    		<p><input type="text" name="fc_email" id="fc_email" /><label for="fc_email"><?php echo $instance['trans_email']; ?></label></p>
    		<p><textarea cols="10" id="fc_text" name="fc_text" rows="10"></textarea></p>
    		<p style="display:none;"><input type="hidden" name="fc_send_email" value= "1" /></p>
    		<p style="display:none;"><input type="hidden" name="fc_user_email" value="<?php echo $instance['email']; ?>" /></p>
    		<p style="display:none;"><input type="hidden" name="fc_user_subject" value="<?php echo $instance['subject']; ?>" /></p>
    		<p><input type="submit" value="<?php echo $instance['trans_button_send']; ?>" tabindex="5" id="fc_submit" class="btn_b" name="fc_submit" /></p>
    	</form>
	<?php 
    	
    }
    
    private function processEmail( $instance ) {
		$sender_name = $_POST['fc_name'];
		$sender_email = $_POST['fc_email'];
		$sender_message = $_POST['fc_text'];
		
		$user_email = $_POST['fc_user_email'];
		$user_subject = $_POST['fc_user_subject'];
		
		$message = '';
		$message .= 'Name:' . $sender_name . "\n";
		$message .= 'Content:' ."\n\n" . $sender_message;

		
		$headers = 'From: '.$sender_email . "\r\n" .
				'Reply-To: '.$sender_email . "\r\n";
		
		$result = mail($user_email, $user_subject, $message, $headers);
		if( $result == true ) {
			echo $instance['trans_message_ok'];
		} else {
			echo $instance['trans_message_wrong'];
		}
	}
    
}
?>
