<?php
class myForm{
public $fields = array('surName', 'firstName', 'id');
public function __construct(){
$this->url = $_SERVER['PHP_SELF'];
$this->primary = 'id';
$this->tableName = 'mytable';
}
public function load($data){
if (is_object($array)){
$array = get_object_vars($array);
}
foreach($this->fields as $field){
if (isset($array[$field])){
$this->$field = $array[$field];
}
}
}
public function send($to){
require_once('../class.phpmailer.php');
$mail = new PHPMailer();
$this->form->freeze();
$body = <<<HTML
A new form was submitted today. <br/>
details of the form were: <br/>
HTML
. $this->form->toHTML();
$mail->AddReplyTo($this->from, $this->fromName);
$mail->SetFrom($this->fromEmail, $this->fromName);
$mail->AddAddress($to, '');
$mail->Subject = "Just received a new form submission";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
public function enquote($data){
if(is_array($data)){
array_walk(array($this, 'enquote'), $data);
}
return "'" . mysql_real_escape_string($data). "'";
}
public function loadFromID($id){
$sql = "select * from $this->tableName where $this->primary=%d";
$result = mysql_query(sprintf($sql, $id));
if($result){
$this->load(mysql_fetch_assoc($result));
}
}
public function constructForm(){
$form = new aacForm('client', 'post', '', '', '', TRUE);
$form->setAttribute('action', $this->url); //we need to do this otherwise the forms class will strip the url before the query string
$form->addElement('header','','Fill in your details');
$form->addElement('text', 'surName', 'SurName:', array('class'=>'regular-text'));
$form->addElement('text','firstName', 'First Name:', array('class'=>'regular-text'));
$form->addElement('hidden', $this->primary);
$form->addElement('hidden', 'action');
$form->addRule('surName', 'You must provide a surname', 'required', '', 'client' );
$form->addRule('firstName', 'You must provide a first name', 'required', '', 'client' );
$this->form = $form;
$this->aacAction = 'saveFormData';
$this->form->setDefaults(get_object_vars($this)); //load info
}
public function saveNew(){
$args = $this->compact();
$args = $this->enquote($args);
$fields = implode (',', array_keys($args));
$values = implode (',', array_values($args));
$sql = "insert into $this-tableName ($fields) values ($values)";
$r = mysql_query($sql);
if ($r) {
$this->{$this->primary} = $wpdb->insert_id;
return true;
} else {
die(mysql_error());
}
}
public function compact(){
foreach($this->fields as $val){
if (is_array($this->$val)){
$array[$val] = serialize($this->$val);
} else {
$array[$val]=$this->$val;
}
}
return $array;
}
/**
* private helper method to update an existing record
* @return
*/
public function update(){
$args= $this->compact();
update($args[$this->primary]);
$set = array();
foreach ($args as $f->$a){
$set [] = "$f=".$this->enquote($a);
}
$values = implode (',', $set) ;
$sql = "Update $this->tableName set $values where {$this->primary} = {$this->{$this->primary}}";
$r = @mysql_query($sql);
return ($r > 0) ? true : false;
}
public function save(){
if (empty($this->{$this->primary})){
$this->saveNew();
} else{
$this->update();
}
}
public function validates(){
if (empty($this->form)){
$this->constructForm();
}
return $this->form->validate();
}
public function showForm(){
if (empty($this->form)){
$this->constructForm();
}
$this->form->display();
}
}
$action = empty($_REQUEST['action']) ? '' : $_REQUEST['action'];
switch ($action){
case 'saveFormData':
$i = new myForm();
if (isset($_REQUEST[$i->primary])){
$i->loadfromid($_REQUEST[$i->primary]);
}
if ($i->validates){
$i->save();
$i->send();
} else {
$i->showForm();
}
break;
default:
$i = new myForm();
$i->showForm();
}
?>