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 / formselect.php < prev    next >
Encoding:
PHP Script  |  2007-12-08  |  6.7 KB  |  222 lines

  1. <?php
  2. // $Id: formselect.php 1162 2007-12-08 06:58:50Z 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.  * @package     kernel
  36.  * @subpackage  form
  37.  * 
  38.  * @author        Kazumi Ono    <onokazu@xoops.org>
  39.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  40.  */
  41.  
  42. /**
  43.  * A select field
  44.  * 
  45.  * @package     kernel
  46.  * @subpackage  form
  47.  * 
  48.  * @author        Kazumi Ono    <onokazu@xoops.org>
  49.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  50.  */
  51. class XoopsFormSelect extends XoopsFormElement {
  52.  
  53.     /**
  54.      * Options
  55.      * @var array   
  56.      * @access    private
  57.      */
  58.     var $_options = array();
  59.  
  60.     /**
  61.      * Allow multiple selections?
  62.      * @var    bool    
  63.      * @access    private
  64.      */
  65.     var $_multiple = false;
  66.  
  67.     /**
  68.      * Number of rows. "1" makes a dropdown list.
  69.      * @var    int 
  70.      * @access    private
  71.      */
  72.     var $_size;
  73.  
  74.     /**
  75.      * Pre-selcted values
  76.      * @var    array   
  77.      * @access    private
  78.      */
  79.     var $_value = array();
  80.  
  81.     /**
  82.      * Constructor
  83.      * 
  84.      * @param    string    $caption    Caption
  85.      * @param    string    $name       "name" attribute
  86.      * @param    mixed    $value        Pre-selected value (or array of them).
  87.      * @param    int        $size        Number or rows. "1" makes a drop-down-list
  88.      * @param    bool    $multiple   Allow multiple selections?
  89.      */
  90.     function XoopsFormSelect($caption, $name, $value = null, $size = 1, $multiple = false){
  91.         $this->setCaption($caption);
  92.         $this->setName($name);
  93.         $this->_multiple = $multiple;
  94.         $this->_size = intval($size);
  95.         if (isset($value)) {
  96.             $this->setValue($value);
  97.         }
  98.     }
  99.  
  100.     /**
  101.      * Are multiple selections allowed?
  102.      * 
  103.      * @return    bool
  104.      */
  105.     function isMultiple() {
  106.         return $this->_multiple;
  107.     }
  108.  
  109.     /**
  110.      * Get the size
  111.      * 
  112.      * @return    int
  113.      */
  114.     function getSize() {
  115.         return $this->_size;
  116.     }
  117.  
  118.     /**
  119.      * Get an array of pre-selected values
  120.      *
  121.      * @param    bool    $encode To sanitizer the text?
  122.      * @return    array
  123.      */
  124.     function getValue($encode = false) {
  125.         if (!$encode) {
  126.             return $this->_value;
  127.         }
  128.         $value = array();
  129.         foreach ($this->_value as $val) {
  130.             $value[] = $val ? htmlspecialchars($val, ENT_QUOTES) : $val;
  131.         }
  132.         return $value;
  133.     }
  134.  
  135.     /**
  136.      * Set pre-selected values
  137.      * 
  138.      * @param    $value    mixed
  139.      */
  140.     function setValue($value) {
  141.         if (is_array($value)) {
  142.             foreach ($value as $v) {
  143.                 $this->_value[] = $v;
  144.             }
  145.         } else {
  146.             $this->_value[] = $value;
  147.         }
  148.     }
  149.  
  150.     /**
  151.      * Add an option
  152.      * 
  153.      * @param    string  $value  "value" attribute
  154.      * @param    string  $name   "name" attribute
  155.      */
  156.     function addOption($value, $name = ""){
  157.         if ( $name != "" ) {
  158.             $this->_options[$value] = $name;
  159.         } else {
  160.             $this->_options[$value] = $value;
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * Add multiple options
  166.      * 
  167.      * @param    array   $options    Associative array of value->name pairs
  168.      */
  169.     function addOptionArray($options) {
  170.         if ( is_array($options) ) {
  171.             foreach ( $options as $k=>$v ) {
  172.                 $this->addOption($k, $v);
  173.             }
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Get an array with all the options
  179.      *
  180.      * Note: both name and value should be sanitized. However for backward compatibility, only value is sanitized for now.
  181.      *
  182.      * @param    int     $encode     To sanitizer the text? potential values: 0 - skip; 1 - only for value; 2 - for both value and name
  183.      * @return    array   Associative array of value->name pairs
  184.      */
  185.     function getOptions($encode = false) {
  186.         if (!$encode) {
  187.             return $this->_options;
  188.         }
  189.         $value = array();
  190.         foreach ($this->_options as $val => $name) {
  191.             $value[ $encode ? htmlspecialchars($val, ENT_QUOTES) : $val ] = ($encode > 1) ? htmlspecialchars($name, ENT_QUOTES) : $name;
  192.         }
  193.         return $value;
  194.     }
  195.  
  196.     /**
  197.      * Prepare HTML for output
  198.      * 
  199.      * @return    string  HTML
  200.      */
  201.     function render() {
  202.         $ele_name = $this->getName();
  203.         $ele_value = $this->getValue();
  204.         $ele_options = $this->getOptions();
  205.         $ret = "<select size='".$this->getSize()."'".$this->getExtra();
  206.         if ($this->isMultiple() != false) {
  207.             $ret .= " name='".$ele_name."[]' id='".$ele_name."[]' multiple='multiple'>\n";
  208.         } else {
  209.             $ret .= " name='".$ele_name."' id='".$ele_name."'>\n";
  210.         }
  211.         foreach ( $ele_options as $value => $name ) {
  212.             $ret .= "<option value='".htmlspecialchars($value, ENT_QUOTES)."'";
  213.             if (count($ele_value) > 0 && in_array($value, $ele_value)) {
  214.                     $ret .= " selected='selected'";
  215.             }
  216.             $ret .= ">".$name."</option>\n";
  217.         }
  218.         $ret .= "</select>";
  219.         return $ret;
  220.     }
  221. }
  222. ?>