Cnz_Html
[ class tree: Cnz_Html ] [ index: Cnz_Html ] [ all elements ]

Source for file Form.php

Documentation is available at Form.php

  1. <?php
  2. /**
  3.  * CNZ Framework
  4.  *
  5.  * A Framework for Creating and Using Complex Web Elements
  6.  *
  7.  * The purpose of this framework is to provide a library of high-level objects
  8.  * to facilitate common HTML coding tasks, such as menus, tables, and forms.
  9.  * The intent is to reduce repetitive HTML coding as much as possible, replacing
  10.  * it with a combination of configuration files and style sheets with
  11.  * standardized naming conventions.
  12.  *
  13.  * This framework is built on and requires the
  14.  * {@link http://framework.zend.com/ Zend Framework}.
  15.  *
  16.  * @category   Cnz
  17.  * @package    Cnz_Html
  18.  * @subpackage Form
  19.  *
  20.  * @author    Lyle Frost <lfrost@cnz.com>
  21.  * @copyright Copyright (c) 2006-2007 Citadel Network <{@link http://www.citadelnetwork.com/}>
  22.  * @filesource
  23.  * @license   http://www.citadelnetwork.com/license/cnzframework New BSD License
  24.  * @version   $Id: Form.php 27 2007-07-19 18:47:54Z lfrost $
  25.  */
  26.  
  27. /**
  28.  * Required classes.
  29.  *
  30.  * @ignore
  31.  */
  32. Zend_Loader::loadClass('Cnz_Html_Form_Abstract');
  33.  
  34. /**
  35.  * HTML Form class
  36.  *
  37.  * This class automates the creation and display of a form.  CSS form style
  38.  * names are standardized as follows:
  39.  *
  40.  * formLabel[-<i>formname</i>-<i>fieldname</i>]:  div block containing a single form label
  41.  * formField[-<i>formname</i>-<i>fieldname</i>]:  div block containing a single form field
  42.  *
  43.  * IDs are standardardized as:
  44.  *
  45.  * form[-<i>formname<i>-<i>fieldname</i>]
  46.  *
  47.  * Or for a radio button:
  48.  *
  49.  * form[-<i>formname<i>-<i>fieldname</i>]-<i>number</i>
  50.  *
  51.  * See demo at {@link http://cfdemo.cnz.com/}.
  52.  *
  53.  * @category   Cnz
  54.  * @package    Cnz_Html
  55.  * @subpackage Form
  56.  */
  57. {
  58.     const HAS_CONFIG    = true;
  59.     const ENCTYPE_FILE    = 'multipart/form-data';
  60.  
  61.     /**#@+ @var string */
  62.     private $action        NULL;
  63.     private $enctype    NULL;
  64.     private $mailTo        NULL;
  65.     private $mailBcc    NULL;
  66.     private $mailSubject    NULL;
  67.     private $method        'post';
  68.     private $onSubmit    NULL;
  69.     private $submitMethod    NULL;
  70.     /**#@-*/
  71.  
  72.     /* Getters/Setters ===================================================*/
  73.  
  74.     /** @return string Action */
  75.     public function getAction()
  76.     {
  77.         return $this->action;
  78.     }
  79.  
  80.     /** @return string Method */
  81.     public function getMethod()
  82.     {
  83.         return $this->method;
  84.     }
  85.  
  86.     /* Methods ===========================================================*/
  87.  
  88.     /**
  89.      * Option fields:
  90.      *   none
  91.      *
  92.      * Configuration fields:
  93.      *   action       Form action
  94.      *   mailTo       Mail to address
  95.      *   mailSubject  Mail subject
  96.      *   mailBcc      BCC address
  97.      *   method       Form method (get or post)
  98.      *   onSubmit     Form onsubmit value
  99.      *
  100.      * @param  array $options Options
  101.      */
  102.     public function __construct(array $options array())
  103.     {
  104.         parent::__construct($options);
  105.  
  106.         // Process configuration.
  107.         if (isset($this->config->action)) $this->action $this->config->action;
  108.         if (isset($this->config->mailTo)) $this->mailTo $this->config->mailTo;
  109.         if (isset($this->config->mailSubject)) $this->mailSubject $this->config->mailSubject;
  110.         if (isset($this->config->mailBcc)) $this->mailBcc $this->config->mailBcc;
  111.         if (isset($this->config->method)) $this->method $this->config->method;
  112.         if (isset($this->config->onSubmit)) $this->onSubmit $this->config->onSubmit;
  113.         return;
  114.     }
  115.  
  116.     /**
  117.      * @param  string $style Value for outermost style attribute
  118.      * @return void 
  119.      */
  120.     public function display($style NULL)
  121.     {
  122.         $indent Cnz_Html::indentGenerate();
  123.  
  124.         echo $indent'<div class = "'$this->genStyles()'"';
  125.         if (!empty($style)) echo ' style = "'$style'"';
  126.         echo '>'"\n";
  127.         Cnz_Html::indentInc();
  128.         $indent Cnz_Html::indentGenerate();
  129.  
  130.         echo $indent'<form action = "'$this->action'"';
  131.         if ($this->fileFlagecho ' enctype = "'self::ENCTYPE_FILE'"';
  132.         echo ' method = "'$this->method'"';
  133.         Cnz_Html::indentInc();
  134.         $indent Cnz_Html::indentGenerate();
  135.  
  136.         if (!empty($this->onSubmit)) echo ' onsubmit = "'$this->onSubmit'"';
  137.         echo '>'"\n";
  138.         parent::display($style);
  139.         echo $indent'<div class = "formButton"><button type = "submit">Send</button></div>'"\n";
  140.  
  141.         Cnz_Html::indentDec();
  142.         $indent Cnz_Html::indentGenerate();
  143.         echo $indent'</form>'"\n";
  144.  
  145.         Cnz_Html::indentDec();
  146.         $indent Cnz_Html::indentGenerate();
  147.         echo $indent'</div>'"\n";
  148.  
  149.         return;
  150.     }
  151.  
  152.     /**
  153.      * Display submitted data (for debugging).
  154.      *
  155.      * @param  boolean $rawFlag Display as raw data, else filter and format.
  156.      * @return void 
  157.      */
  158.     public function submitDisplay($rawFlag false)
  159.     {
  160.         if ($this->method == 'post'$data $_POST;
  161.         else $data $_GET;
  162.  
  163.         echo '<pre>';
  164.         if ($rawFlag)
  165.         {
  166.             print_r($data);
  167.         }
  168.         else
  169.         {
  170.             echo $this->submitString($data);
  171.         }
  172.         echo '</pre>';
  173.         return;
  174.     }
  175.  
  176.     /**
  177.      * Submit the form by mail.
  178.      *
  179.      * If there is a field named <kbd>email</kbd>, this is automatically
  180.      * made the From address.
  181.      *
  182.      * @return boolean Success
  183.      */
  184.     public function submitMail()
  185.     {
  186.         Zend_Loader::loadClass('Zend_Mail');
  187.         $mail new Zend_Mail();
  188.  
  189.         if ($this->method == 'post'$data $_POST;
  190.         else $data $_GET;
  191.  
  192.         if (empty($this->mailTo))
  193.         {
  194.             return false;
  195.         }
  196.  
  197.         $mailBody $this->submitString($_POST);
  198.         $mailBody 'Received from ' $_SERVER['REMOTE_ADDR'' using ' $_SERVER['HTTP_USER_AGENT'"\n\n" $mailBody;
  199.  
  200.         if (isset($data['email']&& !empty($data['email'])) $mail->setFrom($data['email']);
  201.         else $mail->setFrom($this->mailTo);
  202.         $mail->addTo($this->mailTo);
  203.         if (!empty($this->mailBcc)) $mail->addBcc($this->mailBcc);
  204.         $mail->setSubject($this->mailSubject);
  205.         $mail->setBodyText($mailBody);
  206.  
  207.         // Attachments.
  208.         Zend_Loader::loadClass('Zend_Mime');
  209.         foreach ($_FILES as $name => $upload)
  210.         {
  211.             if ($upload['error'== UPLOAD_ERR_OK)
  212.             {
  213.                 if (!is_uploaded_file($upload['tmp_name']))
  214.                 {
  215.                     Cnz_Html::getLogger()->err(__METHOD__ . '[' . __LINE__ . '] ' 'Attempt to attach file that was not uploaded.  File name = ' $upload['tmp_name''.');
  216.                     continue;
  217.                 }
  218.                 $fp fopen($upload['tmp_name']'r');
  219.                 if (!$fp)
  220.                 {
  221.                     Cnz_Html::getLogger()->err(__METHOD__ . '[' . __LINE__ . '] ' 'Attempt to attach file that was not uploaded.  File name = ' $upload['tmp_name''.');
  222.                     echo '<p>Error attaching file '$upload['name']'.</p>'"\n\n";
  223.                     continue;
  224.                 }
  225.                 $at new Zend_Mime_Part($fp);
  226.                 $at->disposition Zend_Mime::DISPOSITION_ATTACHMENT;
  227.                 $at->encoding    Zend_Mime::ENCODING_8BIT;
  228.                 $at->type    Zend_Mime::TYPE_OCTETSTREAM;
  229.                 $at->filename    $upload['name'];
  230.                 $mail->addAttachment($at);
  231.             }
  232.         }
  233.  
  234.         // Send mail.
  235.         try
  236.         {
  237.             $mail->send();
  238.         }
  239.         catch (Zend_Exception $e)
  240.         {
  241.             Cnz_Html::getLogger()->err(__METHOD__ . '[' . __LINE__ . '] ' 'Exception from ' get_class($e':  ' $e->getMessage('.');
  242.             return false;
  243.         }
  244.  
  245.         return true;
  246.     }
  247. }

Documentation generated on Thu, 19 Jul 2007 15:02:02 -0400 by phpDocumentor 1.4.0RC2