home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopsform / form.php next >
Encoding:
PHP Script  |  2008-01-28  |  13.7 KB  |  458 lines

  1. <?php
  2. // $Id: form.php 1279 2008-01-28 06:40:39Z phppp $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31. // public abstract
  32. /**
  33.  * 
  34.  * 
  35.  * @package     kernel
  36.  * @subpackage  form
  37.  * 
  38.  * @author        Kazumi Ono    <onokazu@xoops.org>
  39.  * @author        Taiwen Jiang    <phppp@users.sourceforge.net>
  40.  * @copyright    copyright (c) 2000-2007 XOOPS.org
  41.  */
  42.  
  43.  
  44. /**
  45.  * Abstract base class for forms
  46.  * 
  47.  * @author    Kazumi Ono    <onokazu@xoops.org>
  48.  * @author  Taiwen Jiang    <phppp@users.sourceforge.net>
  49.  * @copyright    copyright (c) 2000-2007 XOOPS.org
  50.  * 
  51.  * @package     kernel
  52.  * @subpackage  form
  53.  */
  54. class XoopsForm {
  55.     /**#@+
  56.      * @access  private
  57.      */
  58.     /** 
  59.      * "action" attribute for the html form
  60.      * @var string  
  61.      */
  62.     var $_action;
  63.  
  64.     /**
  65.      * "method" attribute for the form.
  66.      * @var string  
  67.      */
  68.     var $_method;
  69.  
  70.     /**
  71.      * "name" attribute of the form
  72.      * @var string  
  73.      */
  74.     var $_name;
  75.  
  76.     /**
  77.      * title for the form
  78.      * @var string  
  79.      */
  80.     var $_title;
  81.  
  82.     /**
  83.      * array of {@link XoopsFormElement} objects
  84.      * @var  array   
  85.      */
  86.     var $_elements = array();
  87.  
  88.     /**
  89.      * extra information for the <form> tag
  90.      * @var array  
  91.      */
  92.     var $_extra = array();
  93.  
  94.     /**
  95.      * required elements
  96.      * @var array   
  97.      */
  98.     var $_required = array();
  99.     
  100.     /**#@-*/
  101.  
  102.     /**
  103.      * constructor
  104.      * 
  105.      * @param    string  $title  title of the form
  106.      * @param    string  $name   "name" attribute for the <form> tag
  107.      * @param    string  $action "action" attribute for the <form> tag
  108.      * @param   string  $method "method" attribute for the <form> tag
  109.      * @param   bool    $addtoken whether to add a security token to the form
  110.      */
  111.     function XoopsForm($title, $name, $action, $method = "post", $addtoken = false) {
  112.         $this->_title = $title;
  113.         $this->_name = $name;
  114.         $this->_action = $action;
  115.         $this->_method = $method;
  116.         if ($addtoken != false) {
  117.             $this->addElement(new XoopsFormHiddenToken());
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * return the title of the form
  123.      * 
  124.      * @param    bool    $encode To sanitizer the text?
  125.      * @return    string
  126.      */
  127.     function getTitle($encode = false) {
  128.         return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title;
  129.     }
  130.  
  131.     /**
  132.      * get the "name" attribute for the <form> tag
  133.      * 
  134.      * Deprecated, to be refactored
  135.      *
  136.      * @param    bool    $encode To sanitizer the text?
  137.      * @return    string
  138.      */
  139.     function getName($encode = true) {
  140.         return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name;
  141.     }
  142.  
  143.     /**
  144.      * get the "action" attribute for the <form> tag
  145.      * 
  146.      * @param    bool    $encode To sanitizer the text?
  147.      * @return    string
  148.      */
  149.     function getAction($encode = true) {
  150.         return $encode ? htmlspecialchars($this->_action, ENT_QUOTES) : $this->_action;
  151.     }
  152.  
  153.     /**
  154.      * get the "method" attribute for the <form> tag
  155.      * 
  156.      * @return    string
  157.      */
  158.     function getMethod(){
  159.         return ( strtolower($this->_method) == "get" ) ? "GET" : "POST";
  160.     }
  161.  
  162.     /**
  163.      * Add an element to the form
  164.      *
  165.      * @param    object  &$formElement    reference to a {@link XoopsFormElement}
  166.      * @param    bool    $required       is this a "required" element?
  167.      */
  168.     function addElement(&$formElement, $required = false) {
  169.         if ( is_string( $formElement ) ) {
  170.             $this->_elements[] = $formElement;
  171.         } elseif ( is_subclass_of($formElement, 'xoopsformelement') ) {
  172.             $this->_elements[] =& $formElement;
  173.             if (!$formElement->isContainer()) {
  174.                 if ($required) {
  175.                     $formElement->_required = true;
  176.                     $this->_required[] =& $formElement;
  177.                 }
  178.             } else {
  179.                 $required_elements =& $formElement->getRequired();
  180.                 $count = count($required_elements);
  181.                 for ($i = 0 ; $i < $count; $i++) {
  182.                     $this->_required[] =& $required_elements[$i];
  183.                 }
  184.             }
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * get an array of forms elements
  190.      * 
  191.      * @param    bool    get elements recursively?
  192.      * @return    array   array of {@link XoopsFormElement}s
  193.      */
  194.     function &getElements($recurse = false) {
  195.         if (!$recurse) {
  196.             return $this->_elements;
  197.         } else {
  198.             $ret = array();
  199.             $count = count($this->_elements);
  200.             for ($i = 0; $i < $count; $i++) {
  201.                 if ( is_object( $this->_elements[$i] ) ) {
  202.                     if (!$this->_elements[$i]->isContainer()) {
  203.                         $ret[] =& $this->_elements[$i];
  204.                     } else {
  205.                         $elements =& $this->_elements[$i]->getElements(true);
  206.                         $count2 = count($elements);
  207.                         for ($j = 0; $j < $count2; $j++) {
  208.                             $ret[] =& $elements[$j];
  209.                         }
  210.                         unset($elements);
  211.                     }
  212.                 }
  213.             }
  214.             return $ret;
  215.         }
  216.     }
  217.  
  218.     /**
  219.      * get an array of "name" attributes of form elements
  220.      * 
  221.      * @return    array   array of form element names
  222.      */
  223.     function getElementNames() {
  224.         $ret = array();
  225.         $elements =& $this->getElements(true);
  226.         $count = count($elements);
  227.         for ($i = 0; $i < $count; $i++) {
  228.             $ret[] = $elements[$i]->getName();
  229.         }
  230.         return $ret;
  231.     }
  232.  
  233.     /**
  234.      * get a reference to a {@link XoopsFormElement} object by its "name"
  235.      * 
  236.      * @param  string  $name    "name" attribute assigned to a {@link XoopsFormElement}
  237.      * @return object  reference to a {@link XoopsFormElement}, false if not found
  238.      */
  239.     function &getElementByName($name) {
  240.         $elements = $this->getElements(true);
  241.         $count = count($elements);
  242.         for ($i = 0; $i < $count; $i++) {
  243.             if ($name == $elements[$i]->getName(false)) {
  244.                 return $elements[$i];
  245.             }
  246.         }
  247.         $elt = null;
  248.         return $elt;
  249.     }
  250.  
  251.     /**
  252.      * Sets the "value" attribute of a form element
  253.      * 
  254.      * @param    string $name    the "name" attribute of a form element
  255.      * @param    string $value    the "value" attribute of a form element
  256.      */
  257.     function setElementValue($name, $value){
  258.         $ele =& $this->getElementByName($name);
  259.         if (is_object($ele) && method_exists($ele, 'setValue')) {
  260.             $ele->setValue($value);
  261.         }
  262.     }
  263.  
  264.     /**
  265.      * Sets the "value" attribute of form elements in a batch
  266.      * 
  267.      * @param    array $values    array of name/value pairs to be assigned to form elements
  268.      */
  269.     function setElementValues($values){
  270.         if (is_array($values) && !empty($values)) {
  271.             // will not use getElementByName() for performance..
  272.             $elements =& $this->getElements(true);
  273.             $count = count($elements);
  274.             for ($i = 0; $i < $count; $i++) {
  275.                 $name = $elements[$i]->getName(false);
  276.                 if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) {
  277.                     $elements[$i]->setValue($values[$name]);
  278.                 }
  279.             }
  280.         }
  281.     }
  282.  
  283.     /**
  284.      * Gets the "value" attribute of a form element
  285.      * 
  286.      * @param    string     $name    the "name" attribute of a form element
  287.      * @param    bool    $encode To sanitizer the text?
  288.      * @return    string     the "value" attribute assigned to a form element, null if not set
  289.      */
  290.     function getElementValue($name, $encode = false) {
  291.         $ele =& $this->getElementByName($name);
  292.         if (is_object($ele) && method_exists($ele, 'getValue')) {
  293.             return $ele->getValue($encode);
  294.         }
  295.         return;
  296.     }
  297.  
  298.     /**
  299.      * gets the "value" attribute of all form elements
  300.      * 
  301.      * @param    bool    $encode To sanitizer the text?
  302.      * @return    array     array of name/value pairs assigned to form elements
  303.      */
  304.     function getElementValues($encode = false) {
  305.         // will not use getElementByName() for performance..
  306.         $elements =& $this->getElements(true);
  307.         $count = count($elements);
  308.         $values = array();
  309.         for ($i = 0; $i < $count; $i++) {
  310.             $name = $elements[$i]->getName(false);
  311.             if ($name && method_exists($elements[$i], 'getValue')) {
  312.                 $values[$name] =& $elements[$i]->getValue($encode);
  313.             }
  314.         }
  315.         return $values;
  316.     }
  317.  
  318.     /**
  319.      * set the extra attributes for the <form> tag
  320.      *
  321.      * @param    string  $extra  extra attributes for the <form> tag
  322.      */
  323.     function setExtra($extra) {
  324.         if (!empty($extra)) {
  325.             $this->_extra[] = $extra;
  326.         }
  327.     }
  328.  
  329.     /**
  330.      * get the extra attributes for the <form> tag
  331.      *
  332.      * @return    string
  333.      */
  334.     function &getExtra() {
  335.         $extra = empty($this->_extra) ? "" : " ". implode(" ", $this->_extra);
  336.         return $extra;
  337.     }
  338.  
  339.     /**
  340.      * make an element "required"
  341.      *
  342.      * @param    object  &$formElement    reference to a {@link XoopsFormElement}
  343.      */
  344.     function setRequired(&$formElement) {
  345.         $this->_required[] =& $formElement;
  346.     }
  347.  
  348.     /**
  349.      * get an array of "required" form elements
  350.      * 
  351.      * @return    array   array of {@link XoopsFormElement}s 
  352.      */
  353.     function &getRequired() {
  354.         return $this->_required;
  355.     }
  356.  
  357.     /**
  358.      * insert a break in the form
  359.      * 
  360.      * This method is abstract. It must be overwritten in the child classes.
  361.      *
  362.      * @param    string  $extra  extra information for the break
  363.      * @abstract
  364.      */
  365.     function insertBreak($extra = null) {
  366.     }
  367.  
  368.     /**
  369.      * returns renderered form
  370.      *
  371.      * This method is abstract. It must be overwritten in the child classes.
  372.      *
  373.      * @abstract
  374.      */
  375.     function render() {
  376.     }
  377.  
  378.     /**
  379.      * displays rendered form
  380.      */
  381.     function display() {
  382.         echo $this->render();
  383.     }
  384.  
  385.     /**
  386.      * Renders the Javascript function needed for client-side for validation
  387.      *
  388.      * Form elements that have been declared "required" and not set will prevent the form from being
  389.      * submitted. Additionally, each element class may provide its own "renderValidationJS" method
  390.      * that is supposed to return custom validation code for the element.
  391.      * 
  392.      * The element validation code can assume that the JS "myform" variable points to the form, and must
  393.      * execute <i>return false</i> if validation fails.
  394.      * 
  395.      * A basic element validation method may contain something like this:
  396.      * <code>
  397.      * function renderValidationJS() {
  398.      *   $name = $this->getName();
  399.      *   return "if ( myform.{$name}.value != 'valid' ) { " .
  400.      *     "myform.{$name}.focus(); window.alert( '$name is invalid' ); return false;" .
  401.      *     " }";
  402.      * }
  403.      * </code>
  404.      * 
  405.      * @param        boolean  $withtags    Include the < javascript > tags in the returned string
  406.      */
  407.     function renderValidationJS( $withtags = true ) {
  408.         $js = "";
  409.         if ( $withtags ) {
  410.             $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
  411.         }
  412.         $formname = $this->getName();
  413.         $js .= "function xoopsFormValidate_{$formname}() { var myform = window.document.{$formname}; ";
  414.         $elements = $this->getElements( true );
  415.         foreach ( $elements as $elt ) {
  416.             if ( method_exists( $elt, 'renderValidationJS' ) ) {
  417.                 $js .= $elt->renderValidationJS();
  418.             }
  419.         }
  420.         $js .= "return true;\n}\n";
  421.         if ( $withtags ) {
  422.             $js .= "//--></script>\n<!-- End Form Vaidation JavaScript //-->\n";
  423.         }
  424.         return $js;
  425.     }
  426.     
  427.     /**
  428.      * assign to smarty form template instead of displaying directly
  429.      *
  430.      * @param    object  &$tpl    reference to a {@link Smarty} object
  431.      * @see     Smarty
  432.      */
  433.     function assign(&$tpl) {
  434.         $i = -1;
  435.         $elements = array();
  436.         foreach ( $this->getElements() as $ele ) {
  437.             ++$i;
  438.             if (is_string( $ele )) {
  439.                 $elements[$i]['body']    = $ele;
  440.                 continue;
  441.             }
  442.             $ele_name = $ele->getName();
  443.             $ele_description = $ele->getDescription();
  444.             $n = $ele_name ? $ele_name : $i;
  445.             $elements[$n]['name']       = $ele_name;
  446.             $elements[$n]['caption']    = $ele->getCaption();
  447.             $elements[$n]['body']       = $ele->render();
  448.             $elements[$n]['hidden']     = $ele->isHidden();
  449.             $elements[$n]['required']   = $ele->isRequired();
  450.             if ($ele_description != '') {
  451.                 $elements[$n]['description']  = $ele_description;
  452.             }
  453.         }
  454.         $js = $this->renderValidationJS();
  455.         $tpl->assign($this->getName(), array('title' => $this->getTitle(), 'name' => $this->getName(), 'action' => $this->getAction(),  'method' => $this->getMethod(), 'extra' => 'onsubmit="return xoopsFormValidate_'.$this->getName().'();"'.$this->getExtra(), 'javascript' => $js, 'elements' => $elements));
  456.     }
  457. }
  458. ?>