home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm2.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.1 KB  |  213 lines

  1. <?php
  2. /**
  3.  * Class representing a HTML form
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE:
  8.  * 
  9.  * Copyright (c) 2006, 2007, Alexey Borzov <avb@php.net>,
  10.  *                           Bertrand Mansion <golgote@mamasam.com>
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or without
  14.  * modification, are permitted provided that the following conditions
  15.  * are met:
  16.  *
  17.  *    * Redistributions of source code must retain the above copyright
  18.  *      notice, this list of conditions and the following disclaimer.
  19.  *    * Redistributions in binary form must reproduce the above copyright
  20.  *      notice, this list of conditions and the following disclaimer in the 
  21.  *      documentation and/or other materials provided with the distribution.
  22.  *    * The names of the authors may not be used to endorse or promote products 
  23.  *      derived from this software without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  *
  37.  * @category   HTML
  38.  * @package    HTML_QuickForm2
  39.  * @author     Alexey Borzov <avb@php.net>
  40.  * @author     Bertrand Mansion <golgote@mamasam.com>
  41.  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
  42.  * @version    CVS: $Id: QuickForm2.php,v 1.6 2007/05/29 20:40:10 avb Exp $
  43.  * @link       http://pear.php.net/package/HTML_QuickForm2
  44.  */
  45.  
  46. /**
  47.  * Abstract base class for QuickForm2 containers
  48.  */
  49. require_once 'HTML/QuickForm2/Container.php';
  50.  
  51. /**
  52.  * Data source for HTML_QuickForm2 objects based on superglobal arrays
  53.  */
  54. require_once 'HTML/QuickForm2/DataSource/SuperGlobal.php';
  55.  
  56. /**
  57.  * Class representing a HTML form
  58.  *
  59.  * @category   HTML
  60.  * @package    HTML_QuickForm2
  61.  * @author     Alexey Borzov <avb@php.net>
  62.  * @author     Bertrand Mansion <golgote@mamasam.com>
  63.  * @version    Release: 0.2.0
  64.  */
  65. class HTML_QuickForm2 extends HTML_QuickForm2_Container
  66. {
  67.    /**
  68.     * Data sources providing values for form elements 
  69.     * @var array
  70.     */
  71.     protected $datasources = array();
  72.  
  73.    /**
  74.     * We do not allow setting "method" and "id" other than through constructor
  75.     * @var array
  76.     */
  77.     protected $watchedAttributes = array('id', 'method');
  78.  
  79.    /**
  80.     * Class constructor, form's "id" and "method" attributes can only be set here
  81.     *
  82.     * @param    string  "id" attribute of <form> tag
  83.     * @param    string  HTTP method used to submit the form
  84.     * @param    mixed   Additional attributes (either a string or an array)
  85.     * @param    bool    Whether to track if the form was submitted by adding 
  86.     *                   a special hidden field
  87.     */ 
  88.     public function __construct($id, $method = 'post', $attributes = null, $trackSubmit = true)
  89.     {
  90.         $method = ('GET' == strtoupper($method))? 'get': 'post';
  91.         if (empty($id)) {
  92.             $id          = self::generateId('');
  93.             $trackSubmit = false;
  94.         } else {
  95.             self::storeId($id);
  96.         }
  97.         $this->attributes = array_merge(
  98.                                 self::prepareAttributes($attributes),
  99.                                 array('id' => (string)$id, 'method' => $method)
  100.                             );
  101.         if (!isset($this->attributes['action'])) {
  102.             $this->attributes['action'] = $_SERVER['PHP_SELF'];
  103.         }
  104.         if ($trackSubmit && isset($_REQUEST['_qf__' . $id]) ||
  105.             !$trackSubmit && ('get' == $method && !empty($_GET) ||
  106.                               'post' == $method && (!empty($_POST) || !empty($_FILES))))
  107.         {
  108.             $this->addDataSource(new HTML_QuickForm2_DataSource_SuperGlobal(
  109.                 $method, get_magic_quotes_gpc()
  110.             ));
  111.         }
  112.         if ($trackSubmit) {
  113.             $this->appendChild(HTML_QuickForm2_Factory::createElement(
  114.                 'hidden', '_qf__' . $id
  115.             ));
  116.         }
  117.     }
  118.  
  119.     protected function onAttributeChange($name, $value = null)
  120.     {
  121.         throw new HTML_QuickForm2_InvalidArgumentException(
  122.             'Attribute \'' . $name . '\' is read-only'
  123.         );
  124.     }
  125.  
  126.     protected function setContainer(HTML_QuickForm2_Container $container = null)
  127.     {
  128.         throw new HTML_QuickForm2_Exception('Form cannot be added to container');
  129.     }
  130.  
  131.     public function setId($id = null)
  132.     {
  133.         throw new HTML_QuickForm2_InvalidArgumentException(
  134.             "Attribute 'id' is read-only"
  135.         );
  136.     }
  137.  
  138.  
  139.    /**
  140.     * Adds a new data source to the form
  141.     *
  142.     * @param    HTML_QuickForm2_DataSource  Data source
  143.     */
  144.     public function addDataSource(HTML_QuickForm2_DataSource $datasource)
  145.     {
  146.         $this->datasources[] = $datasource;
  147.         $this->updateValue();
  148.     }
  149.  
  150.    /**
  151.     * Replaces the list of form's data sources with a completely new one
  152.     *
  153.     * @param    array   A new data source list
  154.     * @throws   HTML_QuickForm2_InvalidArgumentException    if given array 
  155.     *               contains something that is not a valid data source
  156.     */
  157.     public function setDataSources(array $datasources)
  158.     {
  159.         foreach ($datasources as $ds) {
  160.             if (!$ds instanceof HTML_QuickForm2_DataSource) {
  161.                 throw new HTML_QuickForm2_InvalidArgumentException(
  162.                     'Array should contain only DataSource instances'
  163.                 );
  164.             }
  165.         }
  166.         $this->datasources = $datasources;
  167.         $this->updateValue();
  168.     }
  169.  
  170.    /**
  171.     * Returns the list of data sources attached to the form
  172.     *
  173.     * @return   array
  174.     */
  175.     public function getDataSources()
  176.     {
  177.         return $this->datasources;
  178.     }
  179.  
  180.     public function getType()
  181.     {
  182.         return 'form';
  183.     }
  184.  
  185.     public function setValue($value)
  186.     {
  187.         throw new HTML_QuickForm2_Exception('Not implemented');
  188.     }
  189.  
  190.     public function __toString()
  191.     {
  192.         throw new HTML_QuickForm2_Exception('Not implemented');
  193.     }
  194.  
  195.    /**
  196.     * Performs the server-side validation
  197.     *
  198.     * @return   boolean Whether all form's elements are valid
  199.     */
  200.     public function validate()
  201.     {
  202.         $isSubmitted = false;
  203.         foreach ($this->datasources as $ds) {
  204.             if ($ds instanceof HTML_QuickForm2_DataSource_Submit) {
  205.                 $isSubmitted = true;
  206.                 break;
  207.             }
  208.         }
  209.         return $isSubmitted? parent::validate(): false;
  210.     }
  211. }
  212. ?>
  213.