Simple email module showcases many PHOCOA features. Code below.
Shared instances are objects that are shared among two or more pages in a module. The shared instances mechanism allows multi-page processes to easily share the same instances. For this module, we have 2 shared instances, a WFUNIXDateFormatter, and our ExampleEmail object. PHOCOA automatically instantiates shared objects as members of your module subclass.
dateSentFormatter:
class : WFUNIXDateFormatter
properties:
formatString: F j, Y, g:i a
email :
class: ExampleEmail
<table border="0">
{WFForm id="form"}
<tr><td valign="top">To Email:</td><td>{WFTextField id="email"}<br /> {WFShowErrors id="email"}</td></tr>
<tr><td valign="top">Subject:</td><td>{WFTextField id="subject"}<br /> {WFShowErrors id="subject"}</td></tr>
<tr><td valign="top">Message:</td><td>{WFTextArea id="message"}<br /></td></tr>
{WFSubmit id="submit"}
{/WFForm}
</table>
form:
children :
email :
bindings :
value:
controllerKey:
instanceID : email
modelKeyPath : toEmail
class : WFTextField
properties:
size: 50
message:
bindings :
value:
controllerKey:
instanceID : email
modelKeyPath : message
class : WFTextArea
properties:
cols: 50
rows: 10
subject:
bindings :
value:
controllerKey:
instanceID : email
modelKeyPath : subject
class : WFTextField
properties:
size: 50
send :
class : WFSubmit
properties:
label: Send Email
class : WFForm
properties:
method: get
<?php
class ExampleEmail extends WFObject
{
protected $toEmail;
protected $subject;
protected $message;
protected $sendTimestamp;
function send()
{
//$sent = mail( $this->toEmail, $this->subject, $this->message );
$this->sendTimestamp = time();
return $sent;
}
function validateToEmail(&$value, &$edited, &$errors)
{
$value = trim($value);
$edited = true;
if (preg_match("/[A-z0-9._-]+@[A-z0-9-]+\.[A-z0-9-\.]*[A-z]+$/", $value) == 1) return true;
$errors[] = new WFError("The email you entered is not a properly formatted email address.");
return false;
}
function validateSubject(&$value, &$edited, &$errors)
{
$value = trim($value);
$edited = true;
if ($value != '') return true;
$errors[] = new WFError("The subject cannot be blank.");
return false;
}
}
class emailform extends WFModule
{
/**
* Tell system which page to show if none specified.
*/
function defaultPage() { return 'compose'; }
}
class emailform_compose
{
function send($page, $params)
{
$page->sharedOutlet('email')->send();
$page->module()->setupResponsePage('emailSuccess');
}
function setupSkin($page, $params, $skin)
{
$skin->setTitle("Compose an email.");
}
}
class emailform_emailSuccess
{
function setupSkin($page, $params, $skin)
{
$skin->setTitle("Email sent successfully.");
}
}
?>