<?php
//========================[ Feedback class ]=======================
//
// 2003-feb-12, Ilya Nemihin (hide@address.com)
// License: free
//
// class Feedback make usecase 'Feedback':
// 1. showing form with fields for fill
// 2. when user press submit send that fields's values to specific email
// 3. show message 'Your info was sent'
//
//
// How to make it work:
// 1. set email address to which send information from web-form
// in function Feedback()
// 2. set fields to show in function initFields()
// 3. change/translate messages in function initTexts()
// [4]. if you change script's name (or directory), also change it
// in function Feedback()
//
//
// Description:
//
// there is one class Feedback, which use:
// - class Request - to take values from GET/POST
// - class EMailSender - to send email message
//
// class Feedback have two interfaces:
// a) IView - return html form
// b) IMakeFeedback - take values and send them to email
//
// and there is 2 scripts:
// a) feedbackViewer.php (use IView) - show form
// b) feedbackController.php (use IMakeFeedback) -
// perform taking from POST values,
// and send them to email, and make redirect to
// feedbackViewer.php with message 'ok'
//
class Feedback{
// names of php-scripts
var $scriptViewer; // are used in IView
var $scriptController; // are used in IMakeFeedback
// email where we send information
var $email; // are used in IMakeFeedback
// body to sending in email
var $body; // are used in IMakeFeedback
// fields for show
var $fields; // are used in both
// class Request
var $objRequest; // are used in both
// class EMailSender
var $objSender; // are used in IMakeFeedback
// texts in class (for translation)
var $texts;
//====[ constructor ]
function Feedback(){
$this->scriptViewer = 'feedbackViewer.php';
$this->scriptController = 'feedbackController.php';
// set your email address
$this->email = '';
$this->objRequest = new Request();
$this->objSender = new EMailSender();
$this->initSender();
$this->fields = array();
$this->initFields();
$this->texts = array();
$this->initTexts();
}
// private (Constructor)
function initFields(){
$this->fields[ "name" ] = array( "descr" => 'Name', "type" => 'input', "params" => array( "size" => 20 ), "value" => '');
$this->fields[ "email" ] = array( "descr" => 'E-mail', "type" => 'input', "params" => array( "size" => 20 ), "value" => '');
$this->fields[ "feedback" ] = array( "descr" => 'Note', "type" => 'textarea', "params" => array( "cols" => 25, "rows" => 5 ), "value" => '');
}
// private (Constructor)
function initTexts(){
$this->texts[ "submit" ] = 'Send'; // text on submit button
$this->texts[ "useCaseName" ] = 'Feedback'; // text over form ( are used in getTextHeader())
$this->texts[ "messageOk" ] = 'You information was sent. Thanks.'; // message after sending information
$this->texts[ "again" ] = 'Again'; // link to make else one feedback
$this->texts[ "bodyheader" ] = 'Feedback information:'; // header in incoming email-message
}
// private (Constructor)
function initSender(){
$this->objSender->setEMail( $this->email );
}
//====[ interface IView ]====
function getView(){
if ( $this->isModeForm() ){
return $this->getForm();
}
else{
return $this->getMessage();
}
}
//====[ end interface IView ]====
//====[ interface IMakeFeedback ]====
function makeFeedback(){
$this->initValuesFromRequest();
$this->makeBody();
$this->sendEMail();
$this->redirect();
}
//====[ end interface IMakeFeedback ]====
// private (IView)
function isModeForm(){
// see redirect()
$msg = $this->objRequest->getParam( 'resultOk' );
if ( $msg == '' ){
return true;
}
else{
return false;
}
}
// private (IView)
function getForm(){
$str = '';
$str .= $this->getTextHeader();
$str .= '<table align="center" cellpadding="10" cellspacing="1" bgcolor="000000"><tr><td bgcolor="#cccccc">';
$str .= '<table align="center" bgcolor="#cccccc" cellpadding="3" cellspacing="0" border="0">'."\n";
$str .= '<form method="post" action="'.$this->scriptController.'">'."\n";
foreach( $this->fields as $name => $field ){
$str .= '<tr>'."\n";
$str .= '<td align="right" valign="top">';
$str .= $field[ "descr" ].':';
$str .= '</td>';
$str .= '<td>';
$str .= $this->getElement( $field[ "type" ], $name, $field[ "params" ] );
$str .= '</td>';
$str .= '</tr>'."\n";
}
$str .= '<tr>'."\n";
$str .= '<td colspan="2" align="center"><hr noshade size="1"><input type="submit" value="'.$this->texts[ "submit" ].'"></td>';
$str .= '</tr>'."\n";
$str .= '</form>'."\n";
$str .= '</table>'."\n";
$str .= '</td></tr></table>';
return $str;
}
// private (IView)
function getMessage(){
$str = '';
$str .= $this->getTextHeader();
$str .= '<p align="center">';
$str .= $this->texts[ "messageOk" ];
$str .= '<br><a href="'.$this->scriptViewer.'">'.$this->texts[ "again" ].'</a>';
$str .= '</p>';
return $str;
}
// private (IView)
function getElement( $type, $name, $params ){
$str = '';
switch( $type ){
case 'input':
$str = '<input type="text" name="'.$name.'" size="'.$params[ "size" ].'">';
break;
case 'textarea':
$str = '<textarea name="'.$name.'" cols="'.$params[ "cols" ].'" rows="'.$params[ "rows" ].'"></textarea>';
break;
default:
$str = 'unknown element type';
}
return $str;
}
// private (IView)
function getTextHeader(){
$str = '';
$str .= '<center>';
$str .= '<h2>'.$this->texts[ "useCaseName" ].'</h2>';
$str .= '</center>';
return $str;
}
// private (IMakeFeedback)
function initValuesFromRequest(){
foreach( $this->fields as $name => $field){
$this->fields[ $name ][ value ] = $this->objRequest->getParam( $name );
}
}
// private (IMakeFeedback)
function makeBody(){
$this->body = $this->texts[ "bodyheader" ]."\n\n";
foreach( $this->fields as $name => $field ){
$this->body .= $field[descr].': ';
$this->body .= $field[value];
$this->body .= "\n";
}
}
// private (IMakeFeedback)
function sendEMail(){
$this->objSender->setBody( $this->body );
$this->objSender->setSubject( 'Feedback' );
$this->objSender->send();
}
// private (IMakeFeedback)
function redirect(){
$url = $this->scriptViewer;
$url .= '?resultOk=1';
header( 'Location: '.$url."\n\n" );
}
} // end class Feedback
//=============================================================
class Request{
function getParam( $name ){
global $HTTP_GET_VARS, $HTTP_POST_VARS;
if ( isset( $HTTP_GET_VARS[ $name ] ) ){
return $HTTP_GET_VARS[ $name ];
}
else if ( isset( $HTTP_POST_VARS[ $name ] ) ){
return $HTTP_POST_VARS[ $name ];
}
return '';
}
}
//=============================================================
class EMailSender{
var $body;
var $email;
var $mode;
function EMailSender(){
$this->body = '';
$this->email = '';
$this->mode = 'real';
$this->headers = '';
$this->subject = '';
}
function setMode( $mode ){
$this->mode = $mode;
}
function setEMail( $email ){
$this->email = $email;
}
function setSubject( $subject ){
$this->subject = $subject;
}
function setHeaders( $headers ){
$this->headers = $headers;
}
function setBody( $body ){
$this->body = $body;
}
function send(){
if ( $this->email == '' ){
echo 'Error. Cant send email - email address empty. Set email address in Feedback constructor.';
exit;
}
if ( $this->mode == 'test' ){
$str = '';
$str .= "Sender send:\n";
$str .= "to: {$this->email}\n";
$str .= "subject: {$this->subject}\n";
$str .= "body: {$this->body}\n";
$fp = fopen( 'sender.log', 'w' );
fputs( $fp, $str );
fclose( $fp );
}
else{ // real send to email
mail( $this->email, $this->subject, $this->body, $this->headers );
}
}
} // end class EMailSender
?>