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 / formcheckbox.php < prev    next >
Encoding:
PHP Script  |  2007-12-08  |  6.3 KB  |  202 lines

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