<?php
require_once "DB.php"; // PEAR's DB
require_once "HTML/QuickForm.php"; 
require_once "HTML/QuickForm/Renderer/ArraySmarty.php";
require_once "smarty/Smarty.class.php";
//require_once "smarty-light/class.template.php";

// get db connection using dsn style connection 
$dsn = 'mysql://root:@localhost/db_test';
$db = & DB::connect($dsn);
if (DB::isError($db)){
  die($db->getMessage());
}

// instantiate smarty template object
// defining Smarty template and compile dir in same 
// directory for simplicity
$tpl =& new Smarty;
//$tpl =& new template;
$tpl->template_dir = '.';
$tpl->compile_dir  = '/tmp';

// enables popup that displays Smarty variables 
// and other items of potential interest
$tpl->debugging = false;

// building 2D arrays for conferences and prefix 
// select element options 
$aConference = $db->GetAssoc("SELECT id, CONCAT(title, " . 
  "' (', DATE_FORMAT(date, '%M %d, %Y'), ', $', " . 
  "cost, ')') FROM tbl_conference WHERE date > NOW()");
$aPrefix = array('Mr.' => 'Mr.', 'Ms.' => 'Ms.', 'Mrs.' => 'Mrs.', 'Sir' => 'Sir');

// instantiate form object and add form elements
$form = new HTML_QuickForm('frmReg', 'POST', $_SERVER['REQUEST_URI']);
$form->addElement('header', 'regHeader', 'Register today for our upcoming conferences!');
$form->addElement('select', 'conferenceid', 'Select conference: ', $aConference);
$form->addElement('select', 'prefix', 'Prefix: ', $aPrefix);
$form->addElement('text', 'firstname', 'First name: ');
$form->addElement('text', 'lastname', 'Last name: ');
$form->addElement('text', 'address1', 'Address: ');
$form->addElement('text', 'address2', 'Address 2: ');

// put city state zip together on one line
$cityStateZip[] = & HTML_QuickForm::createElement('text', 'city', '', 'size=15');
$cityStateZip[] = & HTML_QuickForm::createElement('text', 'state', 'State/Province', 'size=3 maxlength=3');
$cityStateZip[] = & HTML_QuickForm::createElement('text', 'zip', 'Zip/Postal', 'size=6 maxlength=10');
$form->addGroup($cityStateZip, 'cityStateZip', 'City, State, Zip: ', '&nbsp;');

// remaining form elements
$form->addElement('text', 'phone', 'Telephone: ');
$form->addElement('text', 'email', 'Email: ');
$form->addElement('submit', 'btnSubmit', 'Submit');


// register custom validation functions
$form->registerRule('checkCityStateZip', 'callback', 'vCityStateZip');
$form->registerRule('checkPhone', 'regex', '/\d{3}\D*\d{3}\D*\d{4}\D*\d*$/');
// add validation rules
$form->addRule('cityStateZip', 'City, State and Zip are required', 'checkCityStateZip');
$form->addRule('phone', 'Please reformat your phone number (eg 212 867-5309).', 'checkPhone');
$form->addRule('firstname', 'First name is required.', 'required');
$form->addRule('lastname', 'Last name is required.', 'required');
$form->addRule('address1', 'An address is required.', 'required');
$form->addRule('email', 'Email address is required.', 'required');
$form->addRule('email', 'Email address must be valid.', 'email');
$form->addRule('cityStateZip', 'City, State and Zip are required.', 'required');


// instantiates a new renderer object for Smarty 
//$renderer = &new HTML_QuickForm_Renderer_ArraySmarty($tpl);
$renderer = &new HTML_QuickForm_Renderer_SmartyDynamic($tpl);

// define presentation for required values and error values
$renderer->setRequiredTemplate(
      '{if $error}<span class="requiredErrLabel">
         {$label}</span>
       {else}
         {if $required}
           <span class="requiredAsterik">*</span>&nbsp;
         {/if}
         {$label} 
       {/if}');
$renderer->setErrorTemplate(
      '{$html} {if $error}
                 <span class="errorMsg">{$error}</span>
               {/if}');

if ($form->validate()) {
  $form->process('addRegistration');
  // getting firstname element and assigning 
  // a variable for the Smarty template
  $firstname = $form->getElement('firstname');
  $tpl->assign('msg', 'Thank you, ' . $firstname->getValue() . '!');
  $tpl->display('thankyou.tpl');
} else { // form doesn't pass validation
  $form->accept($renderer);
  $tpl->assign('form_data', $renderer->toArray());
  $tpl->display('regform.tpl');
}

function vCityStateZip($data) {
  // custom validation function to ensure values for 
  // city state and zip were submitted
  if (strlen($data['city']) > 0 && 
    strlen($data['state']) >= 2 && 
    strlen($data['zip']) >= 5) {
    return true;
  } 
  return false;
}

function addRegistration($data) {
  global $db;

  // build sql statement and run query to add registration
  $sql = 'INSERT INTO tbl_registration ' . 
    '(registrationdate, conferenceid, prefix, ' . 
    'firstname, lastname, address1, address2, city, ' . 
    'state, zip, phone, email) VALUES (NOW(), ' . 
    $db->quoteSmart($data['conferenceid']) . ', ' . 
    $db->quoteSmart($data['prefix']) . ', ' . 
    $db->quoteSmart($data['firstname']) . ', ' . 
    $db->quoteSmart($data['lastname']) . ', ' . 
    $db->quoteSmart($data['address1']) . ', ' . 
    $db->quoteSmart($data['address2']) . ', ' . 
    $db->quoteSmart($data['cityStateZip']['city']) . ', ' .
    $db->quoteSmart($data['cityStateZip']['state']) . ', ' .
    $db->quoteSmart($data['cityStateZip']['zip']) . ', ' . 
    $db->quoteSmart($data['phone']) . ', ' . 
    $db->quoteSmart($data['email']) . ')';
  $db->Query($sql);
}

?>
