PHOCOA PHP Framework

Simple email module showcases many PHOCOA features. Code below.

  • Form creation.
  • Action handlers.
  • Validation - Notice how there are no calls in code to validate. This is done automatically via Key-Value Validation concept.
  • Error management - Try entering invalid data. Notice how the system autmatically tracks errors per-field and per-form.
  • Bindings - notice how there is no code to move the data from the form to the ExampleEmail object. This is all done via Bindings which are configured graphically (or in a text file).
  • Adjusting page title for each page. This is part of the skin infrastructure.
To Email:
Subject:
Message:

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.

shared.yaml file


dateSentFormatter: 
  class     : WFUNIXDateFormatter
  properties: 
    formatString: F j, Y, g:i a
email            : 
  class: ExampleEmail

compose.tpl file


<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>


compose.yaml file


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

Module Code

<?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.");
    }
}

?>

Examples

Cookbook
  Widgets
  Bindings
  Pagination
  Formatters

Basic Examples
  Email
  Skin Browser
  Forms
  Regex Tester

AJAX Examples
  Infrastructure

  (YUI Examples)
  AutoComplete
  ColorPicker
  Menu
  Tree View
  Tab View
  Container

Appcelerator Integration
  Demo
Copyright (c) 2014 Alan Pinstein. All Rights Reserved.