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 / formradio.php < prev    next >
Encoding:
PHP Script  |  2007-12-29  |  6.0 KB  |  186 lines

  1. <?php
  2. // $Id: formradio.php 1208 2007-12-29 00:04:49Z 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.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  42.  */
  43. /**
  44.  * A Group of radiobuttons
  45.  * 
  46.  * @author    Kazumi Ono    <onokazu@xoops.org>
  47.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  48.  * 
  49.  * @package        kernel
  50.  * @subpackage    form
  51.  */
  52. class XoopsFormRadio extends XoopsFormElement {
  53.  
  54.     /**
  55.      * Array of Options
  56.      * @var    array    
  57.      * @access    private
  58.      */
  59.     var $_options = array();
  60.  
  61.     /**
  62.      * Pre-selected value
  63.      * @var    string    
  64.      * @access    private
  65.      */
  66.     var $_value = null;
  67.  
  68.     /**
  69.      * HTML to seperate the elements
  70.      * @var    string  
  71.      * @access  private
  72.      */
  73.     var $_delimeter;
  74.  
  75.     /**
  76.      * Constructor
  77.      * 
  78.      * @param    string    $caption    Caption
  79.      * @param    string    $name        "name" attribute
  80.      * @param    string    $value        Pre-selected value
  81.      */
  82.     function XoopsFormRadio($caption, $name, $value = null, $delimeter = ""){
  83.         $this->setCaption($caption);
  84.         $this->setName($name);
  85.         if (isset($value)) {
  86.             $this->setValue($value);
  87.         }
  88.         $this->_delimeter = $delimeter;
  89.     }
  90.  
  91.     /**
  92.      * Get the "value" attribute
  93.      * 
  94.      * @param    bool    $encode To sanitizer the text?
  95.      * @return    string
  96.      */
  97.     function getValue($encode = false) {
  98.         return ( $encode && $this->_value !== null ) ? htmlspecialchars($this->_value, ENT_QUOTES) : $this->_value;
  99.     }
  100.  
  101.     /**
  102.      * Set the pre-selected value
  103.      * 
  104.      * @param    $value    string
  105.      */
  106.     function setValue($value){
  107.         $this->_value = $value;
  108.     }
  109.  
  110.     /**
  111.      * Add an option
  112.      * 
  113.      * @param    string    $value    "value" attribute - This gets submitted as form-data.
  114.      * @param    string    $name    "name" attribute - This is displayed. If empty, we use the "value" instead.
  115.      */
  116.     function addOption($value, $name = "") {
  117.         if ( $name != "" ) {
  118.             $this->_options[$value] = $name;
  119.         } else {
  120.             $this->_options[$value] = $value;
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Adds multiple options
  126.      * 
  127.      * @param    array    $options    Associative array of value->name pairs.
  128.      */
  129.     function addOptionArray($options){
  130.         if ( is_array($options) ) {
  131.             foreach ( $options as $k => $v ) {
  132.                 $this->addOption($k, $v);
  133.             }
  134.         }
  135.     }
  136.  
  137.     /**
  138.      * Get an array with all the options
  139.      *
  140.      * @param    int     $encode     To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
  141.      * @return    array   Associative array of value->name pairs
  142.      */
  143.     function getOptions($encode = false) {
  144.         if (!$encode) {
  145.             return $this->_options;
  146.         }
  147.         $value = array();
  148.         foreach ($this->_options as $val => $name) {
  149.             $value[ $encode ? htmlspecialchars($val, ENT_QUOTES) : $val ] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
  150.         }
  151.         return $value;
  152.     }
  153.  
  154.     /**
  155.      * Get the delimiter of this group
  156.      * 
  157.      * @param    bool    $encode To sanitizer the text?
  158.      * @return    string  The delimiter
  159.      */
  160.     function getDelimeter($encode = false) {
  161.         return $encode ? htmlspecialchars(str_replace(' ', ' ', $this->_delimeter)) : $this->_delimeter;
  162.     }
  163.  
  164.     /**
  165.      * Prepare HTML for output
  166.      * 
  167.      * @return    string    HTML
  168.      */
  169.     function render() {
  170.         $ret = "";
  171.         $ele_name = $this->getName();
  172.         $ele_value = $this->getValue();
  173.         $ele_options = $this->getOptions();
  174.         $ele_extra = $this->getExtra();
  175.         $ele_delimeter = $this->getDelimeter();
  176.         foreach ( $ele_options as $value => $name ) {
  177.             $ret .= "<input type='radio' name='".$ele_name."' value='".htmlspecialchars($value, ENT_QUOTES)."'";
  178.             if ( isset($ele_value) && $value == $ele_value ) {
  179.                 $ret .= " checked='checked'";
  180.             }
  181.             $ret .= $ele_extra." />".$name.$ele_delimeter."\n";
  182.         }
  183.         return $ret;
  184.     }
  185. }
  186. ?>