Download
This library requires at least version 5 of PHP.
Documentation
Our code assumes the presence of an autoloader like this:
function __autoload($class_name) {
$f = implode('/', explode('_', $class_name)).'.php';
if (include_file_exists($f)) {
require_once $f;
} else {
throw new Exception('for autoloading '.$class_name.' unable to find '.$f);
}
}
function include_file_exists($file, $is_file = false) {
foreach ( explode( PATH_SEPARATOR, get_include_path() ) as $path ) {
if (file_exists($path.'/'.$file) && is_readable($path.'/'.$file)) {
if (!$is_file ||
($is_file && is_file($path.'/'.$file))) {
return true;
} else {
return false;
}
}
}
return false;
}
Without the autoloader you may need to add some require_once() calls to this code.
CUSD/Form.php
- Require certain fields
- Labels for required fields are automatically marked as required
- Require field data to be in particular formats
- Maxlength
- Date
- Daterange
- Regexp
- Postalcode
- Integer
- Anything else you create a CUSD_FormRequirement subclass for...
- Enforce complex requirements involving multiple form fields
- Automatically populate error page with user's entered data
- Automatically highlight fields with errors
- Optionally obfuscate field 'name=""' values to discourage form spam
- Automatically require that select option be one of the options listed on the form
- getEmailText() function pulls field labels from the form itself and produces a well-formatted listing of the entered data.
- Not yet well documented
You will probably need to create your own subclass of CUSD_Form that understands the layout/formatting of forms on your site. CUSD_StandardForm understand the form conventions on www.hotelschool.cornell.edu
<?php
require_once('CUSD/Form.php');
ob_start();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<fieldset class="row1">
<legend>Your Information</legend>
<div class="form-pair">
<div class="form-item">
<label for="name">Name</label>
</div>
<div class="form-value">
<input type="text" name="name" id="name" size="38" cusd_valid="req" />
</div>
</div>
</fieldset>
<fieldset class="row1">
<div class="form-pair">
<div class="form-item">
<label for="zip">Zip Code</label>
</div>
<div class="form-value">
<input type="text" name="zip" id="zip" size="15" cusd_valid="postalcode" />
</div>
</div>
</fieldset>
<fieldset class="row2">
<div class="form-pair">
<div class="form-item">
<label for="email">Email</label>
</div>
<div class="form-value">
<input type="text" name="email" id="email" size="38" cusd_valid="req|email" />
</div>
</div>
</fieldset>
<fieldset class="row1">
<div class="form-pair">
<div class="form-item">
<label>Preferred Address?</label>
</div>
<div class="form-value">
<input type="radio" name="defaultaddress" id="dah" value="Use Home Address" checked="checked" />
<label for="dah">Use my home address</label>
</div>
<div class="form-value">
<input type="radio" name="defaultaddress" id="daw" value="Use Work Address" />
<label for="daw">Use my work address</label>
</div>
</div>
</fieldset>
<fieldset class="row2">
<div class="form-submit">
<input type="submit" class="input-submit" name="submit" value="Send My Update" />
</div>
</fieldset>
</form>
<?php
$form = new CUSD_StandardForm(ob_get_clean());
if (isset($_REQUEST['submit']) && $formData = $form->validate()) {
$docTitle = 'Updated Address Information Submitted';
$mailer = new SHA_Mailer;
$mailer->to('foo@sha.cornell.edu');
$mailer->subject('Update Alumni Address change (www)');
$body = $form->getEmailText();
$mailer->setBody($body);
$mailer->send();
?>
<p>Thank you for sending us your updated information. An Email has been sent with the information you have provided to the Cornell Hotel Society (CHS). The CHS will use your information to update your records.</p>
<?php
} else {
?>
<p>As a dues paid CHS member, you can now browse alumni listings online. If you move, change jobs, etc, please update your information with us to ensure your records are current.</p>
<p>Required fields denoted with (*)</p>
<div class="section">
<?php
$form->show();
?>
</div>
<?php
}
?>
CUSD/Search.php
Wrap Google Search Appliance results in your site's template. Some documentation is on the Confluence site.
CUSD/Util.php
- bytesToHuman($size) - convert 1048576 to 1MB
- findAndLinkURLs($text)
- mod() mathematically correct modulo where result is always positive
- resizeImage()
The source is documented using the PHPDocumentor style.