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 / formelement.php < prev    next >
Encoding:
PHP Script  |  2007-12-08  |  9.5 KB  |  347 lines

  1. <?php
  2. // $Id: formelement.php 1158 2007-12-08 06:24:20Z 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. if (!defined('XOOPS_ROOT_PATH')) {
  32.     die("XOOPS root path not defined");
  33. }
  34. /**
  35.  * 
  36.  * 
  37.  * @package     kernel
  38.  * @subpackage  form
  39.  * 
  40.  * @author        Kazumi Ono    <onokazu@xoops.org>
  41.  * @author      Taiwen Jiang    <phppp@users.sourceforge.net>
  42.  * @copyright    copyright (c) 2000-2007 XOOPS.org
  43.  */
  44.  
  45.  
  46. /**
  47.  * Abstract base class for form elements
  48.  * 
  49.  * @author    Kazumi Ono    <onokazu@xoops.org>
  50.  * @author  Taiwen Jiang    <phppp@users.sourceforge.net>
  51.  * @copyright    copyright (c) 2000-2007 XOOPS.org
  52.  * 
  53.  * @package     kernel
  54.  * @subpackage  form
  55.  */
  56. class XoopsFormElement {
  57.  
  58.     /**
  59.      * Javascript performing additional validation of this element data
  60.      *
  61.      * This property contains a list of Javascript snippets that will be sent to
  62.      * XoopsForm::renderValidationJS().
  63.      * NB: All elements are added to the output one after the other, so don't forget
  64.      * to add a ";" after each to ensure no Javascript syntax error is generated.
  65.      * 
  66.      * @var array()  
  67.      */
  68.     var $customValidationCode = array();
  69.  
  70.     /**#@+
  71.      * @access private
  72.      */
  73.     /**
  74.      * "name" attribute of the element
  75.      * @var string  
  76.      */
  77.     var $_name;
  78.  
  79.     /**
  80.      * caption of the element
  81.      * @var    string
  82.      */
  83.     var $_caption;
  84.  
  85.     /**
  86.      * Accesskey for this element
  87.      * @var    string
  88.      */
  89.     var $_accesskey = '';
  90.  
  91.     /**
  92.      * HTML classes for this element
  93.      * @var    array
  94.      */
  95.     var $_class = array();
  96.  
  97.     /**
  98.      * hidden?
  99.      * @var    bool
  100.      */
  101.     var $_hidden = false;
  102.  
  103.     /**
  104.      * extra attributes to go in the tag
  105.      * @var    array
  106.      */
  107.     var $_extra = array();
  108.  
  109.     /**
  110.      * required field?
  111.      * @var    bool
  112.      */
  113.     var $_required = false;
  114.  
  115.     /**
  116.      * description of the field
  117.      * @var    string
  118.      */
  119.     var $_description = "";
  120.     /**#@-*/
  121.  
  122.  
  123.     /**
  124.      * constructor
  125.      *
  126.      */
  127.     function XoopsFormElement(){
  128.         exit("This class cannot be instantiated!");
  129.     }
  130.  
  131.     /**
  132.      * Is this element a container of other elements?
  133.      *
  134.      * @return    bool false
  135.      */
  136.     function isContainer()
  137.     {
  138.         return false;
  139.     }
  140.  
  141.     /**
  142.      * set the "name" attribute for the element
  143.      *
  144.      * @param    string  $name   "name" attribute for the element
  145.      */
  146.     function setName($name) {
  147.         $this->_name = trim($name);
  148.     }
  149.  
  150.     /**
  151.      * get the "name" attribute for the element
  152.      *
  153.      * @param    bool    encode?
  154.      * @return    string  "name" attribute
  155.      */
  156.     function getName($encode = true) {
  157.         if (false != $encode) {
  158.             return str_replace("&", "&", htmlspecialchars($this->_name, ENT_QUOTES));
  159.         }
  160.         return $this->_name;
  161.     }
  162.  
  163.     /**
  164.      * set the "accesskey" attribute for the element
  165.      *
  166.      * @param    string  $key   "accesskey" attribute for the element
  167.      */
  168.     function setAccessKey($key) {
  169.         $this->_accesskey = trim($key);
  170.     }
  171.     /**
  172.      * get the "accesskey" attribute for the element
  173.      *
  174.      * @return     string  "accesskey" attribute value
  175.      */
  176.     function getAccessKey() {
  177.         return $this->_accesskey;
  178.     }
  179.     /**
  180.      * If the accesskey is found in the specified string, underlines it
  181.      *
  182.      * @param    string  $str   String where to search the accesskey occurence
  183.      * @return     string  Enhanced string with the 1st occurence of accesskey underlined
  184.      */
  185.     function getAccessString( $str ) {
  186.         $access = $this->getAccessKey();
  187.         if ( !empty($access) && ( false !== ($pos = strpos($str, $access)) ) ) {
  188.             return htmlspecialchars(substr($str, 0, $pos), ENT_QUOTES) . '<span style="text-decoration:underline">' . htmlspecialchars(substr($str, $pos, 1), ENT_QUOTES) . '</span>' . htmlspecialchars(substr($str, $pos+1), ENT_QUOTES);
  189.         }
  190.         return htmlspecialchars($str, ENT_QUOTES);
  191.     }
  192.  
  193.     /**
  194.      * set the "class" attribute for the element
  195.      *
  196.      * @param    string  $key   "class" attribute for the element
  197.      */
  198.     function setClass($class) {
  199.         $class = trim($class);
  200.         if ( !empty($class) ) {
  201.             $this->_class[] = $class;
  202.         }
  203.     }
  204.     /**
  205.      * get the "class" attribute for the element
  206.      *
  207.      * @return     string  "class" attribute value
  208.      */
  209.     function getClass() {
  210.         if( empty($this->_class) ) return '';
  211.         $class = array();
  212.         foreach ($this->_class as $class) {
  213.             $class[] = htmlspecialchars($class, ENT_QUOTES);
  214.         }
  215.         return implode(" ", $class);
  216.     }
  217.  
  218.     /**
  219.      * set the caption for the element
  220.      *
  221.      * @param    string  $caption
  222.      */
  223.     function setCaption($caption) {
  224.         $this->_caption = trim($caption);
  225.     }
  226.  
  227.     /**
  228.      * get the caption for the element
  229.      *
  230.      * @param    bool    $encode To sanitizer the text?
  231.      * @return    string
  232.      */
  233.     function getCaption($encode = false) {
  234.         return $encode ? htmlspecialchars($this->_caption, ENT_QUOTES) : $this->_caption;
  235.     }
  236.  
  237.     /**
  238.      * set the element's description
  239.      *
  240.      * @param    string  $description
  241.      */
  242.     function setDescription($description) {
  243.         $this->_description = trim($description);
  244.     }
  245.  
  246.     /**
  247.      * get the element's description
  248.      *
  249.      * @param    bool    $encode To sanitizer the text?
  250.      * @return    string
  251.      */
  252.     function getDescription($encode = false) {
  253.         return $encode ? htmlspecialchars($this->_description, ENT_QUOTES) : $this->_description;
  254.     }
  255.  
  256.     /**
  257.      * flag the element as "hidden"
  258.      *
  259.      */
  260.     function setHidden() {
  261.         $this->_hidden = true;
  262.     }
  263.  
  264.     /**
  265.      * Find out if an element is "hidden".
  266.      *
  267.      * @return    bool
  268.      */
  269.     function isHidden() {
  270.         return $this->_hidden;
  271.     }
  272.  
  273.     /**
  274.      * Find out if an element is required.
  275.      *
  276.      * @return    bool
  277.      */
  278.     function isRequired() {
  279.         return $this->_required;
  280.     }
  281.  
  282.     /**
  283.      * Add extra attributes to the element.
  284.      *
  285.      * This string will be inserted verbatim and unvalidated in the
  286.      * element's tag. Know what you are doing!
  287.      *
  288.      * @param    string  $extra
  289.      * @param   string  $replace If true, passed string will replace current content otherwise it will be appended to it
  290.      * @return    array   New content of the extra string
  291.      */
  292.     function setExtra($extra, $replace = false) {
  293.         if ( $replace) {
  294.             $this->_extra = array( trim($extra) );
  295.         } else {
  296.             $this->_extra[] = trim($extra);
  297.         }
  298.         return $this->_extra;
  299.     }
  300.  
  301.     /**
  302.      * Get the extra attributes for the element
  303.      *
  304.      * @param    bool    $encode To sanitizer the text?
  305.      * @return    string
  306.      */
  307.     function getExtra($encode = false) {
  308.         if (!$encode) {
  309.             return implode(' ', $this->_extra);
  310.         }
  311.         $value = array();
  312.         foreach ($this->_extra as $val) {
  313.             $value[] = str_replace('>', '>', str_replace('<', '<', $val));
  314.         }
  315.         return empty($value) ? "" : " ".implode(' ', $value);
  316.     }
  317.  
  318.     /**
  319.      * Render custom javascript validation code
  320.      *
  321.      * @seealso XoopsForm::renderValidationJS
  322.     */
  323.     function renderValidationJS() {
  324.         // render custom validation code if any
  325.         if ( !empty( $this->customValidationCode ) ) {
  326.             return implode( "\n", $this->customValidationCode );
  327.         // generate validation code if required 
  328.         } elseif ($this->isRequired()) {
  329.             $eltname    = $this->getName();
  330.             $eltcaption = $this->getCaption();
  331.             $eltmsg = empty($eltcaption) ? sprintf( _FORM_ENTER, $eltname ) : sprintf( _FORM_ENTER, $eltcaption );
  332.             $eltmsg = str_replace('"', '\"', stripslashes( $eltmsg ) );
  333.             return "if ( myform.{$eltname}.value == \"\" ) { window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }";
  334.         }
  335.         return ''; 
  336.     }
  337.  
  338.     /**
  339.      * Generates output for the element.
  340.      *
  341.      * This method is abstract and must be overwritten by the child classes.
  342.      * @abstract
  343.      */
  344.     function render(){
  345.     }
  346. }
  347. ?>